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: 278524 $")
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 CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE)
00405 #define MIN_MS_SINCE_FLASH ( (2000) )
00406 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
00407
00408 struct dahdi_pvt;
00409
00410
00411
00412
00413
00414 static int ringt_base = DEFAULT_RINGT;
00415
00416 #ifdef HAVE_SS7
00417
00418 #define LINKSTATE_INALARM (1 << 0)
00419 #define LINKSTATE_STARTING (1 << 1)
00420 #define LINKSTATE_UP (1 << 2)
00421 #define LINKSTATE_DOWN (1 << 3)
00422
00423 #define SS7_NAI_DYNAMIC -1
00424
00425 #define LINKSET_FLAG_EXPLICITACM (1 << 0)
00426
00427 struct dahdi_ss7 {
00428 pthread_t master;
00429 ast_mutex_t lock;
00430 int fds[NUM_DCHANS];
00431 int numsigchans;
00432 int linkstate[NUM_DCHANS];
00433 int numchans;
00434 int type;
00435 enum {
00436 LINKSET_STATE_DOWN = 0,
00437 LINKSET_STATE_UP
00438 } state;
00439 char called_nai;
00440 char calling_nai;
00441 char internationalprefix[10];
00442 char nationalprefix[10];
00443 char subscriberprefix[20];
00444 char unknownprefix[20];
00445 struct ss7 *ss7;
00446 struct dahdi_pvt *pvts[MAX_CHANNELS];
00447 int flags;
00448 };
00449
00450 static struct dahdi_ss7 linksets[NUM_SPANS];
00451
00452 static int cur_ss7type = -1;
00453 static int cur_linkset = -1;
00454 static int cur_pointcode = -1;
00455 static int cur_cicbeginswith = -1;
00456 static int cur_adjpointcode = -1;
00457 static int cur_networkindicator = -1;
00458 static int cur_defaultdpc = -1;
00459 #endif
00460
00461 #ifdef HAVE_OPENR2
00462 struct dahdi_mfcr2 {
00463 pthread_t r2master;
00464 openr2_context_t *protocol_context;
00465 struct dahdi_pvt *pvts[MAX_CHANNELS];
00466 int numchans;
00467 int monitored_count;
00468 };
00469
00470 struct dahdi_mfcr2_conf {
00471 openr2_variant_t variant;
00472 int mfback_timeout;
00473 int metering_pulse_timeout;
00474 int max_ani;
00475 int max_dnis;
00476 signed int get_ani_first:2;
00477 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
00478 signed int skip_category_request:2;
00479 #endif
00480 unsigned int call_files:1;
00481 unsigned int allow_collect_calls:1;
00482 unsigned int charge_calls:1;
00483 unsigned int accept_on_offer:1;
00484 unsigned int forced_release:1;
00485 unsigned int double_answer:1;
00486 signed int immediate_accept:2;
00487 char logdir[OR2_MAX_PATH];
00488 char r2proto_file[OR2_MAX_PATH];
00489 openr2_log_level_t loglevel;
00490 openr2_calling_party_category_t category;
00491 };
00492
00493
00494 static struct dahdi_mfcr2 **r2links;
00495
00496 static int r2links_count = 0;
00497
00498 #endif
00499
00500 #ifdef HAVE_PRI
00501
00502 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
00503 #define PRI_CHANNEL(p) ((p) & 0xff)
00504 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
00505 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
00506
00507 struct dahdi_pri {
00508 pthread_t master;
00509 ast_mutex_t lock;
00510 char idleext[AST_MAX_EXTENSION];
00511 char idlecontext[AST_MAX_CONTEXT];
00512 char idledial[AST_MAX_EXTENSION];
00513 int minunused;
00514 int minidle;
00515 int nodetype;
00516 int switchtype;
00517 int nsf;
00518 int dialplan;
00519 int localdialplan;
00520 char internationalprefix[10];
00521 char nationalprefix[10];
00522 char localprefix[20];
00523 char privateprefix[20];
00524 char unknownprefix[20];
00525 int dchannels[NUM_DCHANS];
00526 int trunkgroup;
00527 int mastertrunkgroup;
00528 int prilogicalspan;
00529 int numchans;
00530 int overlapdial;
00531 int qsigchannelmapping;
00532 int discardremoteholdretrieval;
00533 int facilityenable;
00534 struct pri *dchans[NUM_DCHANS];
00535 int dchanavail[NUM_DCHANS];
00536 struct pri *pri;
00537
00538 int debug;
00539 int fds[NUM_DCHANS];
00540
00541 int offset;
00542
00543 int span;
00544
00545 int resetting;
00546
00547 int resetpos;
00548 #ifdef HAVE_PRI_INBANDDISCONNECT
00549 unsigned int inbanddisconnect:1;
00550 #endif
00551 time_t lastreset;
00552 long resetinterval;
00553
00554 int sig;
00555 struct dahdi_pvt *pvts[MAX_CHANNELS];
00556 struct dahdi_pvt *crvs;
00557 struct dahdi_pvt *crvend;
00558 };
00559
00560
00561 static struct dahdi_pri pris[NUM_SPANS];
00562
00563 #if 0
00564 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
00565 #else
00566 #define DEFAULT_PRI_DEBUG 0
00567 #endif
00568
00569 static inline void pri_rel(struct dahdi_pri *pri)
00570 {
00571 ast_mutex_unlock(&pri->lock);
00572 }
00573
00574 #else
00575
00576 struct dahdi_pri;
00577 #endif
00578
00579 #define SUB_REAL 0
00580 #define SUB_CALLWAIT 1
00581 #define SUB_THREEWAY 2
00582
00583
00584 #define POLARITY_IDLE 0
00585 #define POLARITY_REV 1
00586
00587
00588 struct distRingData {
00589 int ring[3];
00590 int range;
00591 };
00592 struct ringContextData {
00593 char contextData[AST_MAX_CONTEXT];
00594 };
00595 struct dahdi_distRings {
00596 struct distRingData ringnum[3];
00597 struct ringContextData ringContext[3];
00598 };
00599
00600 static char *subnames[] = {
00601 "Real",
00602 "Callwait",
00603 "Threeway"
00604 };
00605
00606 struct dahdi_subchannel {
00607 int dfd;
00608 struct ast_channel *owner;
00609 int chan;
00610 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
00611 struct ast_frame f;
00612 unsigned int needringing:1;
00613 unsigned int needbusy:1;
00614 unsigned int needcongestion:1;
00615 unsigned int needcallerid:1;
00616 unsigned int needanswer:1;
00617 unsigned int needflash:1;
00618 unsigned int needhold:1;
00619 unsigned int needunhold:1;
00620 unsigned int linear:1;
00621 unsigned int inthreeway:1;
00622 struct dahdi_confinfo curconf;
00623 };
00624
00625 #define CONF_USER_REAL (1 << 0)
00626 #define CONF_USER_THIRDCALL (1 << 1)
00627
00628 #define MAX_SLAVES 4
00629
00630
00631
00632
00633 typedef enum {
00634 MWI_SEND_NULL = 0,
00635 MWI_SEND_SA,
00636 MWI_SEND_SA_WAIT,
00637 MWI_SEND_PAUSE,
00638 MWI_SEND_SPILL,
00639 MWI_SEND_CLEANUP,
00640 MWI_SEND_DONE,
00641 } mwisend_states;
00642
00643 struct mwisend_info {
00644 struct timeval pause;
00645 mwisend_states mwisend_current;
00646 };
00647
00648 static struct dahdi_pvt {
00649 ast_mutex_t lock;
00650 struct ast_channel *owner;
00651
00652
00653 struct dahdi_subchannel sub_unused;
00654 struct dahdi_subchannel subs[3];
00655 struct dahdi_confinfo saveconf;
00656
00657 struct dahdi_pvt *slaves[MAX_SLAVES];
00658 struct dahdi_pvt *master;
00659 int inconference;
00660
00661 int bufsize;
00662 int buf_no;
00663 int buf_policy;
00664 int faxbuf_no;
00665 int faxbuf_policy;
00666 int sig;
00667
00668
00669
00670
00671 int radio;
00672 int outsigmod;
00673 int oprmode;
00674 struct dahdi_pvt *oprpeer;
00675
00676 float cid_rxgain;
00677
00678 float rxgain;
00679
00680 float txgain;
00681 int tonezone;
00682 struct dahdi_pvt *next;
00683 struct dahdi_pvt *prev;
00684
00685
00686
00687
00688
00689
00690
00691 unsigned int adsi:1;
00692
00693
00694
00695
00696
00697 unsigned int answeronpolarityswitch:1;
00698
00699
00700
00701
00702
00703 unsigned int busydetect:1;
00704
00705
00706
00707
00708
00709 unsigned int callreturn:1;
00710
00711
00712
00713
00714
00715
00716 unsigned int callwaiting:1;
00717
00718
00719
00720
00721 unsigned int callwaitingcallerid:1;
00722
00723
00724
00725
00726
00727
00728 unsigned int cancallforward:1;
00729
00730
00731
00732
00733 unsigned int canpark:1;
00734
00735 unsigned int confirmanswer:1;
00736
00737
00738
00739
00740 unsigned int destroy:1;
00741 unsigned int didtdd:1;
00742
00743 unsigned int dialednone:1;
00744
00745 unsigned int dialing:1;
00746
00747 unsigned int digital:1;
00748
00749 unsigned int dnd:1;
00750
00751 unsigned int echobreak:1;
00752
00753
00754
00755
00756
00757 unsigned int echocanbridged:1;
00758
00759 unsigned int echocanon:1;
00760
00761 unsigned int faxhandled:1;
00762
00763 unsigned int usefaxbuffers:1;
00764
00765 unsigned int bufferoverrideinuse:1;
00766
00767 unsigned int firstradio:1;
00768
00769
00770
00771
00772 unsigned int hanguponpolarityswitch:1;
00773
00774 unsigned int hardwaredtmf:1;
00775
00776
00777
00778
00779
00780
00781 unsigned int hidecallerid:1;
00782
00783
00784
00785
00786
00787 unsigned int hidecalleridname:1;
00788
00789 unsigned int ignoredtmf:1;
00790
00791
00792
00793
00794
00795 unsigned int immediate:1;
00796
00797 unsigned int inalarm:1;
00798
00799 unsigned int mate:1;
00800
00801 unsigned int outgoing:1;
00802
00803
00804
00805
00806
00807
00808 unsigned int permcallwaiting:1;
00809
00810
00811
00812
00813 unsigned int permhidecallerid:1;
00814
00815
00816
00817
00818 unsigned int priindication_oob:1;
00819
00820
00821
00822
00823 unsigned int priexclusive:1;
00824
00825
00826
00827
00828 unsigned int pulse:1;
00829
00830 unsigned int pulsedial:1;
00831 unsigned int restartpending:1;
00832
00833
00834
00835
00836
00837 unsigned int restrictcid:1;
00838
00839
00840
00841
00842 unsigned int threewaycalling:1;
00843
00844
00845
00846
00847
00848
00849
00850
00851 unsigned int transfer:1;
00852
00853
00854
00855
00856
00857
00858
00859 unsigned int use_callerid:1;
00860
00861
00862
00863
00864
00865
00866 unsigned int use_callingpres:1;
00867
00868
00869
00870
00871
00872 unsigned int usedistinctiveringdetection:1;
00873
00874
00875
00876
00877 unsigned int dahditrcallerid:1;
00878
00879
00880
00881
00882 unsigned int transfertobusy:1;
00883
00884
00885
00886
00887 unsigned int mwimonitor_neon:1;
00888
00889
00890
00891
00892 unsigned int mwimonitor_fsk:1;
00893
00894
00895
00896
00897
00898 unsigned int mwimonitor_rpas:1;
00899
00900 unsigned int mwimonitoractive:1;
00901
00902 unsigned int mwisendactive:1;
00903
00904
00905
00906
00907 unsigned int inservice:1;
00908
00909
00910
00911
00912 unsigned int locallyblocked:1;
00913
00914
00915
00916
00917 unsigned int remotelyblocked:1;
00918 #if defined(HAVE_PRI) || defined(HAVE_SS7)
00919
00920
00921
00922
00923 unsigned int rlt:1;
00924
00925 unsigned int alerting:1;
00926
00927 unsigned int alreadyhungup:1;
00928
00929
00930
00931
00932 unsigned int isidlecall:1;
00933
00934
00935
00936
00937 unsigned int proceeding:1;
00938
00939 unsigned int progress:1;
00940
00941
00942
00943
00944 unsigned int resetting:1;
00945
00946
00947
00948
00949 unsigned int setup_ack:1;
00950 #endif
00951
00952
00953
00954
00955 unsigned int use_smdi:1;
00956 struct mwisend_info mwisend_data;
00957
00958 struct ast_smdi_interface *smdi_iface;
00959
00960
00961 struct dahdi_distRings drings;
00962
00963
00964
00965
00966
00967 char context[AST_MAX_CONTEXT];
00968
00969
00970
00971 char defcontext[AST_MAX_CONTEXT];
00972
00973 char exten[AST_MAX_EXTENSION];
00974
00975
00976
00977
00978 char language[MAX_LANGUAGE];
00979
00980
00981
00982
00983 char mohinterpret[MAX_MUSICCLASS];
00984
00985
00986
00987
00988 char mohsuggest[MAX_MUSICCLASS];
00989 char parkinglot[AST_MAX_EXTENSION];
00990 #if defined(PRI_ANI) || defined(HAVE_SS7)
00991
00992 char cid_ani[AST_MAX_EXTENSION];
00993 #endif
00994
00995 int cid_ani2;
00996
00997 char cid_num[AST_MAX_EXTENSION];
00998
00999 int cid_ton;
01000
01001 char cid_name[AST_MAX_EXTENSION];
01002
01003 char lastcid_num[AST_MAX_EXTENSION];
01004
01005 char lastcid_name[AST_MAX_EXTENSION];
01006 char *origcid_num;
01007 char *origcid_name;
01008
01009 char callwait_num[AST_MAX_EXTENSION];
01010
01011 char callwait_name[AST_MAX_EXTENSION];
01012
01013 char rdnis[AST_MAX_EXTENSION];
01014
01015 char dnid[AST_MAX_EXTENSION];
01016
01017
01018
01019
01020 ast_group_t group;
01021
01022 int law;
01023 int confno;
01024 int confusers;
01025 int propconfno;
01026
01027
01028
01029
01030 ast_group_t callgroup;
01031
01032
01033
01034
01035 ast_group_t pickupgroup;
01036
01037
01038
01039
01040 struct ast_variable *vars;
01041 int channel;
01042 int span;
01043 time_t guardtime;
01044 int cid_signalling;
01045 int cid_start;
01046 int callingpres;
01047 int callwaitingrepeat;
01048 int cidcwexpire;
01049
01050 unsigned char *cidspill;
01051
01052 int cidpos;
01053
01054 int cidlen;
01055
01056 int ringt;
01057
01058
01059
01060
01061 int ringt_base;
01062
01063
01064
01065
01066
01067
01068 int stripmsd;
01069
01070 int callwaitcas;
01071
01072 int callwaitrings;
01073
01074 struct {
01075 struct dahdi_echocanparams head;
01076 struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
01077 } echocancel;
01078
01079
01080
01081
01082 int echotraining;
01083
01084 char echorest[20];
01085
01086
01087
01088
01089 int busycount;
01090
01091
01092
01093
01094 int busy_tonelength;
01095
01096
01097
01098
01099 int busy_quietlength;
01100
01101
01102
01103
01104 int callprogress;
01105
01106
01107
01108
01109 int waitfordialtone;
01110 struct timeval waitingfordt;
01111 struct timeval flashtime;
01112
01113 struct ast_dsp *dsp;
01114
01115
01116 struct dahdi_dialoperation dop;
01117 int whichwink;
01118
01119 char finaldial[64];
01120 char accountcode[AST_MAX_ACCOUNT_CODE];
01121 int amaflags;
01122 struct tdd_state *tdd;
01123
01124 char call_forward[AST_MAX_EXTENSION];
01125
01126
01127
01128
01129 char mailbox[AST_MAX_EXTENSION];
01130
01131 struct ast_event_sub *mwi_event_sub;
01132
01133 char dialdest[256];
01134
01135 int onhooktime;
01136
01137 int fxsoffhookstate;
01138
01139 int msgstate;
01140 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
01141 struct dahdi_vmwi_info mwisend_setting;
01142 unsigned int mwisend_fsk: 1;
01143 unsigned int mwisend_rpas:1;
01144 #endif
01145 int distinctivering;
01146 int cidrings;
01147 int dtmfrelax;
01148
01149 int fake_event;
01150
01151
01152
01153
01154 int polarityonanswerdelay;
01155
01156 struct timeval polaritydelaytv;
01157
01158
01159
01160
01161 int sendcalleridafter;
01162 #ifdef HAVE_PRI
01163
01164 struct dahdi_pri *pri;
01165
01166 struct dahdi_pvt *bearer;
01167
01168 struct dahdi_pvt *realcall;
01169
01170 q931_call *call;
01171
01172 int prioffset;
01173
01174 int logicalspan;
01175 #endif
01176
01177 int polarity;
01178
01179 int dsp_features;
01180 #ifdef HAVE_SS7
01181
01182 struct dahdi_ss7 *ss7;
01183
01184 struct isup_call *ss7call;
01185 char charge_number[50];
01186 char gen_add_number[50];
01187 char gen_dig_number[50];
01188 char orig_called_num[50];
01189 char redirecting_num[50];
01190 char generic_name[50];
01191 unsigned char gen_add_num_plan;
01192 unsigned char gen_add_nai;
01193 unsigned char gen_add_pres_ind;
01194 unsigned char gen_add_type;
01195 unsigned char gen_dig_type;
01196 unsigned char gen_dig_scheme;
01197 char jip_number[50];
01198 unsigned char lspi_type;
01199 unsigned char lspi_scheme;
01200 unsigned char lspi_context;
01201 char lspi_ident[50];
01202 unsigned int call_ref_ident;
01203 unsigned int call_ref_pc;
01204 unsigned char calling_party_cat;
01205 int transcap;
01206 int cic;
01207 unsigned int dpc;
01208 unsigned int loopedback:1;
01209 #endif
01210 #ifdef HAVE_OPENR2
01211 struct dahdi_mfcr2 *mfcr2;
01212 openr2_chan_t *r2chan;
01213 openr2_calling_party_category_t mfcr2_recvd_category;
01214 openr2_calling_party_category_t mfcr2_category;
01215 int mfcr2_dnis_index;
01216 int mfcr2_ani_index;
01217 int mfcr2call:1;
01218 int mfcr2_answer_pending:1;
01219 int mfcr2_charge_calls:1;
01220 int mfcr2_allow_collect_calls:1;
01221 int mfcr2_forced_release:1;
01222 int mfcr2_dnis_matched:1;
01223 int mfcr2_call_accepted:1;
01224 int mfcr2_accept_on_offer:1;
01225 #endif
01226
01227 char begindigit;
01228
01229 int muting;
01230 } *iflist = NULL, *ifend = NULL;
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242 struct dahdi_chan_conf {
01243 struct dahdi_pvt chan;
01244 #ifdef HAVE_PRI
01245 struct dahdi_pri pri;
01246 #endif
01247
01248 #ifdef HAVE_SS7
01249 struct dahdi_ss7 ss7;
01250 #endif
01251
01252 #ifdef HAVE_OPENR2
01253 struct dahdi_mfcr2_conf mfcr2;
01254 #endif
01255 struct dahdi_params timing;
01256 int is_sig_auto;
01257
01258
01259
01260
01261
01262 char smdi_port[SMDI_MAX_FILENAME_LEN];
01263 };
01264
01265
01266 static struct dahdi_chan_conf dahdi_chan_conf_default(void)
01267 {
01268
01269
01270
01271 struct dahdi_chan_conf conf = {
01272 #ifdef HAVE_PRI
01273 .pri = {
01274 .nsf = PRI_NSF_NONE,
01275 .switchtype = PRI_SWITCH_NI2,
01276 .dialplan = PRI_UNKNOWN + 1,
01277 .localdialplan = PRI_NATIONAL_ISDN + 1,
01278 .nodetype = PRI_CPE,
01279 .qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL,
01280
01281 .minunused = 2,
01282 .idleext = "",
01283 .idledial = "",
01284 .internationalprefix = "",
01285 .nationalprefix = "",
01286 .localprefix = "",
01287 .privateprefix = "",
01288 .unknownprefix = "",
01289 .resetinterval = -1,
01290 },
01291 #endif
01292 #ifdef HAVE_SS7
01293 .ss7 = {
01294 .called_nai = SS7_NAI_NATIONAL,
01295 .calling_nai = SS7_NAI_NATIONAL,
01296 .internationalprefix = "",
01297 .nationalprefix = "",
01298 .subscriberprefix = "",
01299 .unknownprefix = ""
01300 },
01301 #endif
01302 #ifdef HAVE_OPENR2
01303 .mfcr2 = {
01304 .variant = OR2_VAR_ITU,
01305 .mfback_timeout = -1,
01306 .metering_pulse_timeout = -1,
01307 .max_ani = 10,
01308 .max_dnis = 4,
01309 .get_ani_first = -1,
01310 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
01311 .skip_category_request = -1,
01312 #endif
01313 .call_files = 0,
01314 .allow_collect_calls = 0,
01315 .charge_calls = 1,
01316 .accept_on_offer = 1,
01317 .forced_release = 0,
01318 .double_answer = 0,
01319 .immediate_accept = -1,
01320 .logdir = "",
01321 .r2proto_file = "",
01322 .loglevel = OR2_LOG_ERROR | OR2_LOG_WARNING,
01323 .category = OR2_CALLING_PARTY_CATEGORY_NATIONAL_SUBSCRIBER
01324 },
01325 #endif
01326 .chan = {
01327 .context = "default",
01328 .cid_num = "",
01329 .cid_name = "",
01330 .mohinterpret = "default",
01331 .mohsuggest = "",
01332 .parkinglot = "",
01333 .transfertobusy = 1,
01334
01335 .cid_signalling = CID_SIG_BELL,
01336 .cid_start = CID_START_RING,
01337 .dahditrcallerid = 0,
01338 .use_callerid = 1,
01339 .sig = -1,
01340 .outsigmod = -1,
01341
01342 .cid_rxgain = +5.0,
01343
01344 .tonezone = -1,
01345
01346 .echocancel.head.tap_length = 1,
01347
01348 .busycount = 3,
01349
01350 .accountcode = "",
01351
01352 .mailbox = "",
01353
01354 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
01355 .mwisend_fsk = 1,
01356 #endif
01357 .polarityonanswerdelay = 600,
01358
01359 .sendcalleridafter = DEFAULT_CIDRINGS,
01360
01361 .buf_policy = DAHDI_POLICY_IMMEDIATE,
01362 .buf_no = numbufs,
01363 .usefaxbuffers = 0,
01364 .faxbuf_policy = DAHDI_POLICY_IMMEDIATE,
01365 .faxbuf_no = numbufs,
01366 },
01367 .timing = {
01368 .prewinktime = -1,
01369 .preflashtime = -1,
01370 .winktime = -1,
01371 .flashtime = -1,
01372 .starttime = -1,
01373 .rxwinktime = -1,
01374 .rxflashtime = -1,
01375 .debouncetime = -1
01376 },
01377 .is_sig_auto = 1,
01378 .smdi_port = "/dev/ttyS0",
01379 };
01380
01381 return conf;
01382 }
01383
01384
01385 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause);
01386 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
01387 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
01388 static int dahdi_sendtext(struct ast_channel *c, const char *text);
01389 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
01390 static int dahdi_hangup(struct ast_channel *ast);
01391 static int dahdi_answer(struct ast_channel *ast);
01392 static struct ast_frame *dahdi_read(struct ast_channel *ast);
01393 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
01394 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
01395 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
01396 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01397 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
01398 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
01399 static int dahdi_func_write(struct ast_channel *chan, const char *function, char *data, const char *value);
01400 static struct dahdi_pvt *handle_init_event(struct dahdi_pvt *i, int event);
01401
01402 static const struct ast_channel_tech dahdi_tech = {
01403 .type = "DAHDI",
01404 .description = tdesc,
01405 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
01406 .requester = dahdi_request,
01407 .send_digit_begin = dahdi_digit_begin,
01408 .send_digit_end = dahdi_digit_end,
01409 .send_text = dahdi_sendtext,
01410 .call = dahdi_call,
01411 .hangup = dahdi_hangup,
01412 .answer = dahdi_answer,
01413 .read = dahdi_read,
01414 .write = dahdi_write,
01415 .bridge = dahdi_bridge,
01416 .exception = dahdi_exception,
01417 .indicate = dahdi_indicate,
01418 .fixup = dahdi_fixup,
01419 .setoption = dahdi_setoption,
01420 .func_channel_read = dahdi_func_read,
01421 .func_channel_write = dahdi_func_write,
01422 };
01423
01424 #ifdef HAVE_PRI
01425 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
01426 #else
01427 #define GET_CHANNEL(p) ((p)->channel)
01428 #endif
01429
01430 struct dahdi_pvt *round_robin[32];
01431
01432 #if defined(HAVE_PRI)
01433 static inline int pri_grab(struct dahdi_pvt *pvt, struct dahdi_pri *pri)
01434 {
01435 int res;
01436
01437 do {
01438 res = ast_mutex_trylock(&pri->lock);
01439 if (res) {
01440 DEADLOCK_AVOIDANCE(&pvt->lock);
01441 }
01442 } while (res);
01443
01444 if (pri->master != AST_PTHREADT_NULL)
01445 pthread_kill(pri->master, SIGURG);
01446 return 0;
01447 }
01448 #endif
01449
01450 #if defined(HAVE_SS7)
01451 static inline void ss7_rel(struct dahdi_ss7 *ss7)
01452 {
01453 ast_mutex_unlock(&ss7->lock);
01454 }
01455 #endif
01456
01457 #if defined(HAVE_SS7)
01458 static inline int ss7_grab(struct dahdi_pvt *pvt, struct dahdi_ss7 *pri)
01459 {
01460 int res;
01461
01462 do {
01463 res = ast_mutex_trylock(&pri->lock);
01464 if (res) {
01465 DEADLOCK_AVOIDANCE(&pvt->lock);
01466 }
01467 } while (res);
01468
01469 if (pri->master != AST_PTHREADT_NULL)
01470 pthread_kill(pri->master, SIGURG);
01471 return 0;
01472 }
01473 #endif
01474 #define NUM_CADENCE_MAX 25
01475 static int num_cadence = 4;
01476 static int user_has_defined_cadences = 0;
01477
01478 static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
01479 { { 125, 125, 2000, 4000 } },
01480 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } },
01481 { { 125, 125, 125, 125, 125, 4000 } },
01482 { { 1000, 500, 2500, 5000 } },
01483 };
01484
01485
01486
01487
01488
01489 static int cidrings[NUM_CADENCE_MAX] = {
01490 2,
01491 4,
01492 3,
01493 2,
01494 };
01495
01496
01497 static struct dahdi_ring_cadence AS_RP_cadence = {{250, 10000}};
01498
01499 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
01500 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
01501
01502 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
01503 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
01504
01505 static int dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok)
01506 {
01507 int res;
01508 if (p->subs[SUB_REAL].owner == ast)
01509 res = 0;
01510 else if (p->subs[SUB_CALLWAIT].owner == ast)
01511 res = 1;
01512 else if (p->subs[SUB_THREEWAY].owner == ast)
01513 res = 2;
01514 else {
01515 res = -1;
01516 if (!nullok)
01517 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
01518 }
01519 return res;
01520 }
01521
01522 static void wakeup_sub(struct dahdi_pvt *p, int a, struct dahdi_pri *pri)
01523 {
01524 #ifdef HAVE_PRI
01525 if (pri)
01526 ast_mutex_unlock(&pri->lock);
01527 #endif
01528 for (;;) {
01529 if (p->subs[a].owner) {
01530 if (ast_channel_trylock(p->subs[a].owner)) {
01531 DEADLOCK_AVOIDANCE(&p->lock);
01532 } else {
01533 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
01534 ast_channel_unlock(p->subs[a].owner);
01535 break;
01536 }
01537 } else
01538 break;
01539 }
01540 #ifdef HAVE_PRI
01541 if (pri)
01542 ast_mutex_lock(&pri->lock);
01543 #endif
01544 }
01545
01546 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *data)
01547 {
01548 #ifdef HAVE_PRI
01549 struct dahdi_pri *pri = (struct dahdi_pri*) data;
01550 #endif
01551 #ifdef HAVE_SS7
01552 struct dahdi_ss7 *ss7 = (struct dahdi_ss7*) data;
01553 #endif
01554
01555 #if defined(HAVE_PRI) || defined(HAVE_SS7)
01556 if (data) {
01557 switch (p->sig) {
01558 #ifdef HAVE_PRI
01559 case SIG_BRI:
01560 case SIG_BRI_PTMP:
01561 case SIG_PRI:
01562 ast_mutex_unlock(&pri->lock);
01563 break;
01564 #endif
01565 #ifdef HAVE_SS7
01566 case SIG_SS7:
01567 ast_mutex_unlock(&ss7->lock);
01568 break;
01569 #endif
01570 default:
01571 break;
01572 }
01573 }
01574 #endif
01575 for (;;) {
01576 if (p->owner) {
01577 if (ast_channel_trylock(p->owner)) {
01578 DEADLOCK_AVOIDANCE(&p->lock);
01579 } else {
01580 ast_queue_frame(p->owner, f);
01581 ast_channel_unlock(p->owner);
01582 break;
01583 }
01584 } else
01585 break;
01586 }
01587 #if defined(HAVE_PRI) || defined(HAVE_SS7)
01588 if (data) {
01589 switch (p->sig) {
01590 #ifdef HAVE_PRI
01591 case SIG_BRI:
01592 case SIG_BRI_PTMP:
01593 case SIG_PRI:
01594 ast_mutex_lock(&pri->lock);
01595 break;
01596 #endif
01597 #ifdef HAVE_SS7
01598 case SIG_SS7:
01599 ast_mutex_lock(&ss7->lock);
01600 break;
01601 #endif
01602 default:
01603 break;
01604 }
01605 }
01606 #endif
01607 }
01608
01609 static struct ast_channel *dahdi_new(struct dahdi_pvt *, int, int, int, int, int);
01610 #ifdef HAVE_OPENR2
01611
01612 static int dahdi_r2_answer(struct dahdi_pvt *p)
01613 {
01614 int res = 0;
01615
01616
01617
01618 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
01619 const char *double_answer = pbx_builtin_getvar_helper(p->owner, "MFCR2_DOUBLE_ANSWER");
01620 int wants_double_answer = ast_true(double_answer) ? 1 : 0;
01621 if (!double_answer) {
01622
01623
01624 res = openr2_chan_answer_call(p->r2chan);
01625 } else if (wants_double_answer) {
01626 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_DOUBLE);
01627 } else {
01628 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_SIMPLE);
01629 }
01630 #else
01631 res = openr2_chan_answer_call(p->r2chan);
01632 #endif
01633 return res;
01634 }
01635
01636
01637
01638
01639 static openr2_calling_party_category_t dahdi_r2_get_channel_category(struct ast_channel *c)
01640 {
01641 openr2_calling_party_category_t cat;
01642 const char *catstr = pbx_builtin_getvar_helper(c, "MFCR2_CATEGORY");
01643 struct dahdi_pvt *p = c->tech_pvt;
01644 if (ast_strlen_zero(catstr)) {
01645 ast_debug(1, "No MFC/R2 category specified for chan %s, using default %s\n",
01646 c->name, openr2_proto_get_category_string(p->mfcr2_category));
01647 return p->mfcr2_category;
01648 }
01649 if ((cat = openr2_proto_get_category(catstr)) == OR2_CALLING_PARTY_CATEGORY_UNKNOWN) {
01650 ast_log(LOG_WARNING, "Invalid category specified '%s' for chan %s, using default %s\n",
01651 catstr, c->name, openr2_proto_get_category_string(p->mfcr2_category));
01652 return p->mfcr2_category;
01653 }
01654 ast_debug(1, "Using category %s\n", catstr);
01655 return cat;
01656 }
01657
01658 static void dahdi_r2_on_call_init(openr2_chan_t *r2chan)
01659 {
01660 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01661 ast_mutex_lock(&p->lock);
01662 if (p->mfcr2call) {
01663 ast_mutex_unlock(&p->lock);
01664
01665
01666
01667
01668
01669 ast_log(LOG_ERROR, "Collision of calls on chan %d detected!.\n", openr2_chan_get_number(r2chan));
01670 return;
01671 }
01672 p->mfcr2call = 1;
01673
01674 p->cid_name[0] = '\0';
01675 p->cid_num[0] = '\0';
01676 p->rdnis[0] = '\0';
01677 p->exten[0] = '\0';
01678 p->mfcr2_ani_index = '\0';
01679 p->mfcr2_dnis_index = '\0';
01680 p->mfcr2_dnis_matched = 0;
01681 p->mfcr2_answer_pending = 0;
01682 p->mfcr2_call_accepted = 0;
01683 ast_mutex_unlock(&p->lock);
01684 ast_verbose("New MFC/R2 call detected on chan %d.\n", openr2_chan_get_number(r2chan));
01685 }
01686
01687 static int get_alarms(struct dahdi_pvt *p);
01688 static void handle_alarms(struct dahdi_pvt *p, int alms);
01689 static void dahdi_r2_on_hardware_alarm(openr2_chan_t *r2chan, int alarm)
01690 {
01691 int res;
01692 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01693 ast_mutex_lock(&p->lock);
01694 p->inalarm = alarm ? 1 : 0;
01695 if (p->inalarm) {
01696 res = get_alarms(p);
01697 handle_alarms(p, res);
01698 } else {
01699 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
01700 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear", "Channel: %d\r\n", p->channel);
01701 }
01702 ast_mutex_unlock(&p->lock);
01703 }
01704
01705 static void dahdi_r2_on_os_error(openr2_chan_t *r2chan, int errorcode)
01706 {
01707 ast_log(LOG_ERROR, "OS error on chan %d: %s\n", openr2_chan_get_number(r2chan), strerror(errorcode));
01708 }
01709
01710 static void dahdi_r2_on_protocol_error(openr2_chan_t *r2chan, openr2_protocol_error_t reason)
01711 {
01712 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01713 ast_log(LOG_ERROR, "MFC/R2 protocol error on chan %d: %s\n", openr2_chan_get_number(r2chan), openr2_proto_get_error(reason));
01714 if (p->owner) {
01715 p->owner->hangupcause = AST_CAUSE_PROTOCOL_ERROR;
01716 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01717 }
01718 ast_mutex_lock(&p->lock);
01719 p->mfcr2call = 0;
01720 ast_mutex_unlock(&p->lock);
01721 }
01722
01723 static void dahdi_r2_disconnect_call(struct dahdi_pvt *p, openr2_call_disconnect_cause_t cause)
01724 {
01725 if (openr2_chan_disconnect_call(p->r2chan, cause)) {
01726 ast_log(LOG_NOTICE, "Bad! failed to disconnect call on channel %d with reason %s, hope for the best!\n",
01727 p->channel, openr2_proto_get_disconnect_string(cause));
01728
01729 openr2_chan_set_idle(p->r2chan);
01730 ast_mutex_lock(&p->lock);
01731 p->mfcr2call = 0;
01732 ast_mutex_unlock(&p->lock);
01733 }
01734 }
01735
01736 static void dahdi_r2_on_call_offered(openr2_chan_t *r2chan, const char *ani, const char *dnis, openr2_calling_party_category_t category)
01737 {
01738 struct dahdi_pvt *p;
01739 struct ast_channel *c;
01740 ast_verbose("MFC/R2 call offered on chan %d. ANI = %s, DNIS = %s, Category = %s\n",
01741 openr2_chan_get_number(r2chan), ani ? ani : "(restricted)", dnis,
01742 openr2_proto_get_category_string(category));
01743 p = openr2_chan_get_client_data(r2chan);
01744
01745 if (!p->mfcr2_allow_collect_calls && category == OR2_CALLING_PARTY_CATEGORY_COLLECT_CALL) {
01746 ast_log(LOG_NOTICE, "Rejecting MFC/R2 collect call\n");
01747 dahdi_r2_disconnect_call(p, OR2_CAUSE_COLLECT_CALL_REJECTED);
01748 return;
01749 }
01750 ast_mutex_lock(&p->lock);
01751 p->mfcr2_recvd_category = category;
01752
01753 if (!p->use_callerid) {
01754 ast_log(LOG_DEBUG, "No CID allowed in configuration, CID is being cleared!\n");
01755 p->cid_num[0] = 0;
01756 p->cid_name[0] = 0;
01757 }
01758
01759 if (p->immediate || !openr2_context_get_max_dnis(openr2_chan_get_context(r2chan))) {
01760 ast_log(LOG_DEBUG, "Setting exten => s because of immediate or 0 DNIS configured\n");
01761 p->exten[0] = 's';
01762 p->exten[1] = 0;
01763 }
01764 ast_mutex_unlock(&p->lock);
01765 if (!ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
01766 ast_log(LOG_NOTICE, "MFC/R2 call on channel %d requested non-existent extension '%s' in context '%s'. Rejecting call.\n",
01767 p->channel, p->exten, p->context);
01768 dahdi_r2_disconnect_call(p, OR2_CAUSE_UNALLOCATED_NUMBER);
01769 return;
01770 }
01771 if (!p->mfcr2_accept_on_offer) {
01772
01773 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0);
01774 if (c) {
01775
01776
01777
01778 return;
01779 }
01780 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
01781 dahdi_r2_disconnect_call(p, OR2_CAUSE_OUT_OF_ORDER);
01782 } else if (p->mfcr2_charge_calls) {
01783 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with charge on chan %d\n", p->channel);
01784 openr2_chan_accept_call(r2chan, OR2_CALL_WITH_CHARGE);
01785 } else {
01786 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with no charge on chan %d\n", p->channel);
01787 openr2_chan_accept_call(r2chan, OR2_CALL_NO_CHARGE);
01788 }
01789 }
01790
01791 static void dahdi_r2_on_call_end(openr2_chan_t *r2chan)
01792 {
01793 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01794 ast_verbose("MFC/R2 call end on channel %d\n", p->channel);
01795 ast_mutex_lock(&p->lock);
01796 p->mfcr2call = 0;
01797 ast_mutex_unlock(&p->lock);
01798 }
01799
01800 static void dahdi_enable_ec(struct dahdi_pvt *p);
01801 static void dahdi_r2_on_call_accepted(openr2_chan_t *r2chan, openr2_call_mode_t mode)
01802 {
01803 struct dahdi_pvt *p = NULL;
01804 struct ast_channel *c = NULL;
01805 p = openr2_chan_get_client_data(r2chan);
01806 dahdi_enable_ec(p);
01807 p->mfcr2_call_accepted = 1;
01808
01809 if (OR2_DIR_BACKWARD == openr2_chan_get_direction(r2chan)) {
01810 ast_verbose("MFC/R2 call has been accepted on backward channel %d\n", openr2_chan_get_number(r2chan));
01811
01812
01813
01814
01815 if (!p->mfcr2_accept_on_offer) {
01816 openr2_chan_disable_read(r2chan);
01817 if (p->mfcr2_answer_pending) {
01818 ast_log(LOG_DEBUG, "Answering MFC/R2 call after accepting it on chan %d\n", openr2_chan_get_number(r2chan));
01819 dahdi_r2_answer(p);
01820 }
01821 return;
01822 }
01823 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0);
01824 if (c) {
01825
01826
01827 openr2_chan_disable_read(r2chan);
01828 return;
01829 }
01830 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
01831
01832 dahdi_r2_disconnect_call(p, OR2_CAUSE_OUT_OF_ORDER);
01833 return;
01834 }
01835
01836 ast_verbose("MFC/R2 call has been accepted on forward channel %d\n", p->channel);
01837 p->subs[SUB_REAL].needringing = 1;
01838 p->dialing = 0;
01839
01840 openr2_chan_disable_read(r2chan);
01841 }
01842
01843 static void dahdi_r2_on_call_answered(openr2_chan_t *r2chan)
01844 {
01845 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01846 ast_verbose("MFC/R2 call has been answered on channel %d\n", openr2_chan_get_number(r2chan));
01847 p->subs[SUB_REAL].needanswer = 1;
01848 }
01849
01850 static void dahdi_r2_on_call_read(openr2_chan_t *r2chan, const unsigned char *buf, int buflen)
01851 {
01852
01853 }
01854
01855 static int dahdi_r2_cause_to_ast_cause(openr2_call_disconnect_cause_t cause)
01856 {
01857 switch (cause) {
01858 case OR2_CAUSE_BUSY_NUMBER:
01859 return AST_CAUSE_BUSY;
01860 case OR2_CAUSE_NETWORK_CONGESTION:
01861 return AST_CAUSE_CONGESTION;
01862 case OR2_CAUSE_OUT_OF_ORDER:
01863 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
01864 case OR2_CAUSE_UNALLOCATED_NUMBER:
01865 return AST_CAUSE_UNREGISTERED;
01866 case OR2_CAUSE_NO_ANSWER:
01867 return AST_CAUSE_NO_ANSWER;
01868 case OR2_CAUSE_NORMAL_CLEARING:
01869 return AST_CAUSE_NORMAL_CLEARING;
01870 case OR2_CAUSE_UNSPECIFIED:
01871 default:
01872 return AST_CAUSE_NOTDEFINED;
01873 }
01874 }
01875
01876 static void dahdi_r2_on_call_disconnect(openr2_chan_t *r2chan, openr2_call_disconnect_cause_t cause)
01877 {
01878 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01879 ast_verbose("MFC/R2 call disconnected on channel %d\n", openr2_chan_get_number(r2chan));
01880 ast_mutex_lock(&p->lock);
01881 if (!p->owner) {
01882 ast_mutex_unlock(&p->lock);
01883
01884 dahdi_r2_disconnect_call(p, OR2_CAUSE_NORMAL_CLEARING);
01885 return;
01886 }
01887
01888
01889 if (p->owner->_state == AST_STATE_UP) {
01890 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01891 ast_mutex_unlock(&p->lock);
01892 } else if (openr2_chan_get_direction(r2chan) == OR2_DIR_FORWARD) {
01893
01894 switch (cause) {
01895 case OR2_CAUSE_BUSY_NUMBER:
01896 p->subs[SUB_REAL].needbusy = 1;
01897 break;
01898 case OR2_CAUSE_NETWORK_CONGESTION:
01899 case OR2_CAUSE_OUT_OF_ORDER:
01900 case OR2_CAUSE_UNALLOCATED_NUMBER:
01901 case OR2_CAUSE_NO_ANSWER:
01902 case OR2_CAUSE_UNSPECIFIED:
01903 case OR2_CAUSE_NORMAL_CLEARING:
01904 p->subs[SUB_REAL].needcongestion = 1;
01905 break;
01906 default:
01907 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01908 }
01909 ast_mutex_unlock(&p->lock);
01910 } else {
01911 ast_mutex_unlock(&p->lock);
01912
01913
01914 ast_queue_hangup_with_cause(p->owner, dahdi_r2_cause_to_ast_cause(cause));
01915 }
01916 }
01917
01918 static void dahdi_r2_write_log(openr2_log_level_t level, char *logmessage)
01919 {
01920 switch (level) {
01921 case OR2_LOG_NOTICE:
01922 ast_verbose("%s", logmessage);
01923 break;
01924 case OR2_LOG_WARNING:
01925 ast_log(LOG_WARNING, "%s", logmessage);
01926 break;
01927 case OR2_LOG_ERROR:
01928 ast_log(LOG_ERROR, "%s", logmessage);
01929 break;
01930 case OR2_LOG_STACK_TRACE:
01931 case OR2_LOG_MF_TRACE:
01932 case OR2_LOG_CAS_TRACE:
01933 case OR2_LOG_DEBUG:
01934 case OR2_LOG_EX_DEBUG:
01935 ast_log(LOG_DEBUG, "%s", logmessage);
01936 break;
01937 default:
01938 ast_log(LOG_WARNING, "We should handle logging level %d here.\n", level);
01939 ast_log(LOG_DEBUG, "%s", logmessage);
01940 break;
01941 }
01942 }
01943
01944 static void dahdi_r2_on_line_blocked(openr2_chan_t *r2chan)
01945 {
01946 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01947 ast_mutex_lock(&p->lock);
01948 p->remotelyblocked = 1;
01949 ast_mutex_unlock(&p->lock);
01950 ast_log(LOG_NOTICE, "Far end blocked on chan %d\n", openr2_chan_get_number(r2chan));
01951 }
01952
01953 static void dahdi_r2_on_line_idle(openr2_chan_t *r2chan)
01954 {
01955 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01956 ast_mutex_lock(&p->lock);
01957 p->remotelyblocked = 0;
01958 ast_mutex_unlock(&p->lock);
01959 ast_log(LOG_NOTICE, "Far end unblocked on chan %d\n", openr2_chan_get_number(r2chan));
01960 }
01961
01962 static void dahdi_r2_on_context_log(openr2_context_t *r2context, openr2_log_level_t level, const char *fmt, va_list ap)
01963 __attribute__((format (printf, 3, 0)));
01964 static void dahdi_r2_on_context_log(openr2_context_t *r2context, openr2_log_level_t level, const char *fmt, va_list ap)
01965 {
01966 #define CONTEXT_TAG "Context - "
01967 char logmsg[256];
01968 char completemsg[sizeof(logmsg) + sizeof(CONTEXT_TAG) - 1];
01969 vsnprintf(logmsg, sizeof(logmsg), fmt, ap);
01970 snprintf(completemsg, sizeof(completemsg), CONTEXT_TAG "%s", logmsg);
01971 dahdi_r2_write_log(level, completemsg);
01972 #undef CONTEXT_TAG
01973 }
01974
01975 static void dahdi_r2_on_chan_log(openr2_chan_t *r2chan, openr2_log_level_t level, const char *fmt, va_list ap)
01976 __attribute__((format (printf, 3, 0)));
01977 static void dahdi_r2_on_chan_log(openr2_chan_t *r2chan, openr2_log_level_t level, const char *fmt, va_list ap)
01978 {
01979 #define CHAN_TAG "Chan "
01980 char logmsg[256];
01981 char completemsg[sizeof(logmsg) + sizeof(CHAN_TAG) - 1];
01982 vsnprintf(logmsg, sizeof(logmsg), fmt, ap);
01983 snprintf(completemsg, sizeof(completemsg), CHAN_TAG "%d - %s", openr2_chan_get_number(r2chan), logmsg);
01984 dahdi_r2_write_log(level, completemsg);
01985 }
01986
01987 static int dahdi_r2_on_dnis_digit_received(openr2_chan_t *r2chan, char digit)
01988 {
01989 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01990
01991 if (p->immediate) {
01992 return 0;
01993 }
01994 p->exten[p->mfcr2_dnis_index] = digit;
01995 p->rdnis[p->mfcr2_dnis_index] = digit;
01996 p->mfcr2_dnis_index++;
01997 p->exten[p->mfcr2_dnis_index] = 0;
01998 p->rdnis[p->mfcr2_dnis_index] = 0;
01999
02000 if ((p->mfcr2_dnis_matched ||
02001 (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num) && (p->mfcr2_dnis_matched = 1))) &&
02002 !ast_matchmore_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
02003 return 0;
02004 }
02005
02006 return 1;
02007 }
02008
02009 static void dahdi_r2_on_ani_digit_received(openr2_chan_t *r2chan, char digit)
02010 {
02011 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
02012 p->cid_num[p->mfcr2_ani_index] = digit;
02013 p->cid_name[p->mfcr2_ani_index] = digit;
02014 p->mfcr2_ani_index++;
02015 p->cid_num[p->mfcr2_ani_index] = 0;
02016 p->cid_name[p->mfcr2_ani_index] = 0;
02017 }
02018
02019 static void dahdi_r2_on_billing_pulse_received(openr2_chan_t *r2chan)
02020 {
02021 ast_verbose("MFC/R2 billing pulse received on channel %d\n", openr2_chan_get_number(r2chan));
02022 }
02023
02024 static openr2_event_interface_t dahdi_r2_event_iface = {
02025 .on_call_init = dahdi_r2_on_call_init,
02026 .on_call_offered = dahdi_r2_on_call_offered,
02027 .on_call_accepted = dahdi_r2_on_call_accepted,
02028 .on_call_answered = dahdi_r2_on_call_answered,
02029 .on_call_disconnect = dahdi_r2_on_call_disconnect,
02030 .on_call_end = dahdi_r2_on_call_end,
02031 .on_call_read = dahdi_r2_on_call_read,
02032 .on_hardware_alarm = dahdi_r2_on_hardware_alarm,
02033 .on_os_error = dahdi_r2_on_os_error,
02034 .on_protocol_error = dahdi_r2_on_protocol_error,
02035 .on_line_blocked = dahdi_r2_on_line_blocked,
02036 .on_line_idle = dahdi_r2_on_line_idle,
02037
02038 .on_context_log = (openr2_handle_context_logging_func)dahdi_r2_on_context_log,
02039 .on_dnis_digit_received = dahdi_r2_on_dnis_digit_received,
02040 .on_ani_digit_received = dahdi_r2_on_ani_digit_received,
02041
02042 .on_billing_pulse_received = dahdi_r2_on_billing_pulse_received
02043 };
02044
02045 static inline int16_t dahdi_r2_alaw_to_linear(uint8_t sample)
02046 {
02047 return AST_ALAW(sample);
02048 }
02049
02050 static inline uint8_t dahdi_r2_linear_to_alaw(int sample)
02051 {
02052 return AST_LIN2A(sample);
02053 }
02054
02055 static openr2_transcoder_interface_t dahdi_r2_transcode_iface = {
02056 dahdi_r2_alaw_to_linear,
02057 dahdi_r2_linear_to_alaw
02058 };
02059
02060 #endif
02061
02062 static int restore_gains(struct dahdi_pvt *p);
02063
02064 static void swap_subs(struct dahdi_pvt *p, int a, int b)
02065 {
02066 int tchan;
02067 int tinthreeway;
02068 struct ast_channel *towner;
02069
02070 ast_debug(1, "Swapping %d and %d\n", a, b);
02071
02072 tchan = p->subs[a].chan;
02073 towner = p->subs[a].owner;
02074 tinthreeway = p->subs[a].inthreeway;
02075
02076 p->subs[a].chan = p->subs[b].chan;
02077 p->subs[a].owner = p->subs[b].owner;
02078 p->subs[a].inthreeway = p->subs[b].inthreeway;
02079
02080 p->subs[b].chan = tchan;
02081 p->subs[b].owner = towner;
02082 p->subs[b].inthreeway = tinthreeway;
02083
02084 if (p->subs[a].owner)
02085 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].dfd);
02086 if (p->subs[b].owner)
02087 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].dfd);
02088 wakeup_sub(p, a, NULL);
02089 wakeup_sub(p, b, NULL);
02090 }
02091
02092 static int dahdi_open(char *fn)
02093 {
02094 int fd;
02095 int isnum;
02096 int chan = 0;
02097 int bs;
02098 int x;
02099 isnum = 1;
02100 for (x = 0; x < strlen(fn); x++) {
02101 if (!isdigit(fn[x])) {
02102 isnum = 0;
02103 break;
02104 }
02105 }
02106 if (isnum) {
02107 chan = atoi(fn);
02108 if (chan < 1) {
02109 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
02110 return -1;
02111 }
02112 fn = "/dev/dahdi/channel";
02113 }
02114 fd = open(fn, O_RDWR | O_NONBLOCK);
02115 if (fd < 0) {
02116 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
02117 return -1;
02118 }
02119 if (chan) {
02120 if (ioctl(fd, DAHDI_SPECIFY, &chan)) {
02121 x = errno;
02122 close(fd);
02123 errno = x;
02124 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
02125 return -1;
02126 }
02127 }
02128 bs = READ_SIZE;
02129 if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs) == -1) {
02130 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
02131 x = errno;
02132 close(fd);
02133 errno = x;
02134 return -1;
02135 }
02136 return fd;
02137 }
02138
02139 static void dahdi_close(int fd)
02140 {
02141 if (fd > 0)
02142 close(fd);
02143 }
02144
02145 static void dahdi_close_sub(struct dahdi_pvt *chan_pvt, int sub_num)
02146 {
02147 dahdi_close(chan_pvt->subs[sub_num].dfd);
02148 chan_pvt->subs[sub_num].dfd = -1;
02149 }
02150
02151 #if defined(HAVE_PRI)
02152 static void dahdi_close_pri_fd(struct dahdi_pri *pri, int fd_num)
02153 {
02154 dahdi_close(pri->fds[fd_num]);
02155 pri->fds[fd_num] = -1;
02156 }
02157 #endif
02158
02159 #if defined(HAVE_SS7)
02160 static void dahdi_close_ss7_fd(struct dahdi_ss7 *ss7, int fd_num)
02161 {
02162 dahdi_close(ss7->fds[fd_num]);
02163 ss7->fds[fd_num] = -1;
02164 }
02165 #endif
02166
02167 static int dahdi_setlinear(int dfd, int linear)
02168 {
02169 int res;
02170 res = ioctl(dfd, DAHDI_SETLINEAR, &linear);
02171 if (res)
02172 return res;
02173 return 0;
02174 }
02175
02176
02177 static int alloc_sub(struct dahdi_pvt *p, int x)
02178 {
02179 struct dahdi_bufferinfo bi;
02180 int res;
02181 if (p->subs[x].dfd >= 0) {
02182 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
02183 return -1;
02184 }
02185
02186 p->subs[x].dfd = dahdi_open("/dev/dahdi/pseudo");
02187 if (p->subs[x].dfd <= -1) {
02188 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
02189 return -1;
02190 }
02191
02192 res = ioctl(p->subs[x].dfd, DAHDI_GET_BUFINFO, &bi);
02193 if (!res) {
02194 bi.txbufpolicy = p->buf_policy;
02195 bi.rxbufpolicy = p->buf_policy;
02196 bi.numbufs = p->buf_no;
02197 res = ioctl(p->subs[x].dfd, DAHDI_SET_BUFINFO, &bi);
02198 if (res < 0) {
02199 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", x, strerror(errno));
02200 }
02201 } else
02202 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
02203
02204 if (ioctl(p->subs[x].dfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
02205 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].dfd, strerror(errno));
02206 dahdi_close_sub(p, x);
02207 p->subs[x].dfd = -1;
02208 return -1;
02209 }
02210 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].dfd, p->subs[x].chan);
02211 return 0;
02212 }
02213
02214 static int unalloc_sub(struct dahdi_pvt *p, int x)
02215 {
02216 if (!x) {
02217 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
02218 return -1;
02219 }
02220 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
02221 dahdi_close_sub(p, x);
02222 p->subs[x].linear = 0;
02223 p->subs[x].chan = 0;
02224 p->subs[x].owner = NULL;
02225 p->subs[x].inthreeway = 0;
02226 p->polarity = POLARITY_IDLE;
02227 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
02228 return 0;
02229 }
02230
02231 static int digit_to_dtmfindex(char digit)
02232 {
02233 if (isdigit(digit))
02234 return DAHDI_TONE_DTMF_BASE + (digit - '0');
02235 else if (digit >= 'A' && digit <= 'D')
02236 return DAHDI_TONE_DTMF_A + (digit - 'A');
02237 else if (digit >= 'a' && digit <= 'd')
02238 return DAHDI_TONE_DTMF_A + (digit - 'a');
02239 else if (digit == '*')
02240 return DAHDI_TONE_DTMF_s;
02241 else if (digit == '#')
02242 return DAHDI_TONE_DTMF_p;
02243 else
02244 return -1;
02245 }
02246
02247 static int dahdi_digit_begin(struct ast_channel *chan, char digit)
02248 {
02249 struct dahdi_pvt *pvt;
02250 int idx;
02251 int dtmf = -1;
02252
02253 pvt = chan->tech_pvt;
02254
02255 ast_mutex_lock(&pvt->lock);
02256
02257 idx = dahdi_get_index(chan, pvt, 0);
02258
02259 if ((idx != SUB_REAL) || !pvt->owner)
02260 goto out;
02261
02262 #ifdef HAVE_PRI
02263 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
02264 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
02265 if (pvt->setup_ack) {
02266 if (!pri_grab(pvt, pvt->pri)) {
02267 pri_information(pvt->pri->pri, pvt->call, digit);
02268 pri_rel(pvt->pri);
02269 } else
02270 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
02271 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
02272 int res;
02273 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
02274 res = strlen(pvt->dialdest);
02275 pvt->dialdest[res++] = digit;
02276 pvt->dialdest[res] = '\0';
02277 }
02278 goto out;
02279 }
02280 #endif
02281 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
02282 goto out;
02283
02284 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &dtmf)) {
02285 int res;
02286 struct dahdi_dialoperation zo = {
02287 .op = DAHDI_DIAL_OP_APPEND,
02288 };
02289
02290 zo.dialstr[0] = 'T';
02291 zo.dialstr[1] = digit;
02292 zo.dialstr[2] = '\0';
02293 if ((res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_DIAL, &zo)))
02294 ast_log(LOG_WARNING, "Couldn't dial digit %c: %s\n", digit, strerror(errno));
02295 else
02296 pvt->dialing = 1;
02297 } else {
02298 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
02299 pvt->dialing = 1;
02300 pvt->begindigit = digit;
02301 }
02302
02303 out:
02304 ast_mutex_unlock(&pvt->lock);
02305
02306 return 0;
02307 }
02308
02309 static int dahdi_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
02310 {
02311 struct dahdi_pvt *pvt;
02312 int res = 0;
02313 int idx;
02314 int x;
02315
02316 pvt = chan->tech_pvt;
02317
02318 ast_mutex_lock(&pvt->lock);
02319
02320 idx = dahdi_get_index(chan, pvt, 0);
02321
02322 if ((idx != SUB_REAL) || !pvt->owner || pvt->pulse)
02323 goto out;
02324
02325 #ifdef HAVE_PRI
02326
02327 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
02328 && !pvt->begindigit)
02329 goto out;
02330 #endif
02331
02332 if (pvt->begindigit) {
02333 x = -1;
02334 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
02335 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &x);
02336 pvt->dialing = 0;
02337 pvt->begindigit = 0;
02338 }
02339
02340 out:
02341 ast_mutex_unlock(&pvt->lock);
02342
02343 return res;
02344 }
02345
02346 static char *events[] = {
02347 "No event",
02348 "On hook",
02349 "Ring/Answered",
02350 "Wink/Flash",
02351 "Alarm",
02352 "No more alarm",
02353 "HDLC Abort",
02354 "HDLC Overrun",
02355 "HDLC Bad FCS",
02356 "Dial Complete",
02357 "Ringer On",
02358 "Ringer Off",
02359 "Hook Transition Complete",
02360 "Bits Changed",
02361 "Pulse Start",
02362 "Timer Expired",
02363 "Timer Ping",
02364 "Polarity Reversal",
02365 "Ring Begin",
02366 };
02367
02368 static struct {
02369 int alarm;
02370 char *name;
02371 } alarms[] = {
02372 { DAHDI_ALARM_RED, "Red Alarm" },
02373 { DAHDI_ALARM_YELLOW, "Yellow Alarm" },
02374 { DAHDI_ALARM_BLUE, "Blue Alarm" },
02375 { DAHDI_ALARM_RECOVER, "Recovering" },
02376 { DAHDI_ALARM_LOOPBACK, "Loopback" },
02377 { DAHDI_ALARM_NOTOPEN, "Not Open" },
02378 { DAHDI_ALARM_NONE, "None" },
02379 };
02380
02381 static char *alarm2str(int alm)
02382 {
02383 int x;
02384 for (x = 0; x < ARRAY_LEN(alarms); x++) {
02385 if (alarms[x].alarm & alm)
02386 return alarms[x].name;
02387 }
02388 return alm ? "Unknown Alarm" : "No Alarm";
02389 }
02390
02391 static char *event2str(int event)
02392 {
02393 static char buf[256];
02394 if ((event < (ARRAY_LEN(events))) && (event > -1))
02395 return events[event];
02396 sprintf(buf, "Event %d", event);
02397 return buf;
02398 }
02399
02400 #ifdef HAVE_PRI
02401 static char *dialplan2str(int dialplan)
02402 {
02403 if (dialplan == -1 || dialplan == -2) {
02404 return("Dynamically set dialplan in ISDN");
02405 }
02406 return (pri_plan2str(dialplan));
02407 }
02408 #endif
02409
02410 static char *dahdi_sig2str(int sig)
02411 {
02412 static char buf[256];
02413 switch (sig) {
02414 case SIG_EM:
02415 return "E & M Immediate";
02416 case SIG_EMWINK:
02417 return "E & M Wink";
02418 case SIG_EM_E1:
02419 return "E & M E1";
02420 case SIG_FEATD:
02421 return "Feature Group D (DTMF)";
02422 case SIG_FEATDMF:
02423 return "Feature Group D (MF)";
02424 case SIG_FEATDMF_TA:
02425 return "Feature Groud D (MF) Tandem Access";
02426 case SIG_FEATB:
02427 return "Feature Group B (MF)";
02428 case SIG_E911:
02429 return "E911 (MF)";
02430 case SIG_FGC_CAMA:
02431 return "FGC/CAMA (Dialpulse)";
02432 case SIG_FGC_CAMAMF:
02433 return "FGC/CAMA (MF)";
02434 case SIG_FXSLS:
02435 return "FXS Loopstart";
02436 case SIG_FXSGS:
02437 return "FXS Groundstart";
02438 case SIG_FXSKS:
02439 return "FXS Kewlstart";
02440 case SIG_FXOLS:
02441 return "FXO Loopstart";
02442 case SIG_FXOGS:
02443 return "FXO Groundstart";
02444 case SIG_FXOKS:
02445 return "FXO Kewlstart";
02446 case SIG_PRI:
02447 return "ISDN PRI";
02448 case SIG_BRI:
02449 return "ISDN BRI Point to Point";
02450 case SIG_BRI_PTMP:
02451 return "ISDN BRI Point to MultiPoint";
02452 case SIG_SS7:
02453 return "SS7";
02454 case SIG_MFCR2:
02455 return "MFC/R2";
02456 case SIG_SF:
02457 return "SF (Tone) Immediate";
02458 case SIG_SFWINK:
02459 return "SF (Tone) Wink";
02460 case SIG_SF_FEATD:
02461 return "SF (Tone) with Feature Group D (DTMF)";
02462 case SIG_SF_FEATDMF:
02463 return "SF (Tone) with Feature Group D (MF)";
02464 case SIG_SF_FEATB:
02465 return "SF (Tone) with Feature Group B (MF)";
02466 case SIG_GR303FXOKS:
02467 return "GR-303 with FXOKS";
02468 case SIG_GR303FXSKS:
02469 return "GR-303 with FXSKS";
02470 case 0:
02471 return "Pseudo";
02472 default:
02473 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
02474 return buf;
02475 }
02476 }
02477
02478 #define sig2str dahdi_sig2str
02479
02480 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx, int slavechannel)
02481 {
02482
02483
02484 struct dahdi_confinfo zi;
02485
02486 memset(&zi, 0, sizeof(zi));
02487 zi.chan = 0;
02488
02489 if (slavechannel > 0) {
02490
02491 zi.confmode = DAHDI_CONF_DIGITALMON;
02492 zi.confno = slavechannel;
02493 } else {
02494 if (!idx) {
02495
02496 zi.confmode = DAHDI_CONF_REALANDPSEUDO | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER |
02497 DAHDI_CONF_PSEUDO_TALKER | DAHDI_CONF_PSEUDO_LISTENER;
02498 } else
02499 zi.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
02500 zi.confno = p->confno;
02501 }
02502 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
02503 return 0;
02504 if (c->dfd < 0)
02505 return 0;
02506 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
02507 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d: %s\n", c->dfd, zi.confmode, zi.confno, strerror(errno));
02508 return -1;
02509 }
02510 if (slavechannel < 1) {
02511 p->confno = zi.confno;
02512 }
02513 memcpy(&c->curconf, &zi, sizeof(c->curconf));
02514 ast_debug(1, "Added %d to conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
02515 return 0;
02516 }
02517
02518 static int isourconf(struct dahdi_pvt *p, struct dahdi_subchannel *c)
02519 {
02520
02521 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == DAHDI_CONF_DIGITALMON))
02522 return 1;
02523
02524 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & DAHDI_CONF_TALKER))
02525 return 1;
02526 return 0;
02527 }
02528
02529 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx)
02530 {
02531 struct dahdi_confinfo zi;
02532 if (
02533 (c->dfd < 0) ||
02534
02535 !isourconf(p, c)
02536
02537 ) return 0;
02538 memset(&zi, 0, sizeof(zi));
02539 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
02540 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d: %s\n", c->dfd, c->curconf.confmode, c->curconf.confno, strerror(errno));
02541 return -1;
02542 }
02543 ast_debug(1, "Removed %d from conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
02544 memcpy(&c->curconf, &zi, sizeof(c->curconf));
02545 return 0;
02546 }
02547
02548 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out)
02549 {
02550 int x;
02551 int useslavenative;
02552 struct dahdi_pvt *slave = NULL;
02553
02554 useslavenative = 1;
02555
02556 for (x = 0; x < 3; x++) {
02557
02558
02559 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway)
02560 useslavenative = 0;
02561 }
02562
02563
02564 if (useslavenative) {
02565 for (x = 0; x < MAX_SLAVES; x++) {
02566 if (p->slaves[x]) {
02567 if (slave) {
02568
02569
02570 slave = NULL;
02571 useslavenative = 0;
02572 break;
02573 } else {
02574
02575 slave = p->slaves[x];
02576 }
02577 }
02578 }
02579 }
02580
02581 if (!slave)
02582 useslavenative = 0;
02583 else if (slave->law != p->law) {
02584 useslavenative = 0;
02585 slave = NULL;
02586 }
02587 if (out)
02588 *out = slave;
02589 return useslavenative;
02590 }
02591
02592 static int reset_conf(struct dahdi_pvt *p)
02593 {
02594 p->confno = -1;
02595 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
02596 if (p->subs[SUB_REAL].dfd > -1) {
02597 struct dahdi_confinfo zi;
02598
02599 memset(&zi, 0, sizeof(zi));
02600 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &zi))
02601 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d: %s\n", p->channel, strerror(errno));
02602 }
02603 return 0;
02604 }
02605
02606 static int update_conf(struct dahdi_pvt *p)
02607 {
02608 int needconf = 0;
02609 int x;
02610 int useslavenative;
02611 struct dahdi_pvt *slave = NULL;
02612
02613 useslavenative = isslavenative(p, &slave);
02614
02615 for (x = 0; x < 3; x++) {
02616
02617 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway) {
02618 conf_add(p, &p->subs[x], x, 0);
02619 needconf++;
02620 } else {
02621 conf_del(p, &p->subs[x], x);
02622 }
02623 }
02624
02625
02626 for (x = 0; x < MAX_SLAVES; x++) {
02627 if (p->slaves[x]) {
02628 if (useslavenative)
02629 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
02630 else {
02631 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
02632 needconf++;
02633 }
02634 }
02635 }
02636
02637 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
02638 if (useslavenative)
02639 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
02640 else {
02641 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
02642 needconf++;
02643 }
02644 }
02645
02646 if (p->master) {
02647 if (isslavenative(p->master, NULL)) {
02648 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
02649 } else {
02650 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
02651 }
02652 }
02653 if (!needconf) {
02654
02655
02656 p->confno = -1;
02657 }
02658 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
02659 return 0;
02660 }
02661
02662 static void dahdi_enable_ec(struct dahdi_pvt *p)
02663 {
02664 int x;
02665 int res;
02666 if (!p)
02667 return;
02668 if (p->echocanon) {
02669 ast_debug(1, "Echo cancellation already on\n");
02670 return;
02671 }
02672 if (p->digital) {
02673 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
02674 return;
02675 }
02676 if (p->echocancel.head.tap_length) {
02677 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
02678 x = 1;
02679 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x);
02680 if (res)
02681 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n", p->channel, strerror(errno));
02682 }
02683 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &p->echocancel);
02684 if (res) {
02685 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d (%s)\n", p->channel, strerror(errno));
02686 } else {
02687 p->echocanon = 1;
02688 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
02689 }
02690 } else
02691 ast_debug(1, "No echo cancellation requested\n");
02692 }
02693
02694 static void dahdi_train_ec(struct dahdi_pvt *p)
02695 {
02696 int x;
02697 int res;
02698
02699 if (p && p->echocanon && p->echotraining) {
02700 x = p->echotraining;
02701 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOTRAIN, &x);
02702 if (res)
02703 ast_log(LOG_WARNING, "Unable to request echo training on channel %d: %s\n", p->channel, strerror(errno));
02704 else
02705 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
02706 } else {
02707 ast_debug(1, "No echo training requested\n");
02708 }
02709 }
02710
02711 static void dahdi_disable_ec(struct dahdi_pvt *p)
02712 {
02713 int res;
02714
02715 if (p->echocanon) {
02716 struct dahdi_echocanparams ecp = { .tap_length = 0 };
02717
02718 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &ecp);
02719
02720 if (res)
02721 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d: %s\n", p->channel, strerror(errno));
02722 else
02723 ast_debug(1, "Disabled echo cancellation on channel %d\n", p->channel);
02724 }
02725
02726 p->echocanon = 0;
02727 }
02728
02729 static void fill_txgain(struct dahdi_gains *g, float gain, int law)
02730 {
02731 int j;
02732 int k;
02733 float linear_gain = pow(10.0, gain / 20.0);
02734
02735 switch (law) {
02736 case DAHDI_LAW_ALAW:
02737 for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
02738 if (gain) {
02739 k = (int) (((float) AST_ALAW(j)) * linear_gain);
02740 if (k > 32767) k = 32767;
02741 if (k < -32767) k = -32767;
02742 g->txgain[j] = AST_LIN2A(k);
02743 } else {
02744 g->txgain[j] = j;
02745 }
02746 }
02747 break;
02748 case DAHDI_LAW_MULAW:
02749 for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
02750 if (gain) {
02751 k = (int) (((float) AST_MULAW(j)) * linear_gain);
02752 if (k > 32767) k = 32767;
02753 if (k < -32767) k = -32767;
02754 g->txgain[j] = AST_LIN2MU(k);
02755 } else {
02756 g->txgain[j] = j;
02757 }
02758 }
02759 break;
02760 }
02761 }
02762
02763 static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
02764 {
02765 int j;
02766 int k;
02767 float linear_gain = pow(10.0, gain / 20.0);
02768
02769 switch (law) {
02770 case DAHDI_LAW_ALAW:
02771 for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
02772 if (gain) {
02773 k = (int) (((float) AST_ALAW(j)) * linear_gain);
02774 if (k > 32767) k = 32767;
02775 if (k < -32767) k = -32767;
02776 g->rxgain[j] = AST_LIN2A(k);
02777 } else {
02778 g->rxgain[j] = j;
02779 }
02780 }
02781 break;
02782 case DAHDI_LAW_MULAW:
02783 for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
02784 if (gain) {
02785 k = (int) (((float) AST_MULAW(j)) * linear_gain);
02786 if (k > 32767) k = 32767;
02787 if (k < -32767) k = -32767;
02788 g->rxgain[j] = AST_LIN2MU(k);
02789 } else {
02790 g->rxgain[j] = j;
02791 }
02792 }
02793 break;
02794 }
02795 }
02796
02797 static int set_actual_txgain(int fd, int chan, float gain, int law)
02798 {
02799 struct dahdi_gains g;
02800 int res;
02801
02802 memset(&g, 0, sizeof(g));
02803 g.chan = chan;
02804 res = ioctl(fd, DAHDI_GETGAINS, &g);
02805 if (res) {
02806 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
02807 return res;
02808 }
02809
02810 fill_txgain(&g, gain, law);
02811
02812 return ioctl(fd, DAHDI_SETGAINS, &g);
02813 }
02814
02815 static int set_actual_rxgain(int fd, int chan, float gain, int law)
02816 {
02817 struct dahdi_gains g;
02818 int res;
02819
02820 memset(&g, 0, sizeof(g));
02821 g.chan = chan;
02822 res = ioctl(fd, DAHDI_GETGAINS, &g);
02823 if (res) {
02824 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
02825 return res;
02826 }
02827
02828 fill_rxgain(&g, gain, law);
02829
02830 return ioctl(fd, DAHDI_SETGAINS, &g);
02831 }
02832
02833 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
02834 {
02835 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
02836 }
02837
02838 static int bump_gains(struct dahdi_pvt *p)
02839 {
02840 int res;
02841
02842
02843 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
02844 if (res) {
02845 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
02846 return -1;
02847 }
02848
02849 return 0;
02850 }
02851
02852 static int restore_gains(struct dahdi_pvt *p)
02853 {
02854 int res;
02855
02856 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
02857 if (res) {
02858 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
02859 return -1;
02860 }
02861
02862 return 0;
02863 }
02864
02865 static inline int dahdi_set_hook(int fd, int hs)
02866 {
02867 int x, res;
02868
02869 x = hs;
02870 res = ioctl(fd, DAHDI_HOOK, &x);
02871
02872 if (res < 0) {
02873 if (errno == EINPROGRESS)
02874 return 0;
02875 ast_log(LOG_WARNING, "DAHDI hook failed returned %d (trying %d): %s\n", res, hs, strerror(errno));
02876
02877 }
02878
02879 return res;
02880 }
02881
02882 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted)
02883 {
02884 int x, y, res;
02885 x = muted;
02886 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
02887 y = 1;
02888 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &y);
02889 if (res)
02890 ast_log(LOG_WARNING, "Unable to set audio mode on %d: %s\n", p->channel, strerror(errno));
02891 }
02892 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_CONFMUTE, &x);
02893 if (res < 0)
02894 ast_log(LOG_WARNING, "DAHDI confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
02895 return res;
02896 }
02897
02898 static int save_conference(struct dahdi_pvt *p)
02899 {
02900 struct dahdi_confinfo c;
02901 int res;
02902 if (p->saveconf.confmode) {
02903 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
02904 return -1;
02905 }
02906 p->saveconf.chan = 0;
02907 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &p->saveconf);
02908 if (res) {
02909 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
02910 p->saveconf.confmode = 0;
02911 return -1;
02912 }
02913 memset(&c, 0, sizeof(c));
02914 c.confmode = DAHDI_CONF_NORMAL;
02915 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &c);
02916 if (res) {
02917 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
02918 return -1;
02919 }
02920 ast_debug(1, "Disabled conferencing\n");
02921 return 0;
02922 }
02923
02924
02925
02926
02927
02928
02929
02930
02931
02932
02933
02934
02935
02936
02937
02938
02939
02940
02941
02942 static void notify_message(char *mailbox_full, int thereornot)
02943 {
02944 char s[sizeof(mwimonitornotify) + 80];
02945 struct ast_event *event;
02946 char *mailbox, *context;
02947
02948
02949 context = mailbox = ast_strdupa(mailbox_full);
02950 strsep(&context, "@");
02951 if (ast_strlen_zero(context))
02952 context = "default";
02953
02954 if (!(event = ast_event_new(AST_EVENT_MWI,
02955 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
02956 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
02957 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
02958 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
02959 AST_EVENT_IE_END))) {
02960 return;
02961 }
02962
02963 ast_event_queue_and_cache(event);
02964
02965 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
02966 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
02967 ast_safe_system(s);
02968 }
02969 }
02970
02971 static int restore_conference(struct dahdi_pvt *p)
02972 {
02973 int res;
02974 if (p->saveconf.confmode) {
02975 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &p->saveconf);
02976 p->saveconf.confmode = 0;
02977 if (res) {
02978 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
02979 return -1;
02980 }
02981 }
02982 ast_debug(1, "Restored conferencing\n");
02983 return 0;
02984 }
02985
02986 static int send_callerid(struct dahdi_pvt *p);
02987
02988 static int send_cwcidspill(struct dahdi_pvt *p)
02989 {
02990 p->callwaitcas = 0;
02991 p->cidcwexpire = 0;
02992 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
02993 return -1;
02994 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
02995
02996 p->cidlen += READ_SIZE * 4;
02997 p->cidpos = 0;
02998 send_callerid(p);
02999 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
03000 return 0;
03001 }
03002
03003 static int has_voicemail(struct dahdi_pvt *p)
03004 {
03005 int new_msgs;
03006 struct ast_event *event;
03007 char *mailbox, *context;
03008
03009 mailbox = context = ast_strdupa(p->mailbox);
03010 strsep(&context, "@");
03011 if (ast_strlen_zero(context))
03012 context = "default";
03013
03014 event = ast_event_get_cached(AST_EVENT_MWI,
03015 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
03016 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
03017 AST_EVENT_IE_END);
03018
03019 if (event) {
03020 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
03021 ast_event_destroy(event);
03022 } else
03023 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
03024
03025 return new_msgs;
03026 }
03027
03028 static int send_callerid(struct dahdi_pvt *p)
03029 {
03030
03031 int res;
03032
03033 if (p->subs[SUB_REAL].linear) {
03034 p->subs[SUB_REAL].linear = 0;
03035 dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
03036 }
03037 while (p->cidpos < p->cidlen) {
03038 res = write(p->subs[SUB_REAL].dfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
03039 if (res < 0) {
03040 if (errno == EAGAIN)
03041 return 0;
03042 else {
03043 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
03044 return -1;
03045 }
03046 }
03047 if (!res)
03048 return 0;
03049 p->cidpos += res;
03050 }
03051 ast_free(p->cidspill);
03052 p->cidspill = NULL;
03053 if (p->callwaitcas) {
03054
03055 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
03056 } else
03057 restore_conference(p);
03058 return 0;
03059 }
03060
03061 static int dahdi_callwait(struct ast_channel *ast)
03062 {
03063 struct dahdi_pvt *p = ast->tech_pvt;
03064 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
03065 if (p->cidspill) {
03066 ast_log(LOG_WARNING, "Spill already exists?!?\n");
03067 ast_free(p->cidspill);
03068 }
03069 if (!(p->cidspill = ast_malloc(2400 + 680 + READ_SIZE * 4)))
03070 return -1;
03071 save_conference(p);
03072
03073 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
03074 if (!p->callwaitrings && p->callwaitingcallerid) {
03075 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
03076 p->callwaitcas = 1;
03077 p->cidlen = 2400 + 680 + READ_SIZE * 4;
03078 } else {
03079 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
03080 p->callwaitcas = 0;
03081 p->cidlen = 2400 + READ_SIZE * 4;
03082 }
03083 p->cidpos = 0;
03084 send_callerid(p);
03085
03086 return 0;
03087 }
03088
03089 #if defined(HAVE_SS7)
03090 static unsigned char cid_pres2ss7pres(int cid_pres)
03091 {
03092 return (cid_pres >> 5) & 0x03;
03093 }
03094 #endif
03095
03096 #if defined(HAVE_SS7)
03097 static unsigned char cid_pres2ss7screen(int cid_pres)
03098 {
03099 return cid_pres & 0x03;
03100 }
03101 #endif
03102
03103 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout)
03104 {
03105 struct dahdi_pvt *p = ast->tech_pvt;
03106 int x, res, idx,mysig;
03107 char *c, *n, *l;
03108 #ifdef HAVE_PRI
03109 char *s = NULL;
03110 #endif
03111 char dest[256];
03112 ast_mutex_lock(&p->lock);
03113 ast_copy_string(dest, rdest, sizeof(dest));
03114 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
03115 if ((ast->_state == AST_STATE_BUSY)) {
03116 p->subs[SUB_REAL].needbusy = 1;
03117 ast_mutex_unlock(&p->lock);
03118 return 0;
03119 }
03120 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
03121 ast_log(LOG_WARNING, "dahdi_call called on %s, neither down nor reserved\n", ast->name);
03122 ast_mutex_unlock(&p->lock);
03123 return -1;
03124 }
03125 p->waitingfordt.tv_sec = 0;
03126 p->dialednone = 0;
03127 if ((p->radio || (p->oprmode < 0)))
03128 {
03129
03130 ast_setstate(ast, AST_STATE_UP);
03131 ast_mutex_unlock(&p->lock);
03132 return 0;
03133 }
03134 x = DAHDI_FLUSH_READ | DAHDI_FLUSH_WRITE;
03135 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
03136 if (res)
03137 ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", p->channel, strerror(errno));
03138 p->outgoing = 1;
03139
03140 if (IS_DIGITAL(ast->transfercapability)){
03141 set_actual_gain(p->subs[SUB_REAL].dfd, 0, 0, 0, p->law);
03142 } else {
03143 set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
03144 }
03145
03146 mysig = p->sig;
03147 if (p->outsigmod > -1)
03148 mysig = p->outsigmod;
03149
03150 switch (mysig) {
03151 case SIG_FXOLS:
03152 case SIG_FXOGS:
03153 case SIG_FXOKS:
03154 if (p->owner == ast) {
03155
03156
03157
03158 p->dialing = 1;
03159 if (p->use_callerid) {
03160
03161 if (p->cidspill) {
03162 ast_log(LOG_WARNING, "cidspill already exists??\n");
03163 ast_free(p->cidspill);
03164 }
03165 p->callwaitcas = 0;
03166 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
03167 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
03168 p->cidpos = 0;
03169 send_callerid(p);
03170 }
03171 }
03172
03173 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
03174 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
03175 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
03176 p->cidrings = cidrings[p->distinctivering - 1];
03177 } else {
03178 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
03179 ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
03180 p->cidrings = p->sendcalleridafter;
03181 }
03182
03183
03184 c = strchr(dest, '/');
03185 if (c)
03186 c++;
03187 if (c && (strlen(c) < p->stripmsd)) {
03188 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03189 c = NULL;
03190 }
03191 if (c) {
03192 p->dop.op = DAHDI_DIAL_OP_REPLACE;
03193 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
03194 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
03195 } else {
03196 p->dop.dialstr[0] = '\0';
03197 }
03198 x = DAHDI_RING;
03199 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x) && (errno != EINPROGRESS)) {
03200 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
03201 ast_mutex_unlock(&p->lock);
03202 return -1;
03203 }
03204 p->dialing = 1;
03205 } else {
03206
03207 p->callwaitrings = 0;
03208 if (ast->cid.cid_num)
03209 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
03210 else
03211 p->callwait_num[0] = '\0';
03212 if (ast->cid.cid_name)
03213 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
03214 else
03215 p->callwait_name[0] = '\0';
03216
03217 if (dahdi_callwait(ast)) {
03218 ast_mutex_unlock(&p->lock);
03219 return -1;
03220 }
03221
03222 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].dfd, DAHDI_TONE_RINGTONE))
03223 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
03224 }
03225 n = ast->cid.cid_name;
03226 l = ast->cid.cid_num;
03227 if (l)
03228 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
03229 else
03230 p->lastcid_num[0] = '\0';
03231 if (n)
03232 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
03233 else
03234 p->lastcid_name[0] = '\0';
03235 ast_setstate(ast, AST_STATE_RINGING);
03236 idx = dahdi_get_index(ast, p, 0);
03237 if (idx > -1) {
03238 p->subs[idx].needringing = 1;
03239 }
03240 break;
03241 case SIG_FXSLS:
03242 case SIG_FXSGS:
03243 case SIG_FXSKS:
03244 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
03245 ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
03246 p->polaritydelaytv = ast_tvnow();
03247 }
03248
03249 case SIG_EMWINK:
03250 case SIG_EM:
03251 case SIG_EM_E1:
03252 case SIG_FEATD:
03253 case SIG_FEATDMF:
03254 case SIG_E911:
03255 case SIG_FGC_CAMA:
03256 case SIG_FGC_CAMAMF:
03257 case SIG_FEATB:
03258 case SIG_SFWINK:
03259 case SIG_SF:
03260 case SIG_SF_FEATD:
03261 case SIG_SF_FEATDMF:
03262 case SIG_FEATDMF_TA:
03263 case SIG_SF_FEATB:
03264 c = strchr(dest, '/');
03265 if (c)
03266 c++;
03267 else
03268 c = "";
03269 if (strlen(c) < p->stripmsd) {
03270 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03271 ast_mutex_unlock(&p->lock);
03272 return -1;
03273 }
03274 #ifdef HAVE_PRI
03275
03276 if (!p->pri) {
03277 #endif
03278 x = DAHDI_START;
03279 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
03280 if (res < 0) {
03281 if (errno != EINPROGRESS) {
03282 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
03283 ast_mutex_unlock(&p->lock);
03284 return -1;
03285 }
03286 }
03287 #ifdef HAVE_PRI
03288 }
03289 #endif
03290 ast_debug(1, "Dialing '%s'\n", c);
03291 p->dop.op = DAHDI_DIAL_OP_REPLACE;
03292
03293 c += p->stripmsd;
03294
03295 switch (mysig) {
03296 case SIG_FEATD:
03297 l = ast->cid.cid_num;
03298 if (l)
03299 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
03300 else
03301 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
03302 break;
03303 case SIG_FEATDMF:
03304 l = ast->cid.cid_num;
03305 if (l)
03306 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
03307 else
03308 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
03309 break;
03310 case SIG_FEATDMF_TA:
03311 {
03312 const char *cic, *ozz;
03313
03314
03315 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
03316 if (!ozz)
03317 ozz = defaultozz;
03318 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
03319 if (!cic)
03320 cic = defaultcic;
03321 if (!ozz || !cic) {
03322 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
03323 ast_mutex_unlock(&p->lock);
03324 return -1;
03325 }
03326 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
03327 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
03328 p->whichwink = 0;
03329 }
03330 break;
03331 case SIG_E911:
03332 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
03333 break;
03334 case SIG_FGC_CAMA:
03335 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
03336 break;
03337 case SIG_FGC_CAMAMF:
03338 case SIG_FEATB:
03339 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
03340 break;
03341 default:
03342 if (p->pulse)
03343 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
03344 else
03345 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
03346 break;
03347 }
03348
03349 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
03350 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
03351 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
03352 p->echorest[sizeof(p->echorest) - 1] = '\0';
03353 p->echobreak = 1;
03354 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
03355 } else
03356 p->echobreak = 0;
03357
03358
03359 #ifdef HAVE_PRI
03360 if (!p->pri) {
03361 #endif
03362 if( p->waitfordialtone && CANPROGRESSDETECT(p) && p->dsp ) {
03363 ast_log(LOG_DEBUG, "Defer dialling for %dms or dialtone\n", p->waitfordialtone);
03364 gettimeofday(&p->waitingfordt,NULL);
03365 ast_setstate(ast, AST_STATE_OFFHOOK);
03366 break;
03367 }
03368 #ifdef HAVE_PRI
03369 }
03370 #endif
03371 if (!res) {
03372 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
03373 int saveerr = errno;
03374
03375 x = DAHDI_ONHOOK;
03376 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
03377 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
03378 ast_mutex_unlock(&p->lock);
03379 return -1;
03380 }
03381 } else
03382 ast_debug(1, "Deferring dialing...\n");
03383
03384 p->dialing = 1;
03385 if (ast_strlen_zero(c))
03386 p->dialednone = 1;
03387 ast_setstate(ast, AST_STATE_DIALING);
03388 break;
03389 case 0:
03390
03391 ast_setstate(ast, AST_STATE_UP);
03392 break;
03393 case SIG_PRI:
03394 case SIG_BRI:
03395 case SIG_BRI_PTMP:
03396 case SIG_SS7:
03397 case SIG_MFCR2:
03398
03399 p->dialdest[0] = '\0';
03400 p->dialing = 1;
03401 break;
03402 default:
03403 ast_debug(1, "not yet implemented\n");
03404 ast_mutex_unlock(&p->lock);
03405 return -1;
03406 }
03407 #ifdef HAVE_SS7
03408 if (p->ss7) {
03409 char ss7_called_nai;
03410 int called_nai_strip;
03411 char ss7_calling_nai;
03412 int calling_nai_strip;
03413 const char *charge_str = NULL;
03414 const char *gen_address = NULL;
03415 const char *gen_digits = NULL;
03416 const char *gen_dig_type = NULL;
03417 const char *gen_dig_scheme = NULL;
03418 const char *gen_name = NULL;
03419 const char *jip_digits = NULL;
03420 const char *lspi_ident = NULL;
03421 const char *rlt_flag = NULL;
03422 const char *call_ref_id = NULL;
03423 const char *call_ref_pc = NULL;
03424 const char *send_far = NULL;
03425
03426 c = strchr(dest, '/');
03427 if (c) {
03428 c++;
03429 } else {
03430 c = "";
03431 }
03432 if (strlen(c) < p->stripmsd) {
03433 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03434 ast_mutex_unlock(&p->lock);
03435 return -1;
03436 }
03437
03438 if (!p->hidecallerid) {
03439 l = ast->cid.cid_num;
03440 } else {
03441 l = NULL;
03442 }
03443
03444 if (ss7_grab(p, p->ss7)) {
03445 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
03446 ast_mutex_unlock(&p->lock);
03447 return -1;
03448 }
03449 p->digital = IS_DIGITAL(ast->transfercapability);
03450 p->ss7call = isup_new_call(p->ss7->ss7);
03451
03452 if (!p->ss7call) {
03453 ss7_rel(p->ss7);
03454 ast_mutex_unlock(&p->lock);
03455 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
03456 return -1;
03457 }
03458
03459 called_nai_strip = 0;
03460 ss7_called_nai = p->ss7->called_nai;
03461 if (ss7_called_nai == SS7_NAI_DYNAMIC) {
03462 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
03463 called_nai_strip = strlen(p->ss7->internationalprefix);
03464 ss7_called_nai = SS7_NAI_INTERNATIONAL;
03465 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
03466 called_nai_strip = strlen(p->ss7->nationalprefix);
03467 ss7_called_nai = SS7_NAI_NATIONAL;
03468 } else {
03469 ss7_called_nai = SS7_NAI_SUBSCRIBER;
03470 }
03471 }
03472 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
03473
03474 calling_nai_strip = 0;
03475 ss7_calling_nai = p->ss7->calling_nai;
03476 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) {
03477 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
03478 calling_nai_strip = strlen(p->ss7->internationalprefix);
03479 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
03480 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
03481 calling_nai_strip = strlen(p->ss7->nationalprefix);
03482 ss7_calling_nai = SS7_NAI_NATIONAL;
03483 } else {
03484 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
03485 }
03486 }
03487 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
03488 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
03489 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
03490
03491 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
03492 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
03493
03494 ast_channel_lock(ast);
03495
03496 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
03497 if (charge_str)
03498 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
03499
03500 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
03501 if (gen_address)
03502 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);
03503
03504 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
03505 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
03506 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
03507 if (gen_digits)
03508 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
03509
03510 gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
03511 if (gen_name)
03512 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
03513
03514 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
03515 if (jip_digits)
03516 isup_set_jip_digits(p->ss7call, jip_digits);
03517
03518 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
03519 if (lspi_ident)
03520 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
03521
03522 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
03523 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
03524 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00);
03525 }
03526
03527 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
03528 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
03529 if (call_ref_id && call_ref_pc) {
03530 isup_set_callref(p->ss7call, atoi(call_ref_id),
03531 call_ref_pc ? atoi(call_ref_pc) : 0);
03532 }
03533
03534 send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
03535 if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
03536 (isup_far(p->ss7->ss7, p->ss7call));
03537
03538 ast_channel_unlock(ast);
03539
03540 isup_iam(p->ss7->ss7, p->ss7call);
03541 ast_setstate(ast, AST_STATE_DIALING);
03542 ss7_rel(p->ss7);
03543 }
03544 #endif
03545 #ifdef HAVE_OPENR2
03546 if (p->mfcr2) {
03547 openr2_calling_party_category_t chancat;
03548 int callres = 0;
03549 char *c, *l;
03550
03551 c = strchr(dest, '/');
03552 if (c) {
03553 c++;
03554 } else {
03555 c = "";
03556 }
03557 if (!p->hidecallerid) {
03558 l = ast->cid.cid_num;
03559 } else {
03560 l = NULL;
03561 }
03562 if (strlen(c) < p->stripmsd) {
03563 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03564 ast_mutex_unlock(&p->lock);
03565 return -1;
03566 }
03567 p->dialing = 1;
03568 ast_channel_lock(ast);
03569 chancat = dahdi_r2_get_channel_category(ast);
03570 ast_channel_unlock(ast);
03571 callres = openr2_chan_make_call(p->r2chan, l, (c + p->stripmsd), chancat);
03572 if (-1 == callres) {
03573 ast_mutex_unlock(&p->lock);
03574 ast_log(LOG_ERROR, "unable to make new MFC/R2 call!\n");
03575 return -1;
03576 }
03577 ast_setstate(ast, AST_STATE_DIALING);
03578 }
03579 #endif
03580 #ifdef HAVE_PRI
03581 if (p->pri) {
03582 struct pri_sr *sr;
03583 #ifdef SUPPORT_USERUSER
03584 const char *useruser;
03585 #endif
03586 int pridialplan;
03587 int dp_strip;
03588 int prilocaldialplan;
03589 int ldp_strip;
03590 int exclusive;
03591 const char *rr_str;
03592 int redirect_reason;
03593
03594 c = strchr(dest, '/');
03595 if (c) {
03596 c++;
03597 } else {
03598 c = "";
03599 }
03600
03601 l = NULL;
03602 n = NULL;
03603 if (!p->hidecallerid) {
03604 l = ast->cid.cid_num;
03605 if (!p->hidecalleridname) {
03606 n = ast->cid.cid_name;
03607 }
03608 }
03609
03610 if (strlen(c) < p->stripmsd) {
03611 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03612 ast_mutex_unlock(&p->lock);
03613 return -1;
03614 }
03615 if (mysig != SIG_FXSKS) {
03616 p->dop.op = DAHDI_DIAL_OP_REPLACE;
03617 s = strchr(c + p->stripmsd, 'w');
03618 if (s) {
03619 if (strlen(s) > 1)
03620 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
03621 else
03622 p->dop.dialstr[0] = '\0';
03623 *s = '\0';
03624 } else {
03625 p->dop.dialstr[0] = '\0';
03626 }
03627 }
03628 if (pri_grab(p, p->pri)) {
03629 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
03630 ast_mutex_unlock(&p->lock);
03631 return -1;
03632 }
03633 if (!(p->call = pri_new_call(p->pri->pri))) {
03634 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
03635 pri_rel(p->pri);
03636 ast_mutex_unlock(&p->lock);
03637 return -1;
03638 }
03639 if (!(sr = pri_sr_new())) {
03640 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
03641 pri_rel(p->pri);
03642 ast_mutex_unlock(&p->lock);
03643 }
03644 if (p->bearer || (mysig == SIG_FXSKS)) {
03645 if (p->bearer) {
03646 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
03647 p->bearer->call = p->call;
03648 } else
03649 ast_debug(1, "I'm being setup with no bearer right now...\n");
03650
03651 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
03652 }
03653 p->digital = IS_DIGITAL(ast->transfercapability);
03654
03655
03656 if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
03657 exclusive = 1;
03658 } else {
03659 exclusive = 0;
03660 }
03661
03662 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
03663 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
03664 (p->digital ? -1 :
03665 ((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
03666 if (p->pri->facilityenable)
03667 pri_facility_enable(p->pri->pri);
03668
03669 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
03670
03671 dp_strip = 0;
03672 pridialplan = p->pri->dialplan - 1;
03673 if (pridialplan == -2 || pridialplan == -3) {
03674 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
03675 if (pridialplan == -2) {
03676 dp_strip = strlen(p->pri->internationalprefix);
03677 }
03678 pridialplan = PRI_INTERNATIONAL_ISDN;
03679 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
03680 if (pridialplan == -2) {
03681 dp_strip = strlen(p->pri->nationalprefix);
03682 }
03683 pridialplan = PRI_NATIONAL_ISDN;
03684 } else {
03685 pridialplan = PRI_LOCAL_ISDN;
03686 }
03687 }
03688 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
03689 switch (c[p->stripmsd]) {
03690 case 'U':
03691 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
03692 break;
03693 case 'I':
03694 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
03695 break;
03696 case 'N':
03697 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
03698 break;
03699 case 'L':
03700 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
03701 break;
03702 case 'S':
03703 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
03704 break;
03705 case 'V':
03706 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
03707 break;
03708 case 'R':
03709 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
03710 break;
03711 case 'u':
03712 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
03713 break;
03714 case 'e':
03715 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
03716 break;
03717 case 'x':
03718 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
03719 break;
03720 case 'f':
03721 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
03722 break;
03723 case 'n':
03724 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
03725 break;
03726 case 'p':
03727 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
03728 break;
03729 case 'r':
03730 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
03731 break;
03732 default:
03733 if (isalpha(c[p->stripmsd])) {
03734 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
03735 c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
03736 }
03737 break;
03738 }
03739 c++;
03740 }
03741 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
03742
03743 ldp_strip = 0;
03744 prilocaldialplan = p->pri->localdialplan - 1;
03745 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) {
03746 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
03747 if (prilocaldialplan == -2) {
03748 ldp_strip = strlen(p->pri->internationalprefix);
03749 }
03750 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
03751 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
03752 if (prilocaldialplan == -2) {
03753 ldp_strip = strlen(p->pri->nationalprefix);
03754 }
03755 prilocaldialplan = PRI_NATIONAL_ISDN;
03756 } else {
03757 prilocaldialplan = PRI_LOCAL_ISDN;
03758 }
03759 }
03760 if (l != NULL) {
03761 while (*l > '9' && *l != '*' && *l != '#') {
03762 switch (*l) {
03763 case 'U':
03764 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
03765 break;
03766 case 'I':
03767 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
03768 break;
03769 case 'N':
03770 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
03771 break;
03772 case 'L':
03773 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
03774 break;
03775 case 'S':
03776 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
03777 break;
03778 case 'V':
03779 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
03780 break;
03781 case 'R':
03782 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
03783 break;
03784 case 'u':
03785 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
03786 break;
03787 case 'e':
03788 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
03789 break;
03790 case 'x':
03791 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
03792 break;
03793 case 'f':
03794 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
03795 break;
03796 case 'n':
03797 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
03798 break;
03799 case 'p':
03800 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
03801 break;
03802 case 'r':
03803 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
03804 break;
03805 default:
03806 if (isalpha(*l)) {
03807 ast_log(LOG_WARNING,
03808 "Unrecognized prilocaldialplan %s modifier: %c\n",
03809 *l > 'Z' ? "NPI" : "TON", *l);
03810 }
03811 break;
03812 }
03813 l++;
03814 }
03815 }
03816 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
03817 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
03818 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
03819 if (!strcasecmp(rr_str, "UNKNOWN"))
03820 redirect_reason = 0;
03821 else if (!strcasecmp(rr_str, "BUSY"))
03822 redirect_reason = 1;
03823 else if (!strcasecmp(rr_str, "NO_REPLY") || !strcasecmp(rr_str, "NOANSWER"))
03824
03825 redirect_reason = 2;
03826 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
03827 redirect_reason = 15;
03828 else
03829 redirect_reason = PRI_REDIR_UNCONDITIONAL;
03830 } else
03831 redirect_reason = PRI_REDIR_UNCONDITIONAL;
03832 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
03833
03834 #ifdef SUPPORT_USERUSER
03835
03836 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
03837 if (useruser)
03838 pri_sr_set_useruser(sr, useruser);
03839 #endif
03840
03841 if (pri_setup(p->pri->pri, p->call, sr)) {
03842 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
03843 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
03844 pri_rel(p->pri);
03845 ast_mutex_unlock(&p->lock);
03846 pri_sr_free(sr);
03847 return -1;
03848 }
03849 pri_sr_free(sr);
03850 ast_setstate(ast, AST_STATE_DIALING);
03851 pri_rel(p->pri);
03852 }
03853 #endif
03854 ast_mutex_unlock(&p->lock);
03855 return 0;
03856 }
03857
03858 static void destroy_dahdi_pvt(struct dahdi_pvt **pvt)
03859 {
03860 struct dahdi_pvt *p = *pvt;
03861
03862 if (p->prev)
03863 p->prev->next = p->next;
03864 if (p->next)
03865 p->next->prev = p->prev;
03866 if (p->use_smdi)
03867 ast_smdi_interface_unref(p->smdi_iface);
03868 if (p->mwi_event_sub)
03869 ast_event_unsubscribe(p->mwi_event_sub);
03870 if (p->vars) {
03871 ast_variables_destroy(p->vars);
03872 }
03873 ast_mutex_destroy(&p->lock);
03874 dahdi_close_sub(p, SUB_REAL);
03875 if (p->owner)
03876 p->owner->tech_pvt = NULL;
03877 free(p);
03878 *pvt = NULL;
03879 }
03880
03881 static int destroy_channel(struct dahdi_pvt *prev, struct dahdi_pvt *cur, int now)
03882 {
03883 int owned = 0;
03884 int i = 0;
03885
03886 if (!now) {
03887 if (cur->owner) {
03888 owned = 1;
03889 }
03890
03891 for (i = 0; i < 3; i++) {
03892 if (cur->subs[i].owner) {
03893 owned = 1;
03894 }
03895 }
03896 if (!owned) {
03897 if (prev) {
03898 prev->next = cur->next;
03899 if (prev->next)
03900 prev->next->prev = prev;
03901 else
03902 ifend = prev;
03903 } else {
03904 iflist = cur->next;
03905 if (iflist)
03906 iflist->prev = NULL;
03907 else
03908 ifend = NULL;
03909 }
03910 destroy_dahdi_pvt(&cur);
03911 }
03912 } else {
03913 if (prev) {
03914 prev->next = cur->next;
03915 if (prev->next)
03916 prev->next->prev = prev;
03917 else
03918 ifend = prev;
03919 } else {
03920 iflist = cur->next;
03921 if (iflist)
03922 iflist->prev = NULL;
03923 else
03924 ifend = NULL;
03925 }
03926 destroy_dahdi_pvt(&cur);
03927 }
03928 return 0;
03929 }
03930
03931 static void destroy_all_channels(void)
03932 {
03933 int x;
03934 struct dahdi_pvt *p, *pl;
03935
03936 while (num_restart_pending) {
03937 usleep(1);
03938 }
03939
03940 ast_mutex_lock(&iflock);
03941
03942 p = iflist;
03943 while (p) {
03944
03945 if (p->cidspill)
03946 ast_free(p->cidspill);
03947 pl = p;
03948 p = p->next;
03949 x = pl->channel;
03950
03951 if (pl)
03952 destroy_dahdi_pvt(&pl);
03953 if (option_verbose > 2)
03954 ast_verbose(VERBOSE_PREFIX_2 "Unregistered channel %d\n", x);
03955 }
03956 iflist = NULL;
03957 ifcount = 0;
03958 ast_mutex_unlock(&iflock);
03959 }
03960
03961 #if defined(HAVE_PRI)
03962 static char *dahdi_send_keypad_facility_app = "DAHDISendKeypadFacility";
03963
03964 static int dahdi_send_keypad_facility_exec(struct ast_channel *chan, void *data)
03965 {
03966
03967 struct dahdi_pvt *p;
03968 char *digits = (char *) data;
03969
03970 if (ast_strlen_zero(digits)) {
03971 ast_debug(1, "No digit string sent to application!\n");
03972 return -1;
03973 }
03974
03975 p = (struct dahdi_pvt *)chan->tech_pvt;
03976
03977 if (!p) {
03978 ast_debug(1, "Unable to find technology private\n");
03979 return -1;
03980 }
03981
03982 ast_mutex_lock(&p->lock);
03983
03984 if (!p->pri || !p->call) {
03985 ast_debug(1, "Unable to find pri or call on channel!\n");
03986 ast_mutex_unlock(&p->lock);
03987 return -1;
03988 }
03989
03990 if (!pri_grab(p, p->pri)) {
03991 pri_keypad_facility(p->pri->pri, p->call, digits);
03992 pri_rel(p->pri);
03993 } else {
03994 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
03995 ast_mutex_unlock(&p->lock);
03996 return -1;
03997 }
03998
03999 ast_mutex_unlock(&p->lock);
04000
04001 return 0;
04002 }
04003 #endif
04004
04005 #if defined(HAVE_PRI)
04006 #if defined(HAVE_PRI_PROG_W_CAUSE)
04007 static char *dahdi_send_callrerouting_facility_app = "DAHDISendCallreroutingFacility";
04008
04009 static int dahdi_send_callrerouting_facility_exec(struct ast_channel *chan, void *data)
04010 {
04011
04012 struct dahdi_pvt *p;
04013 char *parse;
04014 int res = -1;
04015 AST_DECLARE_APP_ARGS(args,
04016 AST_APP_ARG(destination);
04017 AST_APP_ARG(original);
04018 AST_APP_ARG(reason);
04019 );
04020
04021 if (ast_strlen_zero(data)) {
04022 ast_log(LOG_DEBUG, "No data sent to application!\n");
04023 return -1;
04024 }
04025
04026 p = (struct dahdi_pvt *)chan->tech_pvt;
04027
04028 if (!p) {
04029 ast_log(LOG_DEBUG, "Unable to find technology private\n");
04030 return -1;
04031 }
04032
04033 parse = ast_strdupa(data);
04034 AST_STANDARD_APP_ARGS(args, parse);
04035
04036 if (ast_strlen_zero(args.destination)) {
04037 ast_log(LOG_WARNING, "callrerouting facility requires at least destination number argument\n");
04038 return -1;
04039 }
04040
04041 if (ast_strlen_zero(args.original)) {
04042 ast_log(LOG_WARNING, "Callrerouting Facility without original called number argument\n");
04043 args.original = NULL;
04044 }
04045
04046 if (ast_strlen_zero(args.reason)) {
04047 ast_log(LOG_NOTICE, "Callrerouting Facility without diversion reason argument, defaulting to unknown\n");
04048 args.reason = NULL;
04049 }
04050
04051 ast_mutex_lock(&p->lock);
04052
04053 if (!p->pri || !p->call) {
04054 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
04055 ast_mutex_unlock(&p->lock);
04056 return -1;
04057 }
04058
04059 switch (p->sig) {
04060 case SIG_PRI:
04061 if (!pri_grab(p, p->pri)) {
04062 if (chan->_state == AST_STATE_RING) {
04063 res = pri_callrerouting_facility(p->pri->pri, p->call, args.destination, args.original, args.reason);
04064 }
04065 pri_rel(p->pri);
04066 } else {
04067 ast_log(LOG_DEBUG, "Unable to grab pri to send callrerouting facility on span %d!\n", p->span);
04068 ast_mutex_unlock(&p->lock);
04069 return -1;
04070 }
04071 break;
04072 }
04073
04074 ast_mutex_unlock(&p->lock);
04075
04076 return res;
04077 }
04078 #endif
04079 #endif
04080
04081 #if defined(HAVE_PRI)
04082 static int pri_is_up(struct dahdi_pri *pri)
04083 {
04084 int x;
04085 for (x = 0; x < NUM_DCHANS; x++) {
04086 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
04087 return 1;
04088 }
04089 return 0;
04090 }
04091 #endif
04092
04093 #if defined(HAVE_PRI)
04094 static int pri_assign_bearer(struct dahdi_pvt *crv, struct dahdi_pri *pri, struct dahdi_pvt *bearer)
04095 {
04096 bearer->owner = &inuse;
04097 bearer->realcall = crv;
04098 crv->subs[SUB_REAL].dfd = bearer->subs[SUB_REAL].dfd;
04099 if (crv->subs[SUB_REAL].owner)
04100 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].dfd);
04101 crv->bearer = bearer;
04102 crv->call = bearer->call;
04103 crv->pri = pri;
04104 return 0;
04105 }
04106 #endif
04107
04108 #if defined(HAVE_PRI)
04109 static char *pri_order(int level)
04110 {
04111 switch (level) {
04112 case 0:
04113 return "Primary";
04114 case 1:
04115 return "Secondary";
04116 case 2:
04117 return "Tertiary";
04118 case 3:
04119 return "Quaternary";
04120 default:
04121 return "<Unknown>";
04122 }
04123 }
04124 #endif
04125
04126 #if defined(HAVE_PRI)
04127
04128 static int pri_active_dchan_fd(struct dahdi_pri *pri)
04129 {
04130 int x = -1;
04131
04132 for (x = 0; x < NUM_DCHANS; x++) {
04133 if ((pri->dchans[x] == pri->pri))
04134 break;
04135 }
04136
04137 return pri->fds[x];
04138 }
04139 #endif
04140
04141 #if defined(HAVE_PRI)
04142 static int pri_find_dchan(struct dahdi_pri *pri)
04143 {
04144 int oldslot = -1;
04145 struct pri *old;
04146 int newslot = -1;
04147 int x;
04148 old = pri->pri;
04149 for (x = 0; x < NUM_DCHANS; x++) {
04150 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
04151 newslot = x;
04152 if (pri->dchans[x] == old) {
04153 oldslot = x;
04154 }
04155 }
04156 if (newslot < 0) {
04157 newslot = 0;
04158
04159 if (pri->sig != SIG_BRI_PTMP) {
04160 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
04161 pri->dchannels[newslot]);
04162 }
04163 }
04164 if (old && (oldslot != newslot))
04165 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
04166 pri->dchannels[oldslot], pri->dchannels[newslot]);
04167 pri->pri = pri->dchans[newslot];
04168 return 0;
04169 }
04170 #endif
04171
04172 #if defined(HAVE_OPENR2)
04173 static const char *dahdi_accept_r2_call_app = "DAHDIAcceptR2Call";
04174
04175 static int dahdi_accept_r2_call_exec(struct ast_channel *chan, void *data)
04176 {
04177
04178 openr2_call_mode_t accept_mode;
04179 int res, timeout, maxloops;
04180 struct ast_frame *f;
04181 struct dahdi_pvt *p;
04182 char *parse;
04183 AST_DECLARE_APP_ARGS(args,
04184 AST_APP_ARG(charge);
04185 );
04186
04187 if (ast_strlen_zero(data)) {
04188 ast_log(LOG_DEBUG, "No data sent to application!\n");
04189 return -1;
04190 }
04191
04192 if (chan->tech != &dahdi_tech) {
04193 ast_log(LOG_DEBUG, "Only DAHDI technology accepted!\n");
04194 return -1;
04195 }
04196
04197 p = (struct dahdi_pvt *)chan->tech_pvt;
04198 if (!p) {
04199 ast_log(LOG_DEBUG, "Unable to find technology private!\n");
04200 return -1;
04201 }
04202
04203 parse = ast_strdupa(data);
04204 AST_STANDARD_APP_ARGS(args, parse);
04205
04206 if (ast_strlen_zero(args.charge)) {
04207 ast_log(LOG_WARNING, "DAHDIAcceptR2Call requires 'yes' or 'no' for the charge parameter\n");
04208 return -1;
04209 }
04210
04211 ast_mutex_lock(&p->lock);
04212 if (!p->mfcr2 || !p->mfcr2call) {
04213 ast_mutex_unlock(&p->lock);
04214 ast_log(LOG_DEBUG, "Channel %s does not seems to be an R2 active channel!\n", chan->name);
04215 return -1;
04216 }
04217
04218 if (p->mfcr2_call_accepted) {
04219 ast_mutex_unlock(&p->lock);
04220 ast_log(LOG_DEBUG, "MFC/R2 call already accepted on channel %s!\n", chan->name);
04221 return 0;
04222 }
04223 accept_mode = ast_true(args.charge) ? OR2_CALL_WITH_CHARGE : OR2_CALL_NO_CHARGE;
04224 if (openr2_chan_accept_call(p->r2chan, accept_mode)) {
04225 ast_mutex_unlock(&p->lock);
04226 ast_log(LOG_WARNING, "Failed to accept MFC/R2 call!\n");
04227 return -1;
04228 }
04229 ast_mutex_unlock(&p->lock);
04230
04231 res = 0;
04232 timeout = 100;
04233 maxloops = 50;
04234
04235 while (maxloops > 0) {
04236 maxloops--;
04237 if (ast_check_hangup(chan)) {
04238 break;
04239 }
04240 res = ast_waitfor(chan, timeout);
04241 if (res < 0) {
04242 ast_log(LOG_DEBUG, "ast_waitfor failed on channel %s, going out ...\n", chan->name);
04243 res = -1;
04244 break;
04245 }
04246 if (res == 0) {
04247 continue;
04248 }
04249 f = ast_read(chan);
04250 if (!f) {
04251 ast_log(LOG_DEBUG, "No frame read on channel %s, going out ...\n", chan->name);
04252 res = -1;
04253 break;
04254 }
04255 if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_HANGUP) {
04256 ast_log(LOG_DEBUG, "Got HANGUP frame on channel %s, going out ...\n", chan->name);
04257 ast_frfree(f);
04258 res = -1;
04259 break;
04260 }
04261 ast_frfree(f);
04262 ast_mutex_lock(&p->lock);
04263 if (p->mfcr2_call_accepted) {
04264 ast_mutex_unlock(&p->lock);
04265 ast_log(LOG_DEBUG, "Accepted MFC/R2 call!\n");
04266 break;
04267 }
04268 ast_mutex_unlock(&p->lock);
04269 }
04270 if (res == -1) {
04271 ast_log(LOG_WARNING, "Failed to accept MFC/R2 call!\n");
04272 }
04273 return res;
04274 }
04275
04276 static openr2_call_disconnect_cause_t dahdi_ast_cause_to_r2_cause(int cause)
04277 {
04278 openr2_call_disconnect_cause_t r2cause = OR2_CAUSE_NORMAL_CLEARING;
04279 switch (cause) {
04280 case AST_CAUSE_USER_BUSY:
04281 case AST_CAUSE_CALL_REJECTED:
04282 case AST_CAUSE_INTERWORKING:
04283 r2cause = OR2_CAUSE_BUSY_NUMBER;
04284 break;
04285
04286 case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
04287 case AST_CAUSE_SWITCH_CONGESTION:
04288 r2cause = OR2_CAUSE_NETWORK_CONGESTION;
04289 break;
04290
04291 case AST_CAUSE_UNALLOCATED:
04292 r2cause = OR2_CAUSE_UNALLOCATED_NUMBER;
04293 break;
04294
04295 case AST_CAUSE_NETWORK_OUT_OF_ORDER:
04296 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
04297 r2cause = OR2_CAUSE_OUT_OF_ORDER;
04298 break;
04299
04300 case AST_CAUSE_NO_ANSWER:
04301 case AST_CAUSE_NO_USER_RESPONSE:
04302 r2cause = OR2_CAUSE_NO_ANSWER;
04303 break;
04304
04305 default:
04306 r2cause = OR2_CAUSE_NORMAL_CLEARING;
04307 break;
04308 }
04309 ast_log(LOG_DEBUG, "ast cause %d resulted in openr2 cause %d/%s\n",
04310 cause, r2cause, openr2_proto_get_disconnect_string(r2cause));
04311 return r2cause;
04312 }
04313 #endif
04314
04315 static int dahdi_hangup(struct ast_channel *ast)
04316 {
04317 int res;
04318 int idx,x, law;
04319
04320 struct dahdi_pvt *p = ast->tech_pvt;
04321 struct dahdi_pvt *tmp = NULL;
04322 struct dahdi_pvt *prev = NULL;
04323 struct dahdi_params par;
04324
04325 ast_debug(1, "dahdi_hangup(%s)\n", ast->name);
04326 if (!ast->tech_pvt) {
04327 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
04328 return 0;
04329 }
04330
04331 ast_mutex_lock(&p->lock);
04332
04333 idx = dahdi_get_index(ast, p, 1);
04334
04335 switch (p->sig) {
04336 case SIG_PRI:
04337 case SIG_BRI:
04338 case SIG_BRI_PTMP:
04339 case SIG_SS7:
04340 x = 1;
04341 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
04342
04343 case SIG_MFCR2:
04344 p->cid_num[0] = '\0';
04345 p->cid_name[0] = '\0';
04346 break;
04347 default:
04348 break;
04349 }
04350
04351 x = 0;
04352 dahdi_confmute(p, 0);
04353 p->muting = 0;
04354 restore_gains(p);
04355 if (p->origcid_num) {
04356 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
04357 ast_free(p->origcid_num);
04358 p->origcid_num = NULL;
04359 }
04360 if (p->origcid_name) {
04361 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
04362 ast_free(p->origcid_name);
04363 p->origcid_name = NULL;
04364 }
04365 if (p->dsp)
04366 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
04367 p->exten[0] = '\0';
04368
04369 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
04370 p->channel, idx, p->subs[SUB_REAL].dfd, p->subs[SUB_CALLWAIT].dfd, p->subs[SUB_THREEWAY].dfd);
04371 p->ignoredtmf = 0;
04372
04373 if (idx > -1) {
04374
04375 p->subs[idx].owner = NULL;
04376 p->subs[idx].needanswer = 0;
04377 p->subs[idx].needflash = 0;
04378 p->subs[idx].needringing = 0;
04379 p->subs[idx].needbusy = 0;
04380 p->subs[idx].needcongestion = 0;
04381 p->subs[idx].linear = 0;
04382 p->subs[idx].needcallerid = 0;
04383 p->polarity = POLARITY_IDLE;
04384 dahdi_setlinear(p->subs[idx].dfd, 0);
04385 if (idx == SUB_REAL) {
04386 if ((p->subs[SUB_CALLWAIT].dfd > -1) && (p->subs[SUB_THREEWAY].dfd > -1)) {
04387 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
04388 if (p->subs[SUB_CALLWAIT].inthreeway) {
04389
04390 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
04391
04392 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
04393 unalloc_sub(p, SUB_CALLWAIT);
04394 p->owner = NULL;
04395 } else {
04396
04397 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
04398 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04399 unalloc_sub(p, SUB_THREEWAY);
04400 if (p->subs[SUB_REAL].inthreeway) {
04401
04402
04403 ast_debug(1, "Call was complete, setting owner to former third call\n");
04404 p->owner = p->subs[SUB_REAL].owner;
04405 } else {
04406
04407 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
04408 p->owner = NULL;
04409 }
04410 p->subs[SUB_REAL].inthreeway = 0;
04411 }
04412 } else if (p->subs[SUB_CALLWAIT].dfd > -1) {
04413
04414 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
04415 unalloc_sub(p, SUB_CALLWAIT);
04416 p->owner = p->subs[SUB_REAL].owner;
04417 if (p->owner->_state != AST_STATE_UP)
04418 p->subs[SUB_REAL].needanswer = 1;
04419 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
04420 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
04421 } else if (p->subs[SUB_THREEWAY].dfd > -1) {
04422 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04423 unalloc_sub(p, SUB_THREEWAY);
04424 if (p->subs[SUB_REAL].inthreeway) {
04425
04426
04427 ast_debug(1, "Call was complete, setting owner to former third call\n");
04428 p->owner = p->subs[SUB_REAL].owner;
04429 } else {
04430
04431 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
04432 p->owner = NULL;
04433 }
04434 p->subs[SUB_REAL].inthreeway = 0;
04435 }
04436 } else if (idx == SUB_CALLWAIT) {
04437
04438 if (p->subs[SUB_CALLWAIT].inthreeway) {
04439
04440
04441 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
04442 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
04443 S_OR(p->mohsuggest, NULL),
04444 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04445 }
04446 p->subs[SUB_THREEWAY].inthreeway = 0;
04447
04448 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
04449 unalloc_sub(p, SUB_THREEWAY);
04450 } else
04451 unalloc_sub(p, SUB_CALLWAIT);
04452 } else if (idx == SUB_THREEWAY) {
04453 if (p->subs[SUB_CALLWAIT].inthreeway) {
04454
04455
04456 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
04457 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
04458 S_OR(p->mohsuggest, NULL),
04459 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04460 }
04461 p->subs[SUB_CALLWAIT].inthreeway = 0;
04462 }
04463 p->subs[SUB_REAL].inthreeway = 0;
04464
04465
04466 unalloc_sub(p, SUB_THREEWAY);
04467 } else {
04468
04469 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
04470 }
04471 }
04472
04473 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
04474 p->owner = NULL;
04475 p->ringt = 0;
04476 p->distinctivering = 0;
04477 p->confirmanswer = 0;
04478 p->cidrings = 1;
04479 p->outgoing = 0;
04480 p->digital = 0;
04481 p->faxhandled = 0;
04482 p->pulsedial = 0;
04483 p->onhooktime = time(NULL);
04484 #if defined(HAVE_PRI) || defined(HAVE_SS7)
04485 p->proceeding = 0;
04486 p->dialing = 0;
04487 p->progress = 0;
04488 p->alerting = 0;
04489 p->setup_ack = 0;
04490 p->rlt = 0;
04491 #endif
04492 if (p->dsp) {
04493 ast_dsp_free(p->dsp);
04494 p->dsp = NULL;
04495 }
04496
04497 if (p->bufferoverrideinuse) {
04498
04499 struct dahdi_bufferinfo bi = {
04500 .txbufpolicy = p->buf_policy,
04501 .rxbufpolicy = p->buf_policy,
04502 .bufsize = p->bufsize,
04503 .numbufs = p->buf_no
04504 };
04505 int bpres;
04506
04507 if ((bpres = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
04508 ast_log(LOG_WARNING, "Channel '%s' unable to revert faxbuffer policy: %s\n", ast->name, strerror(errno));
04509 }
04510 p->bufferoverrideinuse = 0;
04511 }
04512
04513 law = DAHDI_LAW_DEFAULT;
04514 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETLAW, &law);
04515 if (res < 0)
04516 ast_log(LOG_WARNING, "Unable to set law on channel %d to default: %s\n", p->channel, strerror(errno));
04517
04518 #ifdef HAVE_SS7
04519 if (p->ss7) {
04520 if (p->ss7call) {
04521 if (!ss7_grab(p, p->ss7)) {
04522 if (!p->alreadyhungup) {
04523 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
04524 int icause = ast->hangupcause ? ast->hangupcause : -1;
04525
04526 if (cause) {
04527 if (atoi(cause))
04528 icause = atoi(cause);
04529 }
04530 isup_rel(p->ss7->ss7, p->ss7call, icause);
04531 ss7_rel(p->ss7);
04532 p->alreadyhungup = 1;
04533 } else
04534 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
04535 } else {
04536 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
04537 res = -1;
04538 }
04539 }
04540 }
04541 #endif
04542 #ifdef HAVE_OPENR2
04543 if (p->mfcr2 && p->mfcr2call && openr2_chan_get_direction(p->r2chan) != OR2_DIR_STOPPED) {
04544 ast_log(LOG_DEBUG, "disconnecting MFC/R2 call on chan %d\n", p->channel);
04545
04546 if (openr2_chan_get_direction(p->r2chan) == OR2_DIR_BACKWARD && p->mfcr2_forced_release) {
04547 dahdi_r2_disconnect_call(p, OR2_CAUSE_FORCED_RELEASE);
04548 } else {
04549 const char *r2causestr = pbx_builtin_getvar_helper(ast, "MFCR2_CAUSE");
04550 int r2cause_user = r2causestr ? atoi(r2causestr) : 0;
04551 openr2_call_disconnect_cause_t r2cause = r2cause_user ? dahdi_ast_cause_to_r2_cause(r2cause_user)
04552 : dahdi_ast_cause_to_r2_cause(ast->hangupcause);
04553 dahdi_r2_disconnect_call(p, r2cause);
04554 }
04555 } else if (p->mfcr2call) {
04556 ast_log(LOG_DEBUG, "Clearing call request on channel %d\n", p->channel);
04557
04558
04559
04560
04561 p->mfcr2call = 0;
04562 }
04563 #endif
04564 #ifdef HAVE_PRI
04565 if (p->pri) {
04566 #ifdef SUPPORT_USERUSER
04567 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
04568 #endif
04569
04570
04571 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
04572 if (!pri_grab(p, p->pri)) {
04573 if (p->alreadyhungup) {
04574 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
04575
04576 #ifdef SUPPORT_USERUSER
04577 pri_call_set_useruser(p->call, useruser);
04578 #endif
04579
04580 pri_hangup(p->pri->pri, p->call, -1);
04581 p->call = NULL;
04582 if (p->bearer)
04583 p->bearer->call = NULL;
04584 } else {
04585 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
04586 int icause = ast->hangupcause ? ast->hangupcause : -1;
04587 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
04588
04589 #ifdef SUPPORT_USERUSER
04590 pri_call_set_useruser(p->call, useruser);
04591 #endif
04592
04593 p->alreadyhungup = 1;
04594 if (p->bearer)
04595 p->bearer->alreadyhungup = 1;
04596 if (cause) {
04597 if (atoi(cause))
04598 icause = atoi(cause);
04599 }
04600 pri_hangup(p->pri->pri, p->call, icause);
04601 }
04602 if (res < 0)
04603 ast_log(LOG_WARNING, "pri_disconnect failed\n");
04604 pri_rel(p->pri);
04605 } else {
04606 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04607 res = -1;
04608 }
04609 } else {
04610 if (p->bearer)
04611 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
04612 p->call = NULL;
04613 res = 0;
04614 }
04615 }
04616 #endif
04617 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7)
04618 && (p->sig != SIG_BRI)
04619 && (p->sig != SIG_BRI_PTMP))
04620 && (p->sig != SIG_MFCR2))
04621 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
04622 if (res < 0) {
04623 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
04624 }
04625 switch (p->sig) {
04626 case SIG_FXOGS:
04627 case SIG_FXOLS:
04628 case SIG_FXOKS:
04629 memset(&par, 0, sizeof(par));
04630 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
04631 if (!res) {
04632 #if 0
04633 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
04634 #endif
04635
04636 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
04637 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
04638 else
04639 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
04640 p->fxsoffhookstate = par.rxisoffhook;
04641 }
04642 break;
04643 case SIG_FXSGS:
04644 case SIG_FXSLS:
04645 case SIG_FXSKS:
04646
04647
04648 if (ast->_state != AST_STATE_RESERVED) {
04649 time(&p->guardtime);
04650 p->guardtime += 2;
04651 }
04652 break;
04653 default:
04654 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
04655 }
04656 if (p->cidspill)
04657 ast_free(p->cidspill);
04658 if (p->sig)
04659 dahdi_disable_ec(p);
04660 x = 0;
04661 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
04662 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
04663 p->didtdd = 0;
04664 p->cidspill = NULL;
04665 p->callwaitcas = 0;
04666 p->callwaiting = p->permcallwaiting;
04667 p->hidecallerid = p->permhidecallerid;
04668 p->waitingfordt.tv_sec = 0;
04669 p->dialing = 0;
04670 p->rdnis[0] = '\0';
04671 update_conf(p);
04672 reset_conf(p);
04673
04674 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
04675 x = 0;
04676 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
04677 }
04678 #ifdef HAVE_PRI
04679 if (p->bearer) {
04680 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
04681
04682
04683 update_conf(p->bearer);
04684 reset_conf(p->bearer);
04685 p->bearer->owner = NULL;
04686 p->bearer->realcall = NULL;
04687 p->bearer = NULL;
04688 p->subs[SUB_REAL].dfd = -1;
04689 p->pri = NULL;
04690 }
04691 #endif
04692 if (num_restart_pending == 0)
04693 restart_monitor();
04694 }
04695
04696 p->callwaitingrepeat = 0;
04697 p->cidcwexpire = 0;
04698 p->oprmode = 0;
04699 ast->tech_pvt = NULL;
04700 ast_mutex_unlock(&p->lock);
04701 ast_module_unref(ast_module_info->self);
04702 ast_verb(3, "Hungup '%s'\n", ast->name);
04703
04704 ast_mutex_lock(&iflock);
04705
04706 if (p->restartpending) {
04707 num_restart_pending--;
04708 }
04709
04710 tmp = iflist;
04711 prev = NULL;
04712 if (p->destroy) {
04713 while (tmp) {
04714 if (tmp == p) {
04715 destroy_channel(prev, tmp, 0);
04716 break;
04717 } else {
04718 prev = tmp;
04719 tmp = tmp->next;
04720 }
04721 }
04722 }
04723 ast_mutex_unlock(&iflock);
04724 return 0;
04725 }
04726
04727 static int dahdi_answer(struct ast_channel *ast)
04728 {
04729 struct dahdi_pvt *p = ast->tech_pvt;
04730 int res = 0;
04731 int idx;
04732 int oldstate = ast->_state;
04733 ast_setstate(ast, AST_STATE_UP);
04734 ast_mutex_lock(&p->lock);
04735 idx = dahdi_get_index(ast, p, 0);
04736 if (idx < 0)
04737 idx = SUB_REAL;
04738
04739 if ((p->radio || (p->oprmode < 0))) {
04740 ast_mutex_unlock(&p->lock);
04741 return 0;
04742 }
04743 switch (p->sig) {
04744 case SIG_FXSLS:
04745 case SIG_FXSGS:
04746 case SIG_FXSKS:
04747 p->ringt = 0;
04748
04749 case SIG_EM:
04750 case SIG_EM_E1:
04751 case SIG_EMWINK:
04752 case SIG_FEATD:
04753 case SIG_FEATDMF:
04754 case SIG_FEATDMF_TA:
04755 case SIG_E911:
04756 case SIG_FGC_CAMA:
04757 case SIG_FGC_CAMAMF:
04758 case SIG_FEATB:
04759 case SIG_SF:
04760 case SIG_SFWINK:
04761 case SIG_SF_FEATD:
04762 case SIG_SF_FEATDMF:
04763 case SIG_SF_FEATB:
04764 case SIG_FXOLS:
04765 case SIG_FXOGS:
04766 case SIG_FXOKS:
04767
04768 ast_debug(1, "Took %s off hook\n", ast->name);
04769 if (p->hanguponpolarityswitch) {
04770 p->polaritydelaytv = ast_tvnow();
04771 }
04772 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
04773 tone_zone_play_tone(p->subs[idx].dfd, -1);
04774 p->dialing = 0;
04775 if ((idx == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
04776 if (oldstate == AST_STATE_RINGING) {
04777 ast_debug(1, "Finally swapping real and threeway\n");
04778 tone_zone_play_tone(p->subs[SUB_THREEWAY].dfd, -1);
04779 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04780 p->owner = p->subs[SUB_REAL].owner;
04781 }
04782 }
04783 if (p->sig & __DAHDI_SIG_FXS) {
04784 dahdi_enable_ec(p);
04785 dahdi_train_ec(p);
04786 }
04787 break;
04788 #ifdef HAVE_PRI
04789 case SIG_BRI:
04790 case SIG_BRI_PTMP:
04791 case SIG_PRI:
04792
04793 if (!pri_grab(p, p->pri)) {
04794 p->proceeding = 1;
04795 p->dialing = 0;
04796 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
04797 pri_rel(p->pri);
04798 } else {
04799 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04800 res = -1;
04801 }
04802 break;
04803 #endif
04804 #ifdef HAVE_SS7
04805 case SIG_SS7:
04806 if (!ss7_grab(p, p->ss7)) {
04807 p->proceeding = 1;
04808 res = isup_anm(p->ss7->ss7, p->ss7call);
04809 ss7_rel(p->ss7);
04810 } else {
04811 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
04812 res = -1;
04813 }
04814 break;
04815 #endif
04816 #ifdef HAVE_OPENR2
04817 case SIG_MFCR2:
04818 if (!p->mfcr2_call_accepted) {
04819
04820
04821 p->mfcr2_answer_pending = 1;
04822 if (p->mfcr2_charge_calls) {
04823 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with charge before answering on chan %d\n", p->channel);
04824 openr2_chan_accept_call(p->r2chan, OR2_CALL_WITH_CHARGE);
04825 } else {
04826 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with no charge before answering on chan %d\n", p->channel);
04827 openr2_chan_accept_call(p->r2chan, OR2_CALL_NO_CHARGE);
04828 }
04829 } else {
04830 ast_log(LOG_DEBUG, "Answering MFC/R2 call on chan %d\n", p->channel);
04831 dahdi_r2_answer(p);
04832 }
04833 break;
04834 #endif
04835 case 0:
04836 ast_mutex_unlock(&p->lock);
04837 return 0;
04838 default:
04839 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
04840 res = -1;
04841 }
04842 ast_mutex_unlock(&p->lock);
04843 return res;
04844 }
04845
04846 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen)
04847 {
04848 char *cp;
04849 signed char *scp;
04850 int x;
04851 int idx;
04852 struct dahdi_pvt *p = chan->tech_pvt, *pp;
04853 struct oprmode *oprmode;
04854
04855
04856
04857 if (!data || (datalen < 1)) {
04858 errno = EINVAL;
04859 return -1;
04860 }
04861
04862 switch (option) {
04863 case AST_OPTION_TXGAIN:
04864 scp = (signed char *) data;
04865 idx = dahdi_get_index(chan, p, 0);
04866 if (idx < 0) {
04867 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
04868 return -1;
04869 }
04870 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
04871 return set_actual_txgain(p->subs[idx].dfd, 0, p->txgain + (float) *scp, p->law);
04872 case AST_OPTION_RXGAIN:
04873 scp = (signed char *) data;
04874 idx = dahdi_get_index(chan, p, 0);
04875 if (idx < 0) {
04876 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
04877 return -1;
04878 }
04879 ast_debug(1, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
04880 return set_actual_rxgain(p->subs[idx].dfd, 0, p->rxgain + (float) *scp, p->law);
04881 case AST_OPTION_TONE_VERIFY:
04882 if (!p->dsp)
04883 break;
04884 cp = (char *) data;
04885 switch (*cp) {
04886 case 1:
04887 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
04888 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MUTECONF | p->dtmfrelax);
04889 break;
04890 case 2:
04891 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
04892 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax);
04893 break;
04894 default:
04895 ast_debug(1, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
04896 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
04897 break;
04898 }
04899 break;
04900 case AST_OPTION_TDD:
04901
04902 cp = (char *) data;
04903 p->mate = 0;
04904 if (!*cp) {
04905 ast_debug(1, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
04906 if (p->tdd)
04907 tdd_free(p->tdd);
04908 p->tdd = 0;
04909 break;
04910 }
04911 ast_debug(1, "Set option TDD MODE, value: %s(%d) on %s\n",
04912 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
04913 dahdi_disable_ec(p);
04914
04915 if (!p->didtdd) {
04916 unsigned char mybuf[41000];
04917 unsigned char *buf;
04918 int size, res, fd, len;
04919 struct pollfd fds[1];
04920
04921 buf = mybuf;
04922 memset(buf, 0x7f, sizeof(mybuf));
04923 ast_tdd_gen_ecdisa(buf + 16000, 16000);
04924 len = 40000;
04925 idx = dahdi_get_index(chan, p, 0);
04926 if (idx < 0) {
04927 ast_log(LOG_WARNING, "No index in TDD?\n");
04928 return -1;
04929 }
04930 fd = p->subs[idx].dfd;
04931 while (len) {
04932 if (ast_check_hangup(chan))
04933 return -1;
04934 size = len;
04935 if (size > READ_SIZE)
04936 size = READ_SIZE;
04937 fds[0].fd = fd;
04938 fds[0].events = POLLPRI | POLLOUT;
04939 fds[0].revents = 0;
04940 res = poll(fds, 1, -1);
04941 if (!res) {
04942 ast_debug(1, "poll (for write) ret. 0 on channel %d\n", p->channel);
04943 continue;
04944 }
04945
04946 if (fds[0].revents & POLLPRI)
04947 return -1;
04948 if (!(fds[0].revents & POLLOUT)) {
04949 ast_debug(1, "write fd not ready on channel %d\n", p->channel);
04950 continue;
04951 }
04952 res = write(fd, buf, size);
04953 if (res != size) {
04954 if (res == -1) return -1;
04955 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
04956 break;
04957 }
04958 len -= size;
04959 buf += size;
04960 }
04961 p->didtdd = 1;
04962 }
04963 if (*cp == 2) {
04964 if (p->tdd)
04965 tdd_free(p->tdd);
04966 p->tdd = 0;
04967 p->mate = 1;
04968 break;
04969 }
04970 if (!p->tdd) {
04971 p->tdd = tdd_new();
04972 }
04973 break;
04974 case AST_OPTION_RELAXDTMF:
04975 if (!p->dsp)
04976 break;
04977 cp = (char *) data;
04978 ast_debug(1, "Set option RELAX DTMF, value: %s(%d) on %s\n",
04979 *cp ? "ON" : "OFF", (int) *cp, chan->name);
04980 ast_dsp_set_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
04981 break;
04982 case AST_OPTION_AUDIO_MODE:
04983 cp = (char *) data;
04984 if (!*cp) {
04985 ast_debug(1, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
04986 x = 0;
04987 dahdi_disable_ec(p);
04988 } else {
04989 ast_debug(1, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
04990 x = 1;
04991 }
04992 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x) == -1)
04993 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", p->channel, x, strerror(errno));
04994 break;
04995 case AST_OPTION_OPRMODE:
04996 oprmode = (struct oprmode *) data;
04997
04998 if (strcasecmp(chan->tech->type, oprmode->peer->tech->type)) {
04999 ast_log(LOG_NOTICE, "Operator mode not supported on %s to %s calls.\n",
05000 chan->tech->type, oprmode->peer->tech->type);
05001 errno = EINVAL;
05002 return -1;
05003 }
05004 pp = oprmode->peer->tech_pvt;
05005 p->oprmode = pp->oprmode = 0;
05006
05007 p->oprpeer = pp;
05008 pp->oprpeer = p;
05009
05010 if (oprmode->mode)
05011 {
05012 pp->oprmode = oprmode->mode;
05013 p->oprmode = -oprmode->mode;
05014 }
05015 ast_debug(1, "Set Operator Services mode, value: %d on %s/%s\n",
05016 oprmode->mode, chan->name,oprmode->peer->name);
05017 break;
05018 case AST_OPTION_ECHOCAN:
05019 cp = (char *) data;
05020 if (*cp) {
05021 ast_debug(1, "Enabling echo cancelation on %s\n", chan->name);
05022 dahdi_enable_ec(p);
05023 } else {
05024 ast_debug(1, "Disabling echo cancelation on %s\n", chan->name);
05025 dahdi_disable_ec(p);
05026 }
05027 break;
05028 }
05029 errno = 0;
05030
05031 return 0;
05032 }
05033
05034 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
05035 {
05036 struct dahdi_pvt *p = chan->tech_pvt;
05037 int res = 0;
05038
05039 if (!strcasecmp(data, "rxgain")) {
05040 ast_mutex_lock(&p->lock);
05041 snprintf(buf, len, "%f", p->rxgain);
05042 ast_mutex_unlock(&p->lock);
05043 } else if (!strcasecmp(data, "txgain")) {
05044 ast_mutex_lock(&p->lock);
05045 snprintf(buf, len, "%f", p->txgain);
05046 ast_mutex_unlock(&p->lock);
05047 } else {
05048 ast_copy_string(buf, "", len);
05049 res = -1;
05050 }
05051
05052 return res;
05053 }
05054
05055
05056 static int parse_buffers_policy(const char *parse, int *num_buffers, int *policy)
05057 {
05058 int res;
05059 char policy_str[21] = "";
05060
05061 if (((res = sscanf(parse, "%d,%20s", num_buffers, policy_str)) != 2) &&
05062 ((res = sscanf(parse, "%d|%20s", num_buffers, policy_str)) != 2)) {
05063 ast_log(LOG_WARNING, "Parsing buffer string '%s' failed.\n", parse);
05064 return 1;
05065 }
05066 if (*num_buffers < 0) {
05067 ast_log(LOG_WARNING, "Invalid buffer count given '%d'.\n", *num_buffers);
05068 return -1;
05069 }
05070 if (!strcasecmp(policy_str, "full")) {
05071 *policy = DAHDI_POLICY_WHEN_FULL;
05072 } else if (!strcasecmp(policy_str, "immediate")) {
05073 *policy = DAHDI_POLICY_IMMEDIATE;
05074 #ifdef DAHDI_POLICY_HALF_FULL
05075 } else if (!strcasecmp(policy_str, "half")) {
05076 *policy = DAHDI_POLICY_HALF_FULL;
05077 #endif
05078 } else {
05079 ast_log(LOG_WARNING, "Invalid policy name given '%s'.\n", policy_str);
05080 return -1;
05081 }
05082
05083 return 0;
05084 }
05085
05086 static int dahdi_func_write(struct ast_channel *chan, const char *function, char *data, const char *value)
05087 {
05088 struct dahdi_pvt *p = chan->tech_pvt;
05089 int res = 0;
05090
05091 if (!strcasecmp(data, "buffers")) {
05092 int num_bufs, policy;
05093
05094 if (!(parse_buffers_policy(value, &num_bufs, &policy))) {
05095 struct dahdi_bufferinfo bi = {
05096 .txbufpolicy = policy,
05097 .rxbufpolicy = policy,
05098 .bufsize = p->bufsize,
05099 .numbufs = num_bufs,
05100 };
05101 int bpres;
05102
05103 if ((bpres = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
05104 ast_log(LOG_WARNING, "Channel '%d' unable to override buffer policy: %s\n", p->channel, strerror(errno));
05105 } else {
05106 p->bufferoverrideinuse = 1;
05107 }
05108 } else {
05109 res = -1;
05110 }
05111 } else {
05112 res = -1;
05113 }
05114
05115 return res;
05116 }
05117
05118 static void dahdi_unlink(struct dahdi_pvt *slave, struct dahdi_pvt *master, int needlock)
05119 {
05120
05121 int x;
05122 int hasslaves;
05123 if (!master)
05124 return;
05125 if (needlock) {
05126 ast_mutex_lock(&master->lock);
05127 if (slave) {
05128 while (ast_mutex_trylock(&slave->lock)) {
05129 DEADLOCK_AVOIDANCE(&master->lock);
05130 }
05131 }
05132 }
05133 hasslaves = 0;
05134 for (x = 0; x < MAX_SLAVES; x++) {
05135 if (master->slaves[x]) {
05136 if (!slave || (master->slaves[x] == slave)) {
05137
05138 ast_debug(1, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
05139 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
05140 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
05141 master->slaves[x]->master = NULL;
05142 master->slaves[x] = NULL;
05143 } else
05144 hasslaves = 1;
05145 }
05146 if (!hasslaves)
05147 master->inconference = 0;
05148 }
05149 if (!slave) {
05150 if (master->master) {
05151
05152 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
05153 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
05154 hasslaves = 0;
05155 for (x = 0; x < MAX_SLAVES; x++) {
05156 if (master->master->slaves[x] == master)
05157 master->master->slaves[x] = NULL;
05158 else if (master->master->slaves[x])
05159 hasslaves = 1;
05160 }
05161 if (!hasslaves)
05162 master->master->inconference = 0;
05163 }
05164 master->master = NULL;
05165 }
05166 update_conf(master);
05167 if (needlock) {
05168 if (slave)
05169 ast_mutex_unlock(&slave->lock);
05170 ast_mutex_unlock(&master->lock);
05171 }
05172 }
05173
05174 static void dahdi_link(struct dahdi_pvt *slave, struct dahdi_pvt *master) {
05175 int x;
05176 if (!slave || !master) {
05177 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
05178 return;
05179 }
05180 for (x = 0; x < MAX_SLAVES; x++) {
05181 if (!master->slaves[x]) {
05182 master->slaves[x] = slave;
05183 break;
05184 }
05185 }
05186 if (x >= MAX_SLAVES) {
05187 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
05188 master->slaves[MAX_SLAVES - 1] = slave;
05189 }
05190 if (slave->master)
05191 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
05192 slave->master = master;
05193
05194 ast_debug(1, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
05195 }
05196
05197 static void disable_dtmf_detect(struct dahdi_pvt *p)
05198 {
05199 int val;
05200
05201 p->ignoredtmf = 1;
05202
05203 val = 0;
05204 ioctl(p->subs[SUB_REAL].dfd, DAHDI_TONEDETECT, &val);
05205
05206 if (!p->hardwaredtmf && p->dsp) {
05207 p->dsp_features &= ~DSP_FEATURE_DIGIT_DETECT;
05208 ast_dsp_set_features(p->dsp, p->dsp_features);
05209 }
05210 }
05211
05212 static void enable_dtmf_detect(struct dahdi_pvt *p)
05213 {
05214 int val;
05215
05216 if (p->channel == CHAN_PSEUDO)
05217 return;
05218
05219 p->ignoredtmf = 0;
05220
05221 val = DAHDI_TONEDETECT_ON | DAHDI_TONEDETECT_MUTE;
05222 ioctl(p->subs[SUB_REAL].dfd, DAHDI_TONEDETECT, &val);
05223
05224 if (!p->hardwaredtmf && p->dsp) {
05225 p->dsp_features |= DSP_FEATURE_DIGIT_DETECT;
05226 ast_dsp_set_features(p->dsp, p->dsp_features);
05227 }
05228 }
05229
05230 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)
05231 {
05232 struct ast_channel *who;
05233 struct dahdi_pvt *p0, *p1, *op0, *op1;
05234 struct dahdi_pvt *master = NULL, *slave = NULL;
05235 struct ast_frame *f;
05236 int inconf = 0;
05237 int nothingok = 1;
05238 int ofd0, ofd1;
05239 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
05240 int os0 = -1, os1 = -1;
05241 int priority = 0;
05242 struct ast_channel *oc0, *oc1;
05243 enum ast_bridge_result res;
05244
05245 #ifdef PRI_2BCT
05246 int triedtopribridge = 0;
05247 #endif
05248
05249
05250
05251
05252
05253
05254 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
05255 return AST_BRIDGE_FAILED_NOWARN;
05256
05257 ast_channel_lock(c0);
05258 while (ast_channel_trylock(c1)) {
05259 CHANNEL_DEADLOCK_AVOIDANCE(c0);
05260 }
05261
05262 p0 = c0->tech_pvt;
05263 p1 = c1->tech_pvt;
05264
05265 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
05266 ast_channel_unlock(c0);
05267 ast_channel_unlock(c1);
05268 return AST_BRIDGE_FAILED_NOWARN;
05269 }
05270
05271 oi0 = dahdi_get_index(c0, p0, 0);
05272 oi1 = dahdi_get_index(c1, p1, 0);
05273 if ((oi0 < 0) || (oi1 < 0)) {
05274 ast_channel_unlock(c0);
05275 ast_channel_unlock(c1);
05276 return AST_BRIDGE_FAILED;
05277 }
05278
05279 op0 = p0 = c0->tech_pvt;
05280 op1 = p1 = c1->tech_pvt;
05281 ofd0 = c0->fds[0];
05282 ofd1 = c1->fds[0];
05283 oc0 = p0->owner;
05284 oc1 = p1->owner;
05285
05286 if (ast_mutex_trylock(&p0->lock)) {
05287
05288 ast_channel_unlock(c0);
05289 ast_channel_unlock(c1);
05290 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
05291 return AST_BRIDGE_RETRY;
05292 }
05293 if (ast_mutex_trylock(&p1->lock)) {
05294
05295 ast_mutex_unlock(&p0->lock);
05296 ast_channel_unlock(c0);
05297 ast_channel_unlock(c1);
05298 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
05299 return AST_BRIDGE_RETRY;
05300 }
05301
05302 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
05303 if (p0->owner && p1->owner) {
05304
05305 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
05306 master = p0;
05307 slave = p1;
05308 inconf = 1;
05309 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
05310 master = p1;
05311 slave = p0;
05312 inconf = 1;
05313 } else {
05314 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
05315 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
05316 p0->channel,
05317 oi0, (p0->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
05318 p0->subs[SUB_REAL].inthreeway, p0->channel,
05319 oi0, (p1->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
05320 p1->subs[SUB_REAL].inthreeway);
05321 }
05322 nothingok = 0;
05323 }
05324 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
05325 if (p1->subs[SUB_THREEWAY].inthreeway) {
05326 master = p1;
05327 slave = p0;
05328 nothingok = 0;
05329 }
05330 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
05331 if (p0->subs[SUB_THREEWAY].inthreeway) {
05332 master = p0;
05333 slave = p1;
05334 nothingok = 0;
05335 }
05336 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
05337
05338
05339 if (p1->subs[SUB_CALLWAIT].inthreeway) {
05340 master = p1;
05341 slave = p0;
05342 nothingok = 0;
05343 }
05344 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
05345
05346 if (p0->subs[SUB_CALLWAIT].inthreeway) {
05347 master = p0;
05348 slave = p1;
05349 nothingok = 0;
05350 }
05351 }
05352 ast_debug(1, "master: %d, slave: %d, nothingok: %d\n",
05353 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
05354 if (master && slave) {
05355
05356
05357
05358 if ((oi1 == SUB_THREEWAY) &&
05359 p1->subs[SUB_THREEWAY].inthreeway &&
05360 p1->subs[SUB_REAL].owner &&
05361 p1->subs[SUB_REAL].inthreeway &&
05362 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
05363 ast_debug(1, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
05364 tone_zone_play_tone(p0->subs[oi0].dfd, DAHDI_TONE_RINGTONE);
05365 os1 = p1->subs[SUB_REAL].owner->_state;
05366 } else {
05367 ast_debug(1, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
05368 tone_zone_play_tone(p0->subs[oi0].dfd, -1);
05369 }
05370 if ((oi0 == SUB_THREEWAY) &&
05371 p0->subs[SUB_THREEWAY].inthreeway &&
05372 p0->subs[SUB_REAL].owner &&
05373 p0->subs[SUB_REAL].inthreeway &&
05374 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
05375 ast_debug(1, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
05376 tone_zone_play_tone(p1->subs[oi1].dfd, DAHDI_TONE_RINGTONE);
05377 os0 = p0->subs[SUB_REAL].owner->_state;
05378 } else {
05379 ast_debug(1, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
05380 tone_zone_play_tone(p1->subs[oi0].dfd, -1);
05381 }
05382 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
05383 if (!p0->echocanbridged || !p1->echocanbridged) {
05384
05385 dahdi_disable_ec(p0);
05386 dahdi_disable_ec(p1);
05387 }
05388 }
05389 dahdi_link(slave, master);
05390 master->inconference = inconf;
05391 } else if (!nothingok)
05392 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
05393
05394 update_conf(p0);
05395 update_conf(p1);
05396 t0 = p0->subs[SUB_REAL].inthreeway;
05397 t1 = p1->subs[SUB_REAL].inthreeway;
05398
05399 ast_mutex_unlock(&p0->lock);
05400 ast_mutex_unlock(&p1->lock);
05401
05402 ast_channel_unlock(c0);
05403 ast_channel_unlock(c1);
05404
05405
05406 if ((!master || !slave) && !nothingok) {
05407 dahdi_enable_ec(p0);
05408 dahdi_enable_ec(p1);
05409 return AST_BRIDGE_FAILED;
05410 }
05411
05412 ast_verb(3, "Native bridging %s and %s\n", c0->name, c1->name);
05413
05414 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
05415 disable_dtmf_detect(op0);
05416
05417 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
05418 disable_dtmf_detect(op1);
05419
05420 for (;;) {
05421 struct ast_channel *c0_priority[2] = {c0, c1};
05422 struct ast_channel *c1_priority[2] = {c1, c0};
05423
05424
05425
05426
05427 ast_channel_lock(c0);
05428 while (ast_channel_trylock(c1)) {
05429 CHANNEL_DEADLOCK_AVOIDANCE(c0);
05430 }
05431
05432 p0 = c0->tech_pvt;
05433 p1 = c1->tech_pvt;
05434
05435 if (op0 == p0)
05436 i0 = dahdi_get_index(c0, p0, 1);
05437 if (op1 == p1)
05438 i1 = dahdi_get_index(c1, p1, 1);
05439
05440 ast_channel_unlock(c0);
05441 ast_channel_unlock(c1);
05442
05443 if (!timeoutms ||
05444 (op0 != p0) ||
05445 (op1 != p1) ||
05446 (ofd0 != c0->fds[0]) ||
05447 (ofd1 != c1->fds[0]) ||
05448 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
05449 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
05450 (oc0 != p0->owner) ||
05451 (oc1 != p1->owner) ||
05452 (t0 != p0->subs[SUB_REAL].inthreeway) ||
05453 (t1 != p1->subs[SUB_REAL].inthreeway) ||
05454 (oi0 != i0) ||
05455 (oi1 != i1)) {
05456 ast_debug(1, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
05457 op0->channel, oi0, op1->channel, oi1);
05458 res = AST_BRIDGE_RETRY;
05459 goto return_from_bridge;
05460 }
05461
05462 #ifdef PRI_2BCT
05463 if (!triedtopribridge) {
05464 triedtopribridge = 1;
05465 if (p0->pri && p0->pri == p1->pri && p0->transfer && p1->transfer) {
05466 ast_mutex_lock(&p0->pri->lock);
05467 if (p0->call && p1->call) {
05468 pri_channel_bridge(p0->call, p1->call);
05469 }
05470 ast_mutex_unlock(&p0->pri->lock);
05471 }
05472 }
05473 #endif
05474
05475 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
05476 if (!who) {
05477 ast_debug(1, "Ooh, empty read...\n");
05478 continue;
05479 }
05480 f = ast_read(who);
05481 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
05482 *fo = f;
05483 *rc = who;
05484 res = AST_BRIDGE_COMPLETE;
05485 goto return_from_bridge;
05486 }
05487 if (f->frametype == AST_FRAME_DTMF) {
05488 if ((who == c0) && p0->pulsedial) {
05489 ast_write(c1, f);
05490 } else if ((who == c1) && p1->pulsedial) {
05491 ast_write(c0, f);
05492 } else {
05493 *fo = f;
05494 *rc = who;
05495 res = AST_BRIDGE_COMPLETE;
05496 goto return_from_bridge;
05497 }
05498 }
05499 ast_frfree(f);
05500
05501
05502 priority = !priority;
05503 }
05504
05505 return_from_bridge:
05506 if (op0 == p0)
05507 dahdi_enable_ec(p0);
05508
05509 if (op1 == p1)
05510 dahdi_enable_ec(p1);
05511
05512 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
05513 enable_dtmf_detect(op0);
05514
05515 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
05516 enable_dtmf_detect(op1);
05517
05518 dahdi_unlink(slave, master, 1);
05519
05520 return res;
05521 }
05522
05523 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
05524 {
05525 struct dahdi_pvt *p = newchan->tech_pvt;
05526 int x;
05527 ast_mutex_lock(&p->lock);
05528 ast_debug(1, "New owner for channel %d is %s\n", p->channel, newchan->name);
05529 if (p->owner == oldchan) {
05530 p->owner = newchan;
05531 }
05532 for (x = 0; x < 3; x++)
05533 if (p->subs[x].owner == oldchan) {
05534 if (!x)
05535 dahdi_unlink(NULL, p, 0);
05536 p->subs[x].owner = newchan;
05537 }
05538 update_conf(p);
05539 ast_mutex_unlock(&p->lock);
05540 if (newchan->_state == AST_STATE_RINGING) {
05541 dahdi_indicate(newchan, AST_CONTROL_RINGING, NULL, 0);
05542 }
05543 return 0;
05544 }
05545
05546 static int dahdi_ring_phone(struct dahdi_pvt *p)
05547 {
05548 int x;
05549 int res;
05550
05551 x = 0;
05552 x = DAHDI_ONHOOK;
05553 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
05554 do {
05555 x = DAHDI_RING;
05556 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
05557 if (res) {
05558 switch (errno) {
05559 case EBUSY:
05560 case EINTR:
05561
05562 usleep(10000);
05563 continue;
05564 case EINPROGRESS:
05565 res = 0;
05566 break;
05567 default:
05568 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
05569 res = 0;
05570 }
05571 }
05572 } while (res);
05573 return res;
05574 }
05575
05576 static void *ss_thread(void *data);
05577
05578 static int attempt_transfer(struct dahdi_pvt *p)
05579 {
05580
05581
05582
05583 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
05584
05585
05586 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
05587 ast_queue_control(p->subs[SUB_THREEWAY].owner, AST_CONTROL_UNHOLD);
05588 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
05589 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
05590 }
05591 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RING) {
05592 tone_zone_play_tone(p->subs[SUB_THREEWAY].dfd, DAHDI_TONE_RINGTONE);
05593 }
05594 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
05595 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
05596 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
05597 return -1;
05598 }
05599
05600 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
05601 unalloc_sub(p, SUB_THREEWAY);
05602 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
05603 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
05604 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
05605 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
05606 }
05607 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RING) {
05608 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
05609 }
05610 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
05611 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
05612 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
05613 return -1;
05614 }
05615
05616 swap_subs(p, SUB_THREEWAY, SUB_REAL);
05617 ast_channel_unlock(p->subs[SUB_REAL].owner);
05618 unalloc_sub(p, SUB_THREEWAY);
05619
05620 return 1;
05621 } else {
05622 ast_debug(1, "Neither %s nor %s are in a bridge, nothing to transfer\n",
05623 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
05624 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
05625 return -1;
05626 }
05627 return 0;
05628 }
05629
05630 static int check_for_conference(struct dahdi_pvt *p)
05631 {
05632 struct dahdi_confinfo ci;
05633
05634 if (p->master || (p->confno > -1))
05635 return 0;
05636 memset(&ci, 0, sizeof(ci));
05637 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &ci)) {
05638 ast_log(LOG_WARNING, "Failed to get conference info on channel %d: %s\n", p->channel, strerror(errno));
05639 return 0;
05640 }
05641
05642
05643
05644 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
05645 ast_verb(3, "Avoiding 3-way call when in an external conference\n");
05646 return 1;
05647 }
05648 return 0;
05649 }
05650
05651
05652
05653
05654
05655
05656 static int get_alarms(struct dahdi_pvt *p)
05657 {
05658 int res;
05659 struct dahdi_spaninfo zi;
05660 struct dahdi_params params;
05661
05662 memset(&zi, 0, sizeof(zi));
05663 zi.spanno = p->span;
05664
05665 if ((res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SPANSTAT, &zi)) >= 0) {
05666 if (zi.alarms != DAHDI_ALARM_NONE)
05667 return zi.alarms;
05668 } else {
05669 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d: %s\n", p->channel, strerror(errno));
05670 return 0;
05671 }
05672
05673
05674 memset(¶ms, 0, sizeof(params));
05675 if ((res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, ¶ms)) >= 0)
05676 return params.chan_alarms;
05677
05678 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
05679
05680 return DAHDI_ALARM_NONE;
05681 }
05682
05683 static void dahdi_handle_dtmfup(struct ast_channel *ast, int idx, struct ast_frame **dest)
05684 {
05685 struct dahdi_pvt *p = ast->tech_pvt;
05686 struct ast_frame *f = *dest;
05687
05688 ast_debug(1, "DTMF digit: %c on %s\n", f->subclass, ast->name);
05689
05690 if (p->confirmanswer) {
05691 ast_debug(1, "Confirm answer on %s!\n", ast->name);
05692
05693
05694 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
05695 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
05696 *dest = &p->subs[idx].f;
05697
05698 p->confirmanswer = 0;
05699 } else if (p->callwaitcas) {
05700 if ((f->subclass == 'A') || (f->subclass == 'D')) {
05701 ast_debug(1, "Got some DTMF, but it's for the CAS\n");
05702 if (p->cidspill)
05703 ast_free(p->cidspill);
05704 send_cwcidspill(p);
05705 }
05706 p->callwaitcas = 0;
05707 p->subs[idx].f.frametype = AST_FRAME_NULL;
05708 p->subs[idx].f.subclass = 0;
05709 *dest = &p->subs[idx].f;
05710 } else if (f->subclass == 'f') {
05711
05712 if ((p->callprogress & CALLPROGRESS_FAX) && !p->faxhandled) {
05713
05714 if (p->usefaxbuffers && !p->bufferoverrideinuse) {
05715 struct dahdi_bufferinfo bi = {
05716 .txbufpolicy = p->faxbuf_policy,
05717 .bufsize = p->bufsize,
05718 .numbufs = p->faxbuf_no
05719 };
05720 int res;
05721
05722 if ((res = ioctl(p->subs[idx].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
05723 ast_log(LOG_WARNING, "Channel '%s' unable to set faxbuffer policy, reason: %s\n", ast->name, strerror(errno));
05724 } else {
05725 p->bufferoverrideinuse = 1;
05726 }
05727 }
05728 p->faxhandled = 1;
05729 if (strcmp(ast->exten, "fax")) {
05730 const char *target_context = S_OR(ast->macrocontext, ast->context);
05731
05732
05733
05734
05735
05736 ast_mutex_unlock(&p->lock);
05737 ast_channel_unlock(ast);
05738 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
05739 ast_channel_lock(ast);
05740 ast_mutex_lock(&p->lock);
05741 ast_verb(3, "Redirecting %s to fax extension\n", ast->name);
05742
05743 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
05744 if (ast_async_goto(ast, target_context, "fax", 1))
05745 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
05746 } else {
05747 ast_channel_lock(ast);
05748 ast_mutex_lock(&p->lock);
05749 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
05750 }
05751 } else {
05752 ast_debug(1, "Already in a fax extension, not redirecting\n");
05753 }
05754 } else {
05755 ast_debug(1, "Fax already handled\n");
05756 }
05757 dahdi_confmute(p, 0);
05758 p->subs[idx].f.frametype = AST_FRAME_NULL;
05759 p->subs[idx].f.subclass = 0;
05760 *dest = &p->subs[idx].f;
05761 }
05762 }
05763
05764 static void handle_alarms(struct dahdi_pvt *p, int alms)
05765 {
05766 const char *alarm_str = alarm2str(alms);
05767
05768 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm_str);
05769 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
05770 "Alarm: %s\r\n"
05771 "Channel: %d\r\n",
05772 alarm_str, p->channel);
05773 }
05774
05775 static struct ast_frame *dahdi_handle_event(struct ast_channel *ast)
05776 {
05777 int res, x;
05778 int idx, mysig;
05779 char *c;
05780 struct dahdi_pvt *p = ast->tech_pvt;
05781 pthread_t threadid;
05782 struct ast_channel *chan;
05783 struct ast_frame *f;
05784
05785 idx = dahdi_get_index(ast, p, 0);
05786 mysig = p->sig;
05787 if (p->outsigmod > -1)
05788 mysig = p->outsigmod;
05789 p->subs[idx].f.frametype = AST_FRAME_NULL;
05790 p->subs[idx].f.subclass = 0;
05791 p->subs[idx].f.datalen = 0;
05792 p->subs[idx].f.samples = 0;
05793 p->subs[idx].f.mallocd = 0;
05794 p->subs[idx].f.offset = 0;
05795 p->subs[idx].f.src = "dahdi_handle_event";
05796 p->subs[idx].f.data.ptr = NULL;
05797 f = &p->subs[idx].f;
05798
05799 if (idx < 0)
05800 return &p->subs[idx].f;
05801 if (p->fake_event) {
05802 res = p->fake_event;
05803 p->fake_event = 0;
05804 } else
05805 res = dahdi_get_event(p->subs[idx].dfd);
05806
05807 ast_debug(1, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, idx);
05808
05809 if (res & (DAHDI_EVENT_PULSEDIGIT | DAHDI_EVENT_DTMFUP)) {
05810 p->pulsedial = (res & DAHDI_EVENT_PULSEDIGIT) ? 1 : 0;
05811 ast_debug(1, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse ": "", res & 0xff);
05812 #ifdef HAVE_PRI
05813 if (!p->proceeding && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) && p->pri && (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
05814
05815 } else {
05816 #endif
05817 dahdi_confmute(p, 0);
05818 p->subs[idx].f.frametype = AST_FRAME_DTMF_END;
05819 p->subs[idx].f.subclass = res & 0xff;
05820 #ifdef HAVE_PRI
05821 }
05822 #endif
05823 dahdi_handle_dtmfup(ast, idx, &f);
05824 return f;
05825 }
05826
05827 if (res & DAHDI_EVENT_DTMFDOWN) {
05828 ast_debug(1, "DTMF Down '%c'\n", res & 0xff);
05829
05830 dahdi_confmute(p, 1);
05831 p->subs[idx].f.frametype = AST_FRAME_DTMF_BEGIN;
05832 p->subs[idx].f.subclass = res & 0xff;
05833 return &p->subs[idx].f;
05834 }
05835
05836 switch (res) {
05837 case DAHDI_EVENT_EC_DISABLED:
05838 ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
05839 p->echocanon = 0;
05840 break;
05841 case DAHDI_EVENT_BITSCHANGED:
05842 #ifdef HAVE_OPENR2
05843 if (p->sig != SIG_MFCR2) {
05844 ast_log(LOG_WARNING, "Received bits changed on %s signalling?\n", sig2str(p->sig));
05845 } else {
05846 ast_log(LOG_DEBUG, "bits changed in chan %d\n", p->channel);
05847 openr2_chan_handle_cas(p->r2chan);
05848 }
05849 #else
05850 ast_log(LOG_WARNING, "Received bits changed on %s signalling?\n", sig2str(p->sig));
05851 #endif
05852 case DAHDI_EVENT_PULSE_START:
05853
05854 if (!ast->pbx)
05855 tone_zone_play_tone(p->subs[idx].dfd, -1);
05856 break;
05857 case DAHDI_EVENT_DIALCOMPLETE:
05858 #ifdef HAVE_OPENR2
05859 if ((p->sig & SIG_MFCR2) && p->r2chan && ast->_state != AST_STATE_UP) {
05860
05861
05862 break;
05863 }
05864 #endif
05865 if (p->inalarm) break;
05866 if ((p->radio || (p->oprmode < 0))) break;
05867 if (ioctl(p->subs[idx].dfd,DAHDI_DIALING,&x) == -1) {
05868 ast_log(LOG_DEBUG, "DAHDI_DIALING ioctl failed on %s: %s\n",ast->name, strerror(errno));
05869 return NULL;
05870 }
05871 if (!x) {
05872 dahdi_enable_ec(p);
05873 if (p->echobreak) {
05874 dahdi_train_ec(p);
05875 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
05876 p->dop.op = DAHDI_DIAL_OP_REPLACE;
05877 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
05878 p->echobreak = 0;
05879 } else {
05880 p->dialing = 0;
05881 if ((mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF)) {
05882
05883 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
05884 ast_setstate(ast, AST_STATE_UP);
05885 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
05886 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
05887 break;
05888 } else {
05889
05890 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
05891 }
05892 }
05893 if (ast->_state == AST_STATE_DIALING) {
05894 if ((p->callprogress & CALLPROGRESS_PROGRESS) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
05895 ast_debug(1, "Done dialing, but waiting for progress detection before doing more...\n");
05896 } else if (p->confirmanswer || (!p->dialednone
05897 && ((mysig == SIG_EM) || (mysig == SIG_EM_E1)
05898 || (mysig == SIG_EMWINK) || (mysig == SIG_FEATD)
05899 || (mysig == SIG_FEATDMF_TA) || (mysig == SIG_FEATDMF)
05900 || (mysig == SIG_E911) || (mysig == SIG_FGC_CAMA)
05901 || (mysig == SIG_FGC_CAMAMF) || (mysig == SIG_FEATB)
05902 || (mysig == SIG_SF) || (mysig == SIG_SFWINK)
05903 || (mysig == SIG_SF_FEATD) || (mysig == SIG_SF_FEATDMF)
05904 || (mysig == SIG_SF_FEATB)))) {
05905 ast_setstate(ast, AST_STATE_RINGING);
05906 } else if (!p->answeronpolarityswitch) {
05907 ast_setstate(ast, AST_STATE_UP);
05908 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
05909 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
05910
05911 p->polarity = POLARITY_REV;
05912 } else {
05913
05914 p->polarity = POLARITY_IDLE;
05915 }
05916 }
05917 }
05918 }
05919 break;
05920 case DAHDI_EVENT_ALARM:
05921 #ifdef HAVE_PRI
05922 if ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
05923 if (!p->pri || !p->pri->pri || (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0)) {
05924
05925 if (p->call) {
05926 if (p->pri && p->pri->pri) {
05927 if (!pri_grab(p, p->pri)) {
05928 pri_hangup(p->pri->pri, p->call, -1);
05929 pri_destroycall(p->pri->pri, p->call);
05930 p->call = NULL;
05931 pri_rel(p->pri);
05932 } else
05933 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
05934 } else
05935 ast_log(LOG_WARNING, "The PRI Call has not been destroyed\n");
05936 }
05937 if (p->owner)
05938 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05939 }
05940 }
05941 if (p->bearer)
05942 p->bearer->inalarm = 1;
05943 else
05944 #endif
05945 p->inalarm = 1;
05946 res = get_alarms(p);
05947 handle_alarms(p, res);
05948 #ifdef HAVE_PRI
05949 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
05950
05951 } else {
05952 break;
05953 }
05954 #endif
05955 #ifdef HAVE_SS7
05956 if (p->sig == SIG_SS7)
05957 break;
05958 #endif
05959 #ifdef HAVE_OPENR2
05960 if (p->sig == SIG_MFCR2)
05961 break;
05962 #endif
05963 case DAHDI_EVENT_ONHOOK:
05964 if (p->radio) {
05965 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
05966 p->subs[idx].f.subclass = AST_CONTROL_RADIO_UNKEY;
05967 break;
05968 }
05969 if (p->oprmode < 0)
05970 {
05971 if (p->oprmode != -1) break;
05972 if ((p->sig == SIG_FXOLS) || (p->sig == SIG_FXOKS) || (p->sig == SIG_FXOGS))
05973 {
05974
05975 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
05976 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RING);
05977 save_conference(p->oprpeer);
05978 tone_zone_play_tone(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
05979 }
05980 break;
05981 }
05982 switch (p->sig) {
05983 case SIG_FXOLS:
05984 case SIG_FXOGS:
05985 case SIG_FXOKS:
05986 p->onhooktime = time(NULL);
05987 p->fxsoffhookstate = 0;
05988 p->msgstate = -1;
05989
05990 if (idx == SUB_REAL) {
05991
05992 if (p->subs[SUB_CALLWAIT].owner) {
05993
05994 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
05995 ast_verb(3, "Channel %d still has (callwait) call, ringing phone\n", p->channel);
05996 unalloc_sub(p, SUB_CALLWAIT);
05997 #if 0
05998 p->subs[idx].needanswer = 0;
05999 p->subs[idx].needringing = 0;
06000 #endif
06001 p->callwaitingrepeat = 0;
06002 p->cidcwexpire = 0;
06003 p->owner = NULL;
06004
06005 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
06006 p->dialing = 1;
06007 dahdi_ring_phone(p);
06008 } else if (p->subs[SUB_THREEWAY].owner) {
06009 unsigned int mssinceflash;
06010
06011
06012 while (p->subs[SUB_THREEWAY].owner && ast_channel_trylock(p->subs[SUB_THREEWAY].owner)) {
06013
06014 DLA_UNLOCK(&p->lock);
06015 CHANNEL_DEADLOCK_AVOIDANCE(ast);
06016
06017
06018
06019 DLA_LOCK(&p->lock);
06020 if (p->owner != ast) {
06021 ast_log(LOG_WARNING, "This isn't good...\n");
06022 return NULL;
06023 }
06024 }
06025 if (!p->subs[SUB_THREEWAY].owner) {
06026 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
06027 return NULL;
06028 }
06029 mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
06030 ast_debug(1, "Last flash was %d ms ago\n", mssinceflash);
06031 if (mssinceflash < MIN_MS_SINCE_FLASH) {
06032
06033
06034 if (p->subs[SUB_THREEWAY].owner)
06035 ast_queue_hangup_with_cause(p->subs[SUB_THREEWAY].owner, AST_CAUSE_NO_ANSWER);
06036 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06037 ast_debug(1, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
06038 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06039 } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
06040 if (p->transfer) {
06041
06042 p->subs[SUB_REAL].inthreeway = 0;
06043 p->subs[SUB_THREEWAY].inthreeway = 0;
06044
06045 if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
06046 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06047
06048 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06049 p->owner = NULL;
06050
06051 dahdi_ring_phone(p);
06052 } else {
06053 if ((res = attempt_transfer(p)) < 0) {
06054 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06055 if (p->subs[SUB_THREEWAY].owner)
06056 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06057 } else if (res) {
06058
06059 if (p->subs[SUB_THREEWAY].owner)
06060 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06061 break;
06062 }
06063 }
06064 } else {
06065 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06066 if (p->subs[SUB_THREEWAY].owner)
06067 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06068 }
06069 } else {
06070 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06071
06072 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06073 p->owner = NULL;
06074
06075 dahdi_ring_phone(p);
06076 }
06077 }
06078 } else {
06079 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", idx);
06080 }
06081
06082 default:
06083 dahdi_disable_ec(p);
06084 return NULL;
06085 }
06086 break;
06087 case DAHDI_EVENT_RINGOFFHOOK:
06088 if (p->inalarm) break;
06089 if (p->oprmode < 0)
06090 {
06091 if ((p->sig == SIG_FXOLS) || (p->sig == SIG_FXOKS) || (p->sig == SIG_FXOGS))
06092 {
06093
06094 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
06095 tone_zone_play_tone(p->oprpeer->subs[SUB_REAL].dfd, -1);
06096 restore_conference(p->oprpeer);
06097 }
06098 break;
06099 }
06100 if (p->radio)
06101 {
06102 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06103 p->subs[idx].f.subclass = AST_CONTROL_RADIO_KEY;
06104 break;
06105 }
06106
06107
06108 if (((mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF)) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
06109 c = strchr(p->dialdest, '/');
06110 if (c)
06111 c++;
06112 else
06113 c = p->dialdest;
06114 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
06115 else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
06116 if (strlen(p->dop.dialstr) > 4) {
06117 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
06118 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
06119 p->echorest[sizeof(p->echorest) - 1] = '\0';
06120 p->echobreak = 1;
06121 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
06122 } else
06123 p->echobreak = 0;
06124 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
06125 int saveerr = errno;
06126
06127 x = DAHDI_ONHOOK;
06128 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
06129 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
06130 return NULL;
06131 }
06132 p->dialing = 1;
06133 return &p->subs[idx].f;
06134 }
06135 switch (p->sig) {
06136 case SIG_FXOLS:
06137 case SIG_FXOGS:
06138 case SIG_FXOKS:
06139 p->fxsoffhookstate = 1;
06140 switch (ast->_state) {
06141 case AST_STATE_RINGING:
06142 dahdi_enable_ec(p);
06143 dahdi_train_ec(p);
06144 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06145 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
06146
06147 p->subs[SUB_REAL].needringing = 0;
06148 dahdi_set_hook(p->subs[idx].dfd, DAHDI_OFFHOOK);
06149 ast_debug(1, "channel %d answered\n", p->channel);
06150 if (p->cidspill) {
06151
06152 ast_free(p->cidspill);
06153 p->cidspill = NULL;
06154 }
06155 p->dialing = 0;
06156 p->callwaitcas = 0;
06157 if (p->confirmanswer) {
06158
06159 p->subs[idx].f.frametype = AST_FRAME_NULL;
06160 p->subs[idx].f.subclass = 0;
06161 } else if (!ast_strlen_zero(p->dop.dialstr)) {
06162
06163 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
06164 if (res < 0) {
06165 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
06166 p->dop.dialstr[0] = '\0';
06167 return NULL;
06168 } else {
06169 ast_debug(1, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
06170 p->subs[idx].f.frametype = AST_FRAME_NULL;
06171 p->subs[idx].f.subclass = 0;
06172 p->dialing = 1;
06173 }
06174 p->dop.dialstr[0] = '\0';
06175 ast_setstate(ast, AST_STATE_DIALING);
06176 } else
06177 ast_setstate(ast, AST_STATE_UP);
06178 return &p->subs[idx].f;
06179 case AST_STATE_DOWN:
06180 ast_setstate(ast, AST_STATE_RING);
06181 ast->rings = 1;
06182 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06183 p->subs[idx].f.subclass = AST_CONTROL_OFFHOOK;
06184 ast_debug(1, "channel %d picked up\n", p->channel);
06185 return &p->subs[idx].f;
06186 case AST_STATE_UP:
06187
06188 dahdi_set_hook(p->subs[idx].dfd, DAHDI_OFFHOOK);
06189
06190 if (ast_bridged_channel(p->owner))
06191 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
06192 p->subs[idx].needunhold = 1;
06193 break;
06194 case AST_STATE_RESERVED:
06195
06196 if (has_voicemail(p))
06197 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_STUTTER);
06198 else
06199 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_DIALTONE);
06200 break;
06201 default:
06202 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
06203 }
06204 break;
06205 case SIG_FXSLS:
06206 case SIG_FXSGS:
06207 case SIG_FXSKS:
06208 if (ast->_state == AST_STATE_RING) {
06209 p->ringt = p->ringt_base;
06210 }
06211
06212
06213
06214 ast_debug(1, "Setting IDLE polarity due "
06215 "to ring. Old polarity was %d\n",
06216 p->polarity);
06217 p->polarity = POLARITY_IDLE;
06218
06219
06220 case SIG_EM:
06221 case SIG_EM_E1:
06222 case SIG_EMWINK:
06223 case SIG_FEATD:
06224 case SIG_FEATDMF:
06225 case SIG_FEATDMF_TA:
06226 case SIG_E911:
06227 case SIG_FGC_CAMA:
06228 case SIG_FGC_CAMAMF:
06229 case SIG_FEATB:
06230 case SIG_SF:
06231 case SIG_SFWINK:
06232 case SIG_SF_FEATD:
06233 case SIG_SF_FEATDMF:
06234 case SIG_SF_FEATB:
06235 if (ast->_state == AST_STATE_PRERING)
06236 ast_setstate(ast, AST_STATE_RING);
06237 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
06238 ast_debug(1, "Ring detected\n");
06239 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06240 p->subs[idx].f.subclass = AST_CONTROL_RING;
06241 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
06242 ast_debug(1, "Line answered\n");
06243 if (p->confirmanswer) {
06244 p->subs[idx].f.frametype = AST_FRAME_NULL;
06245 p->subs[idx].f.subclass = 0;
06246 } else {
06247 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06248 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
06249 ast_setstate(ast, AST_STATE_UP);
06250 }
06251 } else if (ast->_state != AST_STATE_RING)
06252 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
06253 break;
06254 default:
06255 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
06256 }
06257 break;
06258 case DAHDI_EVENT_RINGBEGIN:
06259 switch (p->sig) {
06260 case SIG_FXSLS:
06261 case SIG_FXSGS:
06262 case SIG_FXSKS:
06263 if (ast->_state == AST_STATE_RING) {
06264 p->ringt = p->ringt_base;
06265 }
06266 break;
06267 }
06268 break;
06269 case DAHDI_EVENT_RINGEROFF:
06270 if (p->inalarm) break;
06271 if ((p->radio || (p->oprmode < 0))) break;
06272 ast->rings++;
06273 if ((ast->rings > p->cidrings) && (p->cidspill)) {
06274 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
06275 ast_free(p->cidspill);
06276 p->cidspill = NULL;
06277 p->callwaitcas = 0;
06278 }
06279 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06280 p->subs[idx].f.subclass = AST_CONTROL_RINGING;
06281 break;
06282 case DAHDI_EVENT_RINGERON:
06283 break;
06284 case DAHDI_EVENT_NOALARM:
06285 p->inalarm = 0;
06286 #ifdef HAVE_PRI
06287
06288 if (p->bearer)
06289 p->bearer->inalarm = 0;
06290 #endif
06291 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
06292 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
06293 "Channel: %d\r\n", p->channel);
06294 break;
06295 case DAHDI_EVENT_WINKFLASH:
06296 if (p->inalarm) break;
06297 if (p->radio) break;
06298 if (p->oprmode < 0) break;
06299 if (p->oprmode > 1)
06300 {
06301 struct dahdi_params par;
06302
06303 memset(&par, 0, sizeof(par));
06304 if (ioctl(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par) != -1)
06305 {
06306 if (!par.rxisoffhook)
06307 {
06308
06309 dahdi_set_hook(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
06310 dahdi_set_hook(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_RING);
06311 save_conference(p);
06312 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
06313 }
06314 }
06315 break;
06316 }
06317
06318 p->flashtime = ast_tvnow();
06319 switch (mysig) {
06320 case SIG_FXOLS:
06321 case SIG_FXOGS:
06322 case SIG_FXOKS:
06323 ast_debug(1, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
06324 idx, p->subs[SUB_REAL].dfd, p->subs[SUB_CALLWAIT].dfd, p->subs[SUB_THREEWAY].dfd);
06325 p->callwaitcas = 0;
06326
06327 if (idx != SUB_REAL) {
06328 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", idx, p->channel);
06329 goto winkflashdone;
06330 }
06331
06332 if (p->subs[SUB_CALLWAIT].owner) {
06333
06334 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
06335 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
06336 p->owner = p->subs[SUB_REAL].owner;
06337 ast_debug(1, "Making %s the new owner\n", p->owner->name);
06338 if (p->owner->_state == AST_STATE_RINGING) {
06339 ast_setstate(p->owner, AST_STATE_UP);
06340 p->subs[SUB_REAL].needanswer = 1;
06341 }
06342 p->callwaitingrepeat = 0;
06343 p->cidcwexpire = 0;
06344
06345 if (!p->subs[SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
06346 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
06347 S_OR(p->mohsuggest, NULL),
06348 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
06349 }
06350 p->subs[SUB_CALLWAIT].needhold = 1;
06351 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
06352 ast_queue_control_data(p->subs[SUB_REAL].owner, AST_CONTROL_HOLD,
06353 S_OR(p->mohsuggest, NULL),
06354 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
06355 }
06356 p->subs[SUB_REAL].needunhold = 1;
06357 } else if (!p->subs[SUB_THREEWAY].owner) {
06358 if (!p->threewaycalling) {
06359
06360 p->subs[SUB_REAL].needflash = 1;
06361 goto winkflashdone;
06362 } else if (!check_for_conference(p)) {
06363 char cid_num[256];
06364 char cid_name[256];
06365
06366 cid_num[0] = 0;
06367 cid_name[0] = 0;
06368 if (p->dahditrcallerid && p->owner) {
06369 if (p->owner->cid.cid_num)
06370 ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
06371 if (p->owner->cid.cid_name)
06372 ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
06373 }
06374
06375
06376 if (!((ast->pbx) ||
06377 (ast->_state == AST_STATE_UP) ||
06378 (ast->_state == AST_STATE_RING))) {
06379 ast_debug(1, "Flash when call not up or ringing\n");
06380 goto winkflashdone;
06381 }
06382 if (alloc_sub(p, SUB_THREEWAY)) {
06383 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
06384 goto winkflashdone;
06385 }
06386
06387 chan = dahdi_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
06388 if (p->dahditrcallerid) {
06389 if (!p->origcid_num)
06390 p->origcid_num = ast_strdup(p->cid_num);
06391 if (!p->origcid_name)
06392 p->origcid_name = ast_strdup(p->cid_name);
06393 ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
06394 ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
06395 }
06396
06397 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06398
06399 dahdi_disable_ec(p);
06400 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_DIALRECALL);
06401 if (res)
06402 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
06403 p->owner = chan;
06404 if (!chan) {
06405 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
06406 } else if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
06407 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
06408 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
06409 dahdi_enable_ec(p);
06410 ast_hangup(chan);
06411 } else {
06412 struct ast_channel *other = ast_bridged_channel(p->subs[SUB_THREEWAY].owner);
06413 int way3bridge = 0, cdr3way = 0;
06414
06415 if (!other) {
06416 other = ast_bridged_channel(p->subs[SUB_REAL].owner);
06417 } else
06418 way3bridge = 1;
06419
06420 if (p->subs[SUB_THREEWAY].owner->cdr)
06421 cdr3way = 1;
06422
06423 ast_verb(3, "Started three way call on channel %d\n", p->channel);
06424
06425
06426 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
06427 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
06428 S_OR(p->mohsuggest, NULL),
06429 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
06430 }
06431 p->subs[SUB_THREEWAY].needhold = 1;
06432 }
06433 }
06434 } else {
06435
06436 if (p->subs[SUB_THREEWAY].inthreeway) {
06437
06438 ast_debug(1, "Got flash with three way call up, dropping last call on %d\n", p->channel);
06439
06440 if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
06441
06442 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06443 p->owner = p->subs[SUB_REAL].owner;
06444 }
06445
06446 ast_verb(3, "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
06447 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06448 p->subs[SUB_REAL].inthreeway = 0;
06449 p->subs[SUB_THREEWAY].inthreeway = 0;
06450 } else {
06451
06452 if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
06453 (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
06454 int otherindex = SUB_THREEWAY;
06455 struct ast_channel *other = ast_bridged_channel(p->subs[SUB_THREEWAY].owner);
06456 int way3bridge = 0, cdr3way = 0;
06457
06458 if (!other) {
06459 other = ast_bridged_channel(p->subs[SUB_REAL].owner);
06460 } else
06461 way3bridge = 1;
06462
06463 if (p->subs[SUB_THREEWAY].owner->cdr)
06464 cdr3way = 1;
06465
06466 ast_verb(3, "Building conference on call on %s and %s\n", p->subs[SUB_THREEWAY].owner->name, p->subs[SUB_REAL].owner->name);
06467
06468 p->subs[SUB_THREEWAY].inthreeway = 1;
06469 p->subs[SUB_REAL].inthreeway = 1;
06470 if (ast->_state == AST_STATE_UP) {
06471 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06472 otherindex = SUB_REAL;
06473 }
06474 if (p->subs[otherindex].owner && ast_bridged_channel(p->subs[otherindex].owner))
06475 ast_queue_control(p->subs[otherindex].owner, AST_CONTROL_UNHOLD);
06476 p->subs[otherindex].needunhold = 1;
06477 p->owner = p->subs[SUB_REAL].owner;
06478 if (ast->_state == AST_STATE_RINGING) {
06479 ast_debug(1, "Enabling ringtone on real and threeway\n");
06480 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
06481 res = tone_zone_play_tone(p->subs[SUB_THREEWAY].dfd, DAHDI_TONE_RINGTONE);
06482 }
06483 } else {
06484 ast_verb(3, "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name);
06485 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06486 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06487 p->owner = p->subs[SUB_REAL].owner;
06488 if (p->subs[SUB_REAL].owner && ast_bridged_channel(p->subs[SUB_REAL].owner))
06489 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
06490 p->subs[SUB_REAL].needunhold = 1;
06491 dahdi_enable_ec(p);
06492 }
06493 }
06494 }
06495 winkflashdone:
06496 update_conf(p);
06497 break;
06498 case SIG_EM:
06499 case SIG_EM_E1:
06500 case SIG_FEATD:
06501 case SIG_SF:
06502 case SIG_SFWINK:
06503 case SIG_SF_FEATD:
06504 case SIG_FXSLS:
06505 case SIG_FXSGS:
06506 if (option_debug) {
06507 if (p->dialing)
06508 ast_debug(1, "Ignoring wink on channel %d\n", p->channel);
06509 else
06510 ast_debug(1, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
06511 }
06512 break;
06513 case SIG_FEATDMF_TA:
06514 switch (p->whichwink) {
06515 case 0:
06516 ast_debug(1, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
06517 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
06518 break;
06519 case 1:
06520 ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
06521 break;
06522 case 2:
06523 ast_log(LOG_WARNING, "Received unexpected wink on channel of type SIG_FEATDMF_TA\n");
06524 return NULL;
06525 }
06526 p->whichwink++;
06527
06528 case SIG_FEATDMF:
06529 case SIG_E911:
06530 case SIG_FGC_CAMAMF:
06531 case SIG_FGC_CAMA:
06532 case SIG_FEATB:
06533 case SIG_SF_FEATDMF:
06534 case SIG_SF_FEATB:
06535 case SIG_EMWINK:
06536
06537 if (!ast_strlen_zero(p->dop.dialstr)) {
06538 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
06539 if (res < 0) {
06540 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
06541 p->dop.dialstr[0] = '\0';
06542 return NULL;
06543 } else
06544 ast_debug(1, "Sent deferred digit string: %s\n", p->dop.dialstr);
06545 }
06546 p->dop.dialstr[0] = '\0';
06547 break;
06548 default:
06549 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
06550 }
06551 break;
06552 case DAHDI_EVENT_HOOKCOMPLETE:
06553 if (p->inalarm) break;
06554 if ((p->radio || (p->oprmode < 0))) break;
06555 if (p->waitingfordt.tv_sec) break;
06556 switch (mysig) {
06557 case SIG_FXSLS:
06558 case SIG_FXSGS:
06559 case SIG_FXSKS:
06560 case SIG_EM:
06561 case SIG_EM_E1:
06562 case SIG_EMWINK:
06563 case SIG_FEATD:
06564 case SIG_SF:
06565 case SIG_SFWINK:
06566 case SIG_SF_FEATD:
06567 if (!ast_strlen_zero(p->dop.dialstr)) {
06568 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
06569 if (res < 0) {
06570 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
06571 p->dop.dialstr[0] = '\0';
06572 return NULL;
06573 } else
06574 ast_debug(1, "Sent deferred digit string: %s\n", p->dop.dialstr);
06575 }
06576 p->dop.dialstr[0] = '\0';
06577 p->dop.op = DAHDI_DIAL_OP_REPLACE;
06578 break;
06579 case SIG_FEATDMF:
06580 case SIG_FEATDMF_TA:
06581 case SIG_E911:
06582 case SIG_FGC_CAMA:
06583 case SIG_FGC_CAMAMF:
06584 case SIG_FEATB:
06585 case SIG_SF_FEATDMF:
06586 case SIG_SF_FEATB:
06587 ast_debug(1, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
06588 break;
06589 default:
06590 break;
06591 }
06592 break;
06593 case DAHDI_EVENT_POLARITY:
06594
06595
06596
06597
06598
06599
06600
06601
06602
06603 if (p->polarityonanswerdelay > 0) {
06604
06605 if (ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
06606 switch (ast->_state) {
06607 case AST_STATE_DIALING:
06608 case AST_STATE_RINGING:
06609 if (p->answeronpolarityswitch) {
06610 ast_debug(1, "Answering on polarity switch! channel %d\n", p->channel);
06611 ast_setstate(p->owner, AST_STATE_UP);
06612 p->polarity = POLARITY_REV;
06613 if (p->hanguponpolarityswitch) {
06614 p->polaritydelaytv = ast_tvnow();
06615 }
06616 } else {
06617 ast_debug(1, "Ignore Answer on polarity switch, channel %d\n", p->channel);
06618 }
06619 break;
06620
06621 case AST_STATE_UP:
06622 case AST_STATE_RING:
06623 if (p->hanguponpolarityswitch) {
06624 ast_debug(1, "HangingUp on polarity switch! channel %d\n", p->channel);
06625 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
06626 p->polarity = POLARITY_IDLE;
06627 } else {
06628 ast_debug(1, "Ignore Hangup on polarity switch, channel %d\n", p->channel);
06629 }
06630 break;
06631
06632 case AST_STATE_DOWN:
06633 case AST_STATE_RESERVED:
06634 case AST_STATE_OFFHOOK:
06635 case AST_STATE_BUSY:
06636 case AST_STATE_DIALING_OFFHOOK:
06637 case AST_STATE_PRERING:
06638 default:
06639 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
06640 ast_debug(1, "Ignoring Polarity switch on channel %d, state %d\n", p->channel, ast->_state);
06641 }
06642 }
06643
06644 } else {
06645
06646 switch (ast->_state) {
06647 case AST_STATE_DIALING:
06648 case AST_STATE_RINGING:
06649 if (p->answeronpolarityswitch) {
06650 ast_debug(1, "Polarity switch detected but NOT answering (too close to OffHook event) on channel %d, state %d\n", p->channel, ast->_state);
06651 }
06652 break;
06653
06654 case AST_STATE_UP:
06655 case AST_STATE_RING:
06656 if (p->hanguponpolarityswitch) {
06657 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);
06658 }
06659 break;
06660
06661 default:
06662 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
06663 ast_debug(1, "Polarity switch detected (too close to previous event) on channel %d, state %d\n", p->channel, ast->_state);
06664 }
06665 }
06666 }
06667 }
06668
06669
06670 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) );
06671 break;
06672 default:
06673 ast_debug(1, "Dunno what to do with event %d on channel %d\n", res, p->channel);
06674 }
06675 return &p->subs[idx].f;
06676 }
06677
06678 static struct ast_frame *__dahdi_exception(struct ast_channel *ast)
06679 {
06680 struct dahdi_pvt *p = ast->tech_pvt;
06681 int res;
06682 int usedindex=-1;
06683 int idx;
06684 struct ast_frame *f;
06685
06686
06687 idx = dahdi_get_index(ast, p, 1);
06688
06689 p->subs[idx].f.frametype = AST_FRAME_NULL;
06690 p->subs[idx].f.datalen = 0;
06691 p->subs[idx].f.samples = 0;
06692 p->subs[idx].f.mallocd = 0;
06693 p->subs[idx].f.offset = 0;
06694 p->subs[idx].f.subclass = 0;
06695 p->subs[idx].f.delivery = ast_tv(0,0);
06696 p->subs[idx].f.src = "dahdi_exception";
06697 p->subs[idx].f.data.ptr = NULL;
06698
06699
06700 if ((!p->owner) && (!(p->radio || (p->oprmode < 0)))) {
06701
06702
06703
06704
06705
06706 if (p->fake_event) {
06707 res = p->fake_event;
06708 p->fake_event = 0;
06709 } else
06710 res = dahdi_get_event(p->subs[SUB_REAL].dfd);
06711
06712 if ((res != DAHDI_EVENT_RINGEROFF) && (res != DAHDI_EVENT_RINGERON) &&
06713 (res != DAHDI_EVENT_HOOKCOMPLETE)) {
06714 ast_debug(1, "Restoring owner of channel %d on event %d\n", p->channel, res);
06715 p->owner = p->subs[SUB_REAL].owner;
06716 if (p->owner && ast_bridged_channel(p->owner))
06717 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
06718 p->subs[SUB_REAL].needunhold = 1;
06719 }
06720 switch (res) {
06721 case DAHDI_EVENT_ONHOOK:
06722 dahdi_disable_ec(p);
06723 if (p->owner) {
06724 ast_verb(3, "Channel %s still has call, ringing phone\n", p->owner->name);
06725 dahdi_ring_phone(p);
06726 p->callwaitingrepeat = 0;
06727 p->cidcwexpire = 0;
06728 } else
06729 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
06730 update_conf(p);
06731 break;
06732 case DAHDI_EVENT_RINGOFFHOOK:
06733 dahdi_enable_ec(p);
06734 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
06735 if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
06736 p->subs[SUB_REAL].needanswer = 1;
06737 p->dialing = 0;
06738 }
06739 break;
06740 case DAHDI_EVENT_HOOKCOMPLETE:
06741 case DAHDI_EVENT_RINGERON:
06742 case DAHDI_EVENT_RINGEROFF:
06743
06744 break;
06745 case DAHDI_EVENT_WINKFLASH:
06746 p->flashtime = ast_tvnow();
06747 if (p->owner) {
06748 ast_verb(3, "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
06749 if (p->owner->_state != AST_STATE_UP) {
06750
06751 usedindex = dahdi_get_index(p->owner, p, 0);
06752 if (usedindex > -1) {
06753 p->subs[usedindex].needanswer = 1;
06754 }
06755 ast_setstate(p->owner, AST_STATE_UP);
06756 }
06757 p->callwaitingrepeat = 0;
06758 p->cidcwexpire = 0;
06759 if (ast_bridged_channel(p->owner))
06760 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
06761 p->subs[SUB_REAL].needunhold = 1;
06762 } else
06763 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
06764 update_conf(p);
06765 break;
06766 default:
06767 ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
06768 }
06769 f = &p->subs[idx].f;
06770 return f;
06771 }
06772 if (!(p->radio || (p->oprmode < 0)))
06773 ast_debug(1, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
06774
06775 if (ast != p->owner) {
06776 ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
06777 f = &p->subs[idx].f;
06778 return f;
06779 }
06780 f = dahdi_handle_event(ast);
06781 return f;
06782 }
06783
06784 static struct ast_frame *dahdi_exception(struct ast_channel *ast)
06785 {
06786 struct dahdi_pvt *p = ast->tech_pvt;
06787 struct ast_frame *f;
06788 ast_mutex_lock(&p->lock);
06789 f = __dahdi_exception(ast);
06790 ast_mutex_unlock(&p->lock);
06791 return f;
06792 }
06793
06794 static struct ast_frame *dahdi_read(struct ast_channel *ast)
06795 {
06796 struct dahdi_pvt *p = ast->tech_pvt;
06797 int res;
06798 int idx;
06799 void *readbuf;
06800 struct ast_frame *f;
06801
06802 while (ast_mutex_trylock(&p->lock)) {
06803 CHANNEL_DEADLOCK_AVOIDANCE(ast);
06804 }
06805
06806 idx = dahdi_get_index(ast, p, 0);
06807
06808
06809 if (idx < 0) {
06810 ast_log(LOG_WARNING, "We don't exist?\n");
06811 ast_mutex_unlock(&p->lock);
06812 return NULL;
06813 }
06814
06815 if ((p->radio || (p->oprmode < 0)) && p->inalarm) {
06816 ast_mutex_unlock(&p->lock);
06817 return NULL;
06818 }
06819
06820 p->subs[idx].f.frametype = AST_FRAME_NULL;
06821 p->subs[idx].f.datalen = 0;
06822 p->subs[idx].f.samples = 0;
06823 p->subs[idx].f.mallocd = 0;
06824 p->subs[idx].f.offset = 0;
06825 p->subs[idx].f.subclass = 0;
06826 p->subs[idx].f.delivery = ast_tv(0,0);
06827 p->subs[idx].f.src = "dahdi_read";
06828 p->subs[idx].f.data.ptr = NULL;
06829
06830
06831 if ((p->radio || (p->oprmode < 0)) && (!p->firstradio))
06832 {
06833 struct dahdi_params ps;
06834
06835 memset(&ps, 0, sizeof(ps));
06836 ps.channo = p->channel;
06837 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps) < 0) {
06838 ast_mutex_unlock(&p->lock);
06839 return NULL;
06840 }
06841 p->firstradio = 1;
06842 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06843 if (ps.rxisoffhook)
06844 {
06845 p->subs[idx].f.subclass = AST_CONTROL_RADIO_KEY;
06846 }
06847 else
06848 {
06849 p->subs[idx].f.subclass = AST_CONTROL_RADIO_UNKEY;
06850 }
06851 ast_mutex_unlock(&p->lock);
06852 return &p->subs[idx].f;
06853 }
06854 if (p->ringt == 1) {
06855 ast_mutex_unlock(&p->lock);
06856 return NULL;
06857 }
06858 else if (p->ringt > 0)
06859 p->ringt--;
06860
06861 #ifdef HAVE_OPENR2
06862 if (p->mfcr2) {
06863 openr2_chan_process_event(p->r2chan);
06864 }
06865 #endif
06866
06867 if (p->subs[idx].needringing) {
06868
06869 p->subs[idx].needringing = 0;
06870 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06871 p->subs[idx].f.subclass = AST_CONTROL_RINGING;
06872 ast_setstate(ast, AST_STATE_RINGING);
06873 ast_mutex_unlock(&p->lock);
06874 return &p->subs[idx].f;
06875 }
06876
06877 if (p->subs[idx].needbusy) {
06878
06879 p->subs[idx].needbusy = 0;
06880 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06881 p->subs[idx].f.subclass = AST_CONTROL_BUSY;
06882 ast_mutex_unlock(&p->lock);
06883 return &p->subs[idx].f;
06884 }
06885
06886 if (p->subs[idx].needcongestion) {
06887
06888 p->subs[idx].needcongestion = 0;
06889 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06890 p->subs[idx].f.subclass = AST_CONTROL_CONGESTION;
06891 ast_mutex_unlock(&p->lock);
06892 return &p->subs[idx].f;
06893 }
06894
06895 if (p->subs[idx].needcallerid) {
06896 ast_set_callerid(ast, S_OR(p->lastcid_num, NULL),
06897 S_OR(p->lastcid_name, NULL),
06898 S_OR(p->lastcid_num, NULL)
06899 );
06900 p->subs[idx].needcallerid = 0;
06901 }
06902
06903 if (p->subs[idx].needanswer) {
06904
06905 p->subs[idx].needanswer = 0;
06906 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06907 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
06908 ast_mutex_unlock(&p->lock);
06909 return &p->subs[idx].f;
06910 }
06911 #ifdef HAVE_OPENR2
06912 if (p->mfcr2 && openr2_chan_get_read_enabled(p->r2chan)) {
06913
06914
06915
06916
06917 ast_mutex_unlock(&p->lock);
06918 return &ast_null_frame;
06919 }
06920 #endif
06921
06922 if (p->subs[idx].needflash) {
06923
06924 p->subs[idx].needflash = 0;
06925 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06926 p->subs[idx].f.subclass = AST_CONTROL_FLASH;
06927 ast_mutex_unlock(&p->lock);
06928 return &p->subs[idx].f;
06929 }
06930
06931 if (p->subs[idx].needhold) {
06932
06933 p->subs[idx].needhold = 0;
06934 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06935 p->subs[idx].f.subclass = AST_CONTROL_HOLD;
06936 ast_mutex_unlock(&p->lock);
06937 ast_debug(1, "Sending hold on '%s'\n", ast->name);
06938 return &p->subs[idx].f;
06939 }
06940
06941 if (p->subs[idx].needunhold) {
06942
06943 p->subs[idx].needunhold = 0;
06944 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06945 p->subs[idx].f.subclass = AST_CONTROL_UNHOLD;
06946 ast_mutex_unlock(&p->lock);
06947 ast_debug(1, "Sending unhold on '%s'\n", ast->name);
06948 return &p->subs[idx].f;
06949 }
06950
06951 if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
06952 if (!p->subs[idx].linear) {
06953 p->subs[idx].linear = 1;
06954 res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
06955 if (res)
06956 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, idx);
06957 }
06958 } else if ((ast->rawreadformat == AST_FORMAT_ULAW) ||
06959 (ast->rawreadformat == AST_FORMAT_ALAW)) {
06960 if (p->subs[idx].linear) {
06961 p->subs[idx].linear = 0;
06962 res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
06963 if (res)
06964 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to companded mode.\n", p->channel, idx);
06965 }
06966 } else {
06967 ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast->rawreadformat));
06968 ast_mutex_unlock(&p->lock);
06969 return NULL;
06970 }
06971 readbuf = ((unsigned char *)p->subs[idx].buffer) + AST_FRIENDLY_OFFSET;
06972 CHECK_BLOCKING(ast);
06973 res = read(p->subs[idx].dfd, readbuf, p->subs[idx].linear ? READ_SIZE * 2 : READ_SIZE);
06974 ast_clear_flag(ast, AST_FLAG_BLOCKING);
06975
06976 if (res < 0) {
06977 f = NULL;
06978 if (res == -1) {
06979 if (errno == EAGAIN) {
06980
06981 ast_mutex_unlock(&p->lock);
06982 return &p->subs[idx].f;
06983 } else if (errno == ELAST) {
06984 f = __dahdi_exception(ast);
06985 } else
06986 ast_log(LOG_WARNING, "dahdi_rec: %s\n", strerror(errno));
06987 }
06988 ast_mutex_unlock(&p->lock);
06989 return f;
06990 }
06991 if (res != (p->subs[idx].linear ? READ_SIZE * 2 : READ_SIZE)) {
06992 ast_debug(1, "Short read (%d/%d), must be an event...\n", res, p->subs[idx].linear ? READ_SIZE * 2 : READ_SIZE);
06993 f = __dahdi_exception(ast);
06994 ast_mutex_unlock(&p->lock);
06995 return f;
06996 }
06997 if (p->tdd) {
06998 int c;
06999
07000 c = tdd_feed(p->tdd,readbuf,READ_SIZE);
07001 if (c < 0) {
07002 ast_debug(1,"tdd_feed failed\n");
07003 ast_mutex_unlock(&p->lock);
07004 return NULL;
07005 }
07006 if (c) {
07007 p->subs[idx].f.subclass = 0;
07008 p->subs[idx].f.frametype = AST_FRAME_TEXT;
07009 p->subs[idx].f.mallocd = 0;
07010 p->subs[idx].f.offset = AST_FRIENDLY_OFFSET;
07011 p->subs[idx].f.data.ptr = p->subs[idx].buffer + AST_FRIENDLY_OFFSET;
07012 p->subs[idx].f.datalen = 1;
07013 *((char *) p->subs[idx].f.data.ptr) = c;
07014 ast_mutex_unlock(&p->lock);
07015 return &p->subs[idx].f;
07016 }
07017 }
07018
07019 if (p->callwaitingrepeat && dahdi_get_index(ast, p, 1) == SUB_REAL) {
07020 p->callwaitingrepeat--;
07021 }
07022 if (p->cidcwexpire)
07023 p->cidcwexpire--;
07024
07025 if (p->callwaitingrepeat == 1) {
07026 p->callwaitrings++;
07027 dahdi_callwait(ast);
07028 }
07029
07030 if (p->cidcwexpire == 1) {
07031 ast_verb(3, "CPE does not support Call Waiting Caller*ID.\n");
07032 restore_conference(p);
07033 }
07034 if (p->subs[idx].linear) {
07035 p->subs[idx].f.datalen = READ_SIZE * 2;
07036 } else
07037 p->subs[idx].f.datalen = READ_SIZE;
07038
07039
07040 if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
07041 send_callerid(p);
07042 }
07043
07044 p->subs[idx].f.frametype = AST_FRAME_VOICE;
07045 p->subs[idx].f.subclass = ast->rawreadformat;
07046 p->subs[idx].f.samples = READ_SIZE;
07047 p->subs[idx].f.mallocd = 0;
07048 p->subs[idx].f.offset = AST_FRIENDLY_OFFSET;
07049 p->subs[idx].f.data.ptr = p->subs[idx].buffer + AST_FRIENDLY_OFFSET / sizeof(p->subs[idx].buffer[0]);
07050 #if 0
07051 ast_debug(1, "Read %d of voice on %s\n", p->subs[idx].f.datalen, ast->name);
07052 #endif
07053 if (p->dialing ||
07054 (idx && (ast->_state != AST_STATE_UP)) ||
07055 ((idx == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway)
07056 ) {
07057
07058
07059 p->subs[idx].f.frametype = AST_FRAME_NULL;
07060 p->subs[idx].f.subclass = 0;
07061 p->subs[idx].f.samples = 0;
07062 p->subs[idx].f.mallocd = 0;
07063 p->subs[idx].f.offset = 0;
07064 p->subs[idx].f.data.ptr = NULL;
07065 p->subs[idx].f.datalen= 0;
07066 }
07067 if (p->dsp && (!p->ignoredtmf || p->callwaitcas || p->busydetect || p->callprogress || p->waitingfordt.tv_sec) && !idx) {
07068
07069 int mute;
07070
07071 f = ast_dsp_process(ast, p->dsp, &p->subs[idx].f);
07072
07073
07074 mute = ast_dsp_was_muted(p->dsp);
07075 if (p->muting != mute) {
07076 p->muting = mute;
07077 dahdi_confmute(p, mute);
07078 }
07079
07080 if (f) {
07081 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
07082 if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
07083
07084
07085 f = NULL;
07086 }
07087 } else if (f->frametype == AST_FRAME_DTMF) {
07088 #ifdef HAVE_PRI
07089 if (!p->proceeding && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) && p->pri &&
07090 ((!p->outgoing && (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) ||
07091 (p->outgoing && (p->pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING)))) {
07092
07093 f->frametype = AST_FRAME_NULL;
07094 f->subclass = 0;
07095 }
07096 #endif
07097
07098 p->pulsedial = 0;
07099 } else if (p->waitingfordt.tv_sec) {
07100 if (ast_tvdiff_ms(ast_tvnow(), p->waitingfordt) >= p->waitfordialtone ) {
07101 p->waitingfordt.tv_sec = 0;
07102 ast_log(LOG_WARNING, "Never saw dialtone on channel %d\n", p->channel);
07103 f=NULL;
07104 } else if (f->frametype == AST_FRAME_VOICE) {
07105 f->frametype = AST_FRAME_NULL;
07106 f->subclass = 0;
07107 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) {
07108 p->waitingfordt.tv_sec = 0;
07109 p->dsp_features &= ~DSP_FEATURE_WAITDIALTONE;
07110 ast_dsp_set_features(p->dsp, p->dsp_features);
07111 ast_log(LOG_DEBUG, "Got 10 samples of dialtone!\n");
07112 if (!ast_strlen_zero(p->dop.dialstr)) {
07113 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
07114 if (res < 0) {
07115 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
07116 p->dop.dialstr[0] = '\0';
07117 ast_mutex_unlock(&p->lock);
07118 return NULL;
07119 } else {
07120 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
07121 p->dialing = 1;
07122 p->dop.dialstr[0] = '\0';
07123 p->dop.op = DAHDI_DIAL_OP_REPLACE;
07124 ast_setstate(ast, AST_STATE_DIALING);
07125 }
07126 }
07127 }
07128 }
07129 }
07130 }
07131 } else
07132 f = &p->subs[idx].f;
07133
07134 if (f && (f->frametype == AST_FRAME_DTMF))
07135 dahdi_handle_dtmfup(ast, idx, &f);
07136
07137
07138 if (p->fake_event)
07139 ast_set_flag(ast, AST_FLAG_EXCEPTION);
07140
07141 ast_mutex_unlock(&p->lock);
07142 return f;
07143 }
07144
07145 static int my_dahdi_write(struct dahdi_pvt *p, unsigned char *buf, int len, int idx, int linear)
07146 {
07147 int sent=0;
07148 int size;
07149 int res;
07150 int fd;
07151 fd = p->subs[idx].dfd;
07152 while (len) {
07153 size = len;
07154 if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
07155 size = (linear ? READ_SIZE * 2 : READ_SIZE);
07156 res = write(fd, buf, size);
07157 if (res != size) {
07158 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
07159 return sent;
07160 }
07161 len -= size;
07162 buf += size;
07163 }
07164 return sent;
07165 }
07166
07167 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame)
07168 {
07169 struct dahdi_pvt *p = ast->tech_pvt;
07170 int res;
07171 int idx;
07172 idx = dahdi_get_index(ast, p, 0);
07173 if (idx < 0) {
07174 ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
07175 return -1;
07176 }
07177
07178 #if 0
07179 #ifdef HAVE_PRI
07180 ast_mutex_lock(&p->lock);
07181 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
07182 if (p->pri->pri) {
07183 if (!pri_grab(p, p->pri)) {
07184 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
07185 pri_rel(p->pri);
07186 } else
07187 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07188 }
07189 p->proceeding=1;
07190 }
07191 ast_mutex_unlock(&p->lock);
07192 #endif
07193 #endif
07194
07195 if (frame->frametype != AST_FRAME_VOICE) {
07196 if (frame->frametype != AST_FRAME_IMAGE)
07197 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
07198 return 0;
07199 }
07200 if ((frame->subclass != AST_FORMAT_SLINEAR) &&
07201 (frame->subclass != AST_FORMAT_ULAW) &&
07202 (frame->subclass != AST_FORMAT_ALAW)) {
07203 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
07204 return -1;
07205 }
07206 if (p->dialing) {
07207 ast_debug(1, "Dropping frame since I'm still dialing on %s...\n",ast->name);
07208 return 0;
07209 }
07210 if (!p->owner) {
07211 ast_debug(1, "Dropping frame since there is no active owner on %s...\n",ast->name);
07212 return 0;
07213 }
07214 if (p->cidspill) {
07215 ast_debug(1, "Dropping frame since I've still got a callerid spill\n");
07216 return 0;
07217 }
07218
07219 if (!frame->data.ptr || !frame->datalen)
07220 return 0;
07221
07222 if (frame->subclass == AST_FORMAT_SLINEAR) {
07223 if (!p->subs[idx].linear) {
07224 p->subs[idx].linear = 1;
07225 res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
07226 if (res)
07227 ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
07228 }
07229 res = my_dahdi_write(p, (unsigned char *)frame->data.ptr, frame->datalen, idx, 1);
07230 } else {
07231
07232 if (p->subs[idx].linear) {
07233 p->subs[idx].linear = 0;
07234 res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
07235 if (res)
07236 ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
07237 }
07238 res = my_dahdi_write(p, (unsigned char *)frame->data.ptr, frame->datalen, idx, 0);
07239 }
07240 if (res < 0) {
07241 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
07242 return -1;
07243 }
07244 return 0;
07245 }
07246
07247 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
07248 {
07249 struct dahdi_pvt *p = chan->tech_pvt;
07250 int res=-1;
07251 int idx;
07252 int func = DAHDI_FLASH;
07253 ast_mutex_lock(&p->lock);
07254 idx = dahdi_get_index(chan, p, 0);
07255 ast_debug(1, "Requested indication %d on channel %s\n", condition, chan->name);
07256 #ifdef HAVE_OPENR2
07257 if (p->mfcr2 && !p->mfcr2_call_accepted) {
07258 ast_mutex_unlock(&p->lock);
07259
07260
07261 return 0;
07262 }
07263 #endif
07264 if (idx == SUB_REAL) {
07265 switch (condition) {
07266 case AST_CONTROL_BUSY:
07267 #ifdef HAVE_PRI
07268 if (p->priindication_oob && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))) {
07269 chan->hangupcause = AST_CAUSE_USER_BUSY;
07270 chan->_softhangup |= AST_SOFTHANGUP_DEV;
07271 res = 0;
07272 } else if (!p->progress &&
07273 ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07274 && p->pri && !p->outgoing) {
07275 if (p->pri->pri) {
07276 if (!pri_grab(p, p->pri)) {
07277 #ifdef HAVE_PRI_PROG_W_CAUSE
07278 pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_USER_BUSY);
07279 #else
07280 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
07281 #endif
07282 pri_rel(p->pri);
07283 }
07284 else
07285 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07286 }
07287 p->progress = 1;
07288 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_BUSY);
07289 } else
07290 #endif
07291 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_BUSY);
07292 break;
07293 case AST_CONTROL_RINGING:
07294 #ifdef HAVE_PRI
07295 if ((!p->alerting) && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07296 && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
07297 if (p->pri->pri) {
07298 if (!pri_grab(p, p->pri)) {
07299 pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
07300 pri_rel(p->pri);
07301 }
07302 else
07303 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07304 }
07305 p->alerting = 1;
07306 }
07307
07308 #endif
07309 #ifdef HAVE_SS7
07310 if ((!p->alerting) && (p->sig == SIG_SS7) && p->ss7 && !p->outgoing && (chan->_state != AST_STATE_UP)) {
07311 if (p->ss7->ss7) {
07312 ss7_grab(p, p->ss7);
07313
07314 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
07315 p->rlt = 1;
07316 if (p->rlt != 1)
07317 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
07318 p->alerting = 1;
07319 ss7_rel(p->ss7);
07320 }
07321 }
07322 #endif
07323
07324 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_RINGTONE);
07325
07326 if (chan->_state != AST_STATE_UP) {
07327 if ((chan->_state != AST_STATE_RING) ||
07328 ((p->sig != SIG_FXSKS) &&
07329 (p->sig != SIG_FXSLS) &&
07330 (p->sig != SIG_FXSGS)))
07331 ast_setstate(chan, AST_STATE_RINGING);
07332 }
07333 break;
07334 case AST_CONTROL_PROCEEDING:
07335 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
07336 #ifdef HAVE_PRI
07337 if (!p->proceeding && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07338 && p->pri && !p->outgoing) {
07339 if (p->pri->pri) {
07340 if (!pri_grab(p, p->pri)) {
07341 pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
07342 pri_rel(p->pri);
07343 }
07344 else
07345 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07346 }
07347 p->proceeding = 1;
07348 p->dialing = 0;
07349 }
07350 #endif
07351 #ifdef HAVE_SS7
07352
07353 if (chan->_state == AST_STATE_UP && (p->rlt != 1) && (p->sig == SIG_SS7)){
07354 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
07355 p->rlt = 1;
07356 }
07357
07358 if (!p->proceeding && p->sig == SIG_SS7 && p->ss7 && !p->outgoing) {
07359 if (p->ss7->ss7) {
07360 ss7_grab(p, p->ss7);
07361 isup_acm(p->ss7->ss7, p->ss7call);
07362 p->proceeding = 1;
07363 ss7_rel(p->ss7);
07364
07365 }
07366 }
07367 #endif
07368
07369 res = 0;
07370 break;
07371 case AST_CONTROL_PROGRESS:
07372 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
07373 #ifdef HAVE_PRI
07374 p->digital = 0;
07375 if (!p->progress && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07376 && p->pri && !p->outgoing) {
07377 if (p->pri->pri) {
07378 if (!pri_grab(p, p->pri)) {
07379 #ifdef HAVE_PRI_PROG_W_CAUSE
07380 pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1);
07381 #else
07382 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
07383 #endif
07384 pri_rel(p->pri);
07385 }
07386 else
07387 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07388 }
07389 p->progress = 1;
07390 }
07391 #endif
07392 #ifdef HAVE_SS7
07393 if (!p->progress && p->sig==SIG_SS7 && p->ss7 && !p->outgoing) {
07394 if (p->ss7->ss7) {
07395 ss7_grab(p, p->ss7);
07396 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
07397 p->progress = 1;
07398 ss7_rel(p->ss7);
07399
07400 dahdi_enable_ec(p);
07401
07402 }
07403 }
07404 #endif
07405
07406 res = 0;
07407 break;
07408 case AST_CONTROL_CONGESTION:
07409 chan->hangupcause = AST_CAUSE_CONGESTION;
07410 #ifdef HAVE_PRI
07411 if (p->priindication_oob && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))) {
07412 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
07413 chan->_softhangup |= AST_SOFTHANGUP_DEV;
07414 res = 0;
07415 } else if (!p->progress && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07416 && p->pri && !p->outgoing) {
07417 if (p->pri) {
07418 if (!pri_grab(p, p->pri)) {
07419 #ifdef HAVE_PRI_PROG_W_CAUSE
07420 pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_SWITCH_CONGESTION);
07421 #else
07422 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
07423 #endif
07424 pri_rel(p->pri);
07425 } else
07426 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07427 }
07428 p->progress = 1;
07429 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
07430 } else
07431 #endif
07432 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
07433 break;
07434 case AST_CONTROL_HOLD:
07435 #ifdef HAVE_PRI
07436 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
07437 if (!pri_grab(p, p->pri)) {
07438 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
07439 pri_rel(p->pri);
07440 } else
07441 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07442 } else
07443 #endif
07444 ast_moh_start(chan, data, p->mohinterpret);
07445 break;
07446 case AST_CONTROL_UNHOLD:
07447 #ifdef HAVE_PRI
07448 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
07449 if (!pri_grab(p, p->pri)) {
07450 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
07451 pri_rel(p->pri);
07452 } else
07453 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07454 } else
07455 #endif
07456 ast_moh_stop(chan);
07457 break;
07458 case AST_CONTROL_RADIO_KEY:
07459 if (p->radio)
07460 res = dahdi_set_hook(p->subs[idx].dfd, DAHDI_OFFHOOK);
07461 res = 0;
07462 break;
07463 case AST_CONTROL_RADIO_UNKEY:
07464 if (p->radio)
07465 res = dahdi_set_hook(p->subs[idx].dfd, DAHDI_RINGOFF);
07466 res = 0;
07467 break;
07468 case AST_CONTROL_FLASH:
07469
07470 if (ISTRUNK(p) && (p->sig != SIG_PRI)) {
07471
07472 p->dop.dialstr[0] = '\0';
07473 if ((ioctl(p->subs[SUB_REAL].dfd,DAHDI_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
07474 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
07475 chan->name, strerror(errno));
07476 } else
07477 res = 0;
07478 } else
07479 res = 0;
07480 break;
07481 case AST_CONTROL_SRCUPDATE:
07482 res = 0;
07483 break;
07484 case -1:
07485 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
07486 break;
07487 }
07488 } else
07489 res = 0;
07490 ast_mutex_unlock(&p->lock);
07491 return res;
07492 }
07493
07494 static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpbx, int idx, int law, int transfercapability)
07495 {
07496 struct ast_channel *tmp;
07497 int deflaw;
07498 int res;
07499 int x,y;
07500 int features;
07501 struct ast_str *chan_name;
07502 struct ast_variable *v;
07503 struct dahdi_params ps;
07504 if (i->subs[idx].owner) {
07505 ast_log(LOG_WARNING, "Channel %d already has a %s call\n", i->channel,subnames[idx]);
07506 return NULL;
07507 }
07508 y = 1;
07509 chan_name = ast_str_alloca(32);
07510 do {
07511 #ifdef HAVE_PRI
07512 if (i->bearer || (i->pri && (i->sig == SIG_FXSKS)))
07513 ast_str_set(&chan_name, 0, "%d:%d-%d", i->pri->trunkgroup, i->channel, y);
07514 else
07515 #endif
07516 if (i->channel == CHAN_PSEUDO)
07517 ast_str_set(&chan_name, 0, "pseudo-%ld", ast_random());
07518 else
07519 ast_str_set(&chan_name, 0, "%d-%d", i->channel, y);
07520 for (x = 0; x < 3; x++) {
07521 if ((idx != x) && i->subs[x].owner && !strcasecmp(ast_str_buffer(chan_name), i->subs[x].owner->name + 6))
07522 break;
07523 }
07524 y++;
07525 } while (x < 3);
07526 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));
07527 if (!tmp)
07528 return NULL;
07529 tmp->tech = &dahdi_tech;
07530 memset(&ps, 0, sizeof(ps));
07531 ps.channo = i->channel;
07532 res = ioctl(i->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps);
07533 if (res) {
07534 ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW: %s\n", strerror(errno));
07535 ps.curlaw = DAHDI_LAW_MULAW;
07536 }
07537 if (ps.curlaw == DAHDI_LAW_ALAW)
07538 deflaw = AST_FORMAT_ALAW;
07539 else
07540 deflaw = AST_FORMAT_ULAW;
07541 if (law) {
07542 if (law == DAHDI_LAW_ALAW)
07543 deflaw = AST_FORMAT_ALAW;
07544 else
07545 deflaw = AST_FORMAT_ULAW;
07546 }
07547 ast_channel_set_fd(tmp, 0, i->subs[idx].dfd);
07548 tmp->nativeformats = deflaw;
07549
07550 tmp->rawreadformat = deflaw;
07551 tmp->readformat = deflaw;
07552 tmp->rawwriteformat = deflaw;
07553 tmp->writeformat = deflaw;
07554 i->subs[idx].linear = 0;
07555 dahdi_setlinear(i->subs[idx].dfd, i->subs[idx].linear);
07556 features = 0;
07557 if (idx == SUB_REAL) {
07558 if (i->busydetect && CANBUSYDETECT(i))
07559 features |= DSP_FEATURE_BUSY_DETECT;
07560 if ((i->callprogress & CALLPROGRESS_PROGRESS) && CANPROGRESSDETECT(i))
07561 features |= DSP_FEATURE_CALL_PROGRESS;
07562 if ((i->waitfordialtone) && CANPROGRESSDETECT(i))
07563 features |= DSP_FEATURE_WAITDIALTONE;
07564 if ((!i->outgoing && (i->callprogress & CALLPROGRESS_FAX_INCOMING)) ||
07565 (i->outgoing && (i->callprogress & CALLPROGRESS_FAX_OUTGOING))) {
07566 features |= DSP_FEATURE_FAX_DETECT;
07567 }
07568 x = DAHDI_TONEDETECT_ON | DAHDI_TONEDETECT_MUTE;
07569 if (ioctl(i->subs[idx].dfd, DAHDI_TONEDETECT, &x)) {
07570 i->hardwaredtmf = 0;
07571 features |= DSP_FEATURE_DIGIT_DETECT;
07572 } else if (NEED_MFDETECT(i)) {
07573 i->hardwaredtmf = 1;
07574 features |= DSP_FEATURE_DIGIT_DETECT;
07575 }
07576 }
07577 if (features) {
07578 if (i->dsp) {
07579 ast_debug(1, "Already have a dsp on %s?\n", tmp->name);
07580 } else {
07581 if (i->channel != CHAN_PSEUDO)
07582 i->dsp = ast_dsp_new();
07583 else
07584 i->dsp = NULL;
07585 if (i->dsp) {
07586 i->dsp_features = features;
07587 #if defined(HAVE_PRI) || defined(HAVE_SS7)
07588
07589 if (i->outgoing && ((i->sig == SIG_PRI) || (i->sig == SIG_BRI) || (i->sig == SIG_BRI_PTMP) || (i->sig == SIG_SS7))) {
07590
07591
07592 i->dsp_features = features & ~DSP_PROGRESS_TALK;
07593 features = 0;
07594 }
07595 #endif
07596 ast_dsp_set_features(i->dsp, features);
07597 ast_dsp_set_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax);
07598 if (!ast_strlen_zero(progzone))
07599 ast_dsp_set_call_progress_zone(i->dsp, progzone);
07600 if (i->busydetect && CANBUSYDETECT(i)) {
07601 ast_dsp_set_busy_count(i->dsp, i->busycount);
07602 ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength);
07603 }
07604 }
07605 }
07606 }
07607
07608 if (state == AST_STATE_RING)
07609 tmp->rings = 1;
07610 tmp->tech_pvt = i;
07611 if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) {
07612
07613 tmp->callgroup = i->callgroup;
07614 tmp->pickupgroup = i->pickupgroup;
07615 }
07616 if (!ast_strlen_zero(i->parkinglot))
07617 ast_string_field_set(tmp, parkinglot, i->parkinglot);
07618 if (!ast_strlen_zero(i->language))
07619 ast_string_field_set(tmp, language, i->language);
07620 if (!i->owner)
07621 i->owner = tmp;
07622 if (!ast_strlen_zero(i->accountcode))
07623 ast_string_field_set(tmp, accountcode, i->accountcode);
07624 if (i->amaflags)
07625 tmp->amaflags = i->amaflags;
07626 i->subs[idx].owner = tmp;
07627 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
07628 ast_string_field_set(tmp, call_forward, i->call_forward);
07629
07630 if (!i->adsi)
07631 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
07632 if (!ast_strlen_zero(i->exten))
07633 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
07634 if (!ast_strlen_zero(i->rdnis))
07635 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
07636 if (!ast_strlen_zero(i->dnid))
07637 tmp->cid.cid_dnid = ast_strdup(i->dnid);
07638
07639
07640
07641 #ifdef PRI_ANI
07642 if (!ast_strlen_zero(i->cid_ani))
07643 tmp->cid.cid_ani = ast_strdup(i->cid_ani);
07644 else
07645 tmp->cid.cid_ani = ast_strdup(i->cid_num);
07646 #else
07647 tmp->cid.cid_ani = ast_strdup(i->cid_num);
07648 #endif
07649 tmp->cid.cid_pres = i->callingpres;
07650 tmp->cid.cid_ton = i->cid_ton;
07651 tmp->cid.cid_ani2 = i->cid_ani2;
07652 #if defined(HAVE_PRI) || defined(HAVE_SS7)
07653 tmp->transfercapability = transfercapability;
07654 pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
07655 if (transfercapability & AST_TRANS_CAP_DIGITAL)
07656 i->digital = 1;
07657
07658 i->isidlecall = 0;
07659 i->alreadyhungup = 0;
07660 #endif
07661
07662 i->fake_event = 0;
07663
07664 dahdi_confmute(i, 0);
07665 i->muting = 0;
07666
07667 ast_jb_configure(tmp, &global_jbconf);
07668
07669 ast_devstate_changed_literal(ast_state_chan2dev(state), tmp->name);
07670
07671 for (v = i->vars ; v ; v = v->next)
07672 pbx_builtin_setvar_helper(tmp, v->name, v->value);
07673
07674 if (startpbx) {
07675 #ifdef HAVE_OPENR2
07676 if (i->mfcr2call) {
07677 pbx_builtin_setvar_helper(tmp, "MFCR2_CATEGORY", openr2_proto_get_category_string(i->mfcr2_recvd_category));
07678 }
07679 #endif
07680 if (ast_pbx_start(tmp)) {
07681 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
07682 ast_hangup(tmp);
07683 i->owner = NULL;
07684 return NULL;
07685 }
07686 }
07687
07688 ast_module_ref(ast_module_info->self);
07689 return tmp;
07690 }
07691
07692
07693 static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
07694 {
07695 char c;
07696
07697 *str = 0;
07698 for (;;)
07699 {
07700
07701 c = ast_waitfordigit(chan, ms);
07702
07703 if (c < 1)
07704 return c;
07705 *str++ = c;
07706 *str = 0;
07707 if (strchr(term, c))
07708 return 1;
07709 }
07710 }
07711
07712 static int dahdi_wink(struct dahdi_pvt *p, int idx)
07713 {
07714 int j;
07715 dahdi_set_hook(p->subs[idx].dfd, DAHDI_WINK);
07716 for (;;)
07717 {
07718
07719 j = DAHDI_IOMUX_SIGEVENT;
07720
07721 if (ioctl(p->subs[idx].dfd,DAHDI_IOMUX,&j) == -1) return(-1);
07722
07723 if (j & DAHDI_IOMUX_SIGEVENT) break;
07724 }
07725
07726 if (ioctl(p->subs[idx].dfd,DAHDI_GETEVENT,&j) == -1) return(-1);
07727 return 0;
07728 }
07729
07730
07731
07732
07733
07734
07735
07736
07737
07738
07739 static void dahdi_dnd(struct dahdi_pvt *dahdichan, int on)
07740 {
07741
07742 dahdichan->dnd = on;
07743 ast_verb(3, "%s DND on channel %d\n",
07744 on? "Enabled" : "Disabled",
07745 dahdichan->channel);
07746 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
07747 "Channel: DAHDI/%d\r\n"
07748 "Status: %s\r\n", dahdichan->channel,
07749 on? "enabled" : "disabled");
07750 }
07751
07752 static void *ss_thread(void *data)
07753 {
07754 struct ast_channel *chan = data;
07755 struct dahdi_pvt *p = chan->tech_pvt;
07756 char exten[AST_MAX_EXTENSION] = "";
07757 char exten2[AST_MAX_EXTENSION] = "";
07758 unsigned char buf[256];
07759 char dtmfcid[300];
07760 char dtmfbuf[300];
07761 struct callerid_state *cs = NULL;
07762 char *name = NULL, *number = NULL;
07763 int distMatches;
07764 int curRingData[3];
07765 int receivedRingT;
07766 int counter1;
07767 int counter;
07768 int samples = 0;
07769 struct ast_smdi_md_message *smdi_msg = NULL;
07770 int flags = 0;
07771 int i;
07772 int timeout;
07773 int getforward = 0;
07774 char *s1, *s2;
07775 int len = 0;
07776 int res;
07777 int idx;
07778
07779 ast_mutex_lock(&ss_thread_lock);
07780 ss_thread_count++;
07781 ast_mutex_unlock(&ss_thread_lock);
07782
07783
07784
07785 if (!p) {
07786 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
07787 ast_hangup(chan);
07788 goto quit;
07789 }
07790 ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
07791 idx = dahdi_get_index(chan, p, 1);
07792 if (idx < 0) {
07793 ast_log(LOG_WARNING, "Huh?\n");
07794 ast_hangup(chan);
07795 goto quit;
07796 }
07797 if (p->dsp)
07798 ast_dsp_digitreset(p->dsp);
07799 switch (p->sig) {
07800 #ifdef HAVE_PRI
07801 case SIG_PRI:
07802 case SIG_BRI:
07803 case SIG_BRI_PTMP:
07804
07805 ast_copy_string(exten, p->exten, sizeof(exten));
07806 len = strlen(exten);
07807 res = 0;
07808 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
07809 if (len && !ast_ignore_pattern(chan->context, exten))
07810 tone_zone_play_tone(p->subs[idx].dfd, -1);
07811 else
07812 tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALTONE);
07813 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
07814 timeout = matchdigittimeout;
07815 else
07816 timeout = gendigittimeout;
07817 res = ast_waitfordigit(chan, timeout);
07818 if (res < 0) {
07819 ast_debug(1, "waitfordigit returned < 0...\n");
07820 ast_hangup(chan);
07821 goto quit;
07822 } else if (res) {
07823 exten[len++] = res;
07824 exten[len] = '\0';
07825 } else
07826 break;
07827 }
07828
07829 if (ast_strlen_zero(exten)) {
07830 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
07831 exten[0] = 's';
07832 exten[1] = '\0';
07833 }
07834 tone_zone_play_tone(p->subs[idx].dfd, -1);
07835 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
07836
07837 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
07838 if (p->dsp) {
07839 ast_dsp_digitreset(p->dsp);
07840 }
07841 if (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) {
07842 ast_mutex_lock(&p->lock);
07843 if (p->pri->pri) {
07844 if (!pri_grab(p, p->pri)) {
07845 pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
07846 p->proceeding = 1;
07847 pri_rel(p->pri);
07848 } else {
07849 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07850 }
07851 }
07852 ast_mutex_unlock(&p->lock);
07853 }
07854 dahdi_enable_ec(p);
07855 ast_setstate(chan, AST_STATE_RING);
07856 res = ast_pbx_run(chan);
07857 if (res) {
07858 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
07859 }
07860 } else {
07861 ast_debug(1, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
07862 chan->hangupcause = AST_CAUSE_UNALLOCATED;
07863 ast_hangup(chan);
07864 p->exten[0] = '\0';
07865
07866 p->call = NULL;
07867 }
07868 goto quit;
07869 break;
07870 #endif
07871 case SIG_FEATD:
07872 case SIG_FEATDMF:
07873 case SIG_FEATDMF_TA:
07874 case SIG_E911:
07875 case SIG_FGC_CAMAMF:
07876 case SIG_FEATB:
07877 case SIG_EMWINK:
07878 case SIG_SF_FEATD:
07879 case SIG_SF_FEATDMF:
07880 case SIG_SF_FEATB:
07881 case SIG_SFWINK:
07882 if (dahdi_wink(p, idx))
07883 goto quit;
07884
07885 case SIG_EM:
07886 case SIG_EM_E1:
07887 case SIG_SF:
07888 case SIG_FGC_CAMA:
07889 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
07890 if (p->dsp)
07891 ast_dsp_digitreset(p->dsp);
07892
07893 if (p->dsp) {
07894 if (NEED_MFDETECT(p))
07895 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MF | p->dtmfrelax);
07896 else
07897 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
07898 }
07899 memset(dtmfbuf, 0, sizeof(dtmfbuf));
07900
07901 if (!p->immediate)
07902
07903 res = ast_waitfordigit(chan, 5000);
07904 else
07905 res = 0;
07906 if (res > 0) {
07907
07908 dtmfbuf[0] = res;
07909 switch (p->sig) {
07910 case SIG_FEATD:
07911 case SIG_SF_FEATD:
07912 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
07913 if (res > 0)
07914 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
07915 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
07916 break;
07917 case SIG_FEATDMF_TA:
07918 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
07919 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
07920 if (dahdi_wink(p, idx)) goto quit;
07921 dtmfbuf[0] = 0;
07922
07923 res = ast_waitfordigit(chan, 5000);
07924 if (res <= 0) break;
07925 dtmfbuf[0] = res;
07926
07927 case SIG_FEATDMF:
07928 case SIG_E911:
07929 case SIG_FGC_CAMAMF:
07930 case SIG_SF_FEATDMF:
07931 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
07932
07933 if ((p->sig == SIG_FEATDMF) && (dtmfbuf[1] != '0') && (strlen(dtmfbuf) != 14))
07934 {
07935 if (dahdi_wink(p, idx)) goto quit;
07936 dtmfbuf[0] = 0;
07937
07938 res = ast_waitfordigit(chan, 5000);
07939 if (res <= 0) break;
07940 dtmfbuf[0] = res;
07941 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
07942 }
07943 if (res > 0) {
07944
07945 if (p->sig == SIG_E911)
07946 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
07947 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
07948 }
07949 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
07950 break;
07951 case SIG_FEATB:
07952 case SIG_SF_FEATB:
07953 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
07954 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
07955 break;
07956 case SIG_EMWINK:
07957
07958
07959
07960
07961 if (res == '*') {
07962 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
07963 if (res > 0)
07964 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
07965 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
07966 break;
07967 }
07968 default:
07969
07970 len = 1;
07971 dtmfbuf[len] = '\0';
07972 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
07973 if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
07974 timeout = matchdigittimeout;
07975 } else {
07976 timeout = gendigittimeout;
07977 }
07978 res = ast_waitfordigit(chan, timeout);
07979 if (res < 0) {
07980 ast_debug(1, "waitfordigit returned < 0...\n");
07981 ast_hangup(chan);
07982 goto quit;
07983 } else if (res) {
07984 dtmfbuf[len++] = res;
07985 dtmfbuf[len] = '\0';
07986 } else {
07987 break;
07988 }
07989 }
07990 break;
07991 }
07992 }
07993 if (res == -1) {
07994 ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
07995 ast_hangup(chan);
07996 goto quit;
07997 } else if (res < 0) {
07998 ast_debug(1, "Got hung up before digits finished\n");
07999 ast_hangup(chan);
08000 goto quit;
08001 }
08002
08003 if (p->sig == SIG_FGC_CAMA) {
08004 char anibuf[100];
08005
08006 if (ast_safe_sleep(chan,1000) == -1) {
08007 ast_hangup(chan);
08008 goto quit;
08009 }
08010 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
08011 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MF | p->dtmfrelax);
08012 res = my_getsigstr(chan, anibuf, "#", 10000);
08013 if ((res > 0) && (strlen(anibuf) > 2)) {
08014 if (anibuf[strlen(anibuf) - 1] == '#')
08015 anibuf[strlen(anibuf) - 1] = 0;
08016 ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
08017 }
08018 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
08019 }
08020
08021 ast_copy_string(exten, dtmfbuf, sizeof(exten));
08022 if (ast_strlen_zero(exten))
08023 ast_copy_string(exten, "s", sizeof(exten));
08024 if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
08025
08026 if (exten[0] == '*') {
08027 char *stringp=NULL;
08028 ast_copy_string(exten2, exten, sizeof(exten2));
08029
08030 stringp=exten2 +1;
08031 s1 = strsep(&stringp, "*");
08032 s2 = strsep(&stringp, "*");
08033 if (s2) {
08034 if (!ast_strlen_zero(p->cid_num))
08035 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
08036 else
08037 ast_set_callerid(chan, s1, NULL, s1);
08038 ast_copy_string(exten, s2, sizeof(exten));
08039 } else
08040 ast_copy_string(exten, s1, sizeof(exten));
08041 } else if (p->sig == SIG_FEATD)
08042 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
08043 }
08044 if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) {
08045 if (exten[0] == '*') {
08046 char *stringp=NULL;
08047 ast_copy_string(exten2, exten, sizeof(exten2));
08048
08049 stringp=exten2 +1;
08050 s1 = strsep(&stringp, "#");
08051 s2 = strsep(&stringp, "#");
08052 if (s2) {
08053 if (!ast_strlen_zero(p->cid_num))
08054 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
08055 else
08056 if (*(s1 + 2))
08057 ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
08058 ast_copy_string(exten, s2 + 1, sizeof(exten));
08059 } else
08060 ast_copy_string(exten, s1 + 2, sizeof(exten));
08061 } else
08062 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
08063 }
08064 if ((p->sig == SIG_E911) || (p->sig == SIG_FGC_CAMAMF)) {
08065 if (exten[0] == '*') {
08066 char *stringp=NULL;
08067 ast_copy_string(exten2, exten, sizeof(exten2));
08068
08069 stringp=exten2 +1;
08070 s1 = strsep(&stringp, "#");
08071 s2 = strsep(&stringp, "#");
08072 if (s2 && (*(s2 + 1) == '0')) {
08073 if (*(s2 + 2))
08074 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
08075 }
08076 if (s1) ast_copy_string(exten, s1, sizeof(exten));
08077 else ast_copy_string(exten, "911", sizeof(exten));
08078 } else
08079 ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d. Assuming E&M Wink instead\n", p->channel);
08080 }
08081 if (p->sig == SIG_FEATB) {
08082 if (exten[0] == '*') {
08083 char *stringp=NULL;
08084 ast_copy_string(exten2, exten, sizeof(exten2));
08085
08086 stringp=exten2 +1;
08087 s1 = strsep(&stringp, "#");
08088 ast_copy_string(exten, exten2 + 1, sizeof(exten));
08089 } else
08090 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
08091 }
08092 if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) {
08093 dahdi_wink(p, idx);
08094
08095
08096
08097 if (ast_safe_sleep(chan,100)) goto quit;
08098 }
08099 dahdi_enable_ec(p);
08100 if (NEED_MFDETECT(p)) {
08101 if (p->dsp) {
08102 if (!p->hardwaredtmf)
08103 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
08104 else {
08105 ast_dsp_free(p->dsp);
08106 p->dsp = NULL;
08107 }
08108 }
08109 }
08110
08111 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
08112 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
08113 if (p->dsp) ast_dsp_digitreset(p->dsp);
08114 res = ast_pbx_run(chan);
08115 if (res) {
08116 ast_log(LOG_WARNING, "PBX exited non-zero\n");
08117 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08118 }
08119 goto quit;
08120 } else {
08121 ast_verb(2, "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
08122 sleep(2);
08123 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_INFO);
08124 if (res < 0)
08125 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
08126 else
08127 sleep(1);
08128 res = ast_streamfile(chan, "ss-noservice", chan->language);
08129 if (res >= 0)
08130 ast_waitstream(chan, "");
08131 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08132 ast_hangup(chan);
08133 goto quit;
08134 }
08135 break;
08136 case SIG_FXOLS:
08137 case SIG_FXOGS:
08138 case SIG_FXOKS:
08139
08140 timeout = firstdigittimeout;
08141
08142
08143 if (p->subs[SUB_THREEWAY].owner)
08144 timeout = 999999;
08145 while (len < AST_MAX_EXTENSION-1) {
08146
08147
08148 if (p->immediate)
08149 res = 's';
08150 else
08151 res = ast_waitfordigit(chan, timeout);
08152 timeout = 0;
08153 if (res < 0) {
08154 ast_debug(1, "waitfordigit returned < 0...\n");
08155 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
08156 ast_hangup(chan);
08157 goto quit;
08158 } else if (res) {
08159 ast_debug(1,"waitfordigit returned '%c' (%d), timeout = %d\n", res, res, timeout);
08160 exten[len++]=res;
08161 exten[len] = '\0';
08162 }
08163 if (!ast_ignore_pattern(chan->context, exten))
08164 tone_zone_play_tone(p->subs[idx].dfd, -1);
08165 else
08166 tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALTONE);
08167 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
08168 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
08169 if (getforward) {
08170
08171 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
08172 ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
08173 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08174 if (res)
08175 break;
08176 usleep(500000);
08177 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
08178 sleep(1);
08179 memset(exten, 0, sizeof(exten));
08180 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALTONE);
08181 len = 0;
08182 getforward = 0;
08183 } else {
08184 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
08185 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
08186 if (!ast_strlen_zero(p->cid_num)) {
08187 if (!p->hidecallerid)
08188 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
08189 else
08190 ast_set_callerid(chan, NULL, NULL, p->cid_num);
08191 }
08192 if (!ast_strlen_zero(p->cid_name)) {
08193 if (!p->hidecallerid)
08194 ast_set_callerid(chan, NULL, p->cid_name, NULL);
08195 }
08196 ast_setstate(chan, AST_STATE_RING);
08197 dahdi_enable_ec(p);
08198 res = ast_pbx_run(chan);
08199 if (res) {
08200 ast_log(LOG_WARNING, "PBX exited non-zero\n");
08201 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08202 }
08203 goto quit;
08204 }
08205 } else {
08206
08207
08208 timeout = matchdigittimeout;
08209 }
08210 } else if (res == 0) {
08211 ast_debug(1, "not enough digits (and no ambiguous match)...\n");
08212 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08213 dahdi_wait_event(p->subs[idx].dfd);
08214 ast_hangup(chan);
08215 goto quit;
08216 } else if (p->callwaiting && !strcmp(exten, "*70")) {
08217 ast_verb(3, "Disabling call waiting on %s\n", chan->name);
08218
08219 p->callwaiting = 0;
08220 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08221 if (res) {
08222 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
08223 chan->name, strerror(errno));
08224 }
08225 len = 0;
08226 ioctl(p->subs[idx].dfd,DAHDI_CONFDIAG,&len);
08227 memset(exten, 0, sizeof(exten));
08228 timeout = firstdigittimeout;
08229
08230 } else if (!strcmp(exten,ast_pickup_ext())) {
08231
08232
08233
08234
08235 if (idx == SUB_REAL) {
08236
08237 if (p->subs[SUB_THREEWAY].owner) {
08238
08239
08240 alloc_sub(p, SUB_CALLWAIT);
08241 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
08242 unalloc_sub(p, SUB_THREEWAY);
08243 }
08244 dahdi_enable_ec(p);
08245 if (ast_pickup_call(chan)) {
08246 ast_debug(1, "No call pickup possible...\n");
08247 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08248 dahdi_wait_event(p->subs[idx].dfd);
08249 }
08250 ast_hangup(chan);
08251 goto quit;
08252 } else {
08253 ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
08254 ast_hangup(chan);
08255 goto quit;
08256 }
08257
08258 } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
08259 ast_verb(3, "Disabling Caller*ID on %s\n", chan->name);
08260
08261 p->hidecallerid = 1;
08262 if (chan->cid.cid_num)
08263 ast_free(chan->cid.cid_num);
08264 chan->cid.cid_num = NULL;
08265 if (chan->cid.cid_name)
08266 ast_free(chan->cid.cid_name);
08267 chan->cid.cid_name = NULL;
08268 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08269 if (res) {
08270 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
08271 chan->name, strerror(errno));
08272 }
08273 len = 0;
08274 memset(exten, 0, sizeof(exten));
08275 timeout = firstdigittimeout;
08276 } else if (p->callreturn && !strcmp(exten, "*69")) {
08277 res = 0;
08278 if (!ast_strlen_zero(p->lastcid_num)) {
08279 res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
08280 }
08281 if (!res)
08282 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08283 break;
08284 } else if (!strcmp(exten, "*78")) {
08285 dahdi_dnd(p, 1);
08286
08287 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08288 getforward = 0;
08289 memset(exten, 0, sizeof(exten));
08290 len = 0;
08291 } else if (!strcmp(exten, "*79")) {
08292 dahdi_dnd(p, 0);
08293
08294 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08295 getforward = 0;
08296 memset(exten, 0, sizeof(exten));
08297 len = 0;
08298 } else if (p->cancallforward && !strcmp(exten, "*72")) {
08299 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08300 getforward = 1;
08301 memset(exten, 0, sizeof(exten));
08302 len = 0;
08303 } else if (p->cancallforward && !strcmp(exten, "*73")) {
08304 ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
08305 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08306 memset(p->call_forward, 0, sizeof(p->call_forward));
08307 getforward = 0;
08308 memset(exten, 0, sizeof(exten));
08309 len = 0;
08310 } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
08311 p->subs[SUB_THREEWAY].owner &&
08312 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
08313
08314
08315 ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL);
08316 ast_verb(3, "Parking call to '%s'\n", chan->name);
08317 break;
08318 } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
08319 ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
08320 res = ast_db_put("blacklist", p->lastcid_num, "1");
08321 if (!res) {
08322 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08323 memset(exten, 0, sizeof(exten));
08324 len = 0;
08325 }
08326 } else if (p->hidecallerid && !strcmp(exten, "*82")) {
08327 ast_verb(3, "Enabling Caller*ID on %s\n", chan->name);
08328
08329 p->hidecallerid = 0;
08330 if (chan->cid.cid_num)
08331 ast_free(chan->cid.cid_num);
08332 chan->cid.cid_num = NULL;
08333 if (chan->cid.cid_name)
08334 ast_free(chan->cid.cid_name);
08335 chan->cid.cid_name = NULL;
08336 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
08337 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08338 if (res) {
08339 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
08340 chan->name, strerror(errno));
08341 }
08342 len = 0;
08343 memset(exten, 0, sizeof(exten));
08344 timeout = firstdigittimeout;
08345 } else if (!strcmp(exten, "*0")) {
08346 struct ast_channel *nbridge =
08347 p->subs[SUB_THREEWAY].owner;
08348 struct dahdi_pvt *pbridge = NULL;
08349
08350 if (nbridge && ast_bridged_channel(nbridge))
08351 pbridge = ast_bridged_channel(nbridge)->tech_pvt;
08352 if (nbridge && pbridge &&
08353 (nbridge->tech == &dahdi_tech) &&
08354 (ast_bridged_channel(nbridge)->tech == &dahdi_tech) &&
08355 ISTRUNK(pbridge)) {
08356 int func = DAHDI_FLASH;
08357
08358 p->dop.dialstr[0] = '\0';
08359
08360 if ((ioctl(pbridge->subs[SUB_REAL].dfd,DAHDI_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
08361 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
08362 nbridge->name, strerror(errno));
08363 }
08364 swap_subs(p, SUB_REAL, SUB_THREEWAY);
08365 unalloc_sub(p, SUB_THREEWAY);
08366 p->owner = p->subs[SUB_REAL].owner;
08367 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
08368 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
08369 ast_hangup(chan);
08370 goto quit;
08371 } else {
08372 tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08373 dahdi_wait_event(p->subs[idx].dfd);
08374 tone_zone_play_tone(p->subs[idx].dfd, -1);
08375 swap_subs(p, SUB_REAL, SUB_THREEWAY);
08376 unalloc_sub(p, SUB_THREEWAY);
08377 p->owner = p->subs[SUB_REAL].owner;
08378 ast_hangup(chan);
08379 goto quit;
08380 }
08381 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
08382 ((exten[0] != '*') || (strlen(exten) > 2))) {
08383 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);
08384 break;
08385 }
08386 if (!timeout)
08387 timeout = gendigittimeout;
08388 if (len && !ast_ignore_pattern(chan->context, exten))
08389 tone_zone_play_tone(p->subs[idx].dfd, -1);
08390 }
08391 break;
08392 case SIG_FXSLS:
08393 case SIG_FXSGS:
08394 case SIG_FXSKS:
08395 #ifdef HAVE_PRI
08396 if (p->pri) {
08397
08398 struct ast_frame *f;
08399 int res;
08400 time_t start;
08401
08402 time(&start);
08403 ast_setstate(chan, AST_STATE_RING);
08404 while (time(NULL) < start + 3) {
08405 res = ast_waitfor(chan, 1000);
08406 if (res) {
08407 f = ast_read(chan);
08408 if (!f) {
08409 ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
08410 ast_hangup(chan);
08411 goto quit;
08412 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
08413 res = 1;
08414 } else
08415 res = 0;
08416 ast_frfree(f);
08417 if (res) {
08418 ast_debug(1, "Got ring!\n");
08419 res = 0;
08420 break;
08421 }
08422 }
08423 }
08424 }
08425 #endif
08426
08427 if (p->use_smdi && p->smdi_iface) {
08428 smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, SMDI_MD_WAIT_TIMEOUT);
08429
08430 if (smdi_msg != NULL) {
08431 ast_copy_string(chan->exten, smdi_msg->fwd_st, sizeof(chan->exten));
08432
08433 if (smdi_msg->type == 'B')
08434 pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
08435 else if (smdi_msg->type == 'N')
08436 pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
08437
08438 ast_debug(1, "Received SMDI message on %s\n", chan->name);
08439 } else {
08440 ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
08441 }
08442 }
08443
08444 if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
08445 number = smdi_msg->calling_st;
08446
08447
08448
08449
08450 } else if (p->use_callerid && (chan->_state == AST_STATE_PRERING && (p->cid_start == CID_START_POLARITY || p->cid_start == CID_START_POLARITY_IN))) {
08451
08452 if (p->cid_signalling == CID_SIG_DTMF) {
08453 int k = 0;
08454 cs = NULL;
08455 ast_debug(1, "Receiving DTMF cid on channel %s\n", chan->name);
08456 dahdi_setlinear(p->subs[idx].dfd, 0);
08457
08458
08459
08460
08461
08462
08463 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
08464 res = 4000;
08465 for (;;) {
08466 struct ast_frame *f;
08467 res = ast_waitfor(chan, res);
08468 if (res <= 0) {
08469
08470
08471
08472
08473
08474 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
08475 "Exiting simple switch\n");
08476 ast_hangup(chan);
08477 goto quit;
08478 }
08479 f = ast_read(chan);
08480 if (!f)
08481 break;
08482 if (f->frametype == AST_FRAME_DTMF) {
08483 if (k < ARRAY_LEN(dtmfbuf) - 1) {
08484 dtmfbuf[k++] = f->subclass;
08485 }
08486 ast_debug(1, "CID got digit '%c'\n", f->subclass);
08487 res = 4000;
08488 }
08489 ast_frfree(f);
08490 if (chan->_state == AST_STATE_RING ||
08491 chan->_state == AST_STATE_RINGING)
08492 break;
08493 }
08494 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
08495 dtmfbuf[k] = '\0';
08496 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
08497
08498 ast_debug(1, "CID got string '%s'\n", dtmfbuf);
08499 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
08500 ast_debug(1, "CID is '%s', flags %d\n", dtmfcid, flags);
08501
08502 if (!ast_strlen_zero(dtmfcid))
08503 number = dtmfcid;
08504 else
08505 number = NULL;
08506
08507 } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
08508 cs = callerid_new(p->cid_signalling);
08509 if (cs) {
08510 samples = 0;
08511 #if 1
08512 bump_gains(p);
08513 #endif
08514
08515 dahdi_setlinear(p->subs[idx].dfd, 0);
08516
08517
08518 for (;;) {
08519 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
08520 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
08521 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
08522 callerid_free(cs);
08523 ast_hangup(chan);
08524 goto quit;
08525 }
08526 if (i & DAHDI_IOMUX_SIGEVENT) {
08527 res = dahdi_get_event(p->subs[idx].dfd);
08528 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
08529 if (res == DAHDI_EVENT_NOALARM) {
08530 p->inalarm = 0;
08531 }
08532
08533 if (p->cid_signalling == CID_SIG_V23_JP) {
08534 if (res == DAHDI_EVENT_RINGBEGIN) {
08535 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
08536 usleep(1);
08537 }
08538 } else {
08539 res = 0;
08540 break;
08541 }
08542 } else if (i & DAHDI_IOMUX_READ) {
08543 res = read(p->subs[idx].dfd, buf, sizeof(buf));
08544 if (res < 0) {
08545 if (errno != ELAST) {
08546 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
08547 callerid_free(cs);
08548 ast_hangup(chan);
08549 goto quit;
08550 }
08551 break;
08552 }
08553 samples += res;
08554
08555 if (p->cid_signalling == CID_SIG_V23_JP) {
08556 res = callerid_feed_jp(cs, buf, res, AST_LAW(p));
08557 } else {
08558 res = callerid_feed(cs, buf, res, AST_LAW(p));
08559 }
08560 if (res < 0) {
08561
08562
08563
08564
08565 ast_log(LOG_WARNING,
08566 "Failed to decode CallerID on channel '%s'\n",
08567 chan->name);
08568 break;
08569 } else if (res)
08570 break;
08571 else if (samples > (8000 * 10))
08572 break;
08573 }
08574 }
08575 if (res == 1) {
08576 callerid_get(cs, &name, &number, &flags);
08577 ast_log(LOG_NOTICE, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
08578 }
08579
08580 if (p->cid_signalling == CID_SIG_V23_JP) {
08581 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
08582 usleep(1);
08583 }
08584
08585
08586 res = 4000;
08587 for (;;) {
08588 struct ast_frame *f;
08589 res = ast_waitfor(chan, res);
08590 if (res <= 0) {
08591 ast_log(LOG_WARNING, "CID timed out waiting for ring. "
08592 "Exiting simple switch\n");
08593 ast_hangup(chan);
08594 goto quit;
08595 }
08596 if (!(f = ast_read(chan))) {
08597 ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
08598 ast_hangup(chan);
08599 goto quit;
08600 }
08601 ast_frfree(f);
08602 if (chan->_state == AST_STATE_RING ||
08603 chan->_state == AST_STATE_RINGING)
08604 break;
08605 }
08606
08607
08608
08609 if (p->usedistinctiveringdetection) {
08610 len = 0;
08611 distMatches = 0;
08612
08613 for (receivedRingT = 0; receivedRingT < ARRAY_LEN(curRingData); receivedRingT++)
08614 curRingData[receivedRingT] = 0;
08615 receivedRingT = 0;
08616 counter = 0;
08617 counter1 = 0;
08618
08619 if (strcmp(p->context,p->defcontext) != 0) {
08620 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
08621 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
08622 }
08623
08624 for (;;) {
08625 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
08626 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
08627 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
08628 callerid_free(cs);
08629 ast_hangup(chan);
08630 goto quit;
08631 }
08632 if (i & DAHDI_IOMUX_SIGEVENT) {
08633 res = dahdi_get_event(p->subs[idx].dfd);
08634 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
08635 if (res == DAHDI_EVENT_NOALARM) {
08636 p->inalarm = 0;
08637 }
08638 res = 0;
08639
08640
08641 curRingData[receivedRingT] = p->ringt;
08642
08643 if (p->ringt < p->ringt_base/2)
08644 break;
08645
08646
08647 if (++receivedRingT == ARRAY_LEN(curRingData))
08648 break;
08649 } else if (i & DAHDI_IOMUX_READ) {
08650 res = read(p->subs[idx].dfd, buf, sizeof(buf));
08651 if (res < 0) {
08652 if (errno != ELAST) {
08653 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
08654 callerid_free(cs);
08655 ast_hangup(chan);
08656 goto quit;
08657 }
08658 break;
08659 }
08660 if (p->ringt)
08661 p->ringt--;
08662 if (p->ringt == 1) {
08663 res = -1;
08664 break;
08665 }
08666 }
08667 }
08668
08669 ast_verb(3, "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
08670 for (counter = 0; counter < 3; counter++) {
08671
08672
08673 distMatches = 0;
08674 for (counter1 = 0; counter1 < 3; counter1++) {
08675 ast_verb(3, "Ring pattern check range: %d\n", p->drings.ringnum[counter].range);
08676 if (p->drings.ringnum[counter].ring[counter1] == -1) {
08677 ast_verb(3, "Pattern ignore (-1) detected, so matching pattern %d regardless.\n",
08678 curRingData[counter1]);
08679 distMatches++;
08680 } else if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range) &&
08681 curRingData[counter1] >= (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range)) {
08682 ast_verb(3, "Ring pattern matched in range: %d to %d\n",
08683 (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range),
08684 (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range));
08685 distMatches++;
08686 }
08687 }
08688
08689 if (distMatches == 3) {
08690
08691 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
08692 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
08693 ast_verb(3, "Distinctive Ring matched context %s\n",p->context);
08694 break;
08695 }
08696 }
08697 }
08698
08699 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
08700 #if 1
08701 restore_gains(p);
08702 #endif
08703 } else
08704 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
08705 } else {
08706 ast_log(LOG_WARNING, "Channel %s in prering "
08707 "state, but I have nothing to do. "
08708 "Terminating simple switch, should be "
08709 "restarted by the actual ring.\n",
08710 chan->name);
08711 ast_hangup(chan);
08712 goto quit;
08713 }
08714 } else if (p->use_callerid && p->cid_start == CID_START_RING) {
08715 if (p->cid_signalling == CID_SIG_DTMF) {
08716 int k = 0;
08717 cs = NULL;
08718 dahdi_setlinear(p->subs[idx].dfd, 0);
08719 res = 2000;
08720 for (;;) {
08721 struct ast_frame *f;
08722 res = ast_waitfor(chan, res);
08723 if (res <= 0) {
08724 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
08725 "Exiting simple switch\n");
08726 ast_hangup(chan);
08727 return NULL;
08728 }
08729 f = ast_read(chan);
08730 if (f->frametype == AST_FRAME_DTMF) {
08731 dtmfbuf[k++] = f->subclass;
08732 ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
08733 res = 2000;
08734 }
08735 ast_frfree(f);
08736
08737 if (p->ringt_base == p->ringt)
08738 break;
08739 }
08740 dtmfbuf[k] = '\0';
08741 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
08742
08743 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
08744 ast_log(LOG_DEBUG, "CID is '%s', flags %d\n",
08745 dtmfcid, flags);
08746
08747 if (!ast_strlen_zero(dtmfcid))
08748 number = dtmfcid;
08749 else
08750 number = NULL;
08751
08752 } else {
08753
08754 cs = callerid_new(p->cid_signalling);
08755 if (cs) {
08756 #if 1
08757 bump_gains(p);
08758 #endif
08759 samples = 0;
08760 len = 0;
08761 distMatches = 0;
08762
08763 for (receivedRingT = 0; receivedRingT < ARRAY_LEN(curRingData); receivedRingT++)
08764 curRingData[receivedRingT] = 0;
08765 receivedRingT = 0;
08766 counter = 0;
08767 counter1 = 0;
08768
08769 if (strcmp(p->context,p->defcontext) != 0) {
08770 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
08771 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
08772 }
08773
08774
08775 dahdi_setlinear(p->subs[idx].dfd, 0);
08776 for (;;) {
08777 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
08778 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
08779 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
08780 callerid_free(cs);
08781 ast_hangup(chan);
08782 goto quit;
08783 }
08784 if (i & DAHDI_IOMUX_SIGEVENT) {
08785 res = dahdi_get_event(p->subs[idx].dfd);
08786 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
08787 if (res == DAHDI_EVENT_NOALARM) {
08788 p->inalarm = 0;
08789 }
08790
08791 if ( res == DAHDI_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) {
08792 ast_log(LOG_DEBUG, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel);
08793 p->polarity = POLARITY_IDLE;
08794 callerid_free(cs);
08795 ast_hangup(chan);
08796 goto quit;
08797 }
08798 res = 0;
08799
08800
08801 curRingData[receivedRingT] = p->ringt;
08802
08803 if (p->ringt < p->ringt_base/2)
08804 break;
08805
08806
08807 if (++receivedRingT == ARRAY_LEN(curRingData))
08808 break;
08809 } else if (i & DAHDI_IOMUX_READ) {
08810 res = read(p->subs[idx].dfd, buf, sizeof(buf));
08811 if (res < 0) {
08812 if (errno != ELAST) {
08813 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
08814 callerid_free(cs);
08815 ast_hangup(chan);
08816 goto quit;
08817 }
08818 break;
08819 }
08820 if (p->ringt)
08821 p->ringt--;
08822 if (p->ringt == 1) {
08823 res = -1;
08824 break;
08825 }
08826 samples += res;
08827 res = callerid_feed(cs, buf, res, AST_LAW(p));
08828 if (res < 0) {
08829
08830
08831
08832
08833 ast_log(LOG_WARNING,
08834 "Failed to decode CallerID on channel '%s'\n",
08835 chan->name);
08836 break;
08837 } else if (res)
08838 break;
08839 else if (samples > (8000 * 10))
08840 break;
08841 }
08842 }
08843 if (res == 1) {
08844 callerid_get(cs, &name, &number, &flags);
08845 ast_debug(1, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
08846 }
08847 if (distinctiveringaftercid == 1) {
08848
08849 for (receivedRingT = 0; receivedRingT < 3; receivedRingT++) {
08850 curRingData[receivedRingT] = 0;
08851 }
08852 receivedRingT = 0;
08853 ast_verb(3, "Detecting post-CID distinctive ring\n");
08854 for (;;) {
08855 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
08856 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
08857 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
08858 callerid_free(cs);
08859 ast_hangup(chan);
08860 goto quit;
08861 }
08862 if (i & DAHDI_IOMUX_SIGEVENT) {
08863 res = dahdi_get_event(p->subs[idx].dfd);
08864 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
08865 if (res == DAHDI_EVENT_NOALARM) {
08866 p->inalarm = 0;
08867 }
08868 res = 0;
08869
08870
08871 curRingData[receivedRingT] = p->ringt;
08872
08873 if (p->ringt < p->ringt_base/2)
08874 break;
08875
08876
08877 if (++receivedRingT == ARRAY_LEN(curRingData))
08878 break;
08879 } else if (i & DAHDI_IOMUX_READ) {
08880 res = read(p->subs[idx].dfd, buf, sizeof(buf));
08881 if (res < 0) {
08882 if (errno != ELAST) {
08883 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
08884 callerid_free(cs);
08885 ast_hangup(chan);
08886 goto quit;
08887 }
08888 break;
08889 }
08890 if (p->ringt)
08891 p->ringt--;
08892 if (p->ringt == 1) {
08893 res = -1;
08894 break;
08895 }
08896 }
08897 }
08898 }
08899 if (p->usedistinctiveringdetection) {
08900
08901 ast_verb(3, "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
08902
08903 for (counter = 0; counter < 3; counter++) {
08904
08905
08906
08907 ast_verb(3, "Checking %d,%d,%d\n",
08908 p->drings.ringnum[counter].ring[0],
08909 p->drings.ringnum[counter].ring[1],
08910 p->drings.ringnum[counter].ring[2]);
08911 distMatches = 0;
08912 for (counter1 = 0; counter1 < 3; counter1++) {
08913 ast_verb(3, "Ring pattern check range: %d\n", p->drings.ringnum[counter].range);
08914 if (p->drings.ringnum[counter].ring[counter1] == -1) {
08915 ast_verb(3, "Pattern ignore (-1) detected, so matching pattern %d regardless.\n",
08916 curRingData[counter1]);
08917 distMatches++;
08918 }
08919 else if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range) &&
08920 curRingData[counter1] >= (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range)) {
08921 ast_verb(3, "Ring pattern matched in range: %d to %d\n",
08922 (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range),
08923 (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range));
08924 distMatches++;
08925 }
08926 }
08927 if (distMatches == 3) {
08928
08929 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
08930 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
08931 ast_verb(3, "Distinctive Ring matched context %s\n",p->context);
08932 break;
08933 }
08934 }
08935 }
08936
08937 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
08938 #if 1
08939 restore_gains(p);
08940 #endif
08941 if (res < 0) {
08942 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
08943 }
08944 } else
08945 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
08946 }
08947 } else
08948 cs = NULL;
08949
08950 if (number)
08951 ast_shrink_phone_number(number);
08952 ast_set_callerid(chan, number, name, number);
08953
08954 if (smdi_msg)
08955 ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy);
08956
08957 if (cs)
08958 callerid_free(cs);
08959
08960 if (flags & CID_MSGWAITING) {
08961 ast_log(LOG_NOTICE, "MWI: Channel %d message waiting!\n", p->channel);
08962 notify_message(p->mailbox, 1);
08963
08964 if (p->mwimonitor_rpas) {
08965 ast_hangup(chan);
08966 return NULL;
08967 }
08968 } else if (flags & CID_NOMSGWAITING) {
08969 ast_log(LOG_NOTICE, "MWI: Channel %d no message waiting!\n", p->channel);
08970 notify_message(p->mailbox, 0);
08971
08972 if (p->mwimonitor_rpas) {
08973 ast_hangup(chan);
08974 return NULL;
08975 }
08976 }
08977
08978 ast_setstate(chan, AST_STATE_RING);
08979 chan->rings = 1;
08980 p->ringt = p->ringt_base;
08981 res = ast_pbx_run(chan);
08982 if (res) {
08983 ast_hangup(chan);
08984 ast_log(LOG_WARNING, "PBX exited non-zero\n");
08985 }
08986 goto quit;
08987 default:
08988 ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
08989 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08990 if (res < 0)
08991 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
08992 }
08993 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08994 if (res < 0)
08995 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
08996 ast_hangup(chan);
08997 quit:
08998 ast_mutex_lock(&ss_thread_lock);
08999 ss_thread_count--;
09000 ast_cond_signal(&ss_thread_complete);
09001 ast_mutex_unlock(&ss_thread_lock);
09002 return NULL;
09003 }
09004
09005 struct mwi_thread_data {
09006 struct dahdi_pvt *pvt;
09007 unsigned char buf[READ_SIZE];
09008 size_t len;
09009 };
09010
09011 static int calc_energy(const unsigned char *buf, int len, int law)
09012 {
09013 int x;
09014 int sum = 0;
09015
09016 if (!len)
09017 return 0;
09018
09019 for (x = 0; x < len; x++)
09020 sum += abs(law == AST_FORMAT_ULAW ? AST_MULAW(buf[x]) : AST_ALAW(buf[x]));
09021
09022 return sum / len;
09023 }
09024
09025 static void *mwi_thread(void *data)
09026 {
09027 struct mwi_thread_data *mtd = data;
09028 struct callerid_state *cs;
09029 pthread_t threadid;
09030 int samples = 0;
09031 char *name, *number;
09032 int flags;
09033 int i, res;
09034 unsigned int spill_done = 0;
09035 int spill_result = -1;
09036
09037 if (!(cs = callerid_new(mtd->pvt->cid_signalling))) {
09038 mtd->pvt->mwimonitoractive = 0;
09039
09040 return NULL;
09041 }
09042
09043 callerid_feed(cs, mtd->buf, mtd->len, AST_LAW(mtd->pvt));
09044
09045 bump_gains(mtd->pvt);
09046
09047 for (;;) {
09048 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
09049 if ((res = ioctl(mtd->pvt->subs[SUB_REAL].dfd, DAHDI_IOMUX, &i))) {
09050 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
09051 goto quit;
09052 }
09053
09054 if (i & DAHDI_IOMUX_SIGEVENT) {
09055 struct ast_channel *chan;
09056
09057
09058
09059
09060 res = dahdi_get_event(mtd->pvt->subs[SUB_REAL].dfd);
09061
09062 switch (res) {
09063 case DAHDI_EVENT_NEONMWI_ACTIVE:
09064 case DAHDI_EVENT_NEONMWI_INACTIVE:
09065 case DAHDI_EVENT_NONE:
09066 case DAHDI_EVENT_BITSCHANGED:
09067 break;
09068 case DAHDI_EVENT_NOALARM:
09069 mtd->pvt->inalarm = 0;
09070 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", mtd->pvt->channel);
09071 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
09072 "Channel: %d\r\n", mtd->pvt->channel);
09073 break;
09074 case DAHDI_EVENT_ALARM:
09075 mtd->pvt->inalarm = 1;
09076 res = get_alarms(mtd->pvt);
09077 handle_alarms(mtd->pvt, res);
09078 break;
09079 default:
09080 ast_log(LOG_NOTICE, "Got event %d (%s)... Passing along to ss_thread\n", res, event2str(res));
09081 callerid_free(cs);
09082
09083 restore_gains(mtd->pvt);
09084 mtd->pvt->ringt = mtd->pvt->ringt_base;
09085
09086 if ((chan = dahdi_new(mtd->pvt, AST_STATE_RING, 0, SUB_REAL, 0, 0))) {
09087 if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
09088 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", mtd->pvt->channel);
09089 res = tone_zone_play_tone(mtd->pvt->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09090 if (res < 0)
09091 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", mtd->pvt->channel);
09092 ast_hangup(chan);
09093 goto quit;
09094 }
09095 goto quit_no_clean;
09096
09097 } else {
09098 ast_log(LOG_WARNING, "Could not create channel to handle call\n");
09099 }
09100 }
09101 } else if (i & DAHDI_IOMUX_READ) {
09102 if ((res = read(mtd->pvt->subs[SUB_REAL].dfd, mtd->buf, sizeof(mtd->buf))) < 0) {
09103 if (errno != ELAST) {
09104 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
09105 goto quit;
09106 }
09107 break;
09108 }
09109 samples += res;
09110 if (!spill_done) {
09111 if ((spill_result = callerid_feed(cs, mtd->buf, res, AST_LAW(mtd->pvt))) < 0) {
09112
09113
09114
09115
09116 ast_log(LOG_WARNING, "Failed to decode CallerID\n");
09117 break;
09118 } else if (spill_result) {
09119 spill_done = 1;
09120 }
09121 } else {
09122
09123
09124
09125 if (calc_energy(mtd->buf, res, AST_LAW(mtd->pvt)) <= mwilevel)
09126 break;
09127 }
09128 if (samples > (8000 * 4))
09129 break;
09130 }
09131 }
09132
09133 if (spill_result == 1) {
09134 callerid_get(cs, &name, &number, &flags);
09135 if (flags & CID_MSGWAITING) {
09136 ast_log(LOG_NOTICE, "mwi: Have Messages on channel %d\n", mtd->pvt->channel);
09137 notify_message(mtd->pvt->mailbox, 1);
09138 } else if (flags & CID_NOMSGWAITING) {
09139 ast_log(LOG_NOTICE, "mwi: No Messages on channel %d\n", mtd->pvt->channel);
09140 notify_message(mtd->pvt->mailbox, 0);
09141 } else {
09142 ast_log(LOG_NOTICE, "mwi: Status unknown on channel %d\n", mtd->pvt->channel);
09143 }
09144 }
09145
09146
09147 quit:
09148 callerid_free(cs);
09149
09150 restore_gains(mtd->pvt);
09151
09152 quit_no_clean:
09153 mtd->pvt->mwimonitoractive = 0;
09154
09155 ast_free(mtd);
09156
09157 return NULL;
09158 }
09159
09160
09161
09162
09163
09164
09165
09166
09167 static int mwi_send_init(struct dahdi_pvt * pvt)
09168 {
09169 int x, res;
09170
09171 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09172
09173 if (pvt->mwisend_rpas) {
09174 pvt->mwisend_data.mwisend_current = MWI_SEND_SA;
09175 pvt->mwisendactive = 1;
09176 } else if (pvt->mwisend_fsk) {
09177 pvt->mwisend_data.mwisend_current = MWI_SEND_SPILL;
09178 pvt->mwisendactive = 1;
09179 } else {
09180 pvt->mwisendactive = 0;
09181 return 0;
09182 }
09183 #else
09184 if (mwisend_rpas) {
09185 pvt->mwisend_data.mwisend_current = MWI_SEND_SA;
09186 } else {
09187 pvt->mwisend_data.mwisend_current = MWI_SEND_SPILL;
09188 }
09189 pvt->mwisendactive = 1;
09190 #endif
09191
09192 if (pvt->cidspill) {
09193 ast_log(LOG_WARNING, "cidspill already exists when trying to send FSK MWI\n");
09194 ast_free(pvt->cidspill);
09195 pvt->cidspill = NULL;
09196 pvt->cidpos = 0;
09197 pvt->cidlen = 0;
09198 }
09199 pvt->cidspill = ast_calloc(1, MAX_CALLERID_SIZE);
09200 if (!pvt->cidspill) {
09201 pvt->mwisendactive = 0;
09202 return -1;
09203 }
09204 x = DAHDI_FLUSH_BOTH;
09205 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
09206 x = 3000;
09207 ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_ONHOOKTRANSFER, &x);
09208 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09209 if (pvt->mwisend_fsk) {
09210 #endif
09211 pvt->cidlen = vmwi_generate(pvt->cidspill, has_voicemail(pvt), CID_MWI_TYPE_MDMF_FULL,
09212 AST_LAW(pvt), pvt->cid_name, pvt->cid_num, 0);
09213 pvt->cidpos = 0;
09214 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09215 }
09216 #endif
09217 return 0;
09218 }
09219
09220 static int mwi_send_process_buffer(struct dahdi_pvt * pvt, int num_read)
09221 {
09222 struct timeval now;
09223 int res;
09224
09225
09226
09227
09228 if (MWI_SEND_DONE != pvt->mwisend_data.mwisend_current && !pvt->cidspill) {
09229 pvt->mwisend_data.mwisend_current = MWI_SEND_DONE;
09230 } else if (MWI_SEND_DONE != pvt->mwisend_data.mwisend_current) {
09231
09232 switch ( pvt->mwisend_data.mwisend_current) {
09233 case MWI_SEND_SA:
09234
09235 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &AS_RP_cadence);
09236 if (res) {
09237 ast_log(LOG_WARNING, "Unable to set RP-AS ring cadence: %s\n", strerror(errno));
09238 goto quit;
09239 }
09240 res = dahdi_set_hook(pvt->subs[SUB_REAL].dfd, DAHDI_RING);
09241 pvt->mwisend_data.mwisend_current = MWI_SEND_SA_WAIT;
09242 break;
09243 case MWI_SEND_SA_WAIT:
09244 break;
09245 case MWI_SEND_PAUSE:
09246 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09247 if (pvt->mwisend_fsk) {
09248 #endif
09249 gettimeofday(&now, NULL);
09250 if ((int)(now.tv_sec - pvt->mwisend_data.pause.tv_sec) * 1000000 + (int)now.tv_usec - (int)pvt->mwisend_data.pause.tv_usec > 500000) {
09251 pvt->mwisend_data.mwisend_current = MWI_SEND_SPILL;
09252 }
09253 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09254 } else {
09255 pvt->mwisend_data.mwisend_current = MWI_SEND_CLEANUP;
09256 }
09257 #endif
09258 break;
09259 case MWI_SEND_SPILL:
09260
09261 if(0 < num_read) {
09262 if (num_read > pvt->cidlen - pvt->cidpos)
09263 num_read = pvt->cidlen - pvt->cidpos;
09264 res = write(pvt->subs[SUB_REAL].dfd, pvt->cidspill + pvt->cidpos, num_read);
09265 if (res > 0) {
09266 pvt->cidpos += res;
09267 if (pvt->cidpos >= pvt->cidlen) {
09268 pvt->mwisend_data.mwisend_current = MWI_SEND_CLEANUP;
09269 }
09270 } else {
09271 ast_log(LOG_WARNING, "MWI FSK Send Write failed: %s\n", strerror(errno));
09272 goto quit;
09273 }
09274 }
09275 break;
09276 case MWI_SEND_CLEANUP:
09277
09278 pvt->mwisend_data.mwisend_current = MWI_SEND_DONE;
09279 break;
09280 default:
09281
09282 goto quit;
09283 }
09284 }
09285
09286 if (MWI_SEND_DONE == pvt->mwisend_data.mwisend_current) {
09287 if (pvt->cidspill) {
09288 ast_free(pvt->cidspill);
09289 pvt->cidspill = NULL;
09290 pvt->cidpos = 0;
09291 pvt->cidlen = 0;
09292 }
09293 pvt->mwisendactive = 0;
09294 }
09295 return 0;
09296 quit:
09297 if (pvt->cidspill) {
09298 ast_free(pvt->cidspill);
09299 pvt->cidspill = NULL;
09300 pvt->cidpos = 0;
09301 pvt->cidlen = 0;
09302 }
09303 pvt->mwisendactive = 0;
09304 return -1;
09305 }
09306
09307 static int mwi_send_process_event(struct dahdi_pvt * pvt, int event)
09308 {
09309 int handled = 0;
09310
09311 if (MWI_SEND_DONE != pvt->mwisend_data.mwisend_current) {
09312 switch (event) {
09313 case DAHDI_EVENT_RINGEROFF:
09314 if(pvt->mwisend_data.mwisend_current == MWI_SEND_SA_WAIT) {
09315 handled = 1;
09316
09317 if (dahdi_set_hook(pvt->subs[SUB_REAL].dfd, DAHDI_RINGOFF) ) {
09318 ast_log(LOG_WARNING, "Unable to finsh RP-AS: %s mwi send aborted\n", strerror(errno));
09319 if(pvt->cidspill) {
09320 ast_free(pvt->cidspill);
09321 pvt->cidspill = NULL;
09322 }
09323 pvt->mwisend_data.mwisend_current = MWI_SEND_DONE;
09324 pvt->mwisendactive = 0;
09325 } else {
09326 pvt->mwisend_data.mwisend_current = MWI_SEND_PAUSE;
09327 gettimeofday(&pvt->mwisend_data.pause, NULL);
09328 }
09329 }
09330 break;
09331
09332 case DAHDI_EVENT_RINGOFFHOOK:
09333 if (pvt->cidspill) {
09334 ast_free(pvt->cidspill);
09335 pvt->cidspill = NULL;
09336 pvt->cidpos = 0;
09337 pvt->cidlen = 0;
09338 }
09339 pvt->mwisend_data.mwisend_current = MWI_SEND_DONE;
09340 pvt->mwisendactive = 0;
09341 break;
09342 case DAHDI_EVENT_RINGERON:
09343 case DAHDI_EVENT_HOOKCOMPLETE:
09344 break;
09345 default:
09346 break;
09347 }
09348 }
09349 return handled;
09350 }
09351
09352
09353 static int dahdi_destroy_channel_bynum(int channel)
09354 {
09355 struct dahdi_pvt *tmp = NULL;
09356 struct dahdi_pvt *prev = NULL;
09357
09358 tmp = iflist;
09359 while (tmp) {
09360 if (tmp->channel == channel) {
09361 int x = DAHDI_FLASH;
09362 ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
09363 destroy_channel(prev, tmp, 1);
09364 ast_module_unref(ast_module_info->self);
09365 return RESULT_SUCCESS;
09366 }
09367 prev = tmp;
09368 tmp = tmp->next;
09369 }
09370 return RESULT_FAILURE;
09371 }
09372
09373 static struct dahdi_pvt *handle_init_event(struct dahdi_pvt *i, int event)
09374 {
09375 int res;
09376 pthread_t threadid;
09377 struct ast_channel *chan;
09378
09379
09380
09381 switch (event) {
09382 case DAHDI_EVENT_NONE:
09383 case DAHDI_EVENT_BITSCHANGED:
09384 break;
09385 case DAHDI_EVENT_WINKFLASH:
09386 case DAHDI_EVENT_RINGOFFHOOK:
09387 if (i->inalarm) break;
09388 if (i->radio) break;
09389
09390 switch (i->sig) {
09391 case SIG_FXOLS:
09392 case SIG_FXOGS:
09393 case SIG_FXOKS:
09394 res = dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
09395 i->fxsoffhookstate = 1;
09396 if (res && (errno == EBUSY))
09397 break;
09398 if (i->cidspill) {
09399
09400 ast_free(i->cidspill);
09401 i->cidspill = NULL;
09402 }
09403 if (i->immediate) {
09404 dahdi_enable_ec(i);
09405
09406 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
09407 chan = dahdi_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
09408 if (!chan) {
09409 ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
09410 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09411 if (res < 0)
09412 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
09413 }
09414 } else {
09415
09416 chan = dahdi_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
09417 if (chan) {
09418 if (has_voicemail(i))
09419 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_STUTTER);
09420 else
09421 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_DIALTONE);
09422 if (res < 0)
09423 ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel);
09424 if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
09425 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
09426 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09427 if (res < 0)
09428 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
09429 ast_hangup(chan);
09430 }
09431 } else
09432 ast_log(LOG_WARNING, "Unable to create channel\n");
09433 }
09434 break;
09435 case SIG_FXSLS:
09436 case SIG_FXSGS:
09437 case SIG_FXSKS:
09438 i->ringt = i->ringt_base;
09439
09440 case SIG_EMWINK:
09441 case SIG_FEATD:
09442 case SIG_FEATDMF:
09443 case SIG_FEATDMF_TA:
09444 case SIG_E911:
09445 case SIG_FGC_CAMA:
09446 case SIG_FGC_CAMAMF:
09447 case SIG_FEATB:
09448 case SIG_EM:
09449 case SIG_EM_E1:
09450 case SIG_SFWINK:
09451 case SIG_SF_FEATD:
09452 case SIG_SF_FEATDMF:
09453 case SIG_SF_FEATB:
09454 case SIG_SF:
09455
09456 if (i->cid_start == CID_START_POLARITY_IN) {
09457 chan = dahdi_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
09458 } else {
09459 chan = dahdi_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
09460 }
09461
09462 if (!chan) {
09463 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
09464 } else if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
09465 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
09466 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09467 if (res < 0) {
09468 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
09469 }
09470 ast_hangup(chan);
09471 }
09472 break;
09473 default:
09474 ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
09475 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09476 if (res < 0)
09477 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
09478 return NULL;
09479 }
09480 break;
09481 case DAHDI_EVENT_NOALARM:
09482 i->inalarm = 0;
09483 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
09484 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
09485 "Channel: %d\r\n", i->channel);
09486 break;
09487 case DAHDI_EVENT_ALARM:
09488 i->inalarm = 1;
09489 res = get_alarms(i);
09490 handle_alarms(i, res);
09491
09492 case DAHDI_EVENT_ONHOOK:
09493 if (i->radio)
09494 break;
09495
09496 switch (i->sig) {
09497 case SIG_FXOLS:
09498 case SIG_FXOGS:
09499 case SIG_FEATD:
09500 case SIG_FEATDMF:
09501 case SIG_FEATDMF_TA:
09502 case SIG_E911:
09503 case SIG_FGC_CAMA:
09504 case SIG_FGC_CAMAMF:
09505 case SIG_FEATB:
09506 case SIG_EM:
09507 case SIG_EM_E1:
09508 case SIG_EMWINK:
09509 case SIG_SF_FEATD:
09510 case SIG_SF_FEATDMF:
09511 case SIG_SF_FEATB:
09512 case SIG_SF:
09513 case SIG_SFWINK:
09514 case SIG_FXSLS:
09515 case SIG_FXSGS:
09516 case SIG_FXSKS:
09517 case SIG_GR303FXSKS:
09518 dahdi_disable_ec(i);
09519 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
09520 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
09521 break;
09522 case SIG_GR303FXOKS:
09523 case SIG_FXOKS:
09524 dahdi_disable_ec(i);
09525
09526 #ifdef ZHONE_HACK
09527 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
09528 usleep(1);
09529 #endif
09530 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
09531 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
09532 break;
09533 case SIG_PRI:
09534 case SIG_SS7:
09535 case SIG_BRI:
09536 case SIG_BRI_PTMP:
09537 dahdi_disable_ec(i);
09538 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
09539 break;
09540 default:
09541 ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
09542 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
09543 return NULL;
09544 }
09545 if (i->sig & __DAHDI_SIG_FXO) {
09546 i->fxsoffhookstate = 0;
09547 }
09548 break;
09549 case DAHDI_EVENT_POLARITY:
09550 switch (i->sig) {
09551 case SIG_FXSLS:
09552 case SIG_FXSKS:
09553 case SIG_FXSGS:
09554
09555
09556
09557
09558 if (i->hanguponpolarityswitch)
09559 i->polarity = POLARITY_REV;
09560 if (i->cid_start == CID_START_POLARITY || i->cid_start == CID_START_POLARITY_IN) {
09561 i->polarity = POLARITY_REV;
09562 ast_verb(2, "Starting post polarity "
09563 "CID detection on channel %d\n",
09564 i->channel);
09565 chan = dahdi_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
09566 if (!chan) {
09567 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
09568 } else if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
09569 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
09570 }
09571 }
09572 break;
09573 default:
09574 ast_log(LOG_WARNING, "handle_init_event detected "
09575 "polarity reversal on non-FXO (SIG_FXS) "
09576 "interface %d\n", i->channel);
09577 }
09578 break;
09579 case DAHDI_EVENT_REMOVED:
09580 ast_log(LOG_NOTICE,
09581 "Got DAHDI_EVENT_REMOVED. Destroying channel %d\n",
09582 i->channel);
09583 return i;
09584 case DAHDI_EVENT_NEONMWI_ACTIVE:
09585 if (i->mwimonitor_neon) {
09586 notify_message(i->mailbox, 1);
09587 ast_log(LOG_NOTICE, "NEON MWI set for channel %d, mailbox %s \n", i->channel, i->mailbox);
09588 }
09589 break;
09590 case DAHDI_EVENT_NEONMWI_INACTIVE:
09591 if (i->mwimonitor_neon) {
09592 notify_message(i->mailbox, 0);
09593 ast_log(LOG_NOTICE, "NEON MWI cleared for channel %d, mailbox %s\n", i->channel, i->mailbox);
09594 }
09595 break;
09596 }
09597 return NULL;
09598 }
09599
09600 static void *do_monitor(void *data)
09601 {
09602 int count, res, res2, spoint, pollres=0;
09603 struct dahdi_pvt *i;
09604 struct dahdi_pvt *last = NULL;
09605 struct dahdi_pvt *doomed;
09606 time_t thispass = 0, lastpass = 0;
09607 int found;
09608 char buf[1024];
09609 struct pollfd *pfds=NULL;
09610 int lastalloc = -1;
09611
09612
09613
09614 #if 0
09615 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
09616 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
09617 return NULL;
09618 }
09619 ast_debug(1, "Monitor starting...\n");
09620 #endif
09621 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
09622
09623 for (;;) {
09624
09625 ast_mutex_lock(&iflock);
09626 if (!pfds || (lastalloc != ifcount)) {
09627 if (pfds) {
09628 ast_free(pfds);
09629 pfds = NULL;
09630 }
09631 if (ifcount) {
09632 if (!(pfds = ast_calloc(1, ifcount * sizeof(*pfds)))) {
09633 ast_mutex_unlock(&iflock);
09634 return NULL;
09635 }
09636 }
09637 lastalloc = ifcount;
09638 }
09639
09640
09641 count = 0;
09642 i = iflist;
09643 while (i) {
09644 if ((i->subs[SUB_REAL].dfd > -1) && i->sig && (!i->radio) && !(i->sig & SIG_MFCR2)) {
09645 if (!i->owner && !i->subs[SUB_REAL].owner && !i->mwimonitoractive ) {
09646
09647 pfds[count].fd = i->subs[SUB_REAL].dfd;
09648 pfds[count].events = POLLPRI;
09649 pfds[count].revents = 0;
09650
09651
09652 if (i->cidspill || i->mwisendactive || i->mwimonitor_fsk)
09653 pfds[count].events |= POLLIN;
09654 count++;
09655 }
09656 }
09657 i = i->next;
09658 }
09659
09660 ast_mutex_unlock(&iflock);
09661
09662 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
09663 pthread_testcancel();
09664
09665 res = poll(pfds, count, 1000);
09666 pthread_testcancel();
09667 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
09668
09669
09670 if (res < 0) {
09671 if ((errno != EAGAIN) && (errno != EINTR))
09672 ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
09673 continue;
09674 }
09675
09676
09677 ast_mutex_lock(&iflock);
09678 found = 0;
09679 spoint = 0;
09680 lastpass = thispass;
09681 thispass = time(NULL);
09682 i = iflist;
09683 doomed = NULL;
09684 for (i = iflist;; i = i->next) {
09685 if (doomed) {
09686 int res;
09687 res = dahdi_destroy_channel_bynum(doomed->channel);
09688 if (!res) {
09689 ast_log(LOG_WARNING, "Couldn't find channel to destroy, hopefully another destroy operation just happened.\n");
09690 }
09691 doomed = NULL;
09692 }
09693 if (!i) {
09694 break;
09695 }
09696
09697 if (thispass != lastpass) {
09698 if (!found && ((i == last) || ((i == iflist) && !last))) {
09699 last = i;
09700 if (last) {
09701
09702 if (!last->mwisendactive && last->sig & __DAHDI_SIG_FXO &&
09703 !last->fxsoffhookstate && !last->owner &&
09704 !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3)) {
09705 res = has_voicemail(last);
09706 if (last->msgstate != res) {
09707
09708 res2 = ioctl(last->subs[SUB_REAL].dfd, DAHDI_VMWI, &res);
09709 if (res2) {
09710
09711 ast_debug(3, "Unable to control message waiting led on channel %d: %s\n", last->channel, strerror(errno));
09712 }
09713
09714 if (mwi_send_init(last)) {
09715 ast_log(LOG_WARNING, "Unable to initiate mwi send sequence on channel %d\n", last->channel);
09716 }
09717 last->msgstate = res;
09718 found ++;
09719 }
09720 }
09721 last = last->next;
09722 }
09723 }
09724 }
09725 if ((i->subs[SUB_REAL].dfd > -1) && i->sig) {
09726 if (i->radio && !i->owner)
09727 {
09728 res = dahdi_get_event(i->subs[SUB_REAL].dfd);
09729 if (res)
09730 {
09731 ast_debug(1, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
09732
09733 ast_mutex_unlock(&iflock);
09734 doomed = handle_init_event(i, res);
09735 ast_mutex_lock(&iflock);
09736 }
09737 continue;
09738 }
09739 pollres = ast_fdisset(pfds, i->subs[SUB_REAL].dfd, count, &spoint);
09740 if (pollres & POLLIN) {
09741 if (i->owner || i->subs[SUB_REAL].owner) {
09742 #ifdef HAVE_PRI
09743 if (!i->pri)
09744 #endif
09745 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].dfd);
09746 continue;
09747 }
09748 if (!i->mwimonitor_fsk && !i->mwisendactive) {
09749 ast_log(LOG_WARNING, "Whoa.... I'm not looking for MWI or sending MWI but am reading (%d)...\n", i->subs[SUB_REAL].dfd);
09750 continue;
09751 }
09752 res = read(i->subs[SUB_REAL].dfd, buf, sizeof(buf));
09753 if (res > 0) {
09754 if (i->mwimonitor_fsk) {
09755 if (calc_energy((unsigned char *) buf, res, AST_LAW(i)) > mwilevel) {
09756 pthread_attr_t attr;
09757 pthread_t threadid;
09758 struct mwi_thread_data *mtd;
09759
09760 pthread_attr_init(&attr);
09761 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
09762
09763 ast_log(LOG_DEBUG, "Maybe some MWI on port %d!\n", i->channel);
09764 if ((mtd = ast_calloc(1, sizeof(*mtd)))) {
09765 mtd->pvt = i;
09766 memcpy(mtd->buf, buf, res);
09767 mtd->len = res;
09768 if (ast_pthread_create_background(&threadid, &attr, mwi_thread, mtd)) {
09769 ast_log(LOG_WARNING, "Unable to start mwi thread on channel %d\n", i->channel);
09770 ast_free(mtd);
09771 }
09772 i->mwimonitoractive = 1;
09773 }
09774 }
09775 }
09776 if (i->mwisendactive) {
09777 mwi_send_process_buffer(i, res);
09778 }
09779 } else {
09780 ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
09781 }
09782 }
09783 if (pollres & POLLPRI) {
09784 if (i->owner || i->subs[SUB_REAL].owner) {
09785 #ifdef HAVE_PRI
09786 if (!i->pri)
09787 #endif
09788 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].dfd);
09789 continue;
09790 }
09791 res = dahdi_get_event(i->subs[SUB_REAL].dfd);
09792 ast_debug(1, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
09793
09794 ast_mutex_unlock(&iflock);
09795 if (0 == i->mwisendactive || 0 == mwi_send_process_event(i, res)) {
09796 doomed = handle_init_event(i, res);
09797 }
09798 ast_mutex_lock(&iflock);
09799 }
09800 }
09801 }
09802 ast_mutex_unlock(&iflock);
09803 }
09804
09805 return NULL;
09806
09807 }
09808
09809 static int restart_monitor(void)
09810 {
09811
09812 if (monitor_thread == AST_PTHREADT_STOP)
09813 return 0;
09814 ast_mutex_lock(&monlock);
09815 if (monitor_thread == pthread_self()) {
09816 ast_mutex_unlock(&monlock);
09817 ast_log(LOG_WARNING, "Cannot kill myself\n");
09818 return -1;
09819 }
09820 if (monitor_thread != AST_PTHREADT_NULL) {
09821
09822 pthread_kill(monitor_thread, SIGURG);
09823 } else {
09824
09825 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
09826 ast_mutex_unlock(&monlock);
09827 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
09828 return -1;
09829 }
09830 }
09831 ast_mutex_unlock(&monlock);
09832 return 0;
09833 }
09834
09835 #if defined(HAVE_PRI)
09836 static int pri_resolve_span(int *span, int channel, int offset, struct dahdi_spaninfo *si)
09837 {
09838 int x;
09839 int trunkgroup;
09840
09841 trunkgroup = pris[*span].mastertrunkgroup;
09842 if (trunkgroup) {
09843
09844 for (x = 0; x < NUM_SPANS; x++) {
09845 if (pris[x].trunkgroup == trunkgroup) {
09846 *span = x;
09847 return 0;
09848 }
09849 }
09850 ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup);
09851 *span = -1;
09852 } else {
09853 if (pris[*span].trunkgroup) {
09854 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup);
09855 *span = -1;
09856 } else if (pris[*span].mastertrunkgroup) {
09857 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup);
09858 *span = -1;
09859 } else {
09860 if (si->totalchans == 31) {
09861
09862 pris[*span].dchannels[0] = 16 + offset;
09863 } else if (si->totalchans == 24) {
09864
09865 pris[*span].dchannels[0] = 24 + offset;
09866 } else if (si->totalchans == 3) {
09867
09868 pris[*span].dchannels[0] = 3 + offset;
09869 } else {
09870 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);
09871 *span = -1;
09872 return 0;
09873 }
09874 pris[*span].dchanavail[0] |= DCHAN_PROVISIONED;
09875 pris[*span].offset = offset;
09876 pris[*span].span = *span + 1;
09877 }
09878 }
09879 return 0;
09880 }
09881 #endif
09882
09883 #if defined(HAVE_PRI)
09884 static int pri_create_trunkgroup(int trunkgroup, int *channels)
09885 {
09886 struct dahdi_spaninfo si;
09887 struct dahdi_params p;
09888 int fd;
09889 int span;
09890 int ospan=0;
09891 int x,y;
09892 for (x = 0; x < NUM_SPANS; x++) {
09893 if (pris[x].trunkgroup == trunkgroup) {
09894 ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]);
09895 return -1;
09896 }
09897 }
09898 for (y = 0; y < NUM_DCHANS; y++) {
09899 if (!channels[y])
09900 break;
09901 memset(&si, 0, sizeof(si));
09902 memset(&p, 0, sizeof(p));
09903 fd = open("/dev/dahdi/channel", O_RDWR);
09904 if (fd < 0) {
09905 ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno));
09906 return -1;
09907 }
09908 x = channels[y];
09909 if (ioctl(fd, DAHDI_SPECIFY, &x)) {
09910 ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
09911 close(fd);
09912 return -1;
09913 }
09914 if (ioctl(fd, DAHDI_GET_PARAMS, &p)) {
09915 ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno));
09916 return -1;
09917 }
09918 if (ioctl(fd, DAHDI_SPANSTAT, &si)) {
09919 ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d): %s\n", channels[y], p.spanno, strerror(errno));
09920 close(fd);
09921 return -1;
09922 }
09923 span = p.spanno - 1;
09924 if (pris[span].trunkgroup) {
09925 ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
09926 close(fd);
09927 return -1;
09928 }
09929 if (pris[span].pvts[0]) {
09930 ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
09931 close(fd);
09932 return -1;
09933 }
09934 if (!y) {
09935 pris[span].trunkgroup = trunkgroup;
09936 pris[span].offset = channels[y] - p.chanpos;
09937 ospan = span;
09938 }
09939 pris[ospan].dchannels[y] = channels[y];
09940 pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
09941 pris[span].span = span + 1;
09942 close(fd);
09943 }
09944 return 0;
09945 }
09946 #endif
09947
09948 #if defined(HAVE_PRI)
09949 static int pri_create_spanmap(int span, int trunkgroup, int logicalspan)
09950 {
09951 if (pris[span].mastertrunkgroup) {
09952 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);
09953 return -1;
09954 }
09955 pris[span].mastertrunkgroup = trunkgroup;
09956 pris[span].prilogicalspan = logicalspan;
09957 return 0;
09958 }
09959 #endif
09960
09961 #if defined(HAVE_SS7)
09962 static unsigned int parse_pointcode(const char *pcstring)
09963 {
09964 unsigned int code1, code2, code3;
09965 int numvals;
09966
09967 numvals = sscanf(pcstring, "%30d-%30d-%30d", &code1, &code2, &code3);
09968 if (numvals == 1)
09969 return code1;
09970 if (numvals == 3)
09971 return (code1 << 16) | (code2 << 8) | code3;
09972
09973 return 0;
09974 }
09975 #endif
09976
09977 #if defined(HAVE_SS7)
09978 static struct dahdi_ss7 * ss7_resolve_linkset(int linkset)
09979 {
09980 if ((linkset < 0) || (linkset >= NUM_SPANS))
09981 return NULL;
09982 else
09983 return &linksets[linkset - 1];
09984 }
09985 #endif
09986
09987 #ifdef HAVE_OPENR2
09988 static void dahdi_r2_destroy_links(void)
09989 {
09990 int i = 0;
09991 if (!r2links) {
09992 return;
09993 }
09994 for (; i < r2links_count; i++) {
09995 if (r2links[i]->r2master != AST_PTHREADT_NULL) {
09996 pthread_cancel(r2links[i]->r2master);
09997 pthread_join(r2links[i]->r2master, NULL);
09998 openr2_context_delete(r2links[i]->protocol_context);
09999 }
10000 ast_free(r2links[i]);
10001 }
10002 ast_free(r2links);
10003 r2links = NULL;
10004 r2links_count = 0;
10005 }
10006
10007 #define R2_LINK_CAPACITY 10
10008 static struct dahdi_mfcr2 *dahdi_r2_get_link(void)
10009 {
10010 struct dahdi_mfcr2 *new_r2link = NULL;
10011 struct dahdi_mfcr2 **new_r2links = NULL;
10012
10013
10014 if (!r2links_count || (r2links[r2links_count - 1]->monitored_count == R2_LINK_CAPACITY)) {
10015 new_r2link = ast_calloc(1, sizeof(**r2links));
10016 if (!new_r2link) {
10017 ast_log(LOG_ERROR, "Cannot allocate R2 link!\n");
10018 return NULL;
10019 }
10020 new_r2links = ast_realloc(r2links, ((r2links_count + 1) * sizeof(*r2links)));
10021 if (!new_r2links) {
10022 ast_log(LOG_ERROR, "Cannot allocate R2 link!\n");
10023 ast_free(new_r2link);
10024 return NULL;
10025 }
10026 r2links = new_r2links;
10027 new_r2link->r2master = AST_PTHREADT_NULL;
10028 r2links[r2links_count] = new_r2link;
10029 r2links_count++;
10030 ast_log(LOG_DEBUG, "Created new R2 link!\n");
10031 }
10032 return r2links[r2links_count - 1];
10033 }
10034
10035 static int dahdi_r2_set_context(struct dahdi_mfcr2 *r2_link, const struct dahdi_chan_conf *conf)
10036 {
10037 char tmplogdir[] = "/tmp";
10038 char logdir[OR2_MAX_PATH];
10039 int threshold = 0;
10040 int snres = 0;
10041 r2_link->protocol_context = openr2_context_new(NULL, &dahdi_r2_event_iface,
10042 &dahdi_r2_transcode_iface, conf->mfcr2.variant, conf->mfcr2.max_ani,
10043 conf->mfcr2.max_dnis);
10044 if (!r2_link->protocol_context) {
10045 return -1;
10046 }
10047 openr2_context_set_log_level(r2_link->protocol_context, conf->mfcr2.loglevel);
10048 openr2_context_set_ani_first(r2_link->protocol_context, conf->mfcr2.get_ani_first);
10049 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
10050 openr2_context_set_skip_category_request(r2_link->protocol_context, conf->mfcr2.skip_category_request);
10051 #endif
10052 openr2_context_set_mf_threshold(r2_link->protocol_context, threshold);
10053 openr2_context_set_mf_back_timeout(r2_link->protocol_context, conf->mfcr2.mfback_timeout);
10054 openr2_context_set_metering_pulse_timeout(r2_link->protocol_context, conf->mfcr2.metering_pulse_timeout);
10055 openr2_context_set_double_answer(r2_link->protocol_context, conf->mfcr2.double_answer);
10056 openr2_context_set_immediate_accept(r2_link->protocol_context, conf->mfcr2.immediate_accept);
10057 if (ast_strlen_zero(conf->mfcr2.logdir)) {
10058 if (openr2_context_set_log_directory(r2_link->protocol_context, tmplogdir)) {
10059 ast_log(LOG_ERROR, "Failed setting default MFC/R2 log directory %s\n", tmplogdir);
10060 }
10061 } else {
10062 snres = snprintf(logdir, sizeof(logdir), "%s/%s/%s", ast_config_AST_LOG_DIR, "mfcr2", conf->mfcr2.logdir);
10063 if (snres >= sizeof(logdir)) {
10064 ast_log(LOG_ERROR, "MFC/R2 logging directory truncated, using %s\n", tmplogdir);
10065 if (openr2_context_set_log_directory(r2_link->protocol_context, tmplogdir)) {
10066 ast_log(LOG_ERROR, "Failed setting default MFC/R2 log directory %s\n", tmplogdir);
10067 }
10068 } else {
10069 if (openr2_context_set_log_directory(r2_link->protocol_context, logdir)) {
10070 ast_log(LOG_ERROR, "Failed setting MFC/R2 log directory %s\n", logdir);
10071 }
10072 }
10073 }
10074 if (!ast_strlen_zero(conf->mfcr2.r2proto_file)) {
10075 if (openr2_context_configure_from_advanced_file(r2_link->protocol_context, conf->mfcr2.r2proto_file)) {
10076 ast_log(LOG_ERROR, "Failed to configure r2context from advanced configuration file %s\n", conf->mfcr2.r2proto_file);
10077 }
10078 }
10079 r2_link->monitored_count = 0;
10080 return 0;
10081 }
10082 #endif
10083
10084
10085
10086
10087
10088
10089 static int sigtype_to_signalling(int sigtype)
10090 {
10091 return sigtype;
10092 }
10093
10094 static struct dahdi_pvt *mkintf(int channel, const struct dahdi_chan_conf *conf, struct dahdi_pri *pri, int reloading)
10095 {
10096
10097 struct dahdi_pvt *tmp = NULL, *tmp2, *prev = NULL;
10098 char fn[80];
10099 struct dahdi_bufferinfo bi;
10100
10101 int res;
10102 int span = 0;
10103 int here = 0;
10104 int x;
10105 struct dahdi_pvt **wlist;
10106 struct dahdi_pvt **wend;
10107 struct dahdi_params p;
10108
10109 wlist = &iflist;
10110 wend = &ifend;
10111
10112 #ifdef HAVE_PRI
10113 if (pri) {
10114 wlist = &pri->crvs;
10115 wend = &pri->crvend;
10116 }
10117 #endif
10118
10119 tmp2 = *wlist;
10120 prev = NULL;
10121
10122 while (tmp2) {
10123 if (!tmp2->destroy) {
10124 if (tmp2->channel == channel) {
10125 tmp = tmp2;
10126 here = 1;
10127 break;
10128 }
10129 if (tmp2->channel > channel) {
10130 break;
10131 }
10132 }
10133 prev = tmp2;
10134 tmp2 = tmp2->next;
10135 }
10136
10137 if (!here && reloading != 1) {
10138 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
10139 if (tmp)
10140 free(tmp);
10141 return NULL;
10142 }
10143 ast_mutex_init(&tmp->lock);
10144 ifcount++;
10145 for (x = 0; x < 3; x++)
10146 tmp->subs[x].dfd = -1;
10147 tmp->channel = channel;
10148 tmp->priindication_oob = conf->chan.priindication_oob;
10149 }
10150
10151 if (tmp) {
10152 int chan_sig = conf->chan.sig;
10153 if (!here) {
10154 if ((channel != CHAN_PSEUDO) && !pri) {
10155 int count = 0;
10156 snprintf(fn, sizeof(fn), "%d", channel);
10157
10158 tmp->subs[SUB_REAL].dfd = dahdi_open(fn);
10159 while (tmp->subs[SUB_REAL].dfd < 0 && reloading == 2 && count < 1000) {
10160 usleep(1);
10161 tmp->subs[SUB_REAL].dfd = dahdi_open(fn);
10162 count++;
10163 }
10164
10165 if (tmp->subs[SUB_REAL].dfd < 0) {
10166 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);
10167 destroy_dahdi_pvt(&tmp);
10168 return NULL;
10169 }
10170 memset(&p, 0, sizeof(p));
10171 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p);
10172 if (res < 0) {
10173 ast_log(LOG_ERROR, "Unable to get parameters: %s\n", strerror(errno));
10174 destroy_dahdi_pvt(&tmp);
10175 return NULL;
10176 }
10177 if (conf->is_sig_auto)
10178 chan_sig = sigtype_to_signalling(p.sigtype);
10179 if (p.sigtype != (chan_sig & 0x3ffff)) {
10180 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));
10181 destroy_dahdi_pvt(&tmp);
10182 return NULL;
10183 }
10184 tmp->law = p.curlaw;
10185 tmp->span = p.spanno;
10186 span = p.spanno - 1;
10187 } else {
10188 if (channel == CHAN_PSEUDO)
10189 chan_sig = 0;
10190 else if ((chan_sig != SIG_FXOKS) && (chan_sig != SIG_FXSKS)) {
10191 ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n");
10192 return NULL;
10193 }
10194 }
10195 tmp->outsigmod = conf->chan.outsigmod;
10196
10197 #ifdef HAVE_SS7
10198 if (chan_sig == SIG_SS7) {
10199 struct dahdi_ss7 *ss7;
10200 int clear = 0;
10201 if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &clear)) {
10202 ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
10203 destroy_dahdi_pvt(&tmp);
10204 return NULL;
10205 }
10206
10207 ss7 = ss7_resolve_linkset(cur_linkset);
10208 if (!ss7) {
10209 ast_log(LOG_ERROR, "Unable to find linkset %d\n", cur_linkset);
10210 destroy_dahdi_pvt(&tmp);
10211 return NULL;
10212 }
10213 if (cur_cicbeginswith < 0) {
10214 ast_log(LOG_ERROR, "Need to set cicbeginswith for the channels!\n");
10215 destroy_dahdi_pvt(&tmp);
10216 return NULL;
10217 }
10218
10219 tmp->cic = cur_cicbeginswith++;
10220
10221
10222 tmp->dpc = cur_defaultdpc;
10223
10224 tmp->ss7 = ss7;
10225 tmp->ss7call = NULL;
10226 ss7->pvts[ss7->numchans++] = tmp;
10227
10228 ast_copy_string(ss7->internationalprefix, conf->ss7.internationalprefix, sizeof(ss7->internationalprefix));
10229 ast_copy_string(ss7->nationalprefix, conf->ss7.nationalprefix, sizeof(ss7->nationalprefix));
10230 ast_copy_string(ss7->subscriberprefix, conf->ss7.subscriberprefix, sizeof(ss7->subscriberprefix));
10231 ast_copy_string(ss7->unknownprefix, conf->ss7.unknownprefix, sizeof(ss7->unknownprefix));
10232
10233 ss7->called_nai = conf->ss7.called_nai;
10234 ss7->calling_nai = conf->ss7.calling_nai;
10235 }
10236 #endif
10237 #ifdef HAVE_OPENR2
10238 if (chan_sig == SIG_MFCR2 && reloading != 1) {
10239 struct dahdi_mfcr2 *r2_link;
10240 r2_link = dahdi_r2_get_link();
10241 if (!r2_link) {
10242 ast_log(LOG_WARNING, "Cannot get another R2 DAHDI context!\n");
10243 destroy_dahdi_pvt(&tmp);
10244 return NULL;
10245 }
10246 if (!r2_link->protocol_context && dahdi_r2_set_context(r2_link, conf)) {
10247 ast_log(LOG_ERROR, "Cannot create OpenR2 protocol context.\n");
10248 destroy_dahdi_pvt(&tmp);
10249 return NULL;
10250 }
10251 if (r2_link->numchans == (sizeof(r2_link->pvts)/sizeof(r2_link->pvts[0]))) {
10252 ast_log(LOG_ERROR, "Cannot add more channels to this link!\n");
10253 destroy_dahdi_pvt(&tmp);
10254 return NULL;
10255 }
10256 r2_link->pvts[r2_link->numchans++] = tmp;
10257 tmp->r2chan = openr2_chan_new_from_fd(r2_link->protocol_context,
10258 tmp->subs[SUB_REAL].dfd,
10259 NULL, NULL);
10260 if (!tmp->r2chan) {
10261 openr2_liberr_t err = openr2_context_get_last_error(r2_link->protocol_context);
10262 ast_log(LOG_ERROR, "Cannot create OpenR2 channel: %s\n", openr2_context_error_string(err));
10263 destroy_dahdi_pvt(&tmp);
10264 return NULL;
10265 }
10266 tmp->mfcr2 = r2_link;
10267 if (conf->mfcr2.call_files) {
10268 openr2_chan_enable_call_files(tmp->r2chan);
10269 }
10270 openr2_chan_set_client_data(tmp->r2chan, tmp);
10271
10272 openr2_chan_set_logging_func(tmp->r2chan, (openr2_logging_func_t)dahdi_r2_on_chan_log);
10273 openr2_chan_set_log_level(tmp->r2chan, conf->mfcr2.loglevel);
10274 tmp->mfcr2_category = conf->mfcr2.category;
10275 tmp->mfcr2_charge_calls = conf->mfcr2.charge_calls;
10276 tmp->mfcr2_allow_collect_calls = conf->mfcr2.allow_collect_calls;
10277 tmp->mfcr2_forced_release = conf->mfcr2.forced_release;
10278 tmp->mfcr2_accept_on_offer = conf->mfcr2.accept_on_offer;
10279 tmp->mfcr2call = 0;
10280 tmp->mfcr2_dnis_index = 0;
10281 tmp->mfcr2_ani_index = 0;
10282 r2_link->monitored_count++;
10283 }
10284 #endif
10285 #ifdef HAVE_PRI
10286 if ((chan_sig == SIG_PRI) || (chan_sig == SIG_BRI) || (chan_sig == SIG_BRI_PTMP) || (chan_sig == SIG_GR303FXOKS) || (chan_sig == SIG_GR303FXSKS)) {
10287 int offset;
10288 int myswitchtype;
10289 int matchesdchan;
10290 int x,y;
10291 offset = 0;
10292 if (((chan_sig == SIG_PRI) || (chan_sig == SIG_BRI) || (chan_sig == SIG_BRI_PTMP))
10293 && ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &offset)) {
10294 ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
10295 destroy_dahdi_pvt(&tmp);
10296 return NULL;
10297 }
10298 if (span >= NUM_SPANS) {
10299 ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span);
10300 destroy_dahdi_pvt(&tmp);
10301 return NULL;
10302 } else {
10303 struct dahdi_spaninfo si;
10304 si.spanno = 0;
10305 if (ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SPANSTAT,&si) == -1) {
10306 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
10307 destroy_dahdi_pvt(&tmp);
10308 return NULL;
10309 }
10310
10311 tmp->logicalspan = pris[span].prilogicalspan;
10312 pri_resolve_span(&span, channel, (channel - p.chanpos), &si);
10313 if (span < 0) {
10314 ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel);
10315 destroy_dahdi_pvt(&tmp);
10316 return NULL;
10317 }
10318 if ((chan_sig == SIG_PRI) ||
10319 (chan_sig == SIG_BRI) ||
10320 (chan_sig == SIG_BRI_PTMP))
10321 myswitchtype = conf->pri.switchtype;
10322 else
10323 myswitchtype = PRI_SWITCH_GR303_TMC;
10324
10325 matchesdchan=0;
10326 for (x = 0; x < NUM_SPANS; x++) {
10327 for (y = 0; y < NUM_DCHANS; y++) {
10328 if (pris[x].dchannels[y] == tmp->channel) {
10329 matchesdchan = 1;
10330 break;
10331 }
10332 }
10333 }
10334 offset = p.chanpos;
10335 if (!matchesdchan) {
10336 if (pris[span].nodetype && (pris[span].nodetype != conf->pri.nodetype)) {
10337 ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype));
10338 destroy_dahdi_pvt(&tmp);
10339 return NULL;
10340 }
10341 if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) {
10342 ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype));
10343 destroy_dahdi_pvt(&tmp);
10344 return NULL;
10345 }
10346 if ((pris[span].dialplan) && (pris[span].dialplan != conf->pri.dialplan)) {
10347 ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan));
10348 destroy_dahdi_pvt(&tmp);
10349 return NULL;
10350 }
10351 if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, conf->pri.idledial)) {
10352 ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, conf->pri.idledial);
10353 destroy_dahdi_pvt(&tmp);
10354 return NULL;
10355 }
10356 if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, conf->pri.idleext)) {
10357 ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, conf->pri.idleext);
10358 destroy_dahdi_pvt(&tmp);
10359 return NULL;
10360 }
10361 if (pris[span].minunused && (pris[span].minunused != conf->pri.minunused)) {
10362 ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, conf->pri.minunused);
10363 destroy_dahdi_pvt(&tmp);
10364 return NULL;
10365 }
10366 if (pris[span].minidle && (pris[span].minidle != conf->pri.minidle)) {
10367 ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, conf->pri.minidle);
10368 destroy_dahdi_pvt(&tmp);
10369 return NULL;
10370 }
10371 if (pris[span].numchans >= MAX_CHANNELS) {
10372 ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel,
10373 pris[span].trunkgroup);
10374 destroy_dahdi_pvt(&tmp);
10375 return NULL;
10376 }
10377
10378 pris[span].sig = chan_sig;
10379 pris[span].nodetype = conf->pri.nodetype;
10380 pris[span].switchtype = myswitchtype;
10381 pris[span].nsf = conf->pri.nsf;
10382 pris[span].dialplan = conf->pri.dialplan;
10383 pris[span].localdialplan = conf->pri.localdialplan;
10384 pris[span].pvts[pris[span].numchans++] = tmp;
10385 pris[span].minunused = conf->pri.minunused;
10386 pris[span].minidle = conf->pri.minidle;
10387 pris[span].overlapdial = conf->pri.overlapdial;
10388 pris[span].qsigchannelmapping = conf->pri.qsigchannelmapping;
10389 pris[span].discardremoteholdretrieval = conf->pri.discardremoteholdretrieval;
10390 #ifdef HAVE_PRI_INBANDDISCONNECT
10391 pris[span].inbanddisconnect = conf->pri.inbanddisconnect;
10392 #endif
10393 pris[span].facilityenable = conf->pri.facilityenable;
10394 ast_copy_string(pris[span].idledial, conf->pri.idledial, sizeof(pris[span].idledial));
10395 ast_copy_string(pris[span].idleext, conf->pri.idleext, sizeof(pris[span].idleext));
10396 ast_copy_string(pris[span].internationalprefix, conf->pri.internationalprefix, sizeof(pris[span].internationalprefix));
10397 ast_copy_string(pris[span].nationalprefix, conf->pri.nationalprefix, sizeof(pris[span].nationalprefix));
10398 ast_copy_string(pris[span].localprefix, conf->pri.localprefix, sizeof(pris[span].localprefix));
10399 ast_copy_string(pris[span].privateprefix, conf->pri.privateprefix, sizeof(pris[span].privateprefix));
10400 ast_copy_string(pris[span].unknownprefix, conf->pri.unknownprefix, sizeof(pris[span].unknownprefix));
10401 pris[span].resetinterval = conf->pri.resetinterval;
10402
10403 tmp->pri = &pris[span];
10404 tmp->prioffset = offset;
10405 tmp->call = NULL;
10406
10407 tmp->priexclusive = conf->chan.priexclusive;
10408 } else {
10409 ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset);
10410 destroy_dahdi_pvt(&tmp);
10411 return NULL;
10412 }
10413 }
10414 } else {
10415 tmp->prioffset = 0;
10416 }
10417 #endif
10418 } else {
10419 chan_sig = tmp->sig;
10420 if (tmp->subs[SUB_REAL].dfd > -1) {
10421 memset(&p, 0, sizeof(p));
10422 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p);
10423 }
10424 }
10425
10426 switch (chan_sig) {
10427 case SIG_FXSKS:
10428 case SIG_FXSLS:
10429 case SIG_EM:
10430 case SIG_EM_E1:
10431 case SIG_EMWINK:
10432 case SIG_FEATD:
10433 case SIG_FEATDMF:
10434 case SIG_FEATDMF_TA:
10435 case SIG_FEATB:
10436 case SIG_E911:
10437 case SIG_SF:
10438 case SIG_SFWINK:
10439 case SIG_FGC_CAMA:
10440 case SIG_FGC_CAMAMF:
10441 case SIG_SF_FEATD:
10442 case SIG_SF_FEATDMF:
10443 case SIG_SF_FEATB:
10444 p.starttime = 250;
10445 break;
10446 }
10447
10448 if (tmp->radio) {
10449
10450 p.channo = channel;
10451 p.rxwinktime = 1;
10452 p.rxflashtime = 1;
10453 p.starttime = 1;
10454 p.debouncetime = 5;
10455 }
10456 if (!tmp->radio) {
10457 p.channo = channel;
10458
10459 if (conf->timing.prewinktime >= 0)
10460 p.prewinktime = conf->timing.prewinktime;
10461 if (conf->timing.preflashtime >= 0)
10462 p.preflashtime = conf->timing.preflashtime;
10463 if (conf->timing.winktime >= 0)
10464 p.winktime = conf->timing.winktime;
10465 if (conf->timing.flashtime >= 0)
10466 p.flashtime = conf->timing.flashtime;
10467 if (conf->timing.starttime >= 0)
10468 p.starttime = conf->timing.starttime;
10469 if (conf->timing.rxwinktime >= 0)
10470 p.rxwinktime = conf->timing.rxwinktime;
10471 if (conf->timing.rxflashtime >= 0)
10472 p.rxflashtime = conf->timing.rxflashtime;
10473 if (conf->timing.debouncetime >= 0)
10474 p.debouncetime = conf->timing.debouncetime;
10475 }
10476
10477
10478 if (tmp->subs[SUB_REAL].dfd >= 0)
10479 {
10480 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_PARAMS, &p);
10481 if (res < 0) {
10482 ast_log(LOG_ERROR, "Unable to set parameters: %s\n", strerror(errno));
10483 destroy_dahdi_pvt(&tmp);
10484 return NULL;
10485 }
10486 }
10487 #if 1
10488 if (!here && (tmp->subs[SUB_REAL].dfd > -1)) {
10489 memset(&bi, 0, sizeof(bi));
10490 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi);
10491 if (!res) {
10492 bi.txbufpolicy = conf->chan.buf_policy;
10493 bi.rxbufpolicy = conf->chan.buf_policy;
10494 bi.numbufs = conf->chan.buf_no;
10495 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi);
10496 if (res < 0) {
10497 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", channel, strerror(errno));
10498 }
10499 } else {
10500 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", channel, strerror(errno));
10501 }
10502 tmp->buf_policy = conf->chan.buf_policy;
10503 tmp->buf_no = conf->chan.buf_no;
10504 tmp->usefaxbuffers = conf->chan.usefaxbuffers;
10505 tmp->faxbuf_policy = conf->chan.faxbuf_policy;
10506 tmp->faxbuf_no = conf->chan.faxbuf_no;
10507
10508
10509
10510
10511 tmp->bufsize = bi.bufsize;
10512 }
10513 #endif
10514 tmp->immediate = conf->chan.immediate;
10515 tmp->transfertobusy = conf->chan.transfertobusy;
10516 if (chan_sig & __DAHDI_SIG_FXS) {
10517 tmp->mwimonitor_fsk = conf->chan.mwimonitor_fsk;
10518 tmp->mwimonitor_neon = conf->chan.mwimonitor_neon;
10519 tmp->mwimonitor_rpas = conf->chan.mwimonitor_rpas;
10520 }
10521 tmp->sig = chan_sig;
10522 tmp->ringt_base = ringt_base;
10523 tmp->firstradio = 0;
10524 if ((chan_sig == SIG_FXOKS) || (chan_sig == SIG_FXOLS) || (chan_sig == SIG_FXOGS))
10525 tmp->permcallwaiting = conf->chan.callwaiting;
10526 else
10527 tmp->permcallwaiting = 0;
10528
10529 tmp->destroy = 0;
10530 tmp->drings = conf->chan.drings;
10531
10532
10533 if (tmp->drings.ringnum[0].range == 0)
10534 tmp->drings.ringnum[0].range = 10;
10535 if (tmp->drings.ringnum[1].range == 0)
10536 tmp->drings.ringnum[1].range = 10;
10537 if (tmp->drings.ringnum[2].range == 0)
10538 tmp->drings.ringnum[2].range = 10;
10539
10540 tmp->usedistinctiveringdetection = usedistinctiveringdetection;
10541 tmp->callwaitingcallerid = conf->chan.callwaitingcallerid;
10542 tmp->threewaycalling = conf->chan.threewaycalling;
10543 tmp->adsi = conf->chan.adsi;
10544 tmp->use_smdi = conf->chan.use_smdi;
10545 tmp->permhidecallerid = conf->chan.hidecallerid;
10546 tmp->hidecalleridname = conf->chan.hidecalleridname;
10547 tmp->callreturn = conf->chan.callreturn;
10548 tmp->echocancel = conf->chan.echocancel;
10549 tmp->echotraining = conf->chan.echotraining;
10550 tmp->pulse = conf->chan.pulse;
10551 if (tmp->echocancel.head.tap_length) {
10552 tmp->echocanbridged = conf->chan.echocanbridged;
10553 } else {
10554 if (conf->chan.echocanbridged)
10555 ast_log(LOG_NOTICE, "echocancelwhenbridged requires echocancel to be enabled; ignoring\n");
10556 tmp->echocanbridged = 0;
10557 }
10558 tmp->busydetect = conf->chan.busydetect;
10559 tmp->busycount = conf->chan.busycount;
10560 tmp->busy_tonelength = conf->chan.busy_tonelength;
10561 tmp->busy_quietlength = conf->chan.busy_quietlength;
10562 tmp->callprogress = conf->chan.callprogress;
10563 tmp->waitfordialtone = conf->chan.waitfordialtone;
10564 tmp->cancallforward = conf->chan.cancallforward;
10565 tmp->dtmfrelax = conf->chan.dtmfrelax;
10566 tmp->callwaiting = tmp->permcallwaiting;
10567 tmp->hidecallerid = tmp->permhidecallerid;
10568 tmp->channel = channel;
10569 tmp->stripmsd = conf->chan.stripmsd;
10570 tmp->use_callerid = conf->chan.use_callerid;
10571 tmp->cid_signalling = conf->chan.cid_signalling;
10572 tmp->cid_start = conf->chan.cid_start;
10573 tmp->dahditrcallerid = conf->chan.dahditrcallerid;
10574 tmp->restrictcid = conf->chan.restrictcid;
10575 tmp->use_callingpres = conf->chan.use_callingpres;
10576 if (tmp->usedistinctiveringdetection) {
10577 if (!tmp->use_callerid) {
10578 ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n");
10579 tmp->use_callerid = 1;
10580 }
10581 }
10582
10583 if (tmp->cid_signalling == CID_SIG_SMDI) {
10584 if (!tmp->use_smdi) {
10585 ast_log(LOG_WARNING, "SMDI callerid requires SMDI to be enabled, enabling...\n");
10586 tmp->use_smdi = 1;
10587 }
10588 }
10589 if (tmp->use_smdi) {
10590 tmp->smdi_iface = ast_smdi_interface_find(conf->smdi_port);
10591 if (!(tmp->smdi_iface)) {
10592 ast_log(LOG_ERROR, "Invalid SMDI port specfied, disabling SMDI support\n");
10593 tmp->use_smdi = 0;
10594 }
10595 }
10596
10597 ast_copy_string(tmp->accountcode, conf->chan.accountcode, sizeof(tmp->accountcode));
10598 tmp->amaflags = conf->chan.amaflags;
10599 if (!here) {
10600 tmp->confno = -1;
10601 tmp->propconfno = -1;
10602 }
10603 tmp->canpark = conf->chan.canpark;
10604 tmp->transfer = conf->chan.transfer;
10605 ast_copy_string(tmp->defcontext,conf->chan.context,sizeof(tmp->defcontext));
10606 ast_copy_string(tmp->language, conf->chan.language, sizeof(tmp->language));
10607 ast_copy_string(tmp->mohinterpret, conf->chan.mohinterpret, sizeof(tmp->mohinterpret));
10608 ast_copy_string(tmp->mohsuggest, conf->chan.mohsuggest, sizeof(tmp->mohsuggest));
10609 ast_copy_string(tmp->context, conf->chan.context, sizeof(tmp->context));
10610 ast_copy_string(tmp->parkinglot, conf->chan.parkinglot, sizeof(tmp->parkinglot));
10611 tmp->cid_ton = 0;
10612 switch (tmp->sig) {
10613 case SIG_PRI:
10614 case SIG_BRI:
10615 case SIG_BRI_PTMP:
10616 case SIG_SS7:
10617 case SIG_MFCR2:
10618 tmp->cid_num[0] = '\0';
10619 tmp->cid_name[0] = '\0';
10620 break;
10621 default:
10622 ast_copy_string(tmp->cid_num, conf->chan.cid_num, sizeof(tmp->cid_num));
10623 ast_copy_string(tmp->cid_name, conf->chan.cid_name, sizeof(tmp->cid_name));
10624 break;
10625 }
10626 ast_copy_string(tmp->mailbox, conf->chan.mailbox, sizeof(tmp->mailbox));
10627 if (channel != CHAN_PSEUDO && !ast_strlen_zero(tmp->mailbox)) {
10628 char *mailbox, *context;
10629 mailbox = context = ast_strdupa(tmp->mailbox);
10630 strsep(&context, "@");
10631 if (ast_strlen_zero(context))
10632 context = "default";
10633 tmp->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, NULL,
10634 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
10635 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
10636 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
10637 AST_EVENT_IE_END);
10638 }
10639 tmp->msgstate = -1;
10640 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
10641 tmp->mwisend_setting = conf->chan.mwisend_setting;
10642 tmp->mwisend_fsk = conf->chan.mwisend_fsk;
10643 tmp->mwisend_rpas = conf->chan.mwisend_rpas;
10644 #endif
10645 if (chan_sig & __DAHDI_SIG_FXO) {
10646 memset(&p, 0, sizeof(p));
10647 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p);
10648 if (!res) {
10649 tmp->fxsoffhookstate = p.rxisoffhook;
10650 }
10651 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
10652 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_VMWI_CONFIG, &tmp->mwisend_setting);
10653 #endif
10654 }
10655 tmp->onhooktime = time(NULL);
10656 tmp->group = conf->chan.group;
10657 tmp->callgroup = conf->chan.callgroup;
10658 tmp->pickupgroup= conf->chan.pickupgroup;
10659 if (conf->chan.vars) {
10660 struct ast_variable *v, *tmpvar;
10661 for (v = conf->chan.vars ; v ; v = v->next) {
10662 if ((tmpvar = ast_variable_new(v->name, v->value, v->file))) {
10663 tmpvar->next = tmp->vars;
10664 tmp->vars = tmpvar;
10665 }
10666 }
10667 }
10668 tmp->cid_rxgain = conf->chan.cid_rxgain;
10669 tmp->rxgain = conf->chan.rxgain;
10670 tmp->txgain = conf->chan.txgain;
10671 tmp->tonezone = conf->chan.tonezone;
10672 if (tmp->subs[SUB_REAL].dfd > -1) {
10673 set_actual_gain(tmp->subs[SUB_REAL].dfd, 0, tmp->rxgain, tmp->txgain, tmp->law);
10674 if (tmp->dsp)
10675 ast_dsp_set_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax);
10676 update_conf(tmp);
10677 if (!here) {
10678 if ((chan_sig != SIG_BRI) && (chan_sig != SIG_BRI_PTMP) && (chan_sig != SIG_PRI)
10679 && (chan_sig != SIG_SS7) && (chan_sig != SIG_MFCR2))
10680
10681 dahdi_set_hook(tmp->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
10682 }
10683 ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SETTONEZONE,&tmp->tonezone);
10684 #ifdef HAVE_PRI
10685
10686 if (tmp->pri && !pri_is_up(tmp->pri))
10687 tmp->inalarm = 1;
10688 #endif
10689 if ((res = get_alarms(tmp)) != DAHDI_ALARM_NONE) {
10690 tmp->inalarm = 1;
10691 handle_alarms(tmp, res);
10692 }
10693 }
10694
10695 tmp->polarityonanswerdelay = conf->chan.polarityonanswerdelay;
10696 tmp->answeronpolarityswitch = conf->chan.answeronpolarityswitch;
10697 tmp->hanguponpolarityswitch = conf->chan.hanguponpolarityswitch;
10698 tmp->sendcalleridafter = conf->chan.sendcalleridafter;
10699 if (!here) {
10700 tmp->locallyblocked = tmp->remotelyblocked = 0;
10701 if ((chan_sig == SIG_PRI) || (chan_sig == SIG_BRI) || (chan_sig == SIG_BRI_PTMP) || (chan_sig == SIG_SS7))
10702 tmp->inservice = 0;
10703 else
10704 tmp->inservice = 1;
10705 }
10706 }
10707 if (tmp && !here) {
10708
10709 if (!*wlist) {
10710 *wlist = tmp;
10711 tmp->prev = NULL;
10712 tmp->next = NULL;
10713 *wend = tmp;
10714 } else {
10715
10716 struct dahdi_pvt *working = *wlist;
10717
10718
10719 if (working->channel > tmp->channel) {
10720 tmp->next = *wlist;
10721 tmp->prev = NULL;
10722 (*wlist)->prev = tmp;
10723 *wlist = tmp;
10724 } else {
10725
10726 while (working) {
10727
10728 if (working->next) {
10729 if (working->channel < tmp->channel && working->next->channel > tmp->channel) {
10730 tmp->next = working->next;
10731 tmp->prev = working;
10732 working->next->prev = tmp;
10733 working->next = tmp;
10734 break;
10735 }
10736 } else {
10737
10738 if (working->channel < tmp->channel) {
10739 working->next = tmp;
10740 tmp->next = NULL;
10741 tmp->prev = working;
10742 *wend = tmp;
10743 break;
10744 }
10745 }
10746 working = working->next;
10747 }
10748 }
10749 }
10750 }
10751 return tmp;
10752 }
10753
10754 static inline int available(struct dahdi_pvt *p, int channelmatch, ast_group_t groupmatch, int *busy, int *channelmatched, int *groupmatched)
10755 {
10756 int res;
10757 struct dahdi_params par;
10758
10759
10760 if (groupmatch) {
10761 if ((p->group & groupmatch) != groupmatch)
10762 return 0;
10763 *groupmatched = 1;
10764 }
10765
10766 if (channelmatch != -1) {
10767 if (p->channel != channelmatch)
10768 return 0;
10769 *channelmatched = 1;
10770 }
10771
10772 if (busy) {
10773 if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS))
10774 *busy = 1;
10775 }
10776
10777 if (p->dnd)
10778 return 0;
10779
10780 if (p->guardtime && (time(NULL) < p->guardtime))
10781 return 0;
10782
10783 if (p->locallyblocked || p->remotelyblocked)
10784 return 0;
10785
10786
10787 if (!p->owner) {
10788 #ifdef HAVE_PRI
10789
10790 if (p->pri) {
10791 if (p->resetting || p->call)
10792 return 0;
10793 else
10794 return 1;
10795 }
10796 #endif
10797 #ifdef HAVE_SS7
10798
10799 if (p->ss7) {
10800 if (p->ss7call)
10801 return 0;
10802 else
10803 return 1;
10804 }
10805 #endif
10806 #ifdef HAVE_OPENR2
10807
10808 if (p->mfcr2) {
10809 if (p->mfcr2call)
10810 return 0;
10811 else
10812 return 1;
10813 }
10814 #endif
10815
10816
10817 if (p->sig && !(p->radio || (p->oprmode < 0)))
10818 {
10819
10820 if (p->subs[SUB_REAL].dfd > -1) {
10821 memset(&par, 0, sizeof(par));
10822 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
10823 } else {
10824
10825 res = 0;
10826 par.rxisoffhook = 0;
10827 }
10828
10829 if (res) {
10830 ast_log(LOG_WARNING, "Unable to check hook state on channel %d: %s\n", p->channel, strerror(errno));
10831 }
10832 else if ((p->sig != SIG_FXSKS) && (p->sig != SIG_FXSGS) && (p->sig != SIG_FXSLS)) {
10833 if (par.rxisoffhook) {
10834 ast_debug(1, "Channel %d off hook, can't use\n", p->channel);
10835
10836 return 0;
10837 }
10838 }
10839 #ifdef DAHDI_CHECK_HOOKSTATE
10840 } else {
10841
10842 if (par.rxbits > -1)
10843 return 1;
10844
10845 if (par.rxisoffhook)
10846 return 1;
10847 else
10848 return 0;
10849 #endif
10850 }
10851
10852 return 1;
10853 }
10854
10855
10856 if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS))
10857 return 0;
10858
10859 if (!p->callwaiting) {
10860
10861 return 0;
10862 }
10863
10864 if (p->subs[SUB_CALLWAIT].dfd > -1) {
10865
10866 return 0;
10867 }
10868
10869 if ((p->owner->_state != AST_STATE_UP) &&
10870 ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
10871
10872 return 0;
10873 }
10874 if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) {
10875
10876 return 0;
10877 }
10878
10879 return 1;
10880 }
10881
10882
10883
10884
10885
10886
10887 static struct dahdi_pvt *duplicate_pseudo(struct dahdi_pvt *src)
10888 {
10889 struct dahdi_pvt *p;
10890 struct dahdi_bufferinfo bi;
10891 int res;
10892
10893 if ((p = ast_malloc(sizeof(*p)))) {
10894 memcpy(p, src, sizeof(struct dahdi_pvt));
10895 ast_mutex_init(&p->lock);
10896 p->subs[SUB_REAL].dfd = dahdi_open("/dev/dahdi/pseudo");
10897 if (p->subs[SUB_REAL].dfd < 0) {
10898 ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno));
10899 destroy_dahdi_pvt(&p);
10900 return NULL;
10901 }
10902 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi);
10903 if (!res) {
10904 bi.txbufpolicy = src->buf_policy;
10905 bi.rxbufpolicy = src->buf_policy;
10906 bi.numbufs = src->buf_no;
10907 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi);
10908 if (res < 0) {
10909 ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel: %s\n", strerror(errno));
10910 }
10911 } else
10912 ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel: %s\n", strerror(errno));
10913 }
10914 p->destroy = 1;
10915 p->next = iflist;
10916 p->prev = NULL;
10917 iflist = p;
10918 if (iflist->next)
10919 iflist->next->prev = p;
10920 return p;
10921 }
10922
10923 #if defined(HAVE_PRI)
10924 static int pri_find_empty_chan(struct dahdi_pri *pri, int backwards)
10925 {
10926 int x;
10927 if (backwards)
10928 x = pri->numchans;
10929 else
10930 x = 0;
10931 for (;;) {
10932 if (backwards && (x < 0))
10933 break;
10934 if (!backwards && (x >= pri->numchans))
10935 break;
10936 if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
10937 ast_debug(1, "Found empty available channel %d/%d\n",
10938 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
10939 return x;
10940 }
10941 if (backwards)
10942 x--;
10943 else
10944 x++;
10945 }
10946 return -1;
10947 }
10948 #endif
10949
10950 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause)
10951 {
10952 ast_group_t groupmatch = 0;
10953 int channelmatch = -1;
10954 int roundrobin = 0;
10955 int callwait = 0;
10956 int busy = 0;
10957 struct dahdi_pvt *p;
10958 struct ast_channel *tmp = NULL;
10959 char *dest=NULL;
10960 int x;
10961 char *s;
10962 char opt=0;
10963 int res=0, y=0;
10964 int backwards = 0;
10965 #ifdef HAVE_PRI
10966 int crv;
10967 int bearer = -1;
10968 int trunkgroup;
10969 struct dahdi_pri *pri=NULL;
10970 #endif
10971 struct dahdi_pvt *exitpvt, *start, *end;
10972 ast_mutex_t *lock;
10973 int channelmatched = 0;
10974 int groupmatched = 0;
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994 lock = &iflock;
10995 start = iflist;
10996 end = ifend;
10997 if (data) {
10998 dest = ast_strdupa((char *)data);
10999 } else {
11000 ast_log(LOG_WARNING, "Channel requested with no data\n");
11001 return NULL;
11002 }
11003 if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') {
11004
11005 char *stringp;
11006
11007 stringp = dest + 1;
11008 s = strsep(&stringp, "/");
11009 if ((res = sscanf(s, "%30d%1c%30d", &x, &opt, &y)) < 1) {
11010 ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data);
11011 return NULL;
11012 }
11013 groupmatch = ((ast_group_t) 1 << x);
11014 if (toupper(dest[0]) == 'G') {
11015 if (dest[0] == 'G') {
11016 backwards = 1;
11017 p = ifend;
11018 } else
11019 p = iflist;
11020 } else {
11021 if (dest[0] == 'R') {
11022 backwards = 1;
11023 p = round_robin[x]?round_robin[x]->prev:ifend;
11024 if (!p)
11025 p = ifend;
11026 } else {
11027 p = round_robin[x]?round_robin[x]->next:iflist;
11028 if (!p)
11029 p = iflist;
11030 }
11031 roundrobin = 1;
11032 }
11033 } else {
11034 char *stringp;
11035
11036 stringp = dest;
11037 s = strsep(&stringp, "/");
11038 p = iflist;
11039 if (!strcasecmp(s, "pseudo")) {
11040
11041 x = CHAN_PSEUDO;
11042 channelmatch = x;
11043 }
11044 #ifdef HAVE_PRI
11045 else if ((res = sscanf(s, "%30d:%30d%1c%30d", &trunkgroup, &crv, &opt, &y)) > 1) {
11046 if ((trunkgroup < 1) || (crv < 1)) {
11047 ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data);
11048 return NULL;
11049 }
11050 res--;
11051 for (x = 0; x < NUM_SPANS; x++) {
11052 if (pris[x].trunkgroup == trunkgroup) {
11053 pri = pris + x;
11054 lock = &pri->lock;
11055 start = pri->crvs;
11056 end = pri->crvend;
11057 break;
11058 }
11059 }
11060 if (!pri) {
11061 ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup);
11062 return NULL;
11063 }
11064 channelmatch = crv;
11065 p = pris[x].crvs;
11066 }
11067 #endif
11068 else if ((res = sscanf(s, "%30d%1c%30d", &x, &opt, &y)) < 1) {
11069 ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data);
11070 return NULL;
11071 } else {
11072 channelmatch = x;
11073 }
11074 }
11075
11076 ast_mutex_lock(lock);
11077 exitpvt = p;
11078 while (p && !tmp) {
11079 if (roundrobin)
11080 round_robin[x] = p;
11081 #if 0
11082 ast_verbose("name = %s, %d, %d, %llu\n",p->owner ? p->owner->name : "<none>", p->channel, channelmatch, groupmatch);
11083 #endif
11084
11085 if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) {
11086 ast_debug(1, "Using channel %d\n", p->channel);
11087 if (p->inalarm)
11088 goto next;
11089
11090 callwait = (p->owner != NULL);
11091 #ifdef HAVE_PRI
11092 if (pri && (p->subs[SUB_REAL].dfd < 0)) {
11093 if (p->sig != SIG_FXSKS) {
11094
11095
11096 bearer = pri_find_empty_chan(pri, 0);
11097 if (bearer < 0) {
11098 ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv);
11099 p = NULL;
11100 break;
11101 }
11102 pri_assign_bearer(p, pri, pri->pvts[bearer]);
11103 } else {
11104 if (alloc_sub(p, 0)) {
11105 ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n");
11106 p = NULL;
11107 break;
11108 } else
11109 ast_debug(1, "Allocated placeholder pseudo channel\n");
11110
11111 p->pri = pri;
11112 }
11113 }
11114 #endif
11115 #ifdef HAVE_OPENR2
11116 if (p->mfcr2) {
11117 ast_mutex_lock(&p->lock);
11118 if (p->mfcr2call) {
11119 ast_mutex_unlock(&p->lock);
11120 ast_log(LOG_DEBUG, "Yay!, someone just beat us in the race for channel %d.\n", p->channel);
11121 goto next;
11122 }
11123 p->mfcr2call = 1;
11124 ast_mutex_unlock(&p->lock);
11125 }
11126 #endif
11127 if (p->channel == CHAN_PSEUDO) {
11128 p = duplicate_pseudo(p);
11129 if (!p) {
11130 break;
11131 }
11132 }
11133 if (p->owner) {
11134 if (alloc_sub(p, SUB_CALLWAIT)) {
11135 p = NULL;
11136 break;
11137 }
11138 }
11139 p->outgoing = 1;
11140 tmp = dahdi_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0);
11141 if (!tmp) {
11142 p->outgoing = 0;
11143 }
11144 #ifdef HAVE_PRI
11145 if (p->bearer) {
11146
11147 p->bearer->owner = tmp;
11148 }
11149 #endif
11150
11151 if (res > 1) {
11152 if (opt == 'c') {
11153
11154 p->confirmanswer = 1;
11155 } else if (opt == 'r') {
11156
11157 if (res < 3)
11158 ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data);
11159 else
11160 p->distinctivering = y;
11161 } else if (opt == 'd') {
11162
11163 p->digital = 1;
11164 if (tmp)
11165 tmp->transfercapability = AST_TRANS_CAP_DIGITAL;
11166 } else {
11167 ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data);
11168 }
11169 }
11170
11171 if (tmp && callwait)
11172 tmp->cdrflags |= AST_CDR_CALLWAIT;
11173 break;
11174 }
11175 next:
11176 if (backwards) {
11177 p = p->prev;
11178 if (!p)
11179 p = end;
11180 } else {
11181 p = p->next;
11182 if (!p)
11183 p = start;
11184 }
11185
11186 if (p == exitpvt)
11187 break;
11188 }
11189 ast_mutex_unlock(lock);
11190 restart_monitor();
11191 if (callwait)
11192 *cause = AST_CAUSE_BUSY;
11193 else if (!tmp) {
11194 if (channelmatched) {
11195 if (busy)
11196 *cause = AST_CAUSE_BUSY;
11197 } else if (groupmatched) {
11198 *cause = AST_CAUSE_CONGESTION;
11199 }
11200 }
11201
11202 return tmp;
11203 }
11204
11205 #if defined(HAVE_PRI) || defined(HAVE_SS7)
11206 static int dahdi_setlaw(int dfd, int law)
11207 {
11208 return ioctl(dfd, DAHDI_SETLAW, &law);
11209 }
11210 #endif
11211
11212 #if defined(HAVE_SS7)
11213 static int ss7_find_cic(struct dahdi_ss7 *linkset, int cic, unsigned int dpc)
11214 {
11215 int i;
11216 int winner = -1;
11217 for (i = 0; i < linkset->numchans; i++) {
11218 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
11219 winner = i;
11220 break;
11221 }
11222 }
11223 return winner;
11224 }
11225 #endif
11226
11227 #if defined(HAVE_SS7)
11228 static void ss7_handle_cqm(struct dahdi_ss7 *linkset, int startcic, int endcic, unsigned int dpc)
11229 {
11230 unsigned char status[32];
11231 struct dahdi_pvt *p = NULL;
11232 int i, offset;
11233
11234 for (i = 0; i < linkset->numchans; i++) {
11235 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
11236 p = linkset->pvts[i];
11237 offset = p->cic - startcic;
11238 status[offset] = 0;
11239 if (p->locallyblocked)
11240 status[offset] |= (1 << 0) | (1 << 4);
11241 if (p->remotelyblocked)
11242 status[offset] |= (1 << 1) | (1 << 5);
11243 if (p->ss7call) {
11244 if (p->outgoing)
11245 status[offset] |= (1 << 3);
11246 else
11247 status[offset] |= (1 << 2);
11248 } else
11249 status[offset] |= 0x3 << 2;
11250 }
11251 }
11252
11253 if (p)
11254 isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
11255 else
11256 ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
11257
11258 }
11259 #endif
11260
11261 #if defined(HAVE_SS7)
11262 static inline void ss7_hangup_cics(struct dahdi_ss7 *linkset, int startcic, int endcic, unsigned int dpc)
11263 {
11264 int i;
11265
11266 for (i = 0; i < linkset->numchans; i++) {
11267 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
11268 ast_mutex_lock(&linkset->pvts[i]->lock);
11269 if (linkset->pvts[i]->owner)
11270 linkset->pvts[i]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
11271 ast_mutex_unlock(&linkset->pvts[i]->lock);
11272 }
11273 }
11274 }
11275 #endif
11276
11277 #if defined(HAVE_SS7)
11278 static inline void ss7_block_cics(struct dahdi_ss7 *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
11279 {
11280 int i;
11281
11282 for (i = 0; i < linkset->numchans; i++) {
11283 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
11284 if (state) {
11285 if (state[i])
11286 linkset->pvts[i]->remotelyblocked = block;
11287 } else
11288 linkset->pvts[i]->remotelyblocked = block;
11289 }
11290 }
11291 }
11292 #endif
11293
11294 #if defined(HAVE_SS7)
11295 static void ss7_inservice(struct dahdi_ss7 *linkset, int startcic, int endcic, unsigned int dpc)
11296 {
11297 int i;
11298
11299 for (i = 0; i < linkset->numchans; i++) {
11300 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
11301 linkset->pvts[i]->inservice = 1;
11302 }
11303 }
11304 #endif
11305
11306 #if defined(HAVE_SS7)
11307 static void ss7_reset_linkset(struct dahdi_ss7 *linkset)
11308 {
11309 int i, startcic = -1, endcic, dpc;
11310
11311 if (linkset->numchans <= 0)
11312 return;
11313
11314 startcic = linkset->pvts[0]->cic;
11315
11316 dpc = linkset->pvts[0]->dpc;
11317
11318 for (i = 0; i < linkset->numchans; i++) {
11319 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)) {
11320 continue;
11321 } else {
11322 endcic = linkset->pvts[i]->cic;
11323 ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
11324 isup_grs(linkset->ss7, startcic, endcic, dpc);
11325
11326
11327 if (linkset->pvts[i+1]) {
11328 startcic = linkset->pvts[i+1]->cic;
11329 dpc = linkset->pvts[i+1]->dpc;
11330 }
11331 }
11332 }
11333 }
11334 #endif
11335
11336 #if defined(HAVE_SS7)
11337 static void dahdi_loopback(struct dahdi_pvt *p, int enable)
11338 {
11339 if (p->loopedback != enable) {
11340 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_LOOPBACK, &enable)) {
11341 ast_log(LOG_WARNING, "Unable to set loopback on channel %d: %s\n", p->channel, strerror(errno));
11342 return;
11343 }
11344 p->loopedback = enable;
11345 }
11346 }
11347 #endif
11348
11349 #if defined(HAVE_SS7)
11350
11351 static void ss7_start_call(struct dahdi_pvt *p, struct dahdi_ss7 *linkset)
11352 {
11353 struct ss7 *ss7 = linkset->ss7;
11354 int res;
11355 int law = 1;
11356 struct ast_channel *c;
11357 char tmp[256];
11358
11359 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &law) == -1)
11360 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", p->channel, law, strerror(errno));
11361
11362 if (linkset->type == SS7_ITU)
11363 law = DAHDI_LAW_ALAW;
11364 else
11365 law = DAHDI_LAW_MULAW;
11366
11367 res = dahdi_setlaw(p->subs[SUB_REAL].dfd, law);
11368 if (res < 0)
11369 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", p->channel);
11370
11371 if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
11372 p->proceeding = 1;
11373 isup_acm(ss7, p->ss7call);
11374 }
11375
11376 ast_mutex_unlock(&linkset->lock);
11377 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, law, 0);
11378
11379 if (!c) {
11380 ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
11381
11382 ast_mutex_lock(&linkset->lock);
11383 return;
11384 } else
11385 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
11386
11387 dahdi_enable_ec(p);
11388
11389
11390
11391
11392
11393 ast_mutex_unlock(&p->lock);
11394
11395 if (!ast_strlen_zero(p->charge_number)) {
11396 pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
11397
11398 p->charge_number[0] = 0;
11399 }
11400 if (!ast_strlen_zero(p->gen_add_number)) {
11401 pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
11402
11403 p->gen_add_number[0] = 0;
11404 }
11405 if (!ast_strlen_zero(p->jip_number)) {
11406 pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
11407
11408 p->jip_number[0] = 0;
11409 }
11410 if (!ast_strlen_zero(p->gen_dig_number)) {
11411 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
11412
11413 p->gen_dig_number[0] = 0;
11414 }
11415 if (!ast_strlen_zero(p->orig_called_num)) {
11416 pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
11417
11418 p->orig_called_num[0] = 0;
11419 }
11420
11421 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
11422 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
11423
11424 p->gen_dig_type = 0;
11425
11426 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
11427 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
11428
11429 p->gen_dig_scheme = 0;
11430
11431 if (!ast_strlen_zero(p->lspi_ident)) {
11432 pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
11433
11434 p->lspi_ident[0] = 0;
11435 }
11436
11437 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
11438 pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
11439
11440 p->call_ref_ident = 0;
11441
11442 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
11443 pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
11444
11445 p->call_ref_pc = 0;
11446
11447 snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
11448 pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
11449
11450 p->calling_party_cat = 0;
11451
11452 if (!ast_strlen_zero(p->redirecting_num)) {
11453 pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
11454
11455 p->redirecting_num[0] = 0;
11456 }
11457 if (!ast_strlen_zero(p->generic_name)) {
11458 pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
11459
11460 p->generic_name[0] = 0;
11461 }
11462
11463 ast_mutex_lock(&p->lock);
11464 ast_mutex_lock(&linkset->lock);
11465 }
11466 #endif
11467
11468 #if defined(HAVE_SS7)
11469 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct dahdi_ss7 *ss7, const char *number, const unsigned nai)
11470 {
11471 if (ast_strlen_zero(number)) {
11472 if (size) {
11473 *buf = '\0';
11474 }
11475 return;
11476 }
11477 switch (nai) {
11478 case SS7_NAI_INTERNATIONAL:
11479 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
11480 break;
11481 case SS7_NAI_NATIONAL:
11482 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
11483 break;
11484 case SS7_NAI_SUBSCRIBER:
11485 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
11486 break;
11487 case SS7_NAI_UNKNOWN:
11488 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
11489 break;
11490 default:
11491 snprintf(buf, size, "%s", number);
11492 break;
11493 }
11494 }
11495 #endif
11496
11497 #if defined(HAVE_SS7)
11498 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
11499 {
11500 return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
11501 }
11502 #endif
11503
11504 #if defined(HAVE_SS7)
11505 static void *ss7_linkset(void *data)
11506 {
11507 int res, i;
11508 struct timeval *next = NULL, tv;
11509 struct dahdi_ss7 *linkset = (struct dahdi_ss7 *) data;
11510 struct ss7 *ss7 = linkset->ss7;
11511 ss7_event *e = NULL;
11512 struct dahdi_pvt *p;
11513 int chanpos;
11514 struct pollfd pollers[NUM_DCHANS];
11515 int cic;
11516 unsigned int dpc;
11517 int nextms = 0;
11518
11519 ss7_start(ss7);
11520
11521 while(1) {
11522 ast_mutex_lock(&linkset->lock);
11523 if ((next = ss7_schedule_next(ss7))) {
11524 tv = ast_tvnow();
11525 tv.tv_sec = next->tv_sec - tv.tv_sec;
11526 tv.tv_usec = next->tv_usec - tv.tv_usec;
11527 if (tv.tv_usec < 0) {
11528 tv.tv_usec += 1000000;
11529 tv.tv_sec -= 1;
11530 }
11531 if (tv.tv_sec < 0) {
11532 tv.tv_sec = 0;
11533 tv.tv_usec = 0;
11534 }
11535 nextms = tv.tv_sec * 1000;
11536 nextms += tv.tv_usec / 1000;
11537 }
11538 ast_mutex_unlock(&linkset->lock);
11539
11540 for (i = 0; i < linkset->numsigchans; i++) {
11541 pollers[i].fd = linkset->fds[i];
11542 pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
11543 pollers[i].revents = 0;
11544 }
11545
11546 res = poll(pollers, linkset->numsigchans, nextms);
11547 if ((res < 0) && (errno != EINTR)) {
11548 ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
11549 } else if (!res) {
11550 ast_mutex_lock(&linkset->lock);
11551 ss7_schedule_run(ss7);
11552 ast_mutex_unlock(&linkset->lock);
11553 continue;
11554 }
11555
11556 ast_mutex_lock(&linkset->lock);
11557 for (i = 0; i < linkset->numsigchans; i++) {
11558 if (pollers[i].revents & POLLPRI) {
11559 int x;
11560 if (ioctl(pollers[i].fd, DAHDI_GETEVENT, &x)) {
11561 ast_log(LOG_ERROR, "Error in exception retrieval!\n");
11562 }
11563 switch (x) {
11564 case DAHDI_EVENT_OVERRUN:
11565 ast_debug(1, "Overrun detected!\n");
11566 break;
11567 case DAHDI_EVENT_BADFCS:
11568 ast_debug(1, "Bad FCS\n");
11569 break;
11570 case DAHDI_EVENT_ABORT:
11571 ast_debug(1, "HDLC Abort\n");
11572 break;
11573 case DAHDI_EVENT_ALARM:
11574 ast_log(LOG_ERROR, "Alarm on link!\n");
11575 linkset->linkstate[i] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
11576 linkset->linkstate[i] &= ~LINKSTATE_UP;
11577 ss7_link_alarm(ss7, pollers[i].fd);
11578 break;
11579 case DAHDI_EVENT_NOALARM:
11580 ast_log(LOG_ERROR, "Alarm cleared on link\n");
11581 linkset->linkstate[i] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
11582 linkset->linkstate[i] |= LINKSTATE_STARTING;
11583 ss7_link_noalarm(ss7, pollers[i].fd);
11584 break;
11585 default:
11586 ast_log(LOG_ERROR, "Got exception %d!\n", x);
11587 break;
11588 }
11589 }
11590
11591 if (pollers[i].revents & POLLIN) {
11592 res = ss7_read(ss7, pollers[i].fd);
11593 }
11594
11595 if (pollers[i].revents & POLLOUT) {
11596 res = ss7_write(ss7, pollers[i].fd);
11597 if (res < 0) {
11598 ast_debug(1, "Error in write %s\n", strerror(errno));
11599 }
11600 }
11601 }
11602
11603 while ((e = ss7_check_event(ss7))) {
11604 switch (e->e) {
11605 case SS7_EVENT_UP:
11606 if (linkset->state != LINKSET_STATE_UP) {
11607 ast_verbose("--- SS7 Up ---\n");
11608 ss7_reset_linkset(linkset);
11609 }
11610 linkset->state = LINKSET_STATE_UP;
11611 break;
11612 case SS7_EVENT_DOWN:
11613 ast_verbose("--- SS7 Down ---\n");
11614 linkset->state = LINKSET_STATE_DOWN;
11615 for (i = 0; i < linkset->numchans; i++) {
11616 struct dahdi_pvt *p = linkset->pvts[i];
11617 if (p)
11618 p->inalarm = 1;
11619 }
11620 break;
11621 case MTP2_LINK_UP:
11622 ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
11623 break;
11624 case MTP2_LINK_DOWN:
11625 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
11626 break;
11627 case ISUP_EVENT_CPG:
11628 chanpos = ss7_find_cic(linkset, e->cpg.cic, e->cpg.opc);
11629 if (chanpos < 0) {
11630 ast_log(LOG_WARNING, "CPG on unconfigured CIC %d\n", e->cpg.cic);
11631 break;
11632 }
11633 p = linkset->pvts[chanpos];
11634 ast_mutex_lock(&p->lock);
11635 switch (e->cpg.event) {
11636 case CPG_EVENT_ALERTING:
11637 p->alerting = 1;
11638 p->subs[SUB_REAL].needringing = 1;
11639 break;
11640 case CPG_EVENT_PROGRESS:
11641 case CPG_EVENT_INBANDINFO:
11642 {
11643 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
11644 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
11645 dahdi_queue_frame(p, &f, linkset);
11646 p->progress = 1;
11647 p->dialing = 0;
11648 if (p->dsp && p->dsp_features) {
11649 ast_dsp_set_features(p->dsp, p->dsp_features);
11650 p->dsp_features = 0;
11651 }
11652 }
11653 break;
11654 default:
11655 ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
11656 }
11657
11658 ast_mutex_unlock(&p->lock);
11659 break;
11660 case ISUP_EVENT_RSC:
11661 ast_verbose("Resetting CIC %d\n", e->rsc.cic);
11662 chanpos = ss7_find_cic(linkset, e->rsc.cic, e->rsc.opc);
11663 if (chanpos < 0) {
11664 ast_log(LOG_WARNING, "RSC on unconfigured CIC %d\n", e->rsc.cic);
11665 break;
11666 }
11667 p = linkset->pvts[chanpos];
11668 ast_mutex_lock(&p->lock);
11669 p->inservice = 1;
11670 p->remotelyblocked = 0;
11671 dpc = p->dpc;
11672 isup_set_call_dpc(e->rsc.call, dpc);
11673 if (p->ss7call)
11674 p->ss7call = NULL;
11675 if (p->owner)
11676 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
11677 ast_mutex_unlock(&p->lock);
11678 isup_rlc(ss7, e->rsc.call);
11679 break;
11680 case ISUP_EVENT_GRS:
11681 ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
11682 chanpos = ss7_find_cic(linkset, e->grs.startcic, e->grs.opc);
11683 if (chanpos < 0) {
11684 ast_log(LOG_WARNING, "GRS on unconfigured CIC %d\n", e->grs.startcic);
11685 break;
11686 }
11687 p = linkset->pvts[chanpos];
11688 isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
11689 ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
11690 ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
11691 break;
11692 case ISUP_EVENT_CQM:
11693 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
11694 ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
11695 break;
11696 case ISUP_EVENT_GRA:
11697 ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
11698 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
11699 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
11700 break;
11701 case ISUP_EVENT_IAM:
11702 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);
11703 chanpos = ss7_find_cic(linkset, e->iam.cic, e->iam.opc);
11704 if (chanpos < 0) {
11705 ast_log(LOG_WARNING, "IAM on unconfigured CIC %d\n", e->iam.cic);
11706 isup_rel(ss7, e->iam.call, -1);
11707 break;
11708 }
11709 p = linkset->pvts[chanpos];
11710 ast_mutex_lock(&p->lock);
11711 if (p->owner) {
11712 if (p->ss7call == e->iam.call) {
11713 ast_mutex_unlock(&p->lock);
11714 ast_log(LOG_WARNING, "Duplicate IAM requested on CIC %d\n", e->iam.cic);
11715 break;
11716 } else {
11717 ast_mutex_unlock(&p->lock);
11718 ast_log(LOG_WARNING, "Ring requested on CIC %d already in use!\n", e->iam.cic);
11719 break;
11720 }
11721 }
11722
11723 dpc = p->dpc;
11724 p->ss7call = e->iam.call;
11725 isup_set_call_dpc(p->ss7call, dpc);
11726
11727 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
11728 ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
11729 p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
11730 } else
11731 p->cid_num[0] = 0;
11732
11733 if (p->immediate) {
11734 p->exten[0] = 's';
11735 p->exten[1] = '\0';
11736 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
11737 char *st;
11738 ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
11739 st = strchr(p->exten, '#');
11740 if (st)
11741 *st = '\0';
11742 } else
11743 p->exten[0] = '\0';
11744
11745 p->cid_ani[0] = '\0';
11746 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
11747 ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
11748 else
11749 p->cid_name[0] = '\0';
11750
11751 p->cid_ani2 = e->iam.oli_ani2;
11752 p->cid_ton = 0;
11753 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
11754 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
11755 p->gen_add_type = e->iam.gen_add_type;
11756 p->gen_add_nai = e->iam.gen_add_nai;
11757 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
11758 p->gen_add_num_plan = e->iam.gen_add_num_plan;
11759 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
11760 p->gen_dig_type = e->iam.gen_dig_type;
11761 p->gen_dig_scheme = e->iam.gen_dig_scheme;
11762 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
11763 ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
11764 ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
11765 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
11766 p->calling_party_cat = e->iam.calling_party_cat;
11767
11768
11769 if (!ast_strlen_zero(e->iam.called_party_num))
11770 ss7_apply_plan_to_number(p->dnid, sizeof(p->dnid), linkset, e->iam.called_party_num, e->iam.called_nai);
11771
11772 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
11773
11774 if (e->iam.cot_check_required) {
11775 dahdi_loopback(p, 1);
11776 } else
11777 ss7_start_call(p, linkset);
11778 } else {
11779 ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
11780 p->alreadyhungup = 1;
11781 isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
11782 }
11783 ast_mutex_unlock(&p->lock);
11784 break;
11785 case ISUP_EVENT_COT:
11786 chanpos = ss7_find_cic(linkset, e->cot.cic, e->cot.opc);
11787 if (chanpos < 0) {
11788 ast_log(LOG_WARNING, "COT on unconfigured CIC %d\n", e->cot.cic);
11789 isup_rel(ss7, e->cot.call, -1);
11790 break;
11791 }
11792 p = linkset->pvts[chanpos];
11793
11794 ast_mutex_lock(&p->lock);
11795
11796 if (p->loopedback) {
11797 dahdi_loopback(p, 0);
11798 ss7_start_call(p, linkset);
11799 }
11800
11801 ast_mutex_unlock(&p->lock);
11802
11803 break;
11804 case ISUP_EVENT_CCR:
11805 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
11806 chanpos = ss7_find_cic(linkset, e->ccr.cic, e->ccr.opc);
11807 if (chanpos < 0) {
11808 ast_log(LOG_WARNING, "CCR on unconfigured CIC %d\n", e->ccr.cic);
11809 break;
11810 }
11811
11812 p = linkset->pvts[chanpos];
11813
11814 ast_mutex_lock(&p->lock);
11815 dahdi_loopback(p, 1);
11816 ast_mutex_unlock(&p->lock);
11817
11818 isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
11819 break;
11820 case ISUP_EVENT_CVT:
11821 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
11822 chanpos = ss7_find_cic(linkset, e->cvt.cic, e->cvt.opc);
11823 if (chanpos < 0) {
11824 ast_log(LOG_WARNING, "CVT on unconfigured CIC %d\n", e->cvt.cic);
11825 break;
11826 }
11827
11828 p = linkset->pvts[chanpos];
11829
11830 ast_mutex_lock(&p->lock);
11831 dahdi_loopback(p, 1);
11832 ast_mutex_unlock(&p->lock);
11833
11834 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
11835 break;
11836 case ISUP_EVENT_REL:
11837 chanpos = ss7_find_cic(linkset, e->rel.cic, e->rel.opc);
11838 if (chanpos < 0) {
11839 ast_log(LOG_WARNING, "REL on unconfigured CIC %d\n", e->rel.cic);
11840 break;
11841 }
11842 p = linkset->pvts[chanpos];
11843 ast_mutex_lock(&p->lock);
11844 if (p->owner) {
11845 p->owner->hangupcause = e->rel.cause;
11846 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
11847 } else if (!p->restartpending)
11848 ast_log(LOG_WARNING, "REL on channel (CIC %d) without owner!\n", p->cic);
11849
11850
11851 dahdi_loopback(p, 0);
11852
11853 isup_rlc(ss7, e->rel.call);
11854 p->ss7call = NULL;
11855
11856 ast_mutex_unlock(&p->lock);
11857 break;
11858 case ISUP_EVENT_ACM:
11859 chanpos = ss7_find_cic(linkset, e->acm.cic, e->acm.opc);
11860 if (chanpos < 0) {
11861 ast_log(LOG_WARNING, "ACM on unconfigured CIC %d\n", e->acm.cic);
11862 isup_rel(ss7, e->acm.call, -1);
11863 break;
11864 } else {
11865 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
11866
11867 p = linkset->pvts[chanpos];
11868
11869 ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
11870
11871 if (e->acm.call_ref_ident > 0) {
11872 p->rlt = 1;
11873 }
11874
11875 ast_mutex_lock(&p->lock);
11876 dahdi_queue_frame(p, &f, linkset);
11877 p->proceeding = 1;
11878 p->dialing = 0;
11879
11880 if (e->acm.called_party_status_ind == 1) {
11881 p->alerting = 1;
11882 p->subs[SUB_REAL].needringing = 1;
11883 }
11884 ast_mutex_unlock(&p->lock);
11885 }
11886 break;
11887 case ISUP_EVENT_CGB:
11888 chanpos = ss7_find_cic(linkset, e->cgb.startcic, e->cgb.opc);
11889 if (chanpos < 0) {
11890 ast_log(LOG_WARNING, "CGB on unconfigured CIC %d\n", e->cgb.startcic);
11891 break;
11892 }
11893 p = linkset->pvts[chanpos];
11894 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
11895 isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
11896 break;
11897 case ISUP_EVENT_CGU:
11898 chanpos = ss7_find_cic(linkset, e->cgu.startcic, e->cgu.opc);
11899 if (chanpos < 0) {
11900 ast_log(LOG_WARNING, "CGU on unconfigured CIC %d\n", e->cgu.startcic);
11901 break;
11902 }
11903 p = linkset->pvts[chanpos];
11904 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
11905 isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
11906 break;
11907 case ISUP_EVENT_UCIC:
11908 chanpos = ss7_find_cic(linkset, e->ucic.cic, e->ucic.opc);
11909 if (chanpos < 0) {
11910 ast_log(LOG_WARNING, "UCIC on unconfigured CIC %d\n", e->ucic.cic);
11911 break;
11912 }
11913 p = linkset->pvts[chanpos];
11914 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
11915 ast_mutex_lock(&p->lock);
11916 p->remotelyblocked = 1;
11917 p->inservice = 0;
11918 ast_mutex_unlock(&p->lock);
11919 break;
11920 case ISUP_EVENT_BLO:
11921 chanpos = ss7_find_cic(linkset, e->blo.cic, e->blo.opc);
11922 if (chanpos < 0) {
11923 ast_log(LOG_WARNING, "BLO on unconfigured CIC %d\n", e->blo.cic);
11924 break;
11925 }
11926 p = linkset->pvts[chanpos];
11927 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
11928 ast_mutex_lock(&p->lock);
11929 p->remotelyblocked = 1;
11930 ast_mutex_unlock(&p->lock);
11931 isup_bla(linkset->ss7, e->blo.cic, p->dpc);
11932 break;
11933 case ISUP_EVENT_BLA:
11934 chanpos = ss7_find_cic(linkset, e->bla.cic, e->bla.opc);
11935 if (chanpos < 0) {
11936 ast_log(LOG_WARNING, "BLA on unconfigured CIC %d\n", e->bla.cic);
11937 break;
11938 }
11939 ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
11940 p = linkset->pvts[chanpos];
11941 ast_mutex_lock(&p->lock);
11942 p->locallyblocked = 1;
11943 ast_mutex_unlock(&p->lock);
11944 break;
11945 case ISUP_EVENT_UBL:
11946 chanpos = ss7_find_cic(linkset, e->ubl.cic, e->ubl.opc);
11947 if (chanpos < 0) {
11948 ast_log(LOG_WARNING, "UBL on unconfigured CIC %d\n", e->ubl.cic);
11949 break;
11950 }
11951 p = linkset->pvts[chanpos];
11952 ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
11953 ast_mutex_lock(&p->lock);
11954 p->remotelyblocked = 0;
11955 ast_mutex_unlock(&p->lock);
11956 isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
11957 break;
11958 case ISUP_EVENT_UBA:
11959 chanpos = ss7_find_cic(linkset, e->uba.cic, e->uba.opc);
11960 if (chanpos < 0) {
11961 ast_log(LOG_WARNING, "UBA on unconfigured CIC %d\n", e->uba.cic);
11962 break;
11963 }
11964 p = linkset->pvts[chanpos];
11965 ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
11966 ast_mutex_lock(&p->lock);
11967 p->locallyblocked = 0;
11968 ast_mutex_unlock(&p->lock);
11969 break;
11970 case ISUP_EVENT_CON:
11971 case ISUP_EVENT_ANM:
11972 if (e->e == ISUP_EVENT_CON)
11973 cic = e->con.cic;
11974 else
11975 cic = e->anm.cic;
11976
11977 chanpos = ss7_find_cic(linkset, cic, (e->e == ISUP_EVENT_ANM) ? e->anm.opc : e->con.opc);
11978 if (chanpos < 0) {
11979 ast_log(LOG_WARNING, "ANM/CON on unconfigured CIC %d\n", cic);
11980 isup_rel(ss7, (e->e == ISUP_EVENT_ANM) ? e->anm.call : e->con.call, -1);
11981 break;
11982 } else {
11983 p = linkset->pvts[chanpos];
11984 ast_mutex_lock(&p->lock);
11985 p->subs[SUB_REAL].needanswer = 1;
11986 if (p->dsp && p->dsp_features) {
11987 ast_dsp_set_features(p->dsp, p->dsp_features);
11988 p->dsp_features = 0;
11989 }
11990 dahdi_enable_ec(p);
11991 ast_mutex_unlock(&p->lock);
11992 }
11993 break;
11994 case ISUP_EVENT_RLC:
11995 chanpos = ss7_find_cic(linkset, e->rlc.cic, e->rlc.opc);
11996 if (chanpos < 0) {
11997 ast_log(LOG_WARNING, "RLC on unconfigured CIC %d\n", e->rlc.cic);
11998 break;
11999 } else {
12000 p = linkset->pvts[chanpos];
12001 ast_mutex_lock(&p->lock);
12002 if (p->alreadyhungup)
12003 p->ss7call = NULL;
12004 else
12005 ast_log(LOG_NOTICE, "Received RLC out and we haven't sent REL. Ignoring.\n");
12006 ast_mutex_unlock(&p->lock);
12007 }
12008 break;
12009 case ISUP_EVENT_FAA:
12010 chanpos = ss7_find_cic(linkset, e->faa.cic, e->faa.opc);
12011 if (chanpos < 0) {
12012 ast_log(LOG_WARNING, "FAA on unconfigured CIC %d\n", e->faa.cic);
12013 break;
12014 } else {
12015 p = linkset->pvts[chanpos];
12016 ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
12017 ast_mutex_lock(&p->lock);
12018 if (p->alreadyhungup){
12019 p->ss7call = NULL;
12020 ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR. Ignoring.\n");
12021 }
12022 ast_mutex_unlock(&p->lock);
12023 }
12024 break;
12025 default:
12026 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
12027 break;
12028 }
12029 }
12030 ast_mutex_unlock(&linkset->lock);
12031 }
12032
12033 return 0;
12034 }
12035 #endif
12036
12037 #if defined(HAVE_SS7)
12038 static void dahdi_ss7_message(struct ss7 *ss7, char *s)
12039 {
12040 #if 0
12041 int i;
12042
12043 for (i = 0; i < NUM_SPANS; i++)
12044 if (linksets[i].ss7 == ss7)
12045 break;
12046
12047 ast_verbose("[%d] %s", i+1, s);
12048 #else
12049 ast_verbose("%s", s);
12050 #endif
12051 }
12052 #endif
12053
12054 #if defined(HAVE_SS7)
12055 static void dahdi_ss7_error(struct ss7 *ss7, char *s)
12056 {
12057 #if 0
12058 int i;
12059
12060 for (i = 0; i < NUM_SPANS; i++)
12061 if (linksets[i].ss7 == ss7)
12062 break;
12063
12064 #else
12065 ast_log(LOG_ERROR, "%s", s);
12066 #endif
12067 }
12068 #endif
12069
12070 #if defined(HAVE_OPENR2)
12071 static void *mfcr2_monitor(void *data)
12072 {
12073 struct dahdi_mfcr2 *mfcr2 = data;
12074
12075
12076
12077
12078
12079 struct pollfd pollers[sizeof(mfcr2->pvts)];
12080 int res = 0;
12081 int i = 0;
12082 int oldstate = 0;
12083 int quit_loop = 0;
12084 int maxsleep = 20;
12085 int was_idle = 0;
12086 int pollsize = 0;
12087
12088
12089 for (i = 0; i < mfcr2->numchans; i++) {
12090 openr2_chan_set_idle(mfcr2->pvts[i]->r2chan);
12091 openr2_chan_handle_cas(mfcr2->pvts[i]->r2chan);
12092 }
12093 while (1) {
12094
12095
12096 pollsize = 0;
12097 for (i = 0; i < mfcr2->numchans; i++) {
12098 pollers[i].revents = 0;
12099 pollers[i].events = 0;
12100 if (mfcr2->pvts[i]->owner) {
12101 continue;
12102 }
12103 if (!mfcr2->pvts[i]->r2chan) {
12104 ast_log(LOG_DEBUG, "Wow, no r2chan on channel %d\n", mfcr2->pvts[i]->channel);
12105 quit_loop = 1;
12106 break;
12107 }
12108 openr2_chan_enable_read(mfcr2->pvts[i]->r2chan);
12109 pollers[i].events = POLLIN | POLLPRI;
12110 pollers[i].fd = mfcr2->pvts[i]->subs[SUB_REAL].dfd;
12111 pollsize++;
12112 }
12113 if (quit_loop) {
12114 break;
12115 }
12116 if (pollsize == 0) {
12117 if (!was_idle) {
12118 ast_log(LOG_DEBUG, "Monitor thread going idle since everybody has an owner\n");
12119 was_idle = 1;
12120 }
12121 poll(NULL, 0, maxsleep);
12122 continue;
12123 }
12124 was_idle = 0;
12125
12126
12127 pthread_testcancel();
12128 res = poll(pollers, mfcr2->numchans, maxsleep);
12129 pthread_testcancel();
12130 if ((res < 0) && (errno != EINTR)) {
12131 ast_log(LOG_ERROR, "going out, poll failed: %s\n", strerror(errno));
12132 break;
12133 }
12134
12135 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
12136 for (i = 0; i < mfcr2->numchans; i++) {
12137 if (pollers[i].revents & POLLPRI || pollers[i].revents & POLLIN) {
12138 openr2_chan_process_event(mfcr2->pvts[i]->r2chan);
12139 }
12140 }
12141 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
12142 }
12143 ast_log(LOG_NOTICE, "Quitting MFC/R2 monitor thread\n");
12144 return 0;
12145 }
12146 #endif
12147
12148 #if defined(HAVE_PRI)
12149 static struct dahdi_pvt *pri_find_crv(struct dahdi_pri *pri, int crv)
12150 {
12151 struct dahdi_pvt *p;
12152 p = pri->crvs;
12153 while (p) {
12154 if (p->channel == crv)
12155 return p;
12156 p = p->next;
12157 }
12158 return NULL;
12159 }
12160 #endif
12161
12162 #if defined(HAVE_PRI)
12163 static int pri_find_principle(struct dahdi_pri *pri, int channel)
12164 {
12165 int x;
12166 int span = PRI_SPAN(channel);
12167 int spanfd;
12168 struct dahdi_params param;
12169 int principle = -1;
12170 int explicit = PRI_EXPLICIT(channel);
12171 channel = PRI_CHANNEL(channel);
12172
12173 if (!explicit) {
12174 spanfd = pri_active_dchan_fd(pri);
12175 memset(¶m, 0, sizeof(param));
12176 if (ioctl(spanfd, DAHDI_GET_PARAMS, ¶m))
12177 return -1;
12178 span = pris[param.spanno - 1].prilogicalspan;
12179 }
12180
12181 for (x = 0; x < pri->numchans; x++) {
12182 if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
12183 principle = x;
12184 break;
12185 }
12186 }
12187
12188 return principle;
12189 }
12190 #endif
12191
12192 #if defined(HAVE_PRI)
12193 static int pri_fixup_principle(struct dahdi_pri *pri, int principle, q931_call *c)
12194 {
12195 int x;
12196 struct dahdi_pvt *crv;
12197 if (!c) {
12198 if (principle < 0)
12199 return -1;
12200 return principle;
12201 }
12202 if ((principle > -1) &&
12203 (principle < pri->numchans) &&
12204 (pri->pvts[principle]) &&
12205 (pri->pvts[principle]->call == c))
12206 return principle;
12207
12208 for (x = 0; x < pri->numchans; x++) {
12209 if (!pri->pvts[x])
12210 continue;
12211 if (pri->pvts[x]->call == c) {
12212
12213 if (principle != x) {
12214 struct dahdi_pvt *new = pri->pvts[principle], *old = pri->pvts[x];
12215
12216 ast_verb(3, "Moving call from channel %d to channel %d\n",
12217 old->channel, new->channel);
12218 if (new->owner) {
12219 ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
12220 old->channel, new->channel, new->channel);
12221 return -1;
12222 }
12223
12224 new->owner = old->owner;
12225 old->owner = NULL;
12226 if (new->owner) {
12227 ast_string_field_build(new->owner, name,
12228 "DAHDI/%d:%d-%d", pri->trunkgroup,
12229 new->channel, 1);
12230 new->owner->tech_pvt = new;
12231 ast_channel_set_fd(new->owner, 0, new->subs[SUB_REAL].dfd);
12232 new->subs[SUB_REAL].owner = old->subs[SUB_REAL].owner;
12233 old->subs[SUB_REAL].owner = NULL;
12234 } else
12235 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);
12236 new->call = old->call;
12237 old->call = NULL;
12238
12239
12240 new->dsp = old->dsp;
12241 new->dsp_features = old->dsp_features;
12242 old->dsp = NULL;
12243 old->dsp_features = 0;
12244 }
12245 return principle;
12246 }
12247 }
12248
12249 crv = pri->crvs;
12250 while (crv) {
12251 if (crv->call == c) {
12252
12253 if (crv->bearer)
12254 ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n");
12255 else if (pri->pvts[principle]->owner)
12256 ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n");
12257 else {
12258
12259
12260 dahdi_close_sub(crv, SUB_REAL);
12261 pri->pvts[principle]->call = crv->call;
12262 pri_assign_bearer(crv, pri, pri->pvts[principle]);
12263 ast_debug(1, "Assigning bearer %d/%d to CRV %d:%d\n",
12264 pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset,
12265 pri->trunkgroup, crv->channel);
12266 wakeup_sub(crv, SUB_REAL, pri);
12267 }
12268 return principle;
12269 }
12270 crv = crv->next;
12271 }
12272 ast_log(LOG_WARNING, "Call specified, but not found?\n");
12273 return -1;
12274 }
12275 #endif
12276
12277 #if defined(HAVE_PRI)
12278 static void *do_idle_thread(void *vchan)
12279 {
12280 struct ast_channel *chan = vchan;
12281 struct dahdi_pvt *pvt = chan->tech_pvt;
12282 struct ast_frame *f;
12283 char ex[80];
12284
12285 int newms, ms = 30000;
12286 ast_verb(3, "Initiating idle call on channel %s\n", chan->name);
12287 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
12288 if (ast_call(chan, ex, 0)) {
12289 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
12290 ast_hangup(chan);
12291 return NULL;
12292 }
12293 while ((newms = ast_waitfor(chan, ms)) > 0) {
12294 f = ast_read(chan);
12295 if (!f) {
12296
12297 break;
12298 }
12299 if (f->frametype == AST_FRAME_CONTROL) {
12300 switch (f->subclass) {
12301 case AST_CONTROL_ANSWER:
12302
12303 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
12304 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
12305 chan->priority = 1;
12306 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
12307 ast_pbx_run(chan);
12308
12309 return NULL;
12310 case AST_CONTROL_BUSY:
12311 ast_verb(4, "Idle channel '%s' busy, waiting...\n", chan->name);
12312 break;
12313 case AST_CONTROL_CONGESTION:
12314 ast_verb(4, "Idle channel '%s' congested, waiting...\n", chan->name);
12315 break;
12316 };
12317 }
12318 ast_frfree(f);
12319 ms = newms;
12320 }
12321
12322 ast_hangup(chan);
12323 return NULL;
12324 }
12325 #endif
12326
12327 #if defined(HAVE_PRI)
12328 #ifndef PRI_RESTART
12329 #error "Upgrade your libpri"
12330 #endif
12331 static void dahdi_pri_message(struct pri *pri, char *s)
12332 {
12333 int x, y;
12334 int dchan = -1, span = -1;
12335 int dchancount = 0;
12336
12337 if (pri) {
12338 for (x = 0; x < NUM_SPANS; x++) {
12339 for (y = 0; y < NUM_DCHANS; y++) {
12340 if (pris[x].dchans[y])
12341 dchancount++;
12342
12343 if (pris[x].dchans[y] == pri)
12344 dchan = y;
12345 }
12346 if (dchan >= 0) {
12347 span = x;
12348 break;
12349 }
12350 dchancount = 0;
12351 }
12352 if (dchancount > 1 && (span > -1))
12353 ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s);
12354 else
12355 ast_verbose("%s", s);
12356 } else
12357 ast_verbose("%s", s);
12358
12359 ast_mutex_lock(&pridebugfdlock);
12360
12361 if (pridebugfd >= 0) {
12362 if (write(pridebugfd, s, strlen(s)) < 0) {
12363 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
12364 }
12365 }
12366
12367 ast_mutex_unlock(&pridebugfdlock);
12368 }
12369 #endif
12370
12371 #if defined(HAVE_PRI)
12372 static void dahdi_pri_error(struct pri *pri, char *s)
12373 {
12374 int x, y;
12375 int dchan = -1, span = -1;
12376 int dchancount = 0;
12377
12378 if (pri) {
12379 for (x = 0; x < NUM_SPANS; x++) {
12380 for (y = 0; y < NUM_DCHANS; y++) {
12381 if (pris[x].dchans[y])
12382 dchancount++;
12383
12384 if (pris[x].dchans[y] == pri)
12385 dchan = y;
12386 }
12387 if (dchan >= 0) {
12388 span = x;
12389 break;
12390 }
12391 dchancount = 0;
12392 }
12393 if ((dchancount > 1) && (span > -1))
12394 ast_log(LOG_ERROR, "[Span %d D-Channel %d] PRI: %s", span, dchan, s);
12395 else
12396 ast_log(LOG_ERROR, "%s", s);
12397 } else
12398 ast_log(LOG_ERROR, "%s", s);
12399
12400 ast_mutex_lock(&pridebugfdlock);
12401
12402 if (pridebugfd >= 0) {
12403 if (write(pridebugfd, s, strlen(s)) < 0) {
12404 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
12405 }
12406 }
12407
12408 ast_mutex_unlock(&pridebugfdlock);
12409 }
12410 #endif
12411
12412 #if defined(HAVE_PRI)
12413 static int pri_check_restart(struct dahdi_pri *pri)
12414 {
12415 do {
12416 pri->resetpos++;
12417 } while ((pri->resetpos < pri->numchans) &&
12418 (!pri->pvts[pri->resetpos] ||
12419 pri->pvts[pri->resetpos]->call ||
12420 pri->pvts[pri->resetpos]->resetting));
12421 if (pri->resetpos < pri->numchans) {
12422
12423 pri->pvts[pri->resetpos]->resetting = 1;
12424 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
12425 } else {
12426 pri->resetting = 0;
12427 time(&pri->lastreset);
12428 }
12429 return 0;
12430 }
12431 #endif
12432
12433 #if defined(HAVE_PRI)
12434 static int pri_hangup_all(struct dahdi_pvt *p, struct dahdi_pri *pri)
12435 {
12436 int x;
12437 int redo;
12438 ast_mutex_unlock(&pri->lock);
12439 ast_mutex_lock(&p->lock);
12440 do {
12441 redo = 0;
12442 for (x = 0; x < 3; x++) {
12443 while (p->subs[x].owner && ast_channel_trylock(p->subs[x].owner)) {
12444 redo++;
12445 DEADLOCK_AVOIDANCE(&p->lock);
12446 }
12447 if (p->subs[x].owner) {
12448 ast_queue_hangup_with_cause(p->subs[x].owner, AST_CAUSE_PRE_EMPTED);
12449 ast_channel_unlock(p->subs[x].owner);
12450 }
12451 }
12452 } while (redo);
12453 ast_mutex_unlock(&p->lock);
12454 ast_mutex_lock(&pri->lock);
12455 return 0;
12456 }
12457 #endif
12458
12459 #if defined(HAVE_PRI)
12460 static char * redirectingreason2str(int redirectingreason)
12461 {
12462 switch (redirectingreason) {
12463 case 0:
12464 return "UNKNOWN";
12465 case 1:
12466 return "BUSY";
12467 case 2:
12468 return "NO_REPLY";
12469 case 0xF:
12470 return "UNCONDITIONAL";
12471 default:
12472 return "NOREDIRECT";
12473 }
12474 }
12475 #endif
12476
12477 #if defined(HAVE_PRI)
12478 static void apply_plan_to_number(char *buf, size_t size, const struct dahdi_pri *pri, const char *number, const int plan)
12479 {
12480 if (pri->dialplan == -2) {
12481 snprintf(buf, size, "%s", number);
12482 return;
12483 }
12484 if (ast_strlen_zero(number)) {
12485 if (size) {
12486 *buf = '\0';
12487 }
12488 return;
12489 }
12490 switch (plan) {
12491 case PRI_INTERNATIONAL_ISDN:
12492 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
12493 break;
12494 case PRI_NATIONAL_ISDN:
12495 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
12496 break;
12497 case PRI_LOCAL_ISDN:
12498 snprintf(buf, size, "%s%s", pri->localprefix, number);
12499 break;
12500 case PRI_PRIVATE:
12501 snprintf(buf, size, "%s%s", pri->privateprefix, number);
12502 break;
12503 case PRI_UNKNOWN:
12504 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
12505 break;
12506 default:
12507 snprintf(buf, size, "%s", number);
12508 break;
12509 }
12510 }
12511 #endif
12512
12513 #if defined(HAVE_PRI)
12514 static void *pri_dchannel(void *vpri)
12515 {
12516 struct dahdi_pri *pri = vpri;
12517 pri_event *e;
12518 struct pollfd fds[NUM_DCHANS];
12519 int res;
12520 int chanpos = 0;
12521 int x;
12522 int haveidles;
12523 int activeidles;
12524 int nextidle = -1;
12525 struct ast_channel *c;
12526 struct timeval tv, lowest, *next;
12527 struct timeval lastidle = ast_tvnow();
12528 int doidling=0;
12529 char *cc;
12530 char idlen[80];
12531 struct ast_channel *idle;
12532 pthread_t p;
12533 time_t t;
12534 int i, which=-1;
12535 int numdchans;
12536 int cause=0;
12537 struct dahdi_pvt *crv;
12538 pthread_t threadid;
12539 char ani2str[6];
12540 char plancallingnum[256];
12541 char plancallingani[256];
12542 char calledtonstr[10];
12543
12544 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
12545
12546 gettimeofday(&lastidle, NULL);
12547 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
12548
12549 cc = strchr(pri->idleext, '@');
12550 if (cc) {
12551 *cc = '\0';
12552 cc++;
12553 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
12554 #if 0
12555
12556 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
12557 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
12558 else
12559 #endif
12560 doidling = 1;
12561 } else
12562 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
12563 }
12564 for (;;) {
12565 for (i = 0; i < NUM_DCHANS; i++) {
12566 if (!pri->dchannels[i])
12567 break;
12568 fds[i].fd = pri->fds[i];
12569 fds[i].events = POLLIN | POLLPRI;
12570 fds[i].revents = 0;
12571 }
12572 numdchans = i;
12573 time(&t);
12574 ast_mutex_lock(&pri->lock);
12575 if ((pri->switchtype != PRI_SWITCH_GR303_TMC) && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
12576 if (pri->resetting && pri_is_up(pri)) {
12577 if (pri->resetpos < 0)
12578 pri_check_restart(pri);
12579 } else {
12580 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
12581 pri->resetting = 1;
12582 pri->resetpos = -1;
12583 }
12584 }
12585 }
12586
12587 if (doidling && pri_is_up(pri)) {
12588 nextidle = -1;
12589 haveidles = 0;
12590 activeidles = 0;
12591 for (x = pri->numchans; x >= 0; x--) {
12592 if (pri->pvts[x] && !pri->pvts[x]->owner &&
12593 !pri->pvts[x]->call) {
12594 if (haveidles < pri->minunused) {
12595 haveidles++;
12596 } else if (!pri->pvts[x]->resetting) {
12597 nextidle = x;
12598 break;
12599 }
12600 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
12601 activeidles++;
12602 }
12603 if (nextidle > -1) {
12604 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
12605
12606 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
12607 idle = dahdi_request("DAHDI", AST_FORMAT_ULAW, idlen, &cause);
12608 if (idle) {
12609 pri->pvts[nextidle]->isidlecall = 1;
12610 if (ast_pthread_create_background(&p, NULL, do_idle_thread, idle)) {
12611 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
12612 dahdi_hangup(idle);
12613 }
12614 } else
12615 ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
12616 lastidle = ast_tvnow();
12617 }
12618 } else if ((haveidles < pri->minunused) &&
12619 (activeidles > pri->minidle)) {
12620
12621
12622 for (x = pri->numchans; x >= 0; x--) {
12623
12624 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
12625 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
12626 haveidles++;
12627
12628
12629 if ((haveidles >= pri->minunused) ||
12630 (activeidles <= pri->minidle))
12631 break;
12632 }
12633 }
12634 }
12635 }
12636
12637 lowest = ast_tv(60, 0);
12638 for (i = 0; i < NUM_DCHANS; i++) {
12639
12640 if (!pri->dchannels[i])
12641 break;
12642 if ((next = pri_schedule_next(pri->dchans[i]))) {
12643
12644 tv = ast_tvsub(*next, ast_tvnow());
12645 if (tv.tv_sec < 0) {
12646 tv = ast_tv(0,0);
12647 }
12648 if (doidling || pri->resetting) {
12649 if (tv.tv_sec > 1) {
12650 tv = ast_tv(1, 0);
12651 }
12652 } else {
12653 if (tv.tv_sec > 60) {
12654 tv = ast_tv(60, 0);
12655 }
12656 }
12657 } else if (doidling || pri->resetting) {
12658
12659
12660 tv = ast_tv(1,0);
12661 } else {
12662
12663 tv = ast_tv(60, 0);
12664 }
12665 if (!i || ast_tvcmp(tv, lowest) < 0) {
12666 lowest = tv;
12667 }
12668 }
12669 ast_mutex_unlock(&pri->lock);
12670
12671 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
12672 pthread_testcancel();
12673 e = NULL;
12674 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
12675 pthread_testcancel();
12676 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
12677
12678 ast_mutex_lock(&pri->lock);
12679 if (!res) {
12680 for (which = 0; which < NUM_DCHANS; which++) {
12681 if (!pri->dchans[which])
12682 break;
12683
12684 e = pri_schedule_run(pri->dchans[which]);
12685 if (e)
12686 break;
12687 }
12688 } else if (res > -1) {
12689 for (which = 0; which < NUM_DCHANS; which++) {
12690 if (!pri->dchans[which])
12691 break;
12692 if (fds[which].revents & POLLPRI) {
12693
12694 x = 0;
12695 res = ioctl(pri->fds[which], DAHDI_GETEVENT, &x);
12696 if (x) {
12697 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);
12698 manager_event(EVENT_FLAG_SYSTEM, "PRIEvent",
12699 "PRIEvent: %s\r\n"
12700 "PRIEventCode: %d\r\n"
12701 "D-channel: %s\r\n"
12702 "Span: %d\r\n",
12703 event2str(x),
12704 x,
12705 pri_order(which),
12706 pri->span
12707 );
12708 }
12709
12710 if (x == DAHDI_EVENT_ALARM) {
12711 pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
12712 pri_find_dchan(pri);
12713 } else if (x == DAHDI_EVENT_NOALARM) {
12714 pri->dchanavail[which] |= DCHAN_NOTINALARM;
12715 pri_restart(pri->dchans[which]);
12716 }
12717
12718 ast_debug(1, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
12719 } else if (fds[which].revents & POLLIN) {
12720 e = pri_check_event(pri->dchans[which]);
12721 }
12722 if (e)
12723 break;
12724 }
12725 } else if (errno != EINTR)
12726 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
12727
12728 if (e) {
12729 if (pri->debug)
12730 pri_dump_event(pri->dchans[which], e);
12731
12732 if (e->e != PRI_EVENT_DCHAN_DOWN) {
12733 if (!(pri->dchanavail[which] & DCHAN_UP)) {
12734 ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
12735 }
12736 pri->dchanavail[which] |= DCHAN_UP;
12737 } else if (pri->sig != SIG_BRI_PTMP) {
12738 if (pri->dchanavail[which] & DCHAN_UP) {
12739 ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
12740 }
12741 pri->dchanavail[which] &= ~DCHAN_UP;
12742 }
12743
12744 if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
12745
12746 pri->pri = pri->dchans[which];
12747
12748 switch (e->e) {
12749 case PRI_EVENT_DCHAN_UP:
12750 if (!pri->pri) pri_find_dchan(pri);
12751
12752
12753 time(&pri->lastreset);
12754
12755
12756 if (pri->resetinterval > -1) {
12757 pri->lastreset -= pri->resetinterval;
12758 pri->lastreset += 5;
12759 }
12760 pri->resetting = 0;
12761
12762 for (i = 0; i < pri->numchans; i++)
12763 if (pri->pvts[i]) {
12764 pri->pvts[i]->inalarm = 0;
12765 }
12766 break;
12767 case PRI_EVENT_DCHAN_DOWN:
12768 pri_find_dchan(pri);
12769 if (!pri_is_up(pri)) {
12770 pri->resetting = 0;
12771
12772 for (i = 0; i < pri->numchans; i++) {
12773 struct dahdi_pvt *p = pri->pvts[i];
12774 if (p) {
12775 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
12776
12777 if (p->call) {
12778 if (p->pri && p->pri->pri) {
12779 pri_hangup(p->pri->pri, p->call, -1);
12780 pri_destroycall(p->pri->pri, p->call);
12781 p->call = NULL;
12782 } else
12783 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
12784 }
12785 if (p->realcall) {
12786 pri_hangup_all(p->realcall, pri);
12787 } else if (p->owner)
12788 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
12789 }
12790
12791
12792 if (p->sig != SIG_BRI_PTMP) {
12793 p->inalarm = 1;
12794 }
12795 }
12796 }
12797 }
12798 break;
12799 case PRI_EVENT_RESTART:
12800 if (e->restart.channel > -1) {
12801 chanpos = pri_find_principle(pri, e->restart.channel);
12802 if (chanpos < 0)
12803 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
12804 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
12805 else {
12806 ast_verb(3, "B-channel %d/%d restarted on span %d\n",
12807 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
12808 ast_mutex_lock(&pri->pvts[chanpos]->lock);
12809 if (pri->pvts[chanpos]->call) {
12810 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
12811 pri->pvts[chanpos]->call = NULL;
12812 }
12813
12814 if (pri->pvts[chanpos]->realcall)
12815 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
12816 else if (pri->pvts[chanpos]->owner)
12817 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
12818 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
12819 }
12820 } else {
12821 ast_verb(3, "Restart on requested on entire span %d\n", pri->span);
12822 for (x = 0; x < pri->numchans; x++)
12823 if (pri->pvts[x]) {
12824 ast_mutex_lock(&pri->pvts[x]->lock);
12825 if (pri->pvts[x]->call) {
12826 pri_destroycall(pri->pri, pri->pvts[x]->call);
12827 pri->pvts[x]->call = NULL;
12828 }
12829 if (pri->pvts[x]->realcall)
12830 pri_hangup_all(pri->pvts[x]->realcall, pri);
12831 else if (pri->pvts[x]->owner)
12832 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
12833 ast_mutex_unlock(&pri->pvts[x]->lock);
12834 }
12835 }
12836 break;
12837 case PRI_EVENT_KEYPAD_DIGIT:
12838 chanpos = pri_find_principle(pri, e->digit.channel);
12839 if (chanpos < 0) {
12840 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
12841 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
12842 } else {
12843 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
12844 if (chanpos > -1) {
12845 ast_mutex_lock(&pri->pvts[chanpos]->lock);
12846
12847 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
12848
12849 int digitlen = strlen(e->digit.digits);
12850 char digit;
12851 int i;
12852 for (i = 0; i < digitlen; i++) {
12853 digit = e->digit.digits[i];
12854 {
12855 struct ast_frame f = { AST_FRAME_DTMF, digit, };
12856 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
12857 }
12858 }
12859 }
12860 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
12861 }
12862 }
12863 break;
12864
12865 case PRI_EVENT_INFO_RECEIVED:
12866 chanpos = pri_find_principle(pri, e->ring.channel);
12867 if (chanpos < 0) {
12868 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
12869 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
12870 } else {
12871 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
12872 if (chanpos > -1) {
12873 ast_mutex_lock(&pri->pvts[chanpos]->lock);
12874
12875 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
12876
12877 int digitlen = strlen(e->ring.callednum);
12878 char digit;
12879 int i;
12880 for (i = 0; i < digitlen; i++) {
12881 digit = e->ring.callednum[i];
12882 {
12883 struct ast_frame f = { AST_FRAME_DTMF, digit, };
12884 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
12885 }
12886 }
12887 }
12888 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
12889 }
12890 }
12891 break;
12892 case PRI_EVENT_RING:
12893 crv = NULL;
12894 if (e->ring.channel == -1)
12895 chanpos = pri_find_empty_chan(pri, 1);
12896 else
12897 chanpos = pri_find_principle(pri, e->ring.channel);
12898
12899 if (chanpos < 0) {
12900 ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
12901 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
12902 } else {
12903 ast_mutex_lock(&pri->pvts[chanpos]->lock);
12904 if (pri->pvts[chanpos]->owner) {
12905 if (pri->pvts[chanpos]->call == e->ring.call) {
12906 ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
12907 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
12908 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
12909 break;
12910 } else {
12911
12912 ast_debug(1, "Ring requested on channel %d/%d already in use or previously requested on span %d. Attempting to renegotiate channel.\n",
12913 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
12914 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
12915 chanpos = -1;
12916 }
12917 }
12918 if (chanpos > -1)
12919 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
12920 }
12921 if ((chanpos < 0) && (e->ring.flexible))
12922 chanpos = pri_find_empty_chan(pri, 1);
12923 if (chanpos > -1) {
12924 ast_mutex_lock(&pri->pvts[chanpos]->lock);
12925 if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
12926
12927 crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
12928 if (crv)
12929 ast_mutex_lock(&crv->lock);
12930 if (!crv || crv->owner) {
12931 pri->pvts[chanpos]->call = NULL;
12932 if (crv) {
12933 if (crv->owner)
12934 crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
12935 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);
12936 } else
12937 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);
12938 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
12939 if (crv)
12940 ast_mutex_unlock(&crv->lock);
12941 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
12942 break;
12943 }
12944 }
12945 pri->pvts[chanpos]->call = e->ring.call;
12946 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
12947 if (pri->pvts[chanpos]->use_callerid) {
12948 ast_shrink_phone_number(plancallingnum);
12949 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
12950 #ifdef PRI_ANI
12951 if (!ast_strlen_zero(e->ring.callingani)) {
12952 apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
12953 ast_shrink_phone_number(plancallingani);
12954 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
12955 } else {
12956 pri->pvts[chanpos]->cid_ani[0] = '\0';
12957 }
12958 #endif
12959 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
12960 pri->pvts[chanpos]->cid_ton = e->ring.callingplan;
12961 } else {
12962 pri->pvts[chanpos]->cid_num[0] = '\0';
12963 pri->pvts[chanpos]->cid_ani[0] = '\0';
12964 pri->pvts[chanpos]->cid_name[0] = '\0';
12965 pri->pvts[chanpos]->cid_ton = 0;
12966 }
12967 apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
12968 e->ring.redirectingnum, e->ring.callingplanrdnis);
12969
12970 if (pri->pvts[chanpos]->immediate) {
12971 ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
12972 pri->pvts[chanpos]->exten[0] = 's';
12973 pri->pvts[chanpos]->exten[1] = '\0';
12974 }
12975
12976 else if (!ast_strlen_zero(e->ring.callednum)) {
12977 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
12978 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
12979 } else if (pri->overlapdial)
12980 pri->pvts[chanpos]->exten[0] = '\0';
12981 else {
12982
12983 pri->pvts[chanpos]->exten[0] = 's';
12984 pri->pvts[chanpos]->exten[1] = '\0';
12985 }
12986
12987 if (!ast_strlen_zero(e->ring.callednum))
12988 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
12989
12990 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
12991 ast_verb(3, "Going to extension s|1 because of Complete received\n");
12992 pri->pvts[chanpos]->exten[0] = 's';
12993 pri->pvts[chanpos]->exten[1] = '\0';
12994 }
12995
12996
12997 if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
12998 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
12999
13000 int law;
13001 if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
13002
13003 law = 1;
13004 if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &law) == -1)
13005 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", pri->pvts[chanpos]->channel, law, strerror(errno));
13006 }
13007 if (e->ring.layer1 == PRI_LAYER_1_ALAW)
13008 law = DAHDI_LAW_ALAW;
13009 else
13010 law = DAHDI_LAW_MULAW;
13011 res = dahdi_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].dfd, law);
13012 if (res < 0)
13013 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
13014 res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].dfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
13015 if (res < 0)
13016 ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
13017 if (e->ring.complete || !(pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
13018
13019 pri->pvts[chanpos]->proceeding = 1;
13020 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
13021 } else {
13022 if (pri->switchtype != PRI_SWITCH_GR303_TMC)
13023 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
13024 else
13025 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
13026 }
13027
13028 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
13029
13030
13031 if (!e->ring.complete
13032 && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
13033 && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
13034
13035
13036
13037
13038 ast_mutex_unlock(&pri->lock);
13039 if (crv) {
13040
13041 pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
13042 c = dahdi_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
13043 pri->pvts[chanpos]->owner = &inuse;
13044 ast_debug(1, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
13045 } else {
13046 c = dahdi_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
13047 }
13048 ast_mutex_lock(&pri->lock);
13049 if (c) {
13050 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
13051 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
13052 }
13053 if (e->ring.ani2 >= 0) {
13054 snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
13055 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
13056 pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
13057 }
13058
13059 #ifdef SUPPORT_USERUSER
13060 if (!ast_strlen_zero(e->ring.useruserinfo)) {
13061 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
13062 }
13063 #endif
13064
13065 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
13066 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
13067 if (e->ring.redirectingreason >= 0)
13068 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
13069 }
13070 if (c && !ast_pthread_create_detached(&threadid, NULL, ss_thread, c)) {
13071 ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
13072 plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
13073 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
13074 } else {
13075 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
13076 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
13077 if (c)
13078 ast_hangup(c);
13079 else {
13080 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
13081 pri->pvts[chanpos]->call = NULL;
13082 }
13083 }
13084 } else {
13085
13086
13087
13088
13089 ast_mutex_unlock(&pri->lock);
13090 c = dahdi_new(pri->pvts[chanpos], AST_STATE_RING, 0, SUB_REAL, law, e->ring.ctype);
13091 ast_mutex_lock(&pri->lock);
13092 if (c) {
13093
13094
13095
13096
13097
13098
13099
13100
13101 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
13102 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
13103 }
13104 if (e->ring.ani2 >= 0) {
13105 snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
13106 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
13107 pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
13108 }
13109
13110 #ifdef SUPPORT_USERUSER
13111 if (!ast_strlen_zero(e->ring.useruserinfo)) {
13112 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
13113 }
13114 #endif
13115
13116 if (e->ring.redirectingreason >= 0)
13117 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
13118
13119 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
13120 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
13121 }
13122 if (c && !ast_pbx_start(c)) {
13123 ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
13124 plancallingnum, pri->pvts[chanpos]->exten,
13125 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
13126
13127 dahdi_enable_ec(pri->pvts[chanpos]);
13128 } else {
13129 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
13130 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
13131 if (c) {
13132 ast_hangup(c);
13133 } else {
13134 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
13135 pri->pvts[chanpos]->call = NULL;
13136 }
13137 }
13138 }
13139 } else {
13140 ast_verb(3, "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
13141 pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
13142 pri->pvts[chanpos]->prioffset, pri->span);
13143 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
13144 pri->pvts[chanpos]->call = NULL;
13145 pri->pvts[chanpos]->exten[0] = '\0';
13146 }
13147 if (crv)
13148 ast_mutex_unlock(&crv->lock);
13149 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13150 } else {
13151 if (e->ring.flexible)
13152 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
13153 else
13154 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
13155 }
13156 break;
13157 case PRI_EVENT_RINGING:
13158 chanpos = pri_find_principle(pri, e->ringing.channel);
13159 if (chanpos < 0) {
13160 ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
13161 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
13162 } else {
13163 chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
13164 if (chanpos < 0) {
13165 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
13166 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
13167 } else {
13168 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13169 if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
13170 dahdi_enable_ec(pri->pvts[chanpos]);
13171 pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1;
13172 pri->pvts[chanpos]->alerting = 1;
13173 } else
13174 ast_debug(1, "Deferring ringing notification because of extra digits to dial...\n");
13175
13176 if (
13177 #ifdef PRI_PROGRESS_MASK
13178 e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE
13179 #else
13180 e->ringing.progress == 8
13181 #endif
13182 ) {
13183
13184 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
13185
13186 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING);
13187 pri->pvts[chanpos]->dsp_features = 0;
13188 }
13189 }
13190
13191 #ifdef SUPPORT_USERUSER
13192 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
13193 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13194 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13195 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->ringing.useruserinfo);
13196 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13197 }
13198 #endif
13199
13200 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13201 }
13202 }
13203 break;
13204 case PRI_EVENT_PROGRESS:
13205
13206 chanpos = pri_find_principle(pri, e->proceeding.channel);
13207 if (chanpos > -1) {
13208 if ((!pri->pvts[chanpos]->progress)
13209 #ifdef PRI_PROGRESS_MASK
13210 || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
13211 #else
13212 || (e->proceeding.progress == 8)
13213 #endif
13214 ) {
13215 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
13216
13217 if (e->proceeding.cause > -1) {
13218 ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
13219
13220
13221 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
13222 if (pri->pvts[chanpos]->owner) {
13223 ast_verb(3, "PROGRESS with 'user busy' received, signalling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
13224
13225 pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
13226 f.subclass = AST_CONTROL_BUSY;
13227 }
13228 }
13229 }
13230
13231 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13232 ast_debug(1, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
13233 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
13234 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13235 if (
13236 #ifdef PRI_PROGRESS_MASK
13237 e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
13238 #else
13239 e->proceeding.progress == 8
13240 #endif
13241 ) {
13242
13243 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
13244 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
13245 pri->pvts[chanpos]->dsp_features = 0;
13246 }
13247
13248 f.subclass = AST_CONTROL_PROGRESS;
13249 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13250 }
13251 pri->pvts[chanpos]->progress = 1;
13252 pri->pvts[chanpos]->dialing = 0;
13253 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13254 }
13255 }
13256 break;
13257 case PRI_EVENT_PROCEEDING:
13258 chanpos = pri_find_principle(pri, e->proceeding.channel);
13259 if (chanpos > -1) {
13260 if (!pri->pvts[chanpos]->proceeding) {
13261 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
13262
13263 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13264 ast_debug(1, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
13265 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
13266 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13267 if (
13268 #ifdef PRI_PROGRESS_MASK
13269 e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
13270 #else
13271 e->proceeding.progress == 8
13272 #endif
13273 ) {
13274
13275 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
13276 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
13277 pri->pvts[chanpos]->dsp_features = 0;
13278 }
13279
13280 f.subclass = AST_CONTROL_PROGRESS;
13281 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13282 }
13283 pri->pvts[chanpos]->proceeding = 1;
13284 pri->pvts[chanpos]->dialing = 0;
13285 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13286 }
13287 }
13288 break;
13289 case PRI_EVENT_FACNAME:
13290 chanpos = pri_find_principle(pri, e->facname.channel);
13291 if (chanpos < 0) {
13292 ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
13293 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
13294 } else {
13295 chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
13296 if (chanpos < 0) {
13297 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
13298 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
13299 } else {
13300
13301 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13302 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
13303 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
13304 pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
13305 dahdi_enable_ec(pri->pvts[chanpos]);
13306 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13307 }
13308 }
13309 break;
13310 case PRI_EVENT_ANSWER:
13311 chanpos = pri_find_principle(pri, e->answer.channel);
13312 if (chanpos < 0) {
13313 ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
13314 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
13315 } else {
13316 chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
13317 if (chanpos < 0) {
13318 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
13319 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
13320 } else {
13321 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13322
13323
13324
13325
13326
13327 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
13328 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
13329 pri->pvts[chanpos]->dsp_features = 0;
13330 }
13331 if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) {
13332 ast_debug(1, "Starting up GR-303 trunk now that we got CONNECT...\n");
13333 x = DAHDI_START;
13334 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
13335 if (res < 0) {
13336 if (errno != EINPROGRESS) {
13337 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
13338 }
13339 }
13340 } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
13341 pri->pvts[chanpos]->dialing = 1;
13342
13343 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_DIAL, &pri->pvts[chanpos]->dop);
13344 if (res < 0) {
13345 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", pri->pvts[chanpos]->channel, strerror(errno));
13346 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
13347 } else
13348 ast_debug(1, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
13349
13350 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
13351 } else if (pri->pvts[chanpos]->confirmanswer) {
13352 ast_debug(1, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
13353 } else {
13354 pri->pvts[chanpos]->dialing = 0;
13355 pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
13356
13357 dahdi_enable_ec(pri->pvts[chanpos]);
13358 }
13359
13360 #ifdef SUPPORT_USERUSER
13361 if (!ast_strlen_zero(e->answer.useruserinfo)) {
13362 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13363 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13364 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->answer.useruserinfo);
13365 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13366 }
13367 #endif
13368
13369 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13370 }
13371 }
13372 break;
13373 case PRI_EVENT_HANGUP:
13374 chanpos = pri_find_principle(pri, e->hangup.channel);
13375 if (chanpos < 0) {
13376 ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
13377 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13378 } else {
13379 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
13380 if (chanpos > -1) {
13381 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13382 if (!pri->pvts[chanpos]->alreadyhungup) {
13383
13384 pri->pvts[chanpos]->alreadyhungup = 1;
13385 if (pri->pvts[chanpos]->realcall)
13386 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13387 else if (pri->pvts[chanpos]->owner) {
13388
13389 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
13390 switch (pri->pvts[chanpos]->owner->_state) {
13391 case AST_STATE_BUSY:
13392 case AST_STATE_UP:
13393 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13394 break;
13395 default:
13396 switch (e->hangup.cause) {
13397 case PRI_CAUSE_USER_BUSY:
13398 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
13399 break;
13400 case PRI_CAUSE_CALL_REJECTED:
13401 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
13402 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
13403 case PRI_CAUSE_SWITCH_CONGESTION:
13404 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
13405 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
13406 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
13407 break;
13408 default:
13409 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13410 }
13411 break;
13412 }
13413 }
13414 ast_verb(3, "Channel %d/%d, span %d got hangup, cause %d\n",
13415 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
13416 } else {
13417 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
13418 pri->pvts[chanpos]->call = NULL;
13419 }
13420 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
13421 ast_verb(3, "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
13422 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13423 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
13424 pri->pvts[chanpos]->resetting = 1;
13425 }
13426 if (e->hangup.aoc_units > -1)
13427 ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
13428 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
13429
13430 #ifdef SUPPORT_USERUSER
13431 if (pri->pvts[chanpos]->owner && !ast_strlen_zero(e->hangup.useruserinfo)) {
13432 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13433 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13434 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
13435 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13436 }
13437 #endif
13438
13439 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13440 } else {
13441 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
13442 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13443 }
13444 }
13445 break;
13446 #ifndef PRI_EVENT_HANGUP_REQ
13447 #error please update libpri
13448 #endif
13449 case PRI_EVENT_HANGUP_REQ:
13450 chanpos = pri_find_principle(pri, e->hangup.channel);
13451 if (chanpos < 0) {
13452 ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
13453 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13454 } else {
13455 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
13456 if (chanpos > -1) {
13457 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13458 if (pri->pvts[chanpos]->realcall)
13459 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13460 else if (pri->pvts[chanpos]->owner) {
13461 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
13462 switch (pri->pvts[chanpos]->owner->_state) {
13463 case AST_STATE_BUSY:
13464 case AST_STATE_UP:
13465 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13466 break;
13467 default:
13468 switch (e->hangup.cause) {
13469 case PRI_CAUSE_USER_BUSY:
13470 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
13471 break;
13472 case PRI_CAUSE_CALL_REJECTED:
13473 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
13474 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
13475 case PRI_CAUSE_SWITCH_CONGESTION:
13476 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
13477 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
13478 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
13479 break;
13480 default:
13481 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13482 break;
13483 }
13484 break;
13485 }
13486 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);
13487 if (e->hangup.aoc_units > -1)
13488 ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
13489 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
13490 } else {
13491 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
13492 pri->pvts[chanpos]->call = NULL;
13493 }
13494 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
13495 ast_verb(3, "Forcing restart of channel %d/%d span %d since channel reported in use\n",
13496 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13497 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
13498 pri->pvts[chanpos]->resetting = 1;
13499 }
13500
13501 #ifdef SUPPORT_USERUSER
13502 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
13503 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13504 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13505 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
13506 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13507 }
13508 #endif
13509
13510 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13511 } else {
13512 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);
13513 }
13514 }
13515 break;
13516 case PRI_EVENT_HANGUP_ACK:
13517 chanpos = pri_find_principle(pri, e->hangup.channel);
13518 if (chanpos < 0) {
13519 ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
13520 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13521 } else {
13522 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
13523 if (chanpos > -1) {
13524 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13525 pri->pvts[chanpos]->call = NULL;
13526 pri->pvts[chanpos]->resetting = 0;
13527 if (pri->pvts[chanpos]->owner) {
13528 ast_verb(3, "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13529 }
13530
13531 #ifdef SUPPORT_USERUSER
13532 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
13533 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13534 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13535 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
13536 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13537 }
13538 #endif
13539
13540 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13541 }
13542 }
13543 break;
13544 case PRI_EVENT_CONFIG_ERR:
13545 ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->trunkgroup, e->err.err);
13546 break;
13547 case PRI_EVENT_RESTART_ACK:
13548 chanpos = pri_find_principle(pri, e->restartack.channel);
13549 if (chanpos < 0) {
13550
13551
13552
13553 for (x = 0; x < pri->numchans; x++) {
13554 if (pri->pvts[x] && pri->pvts[x]->resetting) {
13555 chanpos = x;
13556 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13557 ast_debug(1, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
13558 pri->pvts[chanpos]->prioffset, pri->span);
13559 if (pri->pvts[chanpos]->realcall)
13560 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13561 else if (pri->pvts[chanpos]->owner) {
13562 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
13563 pri->pvts[chanpos]->prioffset, pri->span);
13564 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13565 }
13566 pri->pvts[chanpos]->resetting = 0;
13567 ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
13568 pri->pvts[chanpos]->prioffset, pri->span);
13569 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13570 if (pri->resetting)
13571 pri_check_restart(pri);
13572 break;
13573 }
13574 }
13575 if (chanpos < 0) {
13576 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
13577 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
13578 }
13579 } else {
13580 if (pri->pvts[chanpos]) {
13581 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13582 if (pri->pvts[chanpos]->realcall)
13583 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13584 else if (pri->pvts[chanpos]->owner) {
13585 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
13586 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
13587 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13588 }
13589 pri->pvts[chanpos]->resetting = 0;
13590 pri->pvts[chanpos]->inservice = 1;
13591 ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
13592 pri->pvts[chanpos]->prioffset, pri->span);
13593 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13594 if (pri->resetting)
13595 pri_check_restart(pri);
13596 }
13597 }
13598 break;
13599 case PRI_EVENT_SETUP_ACK:
13600 chanpos = pri_find_principle(pri, e->setup_ack.channel);
13601 if (chanpos < 0) {
13602 ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
13603 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
13604 } else {
13605 chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
13606 if (chanpos > -1) {
13607 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13608 pri->pvts[chanpos]->setup_ack = 1;
13609
13610 for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
13611 ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
13612 pri_information(pri->pri, pri->pvts[chanpos]->call,
13613 pri->pvts[chanpos]->dialdest[x]);
13614 }
13615 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13616 } else
13617 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
13618 }
13619 break;
13620 case PRI_EVENT_NOTIFY:
13621 chanpos = pri_find_principle(pri, e->notify.channel);
13622 if (chanpos < 0) {
13623 ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
13624 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
13625 } else if (!pri->discardremoteholdretrieval) {
13626 struct ast_frame f = { AST_FRAME_CONTROL, };
13627 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13628 switch (e->notify.info) {
13629 case PRI_NOTIFY_REMOTE_HOLD:
13630 f.subclass = AST_CONTROL_HOLD;
13631 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13632 break;
13633 case PRI_NOTIFY_REMOTE_RETRIEVAL:
13634 f.subclass = AST_CONTROL_UNHOLD;
13635 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13636 break;
13637 }
13638 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13639 }
13640 break;
13641 default:
13642 ast_debug(1, "Event: %d\n", e->e);
13643 }
13644 }
13645 ast_mutex_unlock(&pri->lock);
13646 }
13647
13648 return NULL;
13649 }
13650 #endif
13651
13652 #if defined(HAVE_PRI)
13653 static int start_pri(struct dahdi_pri *pri)
13654 {
13655 int res, x;
13656 struct dahdi_params p;
13657 struct dahdi_bufferinfo bi;
13658 struct dahdi_spaninfo si;
13659 int i;
13660
13661 for (i = 0; i < NUM_DCHANS; i++) {
13662 if (!pri->dchannels[i])
13663 break;
13664 pri->fds[i] = open("/dev/dahdi/channel", O_RDWR);
13665 x = pri->dchannels[i];
13666 if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],DAHDI_SPECIFY,&x) == -1)) {
13667 ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
13668 return -1;
13669 }
13670 memset(&p, 0, sizeof(p));
13671 res = ioctl(pri->fds[i], DAHDI_GET_PARAMS, &p);
13672 if (res) {
13673 dahdi_close_pri_fd(pri, i);
13674 ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
13675 return -1;
13676 }
13677 if ((p.sigtype != DAHDI_SIG_HDLCFCS) && (p.sigtype != DAHDI_SIG_HARDHDLC)) {
13678 dahdi_close_pri_fd(pri, i);
13679 ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode.\n", x);
13680 return -1;
13681 }
13682 memset(&si, 0, sizeof(si));
13683 res = ioctl(pri->fds[i], DAHDI_SPANSTAT, &si);
13684 if (res) {
13685 dahdi_close_pri_fd(pri, i);
13686 ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
13687 }
13688 if (!si.alarms)
13689 pri->dchanavail[i] |= DCHAN_NOTINALARM;
13690 else
13691 pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
13692 memset(&bi, 0, sizeof(bi));
13693 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
13694 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
13695 bi.numbufs = 32;
13696 bi.bufsize = 1024;
13697 if (ioctl(pri->fds[i], DAHDI_SET_BUFINFO, &bi)) {
13698 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d: %s\n", x, strerror(errno));
13699 dahdi_close_pri_fd(pri, i);
13700 return -1;
13701 }
13702 switch (pri->sig) {
13703 case SIG_BRI:
13704 pri->dchans[i] = pri_new_bri(pri->fds[i], 1, pri->nodetype, pri->switchtype);
13705 break;
13706 case SIG_BRI_PTMP:
13707 pri->dchans[i] = pri_new_bri(pri->fds[i], 0, pri->nodetype, pri->switchtype);
13708 break;
13709 default:
13710 pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
13711 break;
13712 }
13713
13714 if (pri->switchtype == PRI_SWITCH_GR303_TMC)
13715 pri->overlapdial |= DAHDI_OVERLAPDIAL_BOTH;
13716 pri_set_overlapdial(pri->dchans[i],(pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING)?1:0);
13717 #ifdef HAVE_PRI_PROG_W_CAUSE
13718 pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
13719 #endif
13720 #ifdef HAVE_PRI_INBANDDISCONNECT
13721 pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
13722 #endif
13723
13724 if (i)
13725 pri_enslave(pri->dchans[0], pri->dchans[i]);
13726 if (!pri->dchans[i]) {
13727 dahdi_close_pri_fd(pri, i);
13728 ast_log(LOG_ERROR, "Unable to create PRI structure\n");
13729 return -1;
13730 }
13731 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
13732 pri_set_nsf(pri->dchans[i], pri->nsf);
13733 #ifdef PRI_GETSET_TIMERS
13734 for (x = 0; x < PRI_MAX_TIMERS; x++) {
13735 if (pritimers[x] != 0)
13736 pri_set_timer(pri->dchans[i], x, pritimers[x]);
13737 }
13738 #endif
13739 }
13740
13741 pri->pri = pri->dchans[0];
13742 pri->resetpos = -1;
13743 if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
13744 for (i = 0; i < NUM_DCHANS; i++) {
13745 if (!pri->dchannels[i])
13746 break;
13747 dahdi_close_pri_fd(pri, i);
13748 }
13749 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
13750 return -1;
13751 }
13752 return 0;
13753 }
13754 #endif
13755
13756 #if defined(HAVE_PRI)
13757 static char *complete_span_helper(const char *line, const char *word, int pos, int state, int rpos)
13758 {
13759 int which, span;
13760 char *ret = NULL;
13761
13762 if (pos != rpos)
13763 return ret;
13764
13765 for (which = span = 0; span < NUM_SPANS; span++) {
13766 if (pris[span].pri && ++which > state) {
13767 if (asprintf(&ret, "%d", span + 1) < 0) {
13768 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
13769 }
13770 break;
13771 }
13772 }
13773 return ret;
13774 }
13775 #endif
13776
13777 #if defined(HAVE_PRI)
13778 static char *complete_span_4(const char *line, const char *word, int pos, int state)
13779 {
13780 return complete_span_helper(line,word,pos,state,3);
13781 }
13782 #endif
13783
13784 #if defined(HAVE_PRI)
13785 static char *handle_pri_set_debug_file(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13786 {
13787 int myfd;
13788 switch (cmd) {
13789 case CLI_INIT:
13790 e->command = "pri set debug file";
13791 e->usage = "Usage: pri set debug file [output-file]\n"
13792 " Sends PRI debug output to the specified output file\n";
13793 return NULL;
13794 case CLI_GENERATE:
13795 return NULL;
13796 }
13797 if (a->argc < 5)
13798 return CLI_SHOWUSAGE;
13799
13800 if (ast_strlen_zero(a->argv[4]))
13801 return CLI_SHOWUSAGE;
13802
13803 myfd = open(a->argv[4], O_CREAT|O_WRONLY, AST_FILE_MODE);
13804 if (myfd < 0) {
13805 ast_cli(a->fd, "Unable to open '%s' for writing\n", a->argv[4]);
13806 return CLI_SUCCESS;
13807 }
13808
13809 ast_mutex_lock(&pridebugfdlock);
13810
13811 if (pridebugfd >= 0)
13812 close(pridebugfd);
13813
13814 pridebugfd = myfd;
13815 ast_copy_string(pridebugfilename,a->argv[4],sizeof(pridebugfilename));
13816 ast_mutex_unlock(&pridebugfdlock);
13817 ast_cli(a->fd, "PRI debug output will be sent to '%s'\n", a->argv[4]);
13818 return CLI_SUCCESS;
13819 }
13820 #endif
13821
13822 #if defined(HAVE_PRI)
13823 static char *handle_pri_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13824 {
13825 int span;
13826 int x;
13827 int level = 0;
13828 switch (cmd) {
13829 case CLI_INIT:
13830 e->command = "pri set debug {on|off|0|1|2} span";
13831 e->usage =
13832 "Usage: pri set debug {<level>|on|off} span <span>\n"
13833 " Enables debugging on a given PRI span\n";
13834 return NULL;
13835 case CLI_GENERATE:
13836 return complete_span_4(a->line, a->word, a->pos, a->n);
13837 }
13838 if (a->argc < 6) {
13839 return CLI_SHOWUSAGE;
13840 }
13841
13842 if (!strcasecmp(a->argv[3], "on")) {
13843 level = 1;
13844 } else if (!strcasecmp(a->argv[3], "off")) {
13845 level = 0;
13846 } else {
13847 level = atoi(a->argv[3]);
13848 }
13849 span = atoi(a->argv[5]);
13850 if ((span < 1) || (span > NUM_SPANS)) {
13851 ast_cli(a->fd, "Invalid span %s. Should be a number %d to %d\n", a->argv[5], 1, NUM_SPANS);
13852 return CLI_SUCCESS;
13853 }
13854 if (!pris[span-1].pri) {
13855 ast_cli(a->fd, "No PRI running on span %d\n", span);
13856 return CLI_SUCCESS;
13857 }
13858 for (x = 0; x < NUM_DCHANS; x++) {
13859 if (pris[span-1].dchans[x]) {
13860 if (level == 1) {
13861 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU |
13862 PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE |
13863 PRI_DEBUG_Q921_STATE);
13864 ast_cli(a->fd, "Enabled debugging on span %d\n", span);
13865 } else if (level == 0) {
13866 pri_set_debug(pris[span-1].dchans[x], 0);
13867
13868 ast_mutex_lock(&pridebugfdlock);
13869 close(pridebugfd);
13870 pridebugfd = -1;
13871 ast_cli(a->fd, "PRI debug output to file disabled\n");
13872 ast_mutex_unlock(&pridebugfdlock);
13873 } else {
13874 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU |
13875 PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE |
13876 PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_STATE);
13877 ast_cli(a->fd, "Enabled debugging on span %d\n", span);
13878 }
13879 }
13880 }
13881 return CLI_SUCCESS;
13882 }
13883 #endif
13884
13885 #if defined(HAVE_PRI)
13886 static void build_status(char *s, size_t len, int status, int active)
13887 {
13888 if (!s || len < 1) {
13889 return;
13890 }
13891 s[0] = '\0';
13892 if (status & DCHAN_PROVISIONED)
13893 strncat(s, "Provisioned, ", len - strlen(s) - 1);
13894 if (!(status & DCHAN_NOTINALARM))
13895 strncat(s, "In Alarm, ", len - strlen(s) - 1);
13896 if (status & DCHAN_UP)
13897 strncat(s, "Up", len - strlen(s) - 1);
13898 else
13899 strncat(s, "Down", len - strlen(s) - 1);
13900 if (active)
13901 strncat(s, ", Active", len - strlen(s) - 1);
13902 else
13903 strncat(s, ", Standby", len - strlen(s) - 1);
13904 s[len - 1] = '\0';
13905 }
13906 #endif
13907
13908 #if defined(HAVE_PRI)
13909 static char *handle_pri_show_spans(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13910 {
13911 int span;
13912 int x;
13913 char status[256];
13914
13915 switch (cmd) {
13916 case CLI_INIT:
13917 e->command = "pri show spans";
13918 e->usage =
13919 "Usage: pri show spans\n"
13920 " Displays PRI Information\n";
13921 return NULL;
13922 case CLI_GENERATE:
13923 return NULL;
13924 }
13925
13926 if (a->argc != 3)
13927 return CLI_SHOWUSAGE;
13928
13929 for (span = 0; span < NUM_SPANS; span++) {
13930 if (pris[span].pri) {
13931 for (x = 0; x < NUM_DCHANS; x++) {
13932 if (pris[span].dchannels[x]) {
13933 build_status(status, sizeof(status), pris[span].dchanavail[x], pris[span].dchans[x] == pris[span].pri);
13934 ast_cli(a->fd, "PRI span %d/%d: %s\n", span + 1, x, status);
13935 }
13936 }
13937 }
13938 }
13939 return CLI_SUCCESS;
13940 }
13941 #endif
13942
13943 #if defined(HAVE_PRI)
13944 static char *handle_pri_show_span(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13945 {
13946 int span;
13947 int x;
13948 char status[256];
13949 switch (cmd) {
13950 case CLI_INIT:
13951 e->command = "pri show span";
13952 e->usage =
13953 "Usage: pri show span <span>\n"
13954 " Displays PRI Information on a given PRI span\n";
13955 return NULL;
13956 case CLI_GENERATE:
13957 return complete_span_4(a->line, a->word, a->pos, a->n);
13958 }
13959
13960 if (a->argc < 4)
13961 return CLI_SHOWUSAGE;
13962 span = atoi(a->argv[3]);
13963 if ((span < 1) || (span > NUM_SPANS)) {
13964 ast_cli(a->fd, "Invalid span '%s'. Should be a number from %d to %d\n", a->argv[3], 1, NUM_SPANS);
13965 return CLI_SUCCESS;
13966 }
13967 if (!pris[span-1].pri) {
13968 ast_cli(a->fd, "No PRI running on span %d\n", span);
13969 return CLI_SUCCESS;
13970 }
13971 for (x = 0; x < NUM_DCHANS; x++) {
13972 if (pris[span-1].dchannels[x]) {
13973 #ifdef PRI_DUMP_INFO_STR
13974 char *info_str = NULL;
13975 #endif
13976 ast_cli(a->fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
13977 build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
13978 ast_cli(a->fd, "Status: %s\n", status);
13979 #ifdef PRI_DUMP_INFO_STR
13980 info_str = pri_dump_info_str(pris[span-1].pri);
13981 if (info_str) {
13982 ast_cli(a->fd, "%s", info_str);
13983 ast_free(info_str);
13984 }
13985 #else
13986 pri_dump_info(pris[span-1].pri);
13987 #endif
13988 ast_cli(a->fd, "Overlap Recv: %s\n\n", (pris[span-1].overlapdial & DAHDI_OVERLAPDIAL_INCOMING)?"Yes":"No");
13989 }
13990 }
13991 return CLI_SUCCESS;
13992 }
13993 #endif
13994
13995 #if defined(HAVE_PRI)
13996 static char *handle_pri_show_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13997 {
13998 int x;
13999 int span;
14000 int count=0;
14001 int debug=0;
14002
14003 switch (cmd) {
14004 case CLI_INIT:
14005 e->command = "pri show debug";
14006 e->usage =
14007 "Usage: pri show debug\n"
14008 " Show the debug state of pri spans\n";
14009 return NULL;
14010 case CLI_GENERATE:
14011 return NULL;
14012 }
14013
14014 for (span = 0; span < NUM_SPANS; span++) {
14015 if (pris[span].pri) {
14016 for (x = 0; x < NUM_DCHANS; x++) {
14017 debug = 0;
14018 if (pris[span].dchans[x]) {
14019 debug = pri_get_debug(pris[span].dchans[x]);
14020 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" );
14021 count++;
14022 }
14023 }
14024 }
14025
14026 }
14027 ast_mutex_lock(&pridebugfdlock);
14028 if (pridebugfd >= 0)
14029 ast_cli(a->fd, "Logging PRI debug to file %s\n", pridebugfilename);
14030 ast_mutex_unlock(&pridebugfdlock);
14031
14032 if (!count)
14033 ast_cli(a->fd, "No debug set or no PRI running\n");
14034 return CLI_SUCCESS;
14035 }
14036 #endif
14037
14038 #if defined(HAVE_PRI)
14039 static char *handle_pri_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14040 {
14041 switch (cmd) {
14042 case CLI_INIT:
14043 e->command = "pri show version";
14044 e->usage =
14045 "Usage: pri show version\n"
14046 "Show libpri version information\n";
14047 return NULL;
14048 case CLI_GENERATE:
14049 return NULL;
14050 }
14051
14052 ast_cli(a->fd, "libpri version: %s\n", pri_get_version());
14053
14054 return CLI_SUCCESS;
14055 }
14056 #endif
14057
14058 #if defined(HAVE_PRI)
14059 static struct ast_cli_entry dahdi_pri_cli[] = {
14060 AST_CLI_DEFINE(handle_pri_debug, "Enables PRI debugging on a span"),
14061 AST_CLI_DEFINE(handle_pri_show_spans, "Displays PRI Information"),
14062 AST_CLI_DEFINE(handle_pri_show_span, "Displays PRI Information"),
14063 AST_CLI_DEFINE(handle_pri_show_debug, "Displays current PRI debug settings"),
14064 AST_CLI_DEFINE(handle_pri_set_debug_file, "Sends PRI debug output to the specified file"),
14065 AST_CLI_DEFINE(handle_pri_version, "Displays libpri version"),
14066 };
14067 #endif
14068
14069 #ifdef HAVE_OPENR2
14070
14071 static char *handle_mfcr2_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14072 {
14073 switch (cmd) {
14074 case CLI_INIT:
14075 e->command = "mfcr2 show version";
14076 e->usage =
14077 "Usage: mfcr2 show version\n"
14078 " Shows the version of the OpenR2 library being used.\n";
14079 return NULL;
14080 case CLI_GENERATE:
14081 return NULL;
14082 }
14083 ast_cli(a->fd, "OpenR2 version: %s, revision: %s\n", openr2_get_version(), openr2_get_revision());
14084 return CLI_SUCCESS;
14085 }
14086
14087 static char *handle_mfcr2_show_variants(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14088 {
14089 #define FORMAT "%4s %40s\n"
14090 int i = 0;
14091 int numvariants = 0;
14092 const openr2_variant_entry_t *variants;
14093 switch (cmd) {
14094 case CLI_INIT:
14095 e->command = "mfcr2 show variants";
14096 e->usage =
14097 "Usage: mfcr2 show variants\n"
14098 " Shows the list of MFC/R2 variants supported.\n";
14099 return NULL;
14100 case CLI_GENERATE:
14101 return NULL;
14102 }
14103 if (!(variants = openr2_proto_get_variant_list(&numvariants))) {
14104 ast_cli(a->fd, "Failed to get list of variants.\n");
14105 return CLI_FAILURE;
14106 }
14107 ast_cli(a->fd, FORMAT, "Variant Code", "Country");
14108 for (i = 0; i < numvariants; i++) {
14109 ast_cli(a->fd, FORMAT, variants[i].name, variants[i].country);
14110 }
14111 return CLI_SUCCESS;
14112 #undef FORMAT
14113 }
14114
14115 static char *handle_mfcr2_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14116 {
14117 #define FORMAT "%4s %-7.7s %-7.7s %-8.8s %-9.9s %-16.16s %-8.8s %-8.8s\n"
14118 int filtertype = 0;
14119 int targetnum = 0;
14120 char channo[5];
14121 char anino[5];
14122 char dnisno[5];
14123 struct dahdi_pvt *p;
14124 openr2_context_t *r2context;
14125 openr2_variant_t r2variant;
14126 switch (cmd) {
14127 case CLI_INIT:
14128 e->command = "mfcr2 show channels [group|context]";
14129 e->usage =
14130 "Usage: mfcr2 show channels [group <group> | context <context>]\n"
14131 " Shows the DAHDI channels configured with MFC/R2 signaling.\n";
14132 return NULL;
14133 case CLI_GENERATE:
14134 return NULL;
14135 }
14136 if (!((a->argc == 3) || (a->argc == 5))) {
14137 return CLI_SHOWUSAGE;
14138 }
14139 if (a->argc == 5) {
14140 if (!strcasecmp(a->argv[3], "group")) {
14141 targetnum = atoi(a->argv[4]);
14142 if ((targetnum < 0) || (targetnum > 63))
14143 return CLI_SHOWUSAGE;
14144 targetnum = 1 << targetnum;
14145 filtertype = 1;
14146 } else if (!strcasecmp(a->argv[3], "context")) {
14147 filtertype = 2;
14148 } else {
14149 return CLI_SHOWUSAGE;
14150 }
14151 }
14152 ast_cli(a->fd, FORMAT, "Chan", "Variant", "Max ANI", "Max DNIS", "ANI First", "Immediate Accept", "Tx CAS", "Rx CAS");
14153 ast_mutex_lock(&iflock);
14154 p = iflist;
14155 for (p = iflist; p; p = p->next) {
14156 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14157 continue;
14158 }
14159 if (filtertype) {
14160 switch(filtertype) {
14161 case 1:
14162 if (p->group != targetnum) {
14163 continue;
14164 }
14165 break;
14166 case 2:
14167 if (strcasecmp(p->context, a->argv[4])) {
14168 continue;
14169 }
14170 break;
14171 default:
14172 ;
14173 }
14174 }
14175 r2context = openr2_chan_get_context(p->r2chan);
14176 r2variant = openr2_context_get_variant(r2context);
14177 snprintf(channo, sizeof(channo), "%d", p->channel);
14178 snprintf(anino, sizeof(anino), "%d", openr2_context_get_max_ani(r2context));
14179 snprintf(dnisno, sizeof(dnisno), "%d", openr2_context_get_max_dnis(r2context));
14180 ast_cli(a->fd, FORMAT, channo, openr2_proto_get_variant_string(r2variant),
14181 anino, dnisno, openr2_context_get_ani_first(r2context) ? "Yes" : "No",
14182 openr2_context_get_immediate_accept(r2context) ? "Yes" : "No",
14183 openr2_chan_get_tx_cas_string(p->r2chan), openr2_chan_get_rx_cas_string(p->r2chan));
14184 }
14185 ast_mutex_unlock(&iflock);
14186 return CLI_SUCCESS;
14187 #undef FORMAT
14188 }
14189
14190 static char *handle_mfcr2_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14191 {
14192 struct dahdi_pvt *p = NULL;
14193 int channo = 0;
14194 char *toklevel = NULL;
14195 char *saveptr = NULL;
14196 char *logval = NULL;
14197 openr2_log_level_t loglevel = OR2_LOG_NOTHING;
14198 openr2_log_level_t tmplevel = OR2_LOG_NOTHING;
14199 switch (cmd) {
14200 case CLI_INIT:
14201 e->command = "mfcr2 set debug";
14202 e->usage =
14203 "Usage: mfcr2 set debug <loglevel> <channel>\n"
14204 " Set a new logging level for the specified channel.\n"
14205 " If no channel is specified the logging level will be applied to all channels.\n";
14206 return NULL;
14207 case CLI_GENERATE:
14208 return NULL;
14209 }
14210 if (a->argc < 4) {
14211 return CLI_SHOWUSAGE;
14212 }
14213 channo = (a->argc == 5) ? atoi(a->argv[4]) : -1;
14214 logval = ast_strdupa(a->argv[3]);
14215 toklevel = strtok_r(logval, ",", &saveptr);
14216 if (-1 == (tmplevel = openr2_log_get_level(toklevel))) {
14217 ast_cli(a->fd, "Invalid MFC/R2 logging level '%s'.\n", a->argv[3]);
14218 return CLI_FAILURE;
14219 } else if (OR2_LOG_NOTHING == tmplevel) {
14220 loglevel = tmplevel;
14221 } else {
14222 loglevel |= tmplevel;
14223 while ((toklevel = strtok_r(NULL, ",", &saveptr))) {
14224 if (-1 == (tmplevel = openr2_log_get_level(toklevel))) {
14225 ast_cli(a->fd, "Ignoring invalid logging level: '%s'.\n", toklevel);
14226 continue;
14227 }
14228 loglevel |= tmplevel;
14229 }
14230 }
14231 ast_mutex_lock(&iflock);
14232 for (p = iflist; p; p = p->next) {
14233 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14234 continue;
14235 }
14236 if ((channo != -1) && (p->channel != channo )) {
14237 continue;
14238 }
14239 openr2_chan_set_log_level(p->r2chan, loglevel);
14240 if (channo != -1) {
14241 ast_cli(a->fd, "MFC/R2 debugging set to '%s' for channel %d.\n", a->argv[3], p->channel);
14242 break;
14243 }
14244 }
14245 if ((channo != -1) && !p) {
14246 ast_cli(a->fd, "MFC/R2 channel %d not found.\n", channo);
14247 }
14248 if (channo == -1) {
14249 ast_cli(a->fd, "MFC/R2 debugging set to '%s' for all channels.\n", a->argv[3]);
14250 }
14251 ast_mutex_unlock(&iflock);
14252 return CLI_SUCCESS;
14253 }
14254
14255 static char *handle_mfcr2_call_files(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14256 {
14257 struct dahdi_pvt *p = NULL;
14258 int channo = 0;
14259 switch (cmd) {
14260 case CLI_INIT:
14261 e->command = "mfcr2 call files [on|off]";
14262 e->usage =
14263 "Usage: mfcr2 call files [on|off] <channel>\n"
14264 " Enable call files creation on the specified channel.\n"
14265 " If no channel is specified call files creation policy will be applied to all channels.\n";
14266 return NULL;
14267 case CLI_GENERATE:
14268 return NULL;
14269 }
14270 if (a->argc < 4) {
14271 return CLI_SHOWUSAGE;
14272 }
14273 channo = (a->argc == 5) ? atoi(a->argv[4]) : -1;
14274 ast_mutex_lock(&iflock);
14275 for (p = iflist; p; p = p->next) {
14276 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14277 continue;
14278 }
14279 if ((channo != -1) && (p->channel != channo )) {
14280 continue;
14281 }
14282 if (ast_true(a->argv[3])) {
14283 openr2_chan_enable_call_files(p->r2chan);
14284 } else {
14285 openr2_chan_disable_call_files(p->r2chan);
14286 }
14287 if (channo != -1) {
14288 if (ast_true(a->argv[3])) {
14289 ast_cli(a->fd, "MFC/R2 call files enabled for channel %d.\n", p->channel);
14290 } else {
14291 ast_cli(a->fd, "MFC/R2 call files disabled for channel %d.\n", p->channel);
14292 }
14293 break;
14294 }
14295 }
14296 if ((channo != -1) && !p) {
14297 ast_cli(a->fd, "MFC/R2 channel %d not found.\n", channo);
14298 }
14299 if (channo == -1) {
14300 if (ast_true(a->argv[3])) {
14301 ast_cli(a->fd, "MFC/R2 Call files enabled for all channels.\n");
14302 } else {
14303 ast_cli(a->fd, "MFC/R2 Call files disabled for all channels.\n");
14304 }
14305 }
14306 ast_mutex_unlock(&iflock);
14307 return CLI_SUCCESS;
14308 }
14309
14310 static char *handle_mfcr2_set_idle(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14311 {
14312 struct dahdi_pvt *p = NULL;
14313 int channo = 0;
14314 switch (cmd) {
14315 case CLI_INIT:
14316 e->command = "mfcr2 set idle";
14317 e->usage =
14318 "Usage: mfcr2 set idle <channel>\n"
14319 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.\n"
14320 " Force the given channel into IDLE state.\n"
14321 " If no channel is specified, all channels will be set to IDLE.\n";
14322 return NULL;
14323 case CLI_GENERATE:
14324 return NULL;
14325 }
14326 channo = (a->argc == 4) ? atoi(a->argv[3]) : -1;
14327 ast_mutex_lock(&iflock);
14328 for (p = iflist; p; p = p->next) {
14329 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14330 continue;
14331 }
14332 if ((channo != -1) && (p->channel != channo )) {
14333 continue;
14334 }
14335 openr2_chan_set_idle(p->r2chan);
14336 ast_mutex_lock(&p->lock);
14337 p->locallyblocked = 0;
14338 p->mfcr2call = 0;
14339 ast_mutex_unlock(&p->lock);
14340 if (channo != -1) {
14341 break;
14342 }
14343 }
14344 if ((channo != -1) && !p) {
14345 ast_cli(a->fd, "MFC/R2 channel %d not found.\n", channo);
14346 }
14347 ast_mutex_unlock(&iflock);
14348 return CLI_SUCCESS;
14349 }
14350
14351 static char *handle_mfcr2_set_blocked(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14352 {
14353 struct dahdi_pvt *p = NULL;
14354 int channo = 0;
14355 switch (cmd) {
14356 case CLI_INIT:
14357 e->command = "mfcr2 set blocked";
14358 e->usage =
14359 "Usage: mfcr2 set blocked <channel>\n"
14360 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.\n"
14361 " Force the given channel into BLOCKED state.\n"
14362 " If no channel is specified, all channels will be set to BLOCKED.\n";
14363 return NULL;
14364 case CLI_GENERATE:
14365 return NULL;
14366 }
14367 channo = (a->argc == 4) ? atoi(a->argv[3]) : -1;
14368 ast_mutex_lock(&iflock);
14369 for (p = iflist; p; p = p->next) {
14370 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14371 continue;
14372 }
14373 if ((channo != -1) && (p->channel != channo )) {
14374 continue;
14375 }
14376 openr2_chan_set_blocked(p->r2chan);
14377 ast_mutex_lock(&p->lock);
14378 p->locallyblocked = 1;
14379 ast_mutex_unlock(&p->lock);
14380 if (channo != -1) {
14381 break;
14382 }
14383 }
14384 if ((channo != -1) && !p) {
14385 ast_cli(a->fd, "MFC/R2 channel %d not found.\n", channo);
14386 }
14387 ast_mutex_unlock(&iflock);
14388 return CLI_SUCCESS;
14389 }
14390
14391 static struct ast_cli_entry dahdi_mfcr2_cli[] = {
14392 AST_CLI_DEFINE(handle_mfcr2_version, "Show OpenR2 library version"),
14393 AST_CLI_DEFINE(handle_mfcr2_show_variants, "Show supported MFC/R2 variants"),
14394 AST_CLI_DEFINE(handle_mfcr2_show_channels, "Show MFC/R2 channels"),
14395 AST_CLI_DEFINE(handle_mfcr2_set_debug, "Set MFC/R2 channel logging level"),
14396 AST_CLI_DEFINE(handle_mfcr2_call_files, "Enable/Disable MFC/R2 call files"),
14397 AST_CLI_DEFINE(handle_mfcr2_set_idle, "Reset MFC/R2 channel forcing it to IDLE"),
14398 AST_CLI_DEFINE(handle_mfcr2_set_blocked, "Reset MFC/R2 channel forcing it to BLOCKED"),
14399 };
14400
14401 #endif
14402
14403 static char *dahdi_destroy_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14404 {
14405 int channel;
14406 int ret;
14407 switch (cmd) {
14408 case CLI_INIT:
14409 e->command = "dahdi destroy channel";
14410 e->usage =
14411 "Usage: dahdi destroy channel <chan num>\n"
14412 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n";
14413 return NULL;
14414 case CLI_GENERATE:
14415 return NULL;
14416 }
14417 if (a->argc != 4)
14418 return CLI_SHOWUSAGE;
14419
14420 channel = atoi(a->argv[3]);
14421 ret = dahdi_destroy_channel_bynum(channel);
14422 return ( RESULT_SUCCESS == ret ) ? CLI_SUCCESS : CLI_FAILURE;
14423 }
14424
14425 static void dahdi_softhangup_all(void)
14426 {
14427 struct dahdi_pvt *p;
14428 retry:
14429 ast_mutex_lock(&iflock);
14430 for (p = iflist; p; p = p->next) {
14431 ast_mutex_lock(&p->lock);
14432 if (p->owner && !p->restartpending) {
14433 if (ast_channel_trylock(p->owner)) {
14434 if (option_debug > 2)
14435 ast_verbose("Avoiding deadlock\n");
14436
14437 ast_mutex_unlock(&p->lock);
14438 ast_mutex_unlock(&iflock);
14439 goto retry;
14440 }
14441 if (option_debug > 2)
14442 ast_verbose("Softhanging up on %s\n", p->owner->name);
14443 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_EXPLICIT);
14444 p->restartpending = 1;
14445 num_restart_pending++;
14446 ast_channel_unlock(p->owner);
14447 }
14448 ast_mutex_unlock(&p->lock);
14449 }
14450 ast_mutex_unlock(&iflock);
14451 }
14452
14453 static int setup_dahdi(int reload);
14454 static int dahdi_restart(void)
14455 {
14456 #if defined(HAVE_PRI) || defined(HAVE_SS7)
14457 int i, j;
14458 #endif
14459 int cancel_code;
14460 struct dahdi_pvt *p;
14461
14462 ast_mutex_lock(&restart_lock);
14463 ast_verb(1, "Destroying channels and reloading DAHDI configuration.\n");
14464 dahdi_softhangup_all();
14465 ast_verb(4, "Initial softhangup of all DAHDI channels complete.\n");
14466 #ifdef HAVE_OPENR2
14467 dahdi_r2_destroy_links();
14468 #endif
14469
14470 #if defined(HAVE_PRI)
14471 for (i = 0; i < NUM_SPANS; i++) {
14472 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL)) {
14473 cancel_code = pthread_cancel(pris[i].master);
14474 pthread_kill(pris[i].master, SIGURG);
14475 ast_debug(4, "Waiting to join thread of span %d with pid=%p, cancel_code=%d\n", i, (void *) pris[i].master, cancel_code);
14476 pthread_join(pris[i].master, NULL);
14477 ast_debug(4, "Joined thread of span %d\n", i);
14478 }
14479 }
14480 #endif
14481
14482 #if defined(HAVE_SS7)
14483 for (i = 0; i < NUM_SPANS; i++) {
14484 if (linksets[i].master && (linksets[i].master != AST_PTHREADT_NULL)) {
14485 cancel_code = pthread_cancel(linksets[i].master);
14486 pthread_kill(linksets[i].master, SIGURG);
14487 ast_debug(4, "Waiting to join thread of span %d with pid=%p, cancel_code=%d\n", i, (void *) linksets[i].master, cancel_code);
14488 pthread_join(linksets[i].master, NULL);
14489 ast_debug(4, "Joined thread of span %d\n", i);
14490 }
14491 }
14492 #endif
14493
14494 ast_mutex_lock(&monlock);
14495 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
14496 cancel_code = pthread_cancel(monitor_thread);
14497 pthread_kill(monitor_thread, SIGURG);
14498 ast_debug(4, "Waiting to join monitor thread with pid=%p, cancel_code=%d\n", (void *) monitor_thread, cancel_code);
14499 pthread_join(monitor_thread, NULL);
14500 ast_debug(4, "Joined monitor thread\n");
14501 }
14502 monitor_thread = AST_PTHREADT_NULL;
14503
14504 ast_mutex_lock(&ss_thread_lock);
14505 while (ss_thread_count > 0) {
14506 int x = DAHDI_FLASH;
14507 ast_debug(3, "Waiting on %d ss_thread(s) to finish\n", ss_thread_count);
14508
14509 for (p = iflist; p; p = p->next) {
14510 if (p->owner)
14511 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
14512 }
14513 ast_cond_wait(&ss_thread_complete, &ss_thread_lock);
14514 }
14515
14516
14517 dahdi_softhangup_all();
14518 ast_verb(4, "Final softhangup of all DAHDI channels complete.\n");
14519 destroy_all_channels();
14520 ast_debug(1, "Channels destroyed. Now re-reading config. %d active channels remaining.\n", ast_active_channels());
14521
14522 ast_mutex_unlock(&monlock);
14523
14524 #ifdef HAVE_PRI
14525 for (i = 0; i < NUM_SPANS; i++) {
14526 for (j = 0; j < NUM_DCHANS; j++)
14527 dahdi_close_pri_fd(&(pris[i]), j);
14528 }
14529
14530 memset(pris, 0, sizeof(pris));
14531 for (i = 0; i < NUM_SPANS; i++) {
14532 ast_mutex_init(&pris[i].lock);
14533 pris[i].offset = -1;
14534 pris[i].master = AST_PTHREADT_NULL;
14535 for (j = 0; j < NUM_DCHANS; j++)
14536 pris[i].fds[j] = -1;
14537 }
14538 pri_set_error(dahdi_pri_error);
14539 pri_set_message(dahdi_pri_message);
14540 #endif
14541 #ifdef HAVE_SS7
14542 for (i = 0; i < NUM_SPANS; i++) {
14543 for (j = 0; j < NUM_DCHANS; j++)
14544 dahdi_close_ss7_fd(&(linksets[i]), j);
14545 }
14546
14547 memset(linksets, 0, sizeof(linksets));
14548 for (i = 0; i < NUM_SPANS; i++) {
14549 ast_mutex_init(&linksets[i].lock);
14550 linksets[i].master = AST_PTHREADT_NULL;
14551 for (j = 0; j < NUM_DCHANS; j++)
14552 linksets[i].fds[j] = -1;
14553 }
14554 ss7_set_error(dahdi_ss7_error);
14555 ss7_set_message(dahdi_ss7_message);
14556 #endif
14557
14558 if (setup_dahdi(2) != 0) {
14559 ast_log(LOG_WARNING, "Reload channels from dahdi config failed!\n");
14560 ast_mutex_unlock(&ss_thread_lock);
14561 return 1;
14562 }
14563 ast_mutex_unlock(&ss_thread_lock);
14564 ast_mutex_unlock(&restart_lock);
14565 return 0;
14566 }
14567
14568 static char *dahdi_restart_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14569 {
14570 switch (cmd) {
14571 case CLI_INIT:
14572 e->command = "dahdi restart";
14573 e->usage =
14574 "Usage: dahdi restart\n"
14575 " Restarts the DAHDI channels: destroys them all and then\n"
14576 " re-reads them from chan_dahdi.conf.\n"
14577 " Note that this will STOP any running CALL on DAHDI channels.\n"
14578 "";
14579 return NULL;
14580 case CLI_GENERATE:
14581 return NULL;
14582 }
14583 if (a->argc != 2)
14584 return CLI_SHOWUSAGE;
14585
14586 if (dahdi_restart() != 0)
14587 return CLI_FAILURE;
14588 return CLI_SUCCESS;
14589 }
14590
14591 static int action_dahdirestart(struct mansession *s, const struct message *m)
14592 {
14593 if (dahdi_restart() != 0) {
14594 astman_send_error(s, m, "Failed rereading DAHDI configuration");
14595 return 1;
14596 }
14597 astman_send_ack(s, m, "DAHDIRestart: Success");
14598 return 0;
14599 }
14600
14601 static char *dahdi_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14602 {
14603 #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s %-10.10s %-10.10s\n"
14604 #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s %-10.10s %-10.10s\n"
14605 unsigned int targetnum = 0;
14606 int filtertype = 0;
14607 struct dahdi_pvt *tmp = NULL;
14608 char tmps[20] = "";
14609 char statestr[20] = "";
14610 char blockstr[20] = "";
14611 ast_mutex_t *lock;
14612 struct dahdi_pvt *start;
14613 #ifdef HAVE_PRI
14614 int trunkgroup;
14615 struct dahdi_pri *pri = NULL;
14616 int x;
14617 #endif
14618 switch (cmd) {
14619 case CLI_INIT:
14620 e->command = "dahdi show channels [trunkgroup|group|context]";
14621 e->usage =
14622 "Usage: dahdi show channels [ trunkgroup <trunkgroup> | group <group> | context <context> ]\n"
14623 " Shows a list of available channels with optional filtering\n"
14624 " <group> must be a number between 0 and 63\n";
14625 return NULL;
14626 case CLI_GENERATE:
14627 return NULL;
14628 }
14629
14630 lock = &iflock;
14631 start = iflist;
14632
14633
14634
14635 if (!((a->argc == 3) || (a->argc == 5)))
14636 return CLI_SHOWUSAGE;
14637
14638 if (a->argc == 5) {
14639 #ifdef HAVE_PRI
14640 if (!strcasecmp(a->argv[3], "trunkgroup")) {
14641
14642 if ((trunkgroup = atoi(a->argv[4])) < 1)
14643 return CLI_SHOWUSAGE;
14644 for (x = 0; x < NUM_SPANS; x++) {
14645 if (pris[x].trunkgroup == trunkgroup) {
14646 pri = pris + x;
14647 break;
14648 }
14649 }
14650 if (pri) {
14651 start = pri->crvs;
14652 lock = &pri->lock;
14653 } else {
14654 ast_cli(a->fd, "No such trunk group %d\n", trunkgroup);
14655 return CLI_FAILURE;
14656 }
14657 } else
14658 #endif
14659 if (!strcasecmp(a->argv[3], "group")) {
14660 targetnum = atoi(a->argv[4]);
14661 if ((targetnum < 0) || (targetnum > 63))
14662 return CLI_SHOWUSAGE;
14663 targetnum = 1 << targetnum;
14664 filtertype = 1;
14665 } else if (!strcasecmp(a->argv[3], "context")) {
14666 filtertype = 2;
14667 }
14668 }
14669
14670 ast_mutex_lock(lock);
14671 #ifdef HAVE_PRI
14672 ast_cli(a->fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MOH Interpret", "Blocked", "State");
14673 #else
14674 ast_cli(a->fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MOH Interpret", "Blocked", "State");
14675 #endif
14676
14677 tmp = start;
14678 while (tmp) {
14679 if (filtertype) {
14680 switch(filtertype) {
14681 case 1:
14682 if (!(tmp->group & targetnum)) {
14683 tmp = tmp->next;
14684 continue;
14685 }
14686 break;
14687 case 2:
14688 if (strcasecmp(tmp->context, a->argv[4])) {
14689 tmp = tmp->next;
14690 continue;
14691 }
14692 break;
14693 default:
14694 ;
14695 }
14696 }
14697 if (tmp->channel > 0) {
14698 snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
14699 } else
14700 ast_copy_string(tmps, "pseudo", sizeof(tmps));
14701
14702 if (tmp->locallyblocked)
14703 blockstr[0] = 'L';
14704 else
14705 blockstr[0] = ' ';
14706
14707 if (tmp->remotelyblocked)
14708 blockstr[1] = 'R';
14709 else
14710 blockstr[1] = ' ';
14711
14712 blockstr[2] = '\0';
14713
14714 snprintf(statestr, sizeof(statestr), "%s", "In Service");
14715
14716 ast_cli(a->fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->mohinterpret, blockstr, statestr);
14717 tmp = tmp->next;
14718 }
14719 ast_mutex_unlock(lock);
14720 return CLI_SUCCESS;
14721 #undef FORMAT
14722 #undef FORMAT2
14723 }
14724
14725 static char *dahdi_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14726 {
14727 int channel;
14728 struct dahdi_pvt *tmp = NULL;
14729 struct dahdi_confinfo ci;
14730 struct dahdi_params ps;
14731 int x;
14732 ast_mutex_t *lock;
14733 struct dahdi_pvt *start;
14734 #ifdef HAVE_PRI
14735 char *c;
14736 int trunkgroup;
14737 struct dahdi_pri *pri=NULL;
14738 #endif
14739 switch (cmd) {
14740 case CLI_INIT:
14741 e->command = "dahdi show channel";
14742 e->usage =
14743 "Usage: dahdi show channel <chan num>\n"
14744 " Detailed information about a given channel\n";
14745 return NULL;
14746 case CLI_GENERATE:
14747 return NULL;
14748 }
14749
14750 lock = &iflock;
14751 start = iflist;
14752
14753 if (a->argc != 4)
14754 return CLI_SHOWUSAGE;
14755 #ifdef HAVE_PRI
14756 if ((c = strchr(a->argv[3], ':'))) {
14757 if (sscanf(a->argv[3], "%30d:%30d", &trunkgroup, &channel) != 2)
14758 return CLI_SHOWUSAGE;
14759 if ((trunkgroup < 1) || (channel < 1))
14760 return CLI_SHOWUSAGE;
14761 for (x = 0; x < NUM_SPANS; x++) {
14762 if (pris[x].trunkgroup == trunkgroup) {
14763 pri = pris + x;
14764 break;
14765 }
14766 }
14767 if (pri) {
14768 start = pri->crvs;
14769 lock = &pri->lock;
14770 } else {
14771 ast_cli(a->fd, "No such trunk group %d\n", trunkgroup);
14772 return CLI_FAILURE;
14773 }
14774 } else
14775 #endif
14776 channel = atoi(a->argv[3]);
14777
14778 ast_mutex_lock(lock);
14779 tmp = start;
14780 while (tmp) {
14781 if (tmp->channel == channel) {
14782 #ifdef HAVE_PRI
14783 if (pri)
14784 ast_cli(a->fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
14785 else
14786 #endif
14787 ast_cli(a->fd, "Channel: %d\n", tmp->channel);
14788 ast_cli(a->fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].dfd);
14789 ast_cli(a->fd, "Span: %d\n", tmp->span);
14790 ast_cli(a->fd, "Extension: %s\n", tmp->exten);
14791 ast_cli(a->fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
14792 ast_cli(a->fd, "Context: %s\n", tmp->context);
14793 ast_cli(a->fd, "Caller ID: %s\n", tmp->cid_num);
14794 ast_cli(a->fd, "Calling TON: %d\n", tmp->cid_ton);
14795 ast_cli(a->fd, "Caller ID name: %s\n", tmp->cid_name);
14796 ast_cli(a->fd, "Mailbox: %s\n", S_OR(tmp->mailbox, "none"));
14797 if (tmp->vars) {
14798 struct ast_variable *v;
14799 ast_cli(a->fd, "Variables:\n");
14800 for (v = tmp->vars ; v ; v = v->next)
14801 ast_cli(a->fd, " %s = %s\n", v->name, v->value);
14802 }
14803 ast_cli(a->fd, "Destroy: %d\n", tmp->destroy);
14804 ast_cli(a->fd, "InAlarm: %d\n", tmp->inalarm);
14805 ast_cli(a->fd, "Signalling Type: %s\n", sig2str(tmp->sig));
14806 ast_cli(a->fd, "Radio: %d\n", tmp->radio);
14807 ast_cli(a->fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
14808 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)" : "");
14809 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)" : "");
14810 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)" : "");
14811 ast_cli(a->fd, "Confno: %d\n", tmp->confno);
14812 ast_cli(a->fd, "Propagated Conference: %d\n", tmp->propconfno);
14813 ast_cli(a->fd, "Real in conference: %d\n", tmp->inconference);
14814 ast_cli(a->fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
14815 ast_cli(a->fd, "Busy Detection: %s\n", tmp->busydetect ? "yes" : "no");
14816 if (tmp->busydetect) {
14817 #if defined(BUSYDETECT_TONEONLY)
14818 ast_cli(a->fd, " Busy Detector Helper: BUSYDETECT_TONEONLY\n");
14819 #elif defined(BUSYDETECT_COMPARE_TONE_AND_SILENCE)
14820 ast_cli(a->fd, " Busy Detector Helper: BUSYDETECT_COMPARE_TONE_AND_SILENCE\n");
14821 #endif
14822 #ifdef BUSYDETECT_DEBUG
14823 ast_cli(a->fd, " Busy Detector Debug: Enabled\n");
14824 #endif
14825 ast_cli(a->fd, " Busy Count: %d\n", tmp->busycount);
14826 ast_cli(a->fd, " Busy Pattern: %d,%d\n", tmp->busy_tonelength, tmp->busy_quietlength);
14827 }
14828 ast_cli(a->fd, "TDD: %s\n", tmp->tdd ? "yes" : "no");
14829 ast_cli(a->fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
14830 ast_cli(a->fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
14831 ast_cli(a->fd, "Default law: %s\n", tmp->law == DAHDI_LAW_MULAW ? "ulaw" : tmp->law == DAHDI_LAW_ALAW ? "alaw" : "unknown");
14832 ast_cli(a->fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
14833 ast_cli(a->fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
14834 ast_cli(a->fd, "DND: %s\n", tmp->dnd ? "yes" : "no");
14835 ast_cli(a->fd, "Echo Cancellation:\n");
14836
14837 if (tmp->echocancel.head.tap_length) {
14838 ast_cli(a->fd, "\t%d taps\n", tmp->echocancel.head.tap_length);
14839 for (x = 0; x < tmp->echocancel.head.param_count; x++) {
14840 ast_cli(a->fd, "\t\t%s: %ud\n", tmp->echocancel.params[x].name, tmp->echocancel.params[x].value);
14841 }
14842 ast_cli(a->fd, "\t%scurrently %s\n", tmp->echocanbridged ? "" : "(unless TDM bridged) ", tmp->echocanon ? "ON" : "OFF");
14843 } else {
14844 ast_cli(a->fd, "\tnone\n");
14845 }
14846 ast_cli(a->fd, "Wait for dialtone: %dms\n", tmp->waitfordialtone);
14847 if (tmp->master)
14848 ast_cli(a->fd, "Master Channel: %d\n", tmp->master->channel);
14849 for (x = 0; x < MAX_SLAVES; x++) {
14850 if (tmp->slaves[x])
14851 ast_cli(a->fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
14852 }
14853 #ifdef HAVE_OPENR2
14854 if (tmp->mfcr2) {
14855 char calldir[OR2_MAX_PATH];
14856 openr2_context_t *r2context = openr2_chan_get_context(tmp->r2chan);
14857 openr2_variant_t r2variant = openr2_context_get_variant(r2context);
14858 ast_cli(a->fd, "MFC/R2 MF State: %s\n", openr2_chan_get_mf_state_string(tmp->r2chan));
14859 ast_cli(a->fd, "MFC/R2 MF Group: %s\n", openr2_chan_get_mf_group_string(tmp->r2chan));
14860 ast_cli(a->fd, "MFC/R2 State: %s\n", openr2_chan_get_r2_state_string(tmp->r2chan));
14861 ast_cli(a->fd, "MFC/R2 Call State: %s\n", openr2_chan_get_call_state_string(tmp->r2chan));
14862 ast_cli(a->fd, "MFC/R2 Call Files Enabled: %s\n", openr2_chan_get_call_files_enabled(tmp->r2chan) ? "Yes" : "No");
14863 ast_cli(a->fd, "MFC/R2 Variant: %s\n", openr2_proto_get_variant_string(r2variant));
14864 ast_cli(a->fd, "MFC/R2 Max ANI: %d\n", openr2_context_get_max_ani(r2context));
14865 ast_cli(a->fd, "MFC/R2 Max DNIS: %d\n", openr2_context_get_max_dnis(r2context));
14866 ast_cli(a->fd, "MFC/R2 Get ANI First: %s\n", openr2_context_get_ani_first(r2context) ? "Yes" : "No");
14867 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
14868 ast_cli(a->fd, "MFC/R2 Skip Category Request: %s\n", openr2_context_get_skip_category_request(r2context) ? "Yes" : "No");
14869 #endif
14870 ast_cli(a->fd, "MFC/R2 Immediate Accept: %s\n", openr2_context_get_immediate_accept(r2context) ? "Yes" : "No");
14871 ast_cli(a->fd, "MFC/R2 Accept on Offer: %s\n", tmp->mfcr2_accept_on_offer ? "Yes" : "No");
14872 ast_cli(a->fd, "MFC/R2 Charge Calls: %s\n", tmp->mfcr2_charge_calls ? "Yes" : "No");
14873 ast_cli(a->fd, "MFC/R2 Allow Collect Calls: %s\n", tmp->mfcr2_allow_collect_calls ? "Yes" : "No");
14874 ast_cli(a->fd, "MFC/R2 Forced Release: %s\n", tmp->mfcr2_forced_release ? "Yes" : "No");
14875 ast_cli(a->fd, "MFC/R2 MF Back Timeout: %dms\n", openr2_context_get_mf_back_timeout(r2context));
14876 ast_cli(a->fd, "MFC/R2 R2 Metering Pulse Timeout: %dms\n", openr2_context_get_metering_pulse_timeout(r2context));
14877 ast_cli(a->fd, "MFC/R2 Rx CAS: %s\n", openr2_chan_get_rx_cas_string(tmp->r2chan));
14878 ast_cli(a->fd, "MFC/R2 Tx CAS: %s\n", openr2_chan_get_tx_cas_string(tmp->r2chan));
14879 ast_cli(a->fd, "MFC/R2 MF Tx Signal: %d\n", openr2_chan_get_tx_mf_signal(tmp->r2chan));
14880 ast_cli(a->fd, "MFC/R2 MF Rx Signal: %d\n", openr2_chan_get_rx_mf_signal(tmp->r2chan));
14881 ast_cli(a->fd, "MFC/R2 Call Files Directory: %s\n", openr2_context_get_log_directory(r2context, calldir, sizeof(calldir)));
14882 }
14883 #endif
14884 #ifdef HAVE_SS7
14885 if (tmp->ss7) {
14886 ast_cli(a->fd, "CIC: %d\n", tmp->cic);
14887 }
14888 #endif
14889 #ifdef HAVE_PRI
14890 if (tmp->pri) {
14891 ast_cli(a->fd, "PRI Flags: ");
14892 if (tmp->resetting)
14893 ast_cli(a->fd, "Resetting ");
14894 if (tmp->call)
14895 ast_cli(a->fd, "Call ");
14896 if (tmp->bearer)
14897 ast_cli(a->fd, "Bearer ");
14898 ast_cli(a->fd, "\n");
14899 if (tmp->logicalspan)
14900 ast_cli(a->fd, "PRI Logical Span: %d\n", tmp->logicalspan);
14901 else
14902 ast_cli(a->fd, "PRI Logical Span: Implicit\n");
14903 }
14904 #endif
14905 memset(&ci, 0, sizeof(ci));
14906 ps.channo = tmp->channel;
14907 if (tmp->subs[SUB_REAL].dfd > -1) {
14908 memset(&ci, 0, sizeof(ci));
14909 if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONF, &ci)) {
14910 ast_cli(a->fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
14911 }
14912 if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONFMUTE, &x)) {
14913 ast_cli(a->fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
14914 }
14915 memset(&ps, 0, sizeof(ps));
14916 if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps) < 0) {
14917 ast_log(LOG_WARNING, "Failed to get parameters on channel %d: %s\n", tmp->channel, strerror(errno));
14918 } else {
14919 ast_cli(a->fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
14920 }
14921 }
14922 ast_mutex_unlock(lock);
14923 return CLI_SUCCESS;
14924 }
14925 tmp = tmp->next;
14926 }
14927
14928 ast_cli(a->fd, "Unable to find given channel %d\n", channel);
14929 ast_mutex_unlock(lock);
14930 return CLI_FAILURE;
14931 }
14932
14933 static char *handle_dahdi_show_cadences(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14934 {
14935 int i, j;
14936 switch (cmd) {
14937 case CLI_INIT:
14938 e->command = "dahdi show cadences";
14939 e->usage =
14940 "Usage: dahdi show cadences\n"
14941 " Shows all cadences currently defined\n";
14942 return NULL;
14943 case CLI_GENERATE:
14944 return NULL;
14945 }
14946 for (i = 0; i < num_cadence; i++) {
14947 char output[1024];
14948 char tmp[16], tmp2[64];
14949 snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
14950 term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
14951
14952 for (j = 0; j < 16; j++) {
14953 if (cadences[i].ringcadence[j] == 0)
14954 break;
14955 snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
14956 if (cidrings[i] * 2 - 1 == j)
14957 term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
14958 else
14959 term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
14960 if (j != 0)
14961 strncat(output, ",", sizeof(output) - strlen(output) - 1);
14962 strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
14963 }
14964 ast_cli(a->fd,"%s\n",output);
14965 }
14966 return CLI_SUCCESS;
14967 }
14968
14969
14970 static char *dahdi_show_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14971 {
14972 #define FORMAT "%-40.40s %-7.7s %-6d %-6d %-6d %-3.3s %-4.4s %-8.8s %s\n"
14973 #define FORMAT2 "%-40.40s %-7.7s %-6.6s %-6.6s %-6.6s %-3.3s %-4.4s %-8.8s %s\n"
14974 int span;
14975 int res;
14976 char alarmstr[50];
14977
14978 int ctl;
14979 struct dahdi_spaninfo s;
14980
14981 switch (cmd) {
14982 case CLI_INIT:
14983 e->command = "dahdi show status";
14984 e->usage =
14985 "Usage: dahdi show status\n"
14986 " Shows a list of DAHDI cards with status\n";
14987 return NULL;
14988 case CLI_GENERATE:
14989 return NULL;
14990 }
14991 ctl = open("/dev/dahdi/ctl", O_RDWR);
14992 if (ctl < 0) {
14993 ast_cli(a->fd, "No DAHDI found. Unable to open /dev/dahdi/ctl: %s\n", strerror(errno));
14994 return CLI_FAILURE;
14995 }
14996 ast_cli(a->fd, FORMAT2, "Description", "Alarms", "IRQ", "bpviol", "CRC4", "Framing", "Coding", "Options", "LBO");
14997
14998 for (span = 1; span < DAHDI_MAX_SPANS; ++span) {
14999 s.spanno = span;
15000 res = ioctl(ctl, DAHDI_SPANSTAT, &s);
15001 if (res) {
15002 continue;
15003 }
15004 alarmstr[0] = '\0';
15005 if (s.alarms > 0) {
15006 if (s.alarms & DAHDI_ALARM_BLUE)
15007 strcat(alarmstr, "BLU/");
15008 if (s.alarms & DAHDI_ALARM_YELLOW)
15009 strcat(alarmstr, "YEL/");
15010 if (s.alarms & DAHDI_ALARM_RED)
15011 strcat(alarmstr, "RED/");
15012 if (s.alarms & DAHDI_ALARM_LOOPBACK)
15013 strcat(alarmstr, "LB/");
15014 if (s.alarms & DAHDI_ALARM_RECOVER)
15015 strcat(alarmstr, "REC/");
15016 if (s.alarms & DAHDI_ALARM_NOTOPEN)
15017 strcat(alarmstr, "NOP/");
15018 if (!strlen(alarmstr))
15019 strcat(alarmstr, "UUU/");
15020 if (strlen(alarmstr)) {
15021
15022 alarmstr[strlen(alarmstr) - 1] = '\0';
15023 }
15024 } else {
15025 if (s.numchans)
15026 strcpy(alarmstr, "OK");
15027 else
15028 strcpy(alarmstr, "UNCONFIGURED");
15029 }
15030
15031 ast_cli(a->fd, FORMAT, s.desc, alarmstr, s.irqmisses, s.bpvcount, s.crc4count,
15032 s.lineconfig & DAHDI_CONFIG_D4 ? "D4" :
15033 s.lineconfig & DAHDI_CONFIG_ESF ? "ESF" :
15034 s.lineconfig & DAHDI_CONFIG_CCS ? "CCS" :
15035 "CAS",
15036 s.lineconfig & DAHDI_CONFIG_B8ZS ? "B8ZS" :
15037 s.lineconfig & DAHDI_CONFIG_HDB3 ? "HDB3" :
15038 s.lineconfig & DAHDI_CONFIG_AMI ? "AMI" :
15039 "Unk",
15040 s.lineconfig & DAHDI_CONFIG_CRC4 ?
15041 s.lineconfig & DAHDI_CONFIG_NOTOPEN ? "CRC4/YEL" : "CRC4" :
15042 s.lineconfig & DAHDI_CONFIG_NOTOPEN ? "YEL" : "",
15043 lbostr[s.lbo]
15044 );
15045 }
15046 close(ctl);
15047
15048 return CLI_SUCCESS;
15049 #undef FORMAT
15050 #undef FORMAT2
15051 }
15052
15053 static char *dahdi_show_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15054 {
15055 int pseudo_fd = -1;
15056 struct dahdi_versioninfo vi;
15057
15058 switch (cmd) {
15059 case CLI_INIT:
15060 e->command = "dahdi show version";
15061 e->usage =
15062 "Usage: dahdi show version\n"
15063 " Shows the DAHDI version in use\n";
15064 return NULL;
15065 case CLI_GENERATE:
15066 return NULL;
15067 }
15068 if ((pseudo_fd = open("/dev/dahdi/ctl", O_RDONLY)) < 0) {
15069 ast_cli(a->fd, "Failed to open control file to get version.\n");
15070 return CLI_SUCCESS;
15071 }
15072
15073 strcpy(vi.version, "Unknown");
15074 strcpy(vi.echo_canceller, "Unknown");
15075
15076 if (ioctl(pseudo_fd, DAHDI_GETVERSION, &vi))
15077 ast_cli(a->fd, "Failed to get DAHDI version: %s\n", strerror(errno));
15078 else
15079 ast_cli(a->fd, "DAHDI Version: %s Echo Canceller: %s\n", vi.version, vi.echo_canceller);
15080
15081 close(pseudo_fd);
15082
15083 return CLI_SUCCESS;
15084 }
15085
15086 static char *dahdi_set_hwgain(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15087 {
15088 int channel;
15089 int gain;
15090 int tx;
15091 struct dahdi_hwgain hwgain;
15092 struct dahdi_pvt *tmp = NULL;
15093
15094 switch (cmd) {
15095 case CLI_INIT:
15096 e->command = "dahdi set hwgain";
15097 e->usage =
15098 "Usage: dahdi set hwgain <rx|tx> <chan#> <gain>\n"
15099 " Sets the hardware gain on a a given channel, overriding the\n"
15100 " value provided at module loadtime, whether the channel is in\n"
15101 " use or not. Changes take effect immediately.\n"
15102 " <rx|tx> which direction do you want to change (relative to our module)\n"
15103 " <chan num> is the channel number relative to the device\n"
15104 " <gain> is the gain in dB (e.g. -3.5 for -3.5dB)\n";
15105 return NULL;
15106 case CLI_GENERATE:
15107 return NULL;
15108 }
15109
15110 if (a->argc != 6)
15111 return CLI_SHOWUSAGE;
15112
15113 if (!strcasecmp("rx", a->argv[3]))
15114 tx = 0;
15115 else if (!strcasecmp("tx", a->argv[3]))
15116 tx = 1;
15117 else
15118 return CLI_SHOWUSAGE;
15119
15120 channel = atoi(a->argv[4]);
15121 gain = atof(a->argv[5])*10.0;
15122
15123 ast_mutex_lock(&iflock);
15124
15125 for (tmp = iflist; tmp; tmp = tmp->next) {
15126
15127 if (tmp->channel != channel)
15128 continue;
15129
15130 if (tmp->subs[SUB_REAL].dfd == -1)
15131 break;
15132
15133 hwgain.newgain = gain;
15134 hwgain.tx = tx;
15135 if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_HWGAIN, &hwgain) < 0) {
15136 ast_cli(a->fd, "Unable to set the hardware gain for channel %d: %s\n", channel, strerror(errno));
15137 ast_mutex_unlock(&iflock);
15138 return CLI_FAILURE;
15139 }
15140 ast_cli(a->fd, "hardware %s gain set to %d (%.1f dB) on channel %d\n",
15141 tx ? "tx" : "rx", gain, (float)gain/10.0, channel);
15142 break;
15143 }
15144
15145 ast_mutex_unlock(&iflock);
15146
15147 if (tmp)
15148 return CLI_SUCCESS;
15149
15150 ast_cli(a->fd, "Unable to find given channel %d\n", channel);
15151 return CLI_FAILURE;
15152
15153 }
15154
15155 static char *dahdi_set_swgain(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15156 {
15157 int channel;
15158 float gain;
15159 int tx;
15160 int res;
15161 ast_mutex_t *lock;
15162 struct dahdi_pvt *tmp = NULL;
15163
15164 switch (cmd) {
15165 case CLI_INIT:
15166 e->command = "dahdi set swgain";
15167 e->usage =
15168 "Usage: dahdi set swgain <rx|tx> <chan#> <gain>\n"
15169 " Sets the software gain on a a given channel, overriding the\n"
15170 " value provided at module loadtime, whether the channel is in\n"
15171 " use or not. Changes take effect immediately.\n"
15172 " <rx|tx> which direction do you want to change (relative to our module)\n"
15173 " <chan num> is the channel number relative to the device\n"
15174 " <gain> is the gain in dB (e.g. -3.5 for -3.5dB)\n";
15175 return NULL;
15176 case CLI_GENERATE:
15177 return NULL;
15178 }
15179
15180 lock = &iflock;
15181
15182 if (a->argc != 6)
15183 return CLI_SHOWUSAGE;
15184
15185 if (!strcasecmp("rx", a->argv[3]))
15186 tx = 0;
15187 else if (!strcasecmp("tx", a->argv[3]))
15188 tx = 1;
15189 else
15190 return CLI_SHOWUSAGE;
15191
15192 channel = atoi(a->argv[4]);
15193 gain = atof(a->argv[5]);
15194
15195 ast_mutex_lock(lock);
15196 for (tmp = iflist; tmp; tmp = tmp->next) {
15197
15198 if (tmp->channel != channel)
15199 continue;
15200
15201 if (tmp->subs[SUB_REAL].dfd == -1)
15202 break;
15203
15204 if (tx)
15205 res = set_actual_txgain(tmp->subs[SUB_REAL].dfd, channel, gain, tmp->law);
15206 else
15207 res = set_actual_rxgain(tmp->subs[SUB_REAL].dfd, channel, gain, tmp->law);
15208
15209 if (res) {
15210 ast_cli(a->fd, "Unable to set the software gain for channel %d\n", channel);
15211 ast_mutex_unlock(lock);
15212 return CLI_FAILURE;
15213 }
15214
15215 ast_cli(a->fd, "software %s gain set to %.1f on channel %d\n",
15216 tx ? "tx" : "rx", gain, channel);
15217 break;
15218 }
15219 ast_mutex_unlock(lock);
15220
15221 if (tmp)
15222 return CLI_SUCCESS;
15223
15224 ast_cli(a->fd, "Unable to find given channel %d\n", channel);
15225 return CLI_FAILURE;
15226
15227 }
15228
15229 static char *dahdi_set_dnd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15230 {
15231 int channel;
15232 int on;
15233 struct dahdi_pvt *dahdi_chan = NULL;
15234
15235 switch (cmd) {
15236 case CLI_INIT:
15237 e->command = "dahdi set dnd";
15238 e->usage =
15239 "Usage: dahdi set dnd <chan#> <on|off>\n"
15240 " Sets/resets DND (Do Not Disturb) mode on a channel.\n"
15241 " Changes take effect immediately.\n"
15242 " <chan num> is the channel number\n"
15243 " <on|off> Enable or disable DND mode?\n"
15244 ;
15245 return NULL;
15246 case CLI_GENERATE:
15247 return NULL;
15248 }
15249
15250 if (a->argc != 5)
15251 return CLI_SHOWUSAGE;
15252
15253 if ((channel = atoi(a->argv[3])) <= 0) {
15254 ast_cli(a->fd, "Expected channel number, got '%s'\n", a->argv[3]);
15255 return CLI_SHOWUSAGE;
15256 }
15257
15258 if (ast_true(a->argv[4]))
15259 on = 1;
15260 else if (ast_false(a->argv[4]))
15261 on = 0;
15262 else {
15263 ast_cli(a->fd, "Expected 'on' or 'off', got '%s'\n", a->argv[4]);
15264 return CLI_SHOWUSAGE;
15265 }
15266
15267 ast_mutex_lock(&iflock);
15268 for (dahdi_chan = iflist; dahdi_chan; dahdi_chan = dahdi_chan->next) {
15269 if (dahdi_chan->channel != channel)
15270 continue;
15271
15272
15273 dahdi_dnd(dahdi_chan, on);
15274 break;
15275 }
15276 ast_mutex_unlock(&iflock);
15277
15278 if (!dahdi_chan) {
15279 ast_cli(a->fd, "Unable to find given channel %d\n", channel);
15280 return CLI_FAILURE;
15281 }
15282
15283 return CLI_SUCCESS;
15284 }
15285
15286 static struct ast_cli_entry dahdi_cli[] = {
15287 AST_CLI_DEFINE(handle_dahdi_show_cadences, "List cadences"),
15288 AST_CLI_DEFINE(dahdi_show_channels, "Show active DAHDI channels"),
15289 AST_CLI_DEFINE(dahdi_show_channel, "Show information on a channel"),
15290 AST_CLI_DEFINE(dahdi_destroy_channel, "Destroy a channel"),
15291 AST_CLI_DEFINE(dahdi_restart_cmd, "Fully restart DAHDI channels"),
15292 AST_CLI_DEFINE(dahdi_show_status, "Show all DAHDI cards status"),
15293 AST_CLI_DEFINE(dahdi_show_version, "Show the DAHDI version in use"),
15294 AST_CLI_DEFINE(dahdi_set_hwgain, "Set hardware gain on a channel"),
15295 AST_CLI_DEFINE(dahdi_set_swgain, "Set software gain on a channel"),
15296 AST_CLI_DEFINE(dahdi_set_dnd, "Sets/resets DND (Do Not Disturb) mode on a channel"),
15297 };
15298
15299 #define TRANSFER 0
15300 #define HANGUP 1
15301
15302 static int dahdi_fake_event(struct dahdi_pvt *p, int mode)
15303 {
15304 if (p) {
15305 switch (mode) {
15306 case TRANSFER:
15307 p->fake_event = DAHDI_EVENT_WINKFLASH;
15308 break;
15309 case HANGUP:
15310 p->fake_event = DAHDI_EVENT_ONHOOK;
15311 break;
15312 default:
15313 ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);
15314 }
15315 }
15316 return 0;
15317 }
15318 static struct dahdi_pvt *find_channel(int channel)
15319 {
15320 struct dahdi_pvt *p = iflist;
15321 while (p) {
15322 if (p->channel == channel) {
15323 break;
15324 }
15325 p = p->next;
15326 }
15327 return p;
15328 }
15329
15330 static int action_dahdidndon(struct mansession *s, const struct message *m)
15331 {
15332 struct dahdi_pvt *p = NULL;
15333 const char *channel = astman_get_header(m, "DAHDIChannel");
15334
15335 if (ast_strlen_zero(channel)) {
15336 astman_send_error(s, m, "No channel specified");
15337 return 0;
15338 }
15339 p = find_channel(atoi(channel));
15340 if (!p) {
15341 astman_send_error(s, m, "No such channel");
15342 return 0;
15343 }
15344 p->dnd = 1;
15345 astman_send_ack(s, m, "DND Enabled");
15346 return 0;
15347 }
15348
15349 static int action_dahdidndoff(struct mansession *s, const struct message *m)
15350 {
15351 struct dahdi_pvt *p = NULL;
15352 const char *channel = astman_get_header(m, "DAHDIChannel");
15353
15354 if (ast_strlen_zero(channel)) {
15355 astman_send_error(s, m, "No channel specified");
15356 return 0;
15357 }
15358 p = find_channel(atoi(channel));
15359 if (!p) {
15360 astman_send_error(s, m, "No such channel");
15361 return 0;
15362 }
15363 p->dnd = 0;
15364 astman_send_ack(s, m, "DND Disabled");
15365 return 0;
15366 }
15367
15368 static int action_transfer(struct mansession *s, const struct message *m)
15369 {
15370 struct dahdi_pvt *p = NULL;
15371 const char *channel = astman_get_header(m, "DAHDIChannel");
15372
15373 if (ast_strlen_zero(channel)) {
15374 astman_send_error(s, m, "No channel specified");
15375 return 0;
15376 }
15377 p = find_channel(atoi(channel));
15378 if (!p) {
15379 astman_send_error(s, m, "No such channel");
15380 return 0;
15381 }
15382 dahdi_fake_event(p,TRANSFER);
15383 astman_send_ack(s, m, "DAHDITransfer");
15384 return 0;
15385 }
15386
15387 static int action_transferhangup(struct mansession *s, const struct message *m)
15388 {
15389 struct dahdi_pvt *p = NULL;
15390 const char *channel = astman_get_header(m, "DAHDIChannel");
15391
15392 if (ast_strlen_zero(channel)) {
15393 astman_send_error(s, m, "No channel specified");
15394 return 0;
15395 }
15396 p = find_channel(atoi(channel));
15397 if (!p) {
15398 astman_send_error(s, m, "No such channel");
15399 return 0;
15400 }
15401 dahdi_fake_event(p,HANGUP);
15402 astman_send_ack(s, m, "DAHDIHangup");
15403 return 0;
15404 }
15405
15406 static int action_dahdidialoffhook(struct mansession *s, const struct message *m)
15407 {
15408 struct dahdi_pvt *p = NULL;
15409 const char *channel = astman_get_header(m, "DAHDIChannel");
15410 const char *number = astman_get_header(m, "Number");
15411 int i;
15412
15413 if (ast_strlen_zero(channel)) {
15414 astman_send_error(s, m, "No channel specified");
15415 return 0;
15416 }
15417 if (ast_strlen_zero(number)) {
15418 astman_send_error(s, m, "No number specified");
15419 return 0;
15420 }
15421 p = find_channel(atoi(channel));
15422 if (!p) {
15423 astman_send_error(s, m, "No such channel");
15424 return 0;
15425 }
15426 if (!p->owner) {
15427 astman_send_error(s, m, "Channel does not have it's owner");
15428 return 0;
15429 }
15430 for (i = 0; i < strlen(number); i++) {
15431 struct ast_frame f = { AST_FRAME_DTMF, number[i] };
15432 dahdi_queue_frame(p, &f, NULL);
15433 }
15434 astman_send_ack(s, m, "DAHDIDialOffhook");
15435 return 0;
15436 }
15437
15438 static int action_dahdishowchannels(struct mansession *s, const struct message *m)
15439 {
15440 struct dahdi_pvt *tmp = NULL;
15441 const char *id = astman_get_header(m, "ActionID");
15442 const char *dahdichannel = astman_get_header(m, "DAHDIChannel");
15443 char idText[256] = "";
15444 int channels = 0;
15445 int dahdichanquery = -1;
15446 if (!ast_strlen_zero(dahdichannel)) {
15447 dahdichanquery = atoi(dahdichannel);
15448 }
15449
15450 astman_send_ack(s, m, "DAHDI channel status will follow");
15451 if (!ast_strlen_zero(id))
15452 snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
15453
15454 ast_mutex_lock(&iflock);
15455
15456 tmp = iflist;
15457 while (tmp) {
15458 if (tmp->channel > 0) {
15459 int alm = get_alarms(tmp);
15460
15461
15462 if (dahdichanquery > 0 && tmp->channel != dahdichanquery)
15463 continue;
15464
15465 channels++;
15466 if (tmp->owner) {
15467
15468 astman_append(s,
15469 "Event: DAHDIShowChannels\r\n"
15470 "DAHDIChannel: %d\r\n"
15471 "Channel: %s\r\n"
15472 "Uniqueid: %s\r\n"
15473 "AccountCode: %s\r\n"
15474 "Signalling: %s\r\n"
15475 "SignallingCode: %d\r\n"
15476 "Context: %s\r\n"
15477 "DND: %s\r\n"
15478 "Alarm: %s\r\n"
15479 "%s"
15480 "\r\n",
15481 tmp->channel,
15482 tmp->owner->name,
15483 tmp->owner->uniqueid,
15484 tmp->owner->accountcode,
15485 sig2str(tmp->sig),
15486 tmp->sig,
15487 tmp->context,
15488 tmp->dnd ? "Enabled" : "Disabled",
15489 alarm2str(alm), idText);
15490 } else {
15491 astman_append(s,
15492 "Event: DAHDIShowChannels\r\n"
15493 "DAHDIChannel: %d\r\n"
15494 "Signalling: %s\r\n"
15495 "SignallingCode: %d\r\n"
15496 "Context: %s\r\n"
15497 "DND: %s\r\n"
15498 "Alarm: %s\r\n"
15499 "%s"
15500 "\r\n",
15501 tmp->channel, sig2str(tmp->sig), tmp->sig,
15502 tmp->context,
15503 tmp->dnd ? "Enabled" : "Disabled",
15504 alarm2str(alm), idText);
15505 }
15506 }
15507
15508 tmp = tmp->next;
15509 }
15510
15511 ast_mutex_unlock(&iflock);
15512
15513 astman_append(s,
15514 "Event: DAHDIShowChannelsComplete\r\n"
15515 "%s"
15516 "Items: %d\r\n"
15517 "\r\n",
15518 idText,
15519 channels);
15520 return 0;
15521 }
15522
15523 #if defined(HAVE_SS7)
15524 static int linkset_addsigchan(int sigchan)
15525 {
15526 struct dahdi_ss7 *link;
15527 int res;
15528 int curfd;
15529 struct dahdi_params p;
15530 struct dahdi_bufferinfo bi;
15531 struct dahdi_spaninfo si;
15532
15533
15534 link = ss7_resolve_linkset(cur_linkset);
15535 if (!link) {
15536 ast_log(LOG_ERROR, "Invalid linkset number. Must be between 1 and %d\n", NUM_SPANS + 1);
15537 return -1;
15538 }
15539
15540 if (cur_ss7type < 0) {
15541 ast_log(LOG_ERROR, "Unspecified or invalid ss7type\n");
15542 return -1;
15543 }
15544
15545 if (!link->ss7)
15546 link->ss7 = ss7_new(cur_ss7type);
15547
15548 if (!link->ss7) {
15549 ast_log(LOG_ERROR, "Can't create new SS7!\n");
15550 return -1;
15551 }
15552
15553 link->type = cur_ss7type;
15554
15555 if (cur_pointcode < 0) {
15556 ast_log(LOG_ERROR, "Unspecified pointcode!\n");
15557 return -1;
15558 } else
15559 ss7_set_pc(link->ss7, cur_pointcode);
15560
15561 if (sigchan < 0) {
15562 ast_log(LOG_ERROR, "Invalid sigchan!\n");
15563 return -1;
15564 } else {
15565 if (link->numsigchans >= NUM_DCHANS) {
15566 ast_log(LOG_ERROR, "Too many sigchans on linkset %d\n", cur_linkset);
15567 return -1;
15568 }
15569 curfd = link->numsigchans;
15570
15571 link->fds[curfd] = open("/dev/dahdi/channel", O_RDWR, 0600);
15572 if ((link->fds[curfd] < 0) || (ioctl(link->fds[curfd],DAHDI_SPECIFY,&sigchan) == -1)) {
15573 ast_log(LOG_ERROR, "Unable to open SS7 sigchan %d (%s)\n", sigchan, strerror(errno));
15574 return -1;
15575 }
15576 memset(&p, 0, sizeof(p));
15577 res = ioctl(link->fds[curfd], DAHDI_GET_PARAMS, &p);
15578 if (res) {
15579 dahdi_close_ss7_fd(link, curfd);
15580 ast_log(LOG_ERROR, "Unable to get parameters for sigchan %d (%s)\n", sigchan, strerror(errno));
15581 return -1;
15582 }
15583 if ((p.sigtype != DAHDI_SIG_HDLCFCS) && (p.sigtype != DAHDI_SIG_HARDHDLC) && (p.sigtype != DAHDI_SIG_MTP2)) {
15584 dahdi_close_ss7_fd(link, curfd);
15585 ast_log(LOG_ERROR, "sigchan %d is not in HDLC/FCS mode.\n", sigchan);
15586 return -1;
15587 }
15588
15589 memset(&bi, 0, sizeof(bi));
15590 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
15591 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
15592 bi.numbufs = 32;
15593 bi.bufsize = 512;
15594
15595 if (ioctl(link->fds[curfd], DAHDI_SET_BUFINFO, &bi)) {
15596 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d: %s\n", sigchan, strerror(errno));
15597 dahdi_close_ss7_fd(link, curfd);
15598 return -1;
15599 }
15600
15601 if (p.sigtype == DAHDI_SIG_MTP2)
15602 ss7_add_link(link->ss7, SS7_TRANSPORT_DAHDIMTP2, link->fds[curfd]);
15603 else
15604 ss7_add_link(link->ss7, SS7_TRANSPORT_DAHDIDCHAN, link->fds[curfd]);
15605
15606 link->numsigchans++;
15607
15608 memset(&si, 0, sizeof(si));
15609 res = ioctl(link->fds[curfd], DAHDI_SPANSTAT, &si);
15610 if (res) {
15611 dahdi_close_ss7_fd(link, curfd);
15612 ast_log(LOG_ERROR, "Unable to get span state for sigchan %d (%s)\n", sigchan, strerror(errno));
15613 }
15614
15615 if (!si.alarms) {
15616 link->linkstate[curfd] = LINKSTATE_DOWN;
15617 ss7_link_noalarm(link->ss7, link->fds[curfd]);
15618 } else {
15619 link->linkstate[curfd] = LINKSTATE_DOWN | LINKSTATE_INALARM;
15620 ss7_link_alarm(link->ss7, link->fds[curfd]);
15621 }
15622 }
15623
15624 if (cur_adjpointcode < 0) {
15625 ast_log(LOG_ERROR, "Unspecified adjpointcode!\n");
15626 return -1;
15627 } else {
15628 ss7_set_adjpc(link->ss7, link->fds[curfd], cur_adjpointcode);
15629 }
15630
15631 if (cur_defaultdpc < 0) {
15632 ast_log(LOG_ERROR, "Unspecified defaultdpc!\n");
15633 return -1;
15634 }
15635
15636 if (cur_networkindicator < 0) {
15637 ast_log(LOG_ERROR, "Invalid networkindicator!\n");
15638 return -1;
15639 } else
15640 ss7_set_network_ind(link->ss7, cur_networkindicator);
15641
15642 return 0;
15643 }
15644 #endif
15645
15646 #if defined(HAVE_SS7)
15647 static char *handle_ss7_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15648 {
15649 int span;
15650 switch (cmd) {
15651 case CLI_INIT:
15652 e->command = "ss7 set debug {on|off} linkset";
15653 e->usage =
15654 "Usage: ss7 set debug {on|off} linkset <linkset>\n"
15655 " Enables debugging on a given SS7 linkset\n";
15656 return NULL;
15657 case CLI_GENERATE:
15658 return NULL;
15659 }
15660 if (a->argc < 6)
15661 return CLI_SHOWUSAGE;
15662 span = atoi(a->argv[5]);
15663 if ((span < 1) || (span > NUM_SPANS)) {
15664 ast_cli(a->fd, "Invalid linkset %s. Should be a number from %d to %d\n", a->argv[5], 1, NUM_SPANS);
15665 return CLI_SUCCESS;
15666 }
15667 if (!linksets[span-1].ss7) {
15668 ast_cli(a->fd, "No SS7 running on linkset %d\n", span);
15669 return CLI_SUCCESS;
15670 }
15671 if (linksets[span-1].ss7) {
15672 if (!strcasecmp(a->argv[3], "on")) {
15673 ss7_set_debug(linksets[span-1].ss7, SS7_DEBUG_MTP2 | SS7_DEBUG_MTP3 | SS7_DEBUG_ISUP);
15674 ast_cli(a->fd, "Enabled debugging on linkset %d\n", span);
15675 } else {
15676 ss7_set_debug(linksets[span-1].ss7, 0);
15677 ast_cli(a->fd, "Disabled debugging on linkset %d\n", span);
15678 }
15679 }
15680
15681 return CLI_SUCCESS;
15682 }
15683 #endif
15684
15685 #if defined(HAVE_SS7)
15686 static char *handle_ss7_block_cic(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15687 {
15688 int linkset, cic;
15689 int blocked = -1, i;
15690 switch (cmd) {
15691 case CLI_INIT:
15692 e->command = "ss7 block cic";
15693 e->usage =
15694 "Usage: ss7 block cic <linkset> <CIC>\n"
15695 " Sends a remote blocking request for the given CIC on the specified linkset\n";
15696 return NULL;
15697 case CLI_GENERATE:
15698 return NULL;
15699 }
15700 if (a->argc == 5)
15701 linkset = atoi(a->argv[3]);
15702 else
15703 return CLI_SHOWUSAGE;
15704
15705 if ((linkset < 1) || (linkset > NUM_SPANS)) {
15706 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
15707 return CLI_SUCCESS;
15708 }
15709
15710 if (!linksets[linkset-1].ss7) {
15711 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
15712 return CLI_SUCCESS;
15713 }
15714
15715 cic = atoi(a->argv[4]);
15716
15717 if (cic < 1) {
15718 ast_cli(a->fd, "Invalid CIC specified!\n");
15719 return CLI_SUCCESS;
15720 }
15721
15722 for (i = 0; i < linksets[linkset-1].numchans; i++) {
15723 if (linksets[linkset-1].pvts[i]->cic == cic) {
15724 blocked = linksets[linkset-1].pvts[i]->locallyblocked;
15725 if (!blocked) {
15726 ast_mutex_lock(&linksets[linkset-1].lock);
15727 isup_blo(linksets[linkset-1].ss7, cic, linksets[linkset-1].pvts[i]->dpc);
15728 ast_mutex_unlock(&linksets[linkset-1].lock);
15729 }
15730 }
15731 }
15732
15733 if (blocked < 0) {
15734 ast_cli(a->fd, "Invalid CIC specified!\n");
15735 return CLI_SUCCESS;
15736 }
15737
15738 if (!blocked)
15739 ast_cli(a->fd, "Sent blocking request for linkset %d on CIC %d\n", linkset, cic);
15740 else
15741 ast_cli(a->fd, "CIC %d already locally blocked\n", cic);
15742
15743
15744 pthread_kill(linksets[linkset-1].master, SIGURG);
15745
15746 return CLI_SUCCESS;
15747 }
15748 #endif
15749
15750 #if defined(HAVE_SS7)
15751 static char *handle_ss7_block_linkset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15752 {
15753 int linkset;
15754 int i;
15755 switch (cmd) {
15756 case CLI_INIT:
15757 e->command = "ss7 block linkset";
15758 e->usage =
15759 "Usage: ss7 block linkset <linkset number>\n"
15760 " Sends a remote blocking request for all CICs on the given linkset\n";
15761 return NULL;
15762 case CLI_GENERATE:
15763 return NULL;
15764 }
15765 if (a->argc == 4)
15766 linkset = atoi(a->argv[3]);
15767 else
15768 return CLI_SHOWUSAGE;
15769
15770 if ((linkset < 1) || (linkset > NUM_SPANS)) {
15771 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
15772 return CLI_SUCCESS;
15773 }
15774
15775 if (!linksets[linkset-1].ss7) {
15776 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
15777 return CLI_SUCCESS;
15778 }
15779
15780 for (i = 0; i < linksets[linkset-1].numchans; i++) {
15781 ast_cli(a->fd, "Sending remote blocking request on CIC %d\n", linksets[linkset-1].pvts[i]->cic);
15782 ast_mutex_lock(&linksets[linkset-1].lock);
15783 isup_blo(linksets[linkset-1].ss7, linksets[linkset-1].pvts[i]->cic, linksets[linkset-1].pvts[i]->dpc);
15784 ast_mutex_unlock(&linksets[linkset-1].lock);
15785 }
15786
15787
15788 pthread_kill(linksets[linkset-1].master, SIGURG);
15789
15790 return CLI_SUCCESS;
15791 }
15792 #endif
15793
15794 #if defined(HAVE_SS7)
15795 static char *handle_ss7_unblock_cic(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15796 {
15797 int linkset, cic;
15798 int i, blocked = -1;
15799 switch (cmd) {
15800 case CLI_INIT:
15801 e->command = "ss7 unblock cic";
15802 e->usage =
15803 "Usage: ss7 unblock cic <linkset> <CIC>\n"
15804 " Sends a remote unblocking request for the given CIC on the specified linkset\n";
15805 return NULL;
15806 case CLI_GENERATE:
15807 return NULL;
15808 }
15809
15810 if (a->argc == 5)
15811 linkset = atoi(a->argv[3]);
15812 else
15813 return CLI_SHOWUSAGE;
15814
15815 if ((linkset < 1) || (linkset > NUM_SPANS)) {
15816 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
15817 return CLI_SUCCESS;
15818 }
15819
15820 if (!linksets[linkset-1].ss7) {
15821 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
15822 return CLI_SUCCESS;
15823 }
15824
15825 cic = atoi(a->argv[4]);
15826
15827 if (cic < 1) {
15828 ast_cli(a->fd, "Invalid CIC specified!\n");
15829 return CLI_SUCCESS;
15830 }
15831
15832 for (i = 0; i < linksets[linkset-1].numchans; i++) {
15833 if (linksets[linkset-1].pvts[i]->cic == cic) {
15834 blocked = linksets[linkset-1].pvts[i]->locallyblocked;
15835 if (blocked) {
15836 ast_mutex_lock(&linksets[linkset-1].lock);
15837 isup_ubl(linksets[linkset-1].ss7, cic, linksets[linkset-1].pvts[i]->dpc);
15838 ast_mutex_unlock(&linksets[linkset-1].lock);
15839 }
15840 }
15841 }
15842
15843 if (blocked > 0)
15844 ast_cli(a->fd, "Sent unblocking request for linkset %d on CIC %d\n", linkset, cic);
15845
15846
15847 pthread_kill(linksets[linkset-1].master, SIGURG);
15848
15849 return CLI_SUCCESS;
15850 }
15851 #endif
15852
15853 #if defined(HAVE_SS7)
15854 static char *handle_ss7_unblock_linkset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15855 {
15856 int linkset;
15857 int i;
15858 switch (cmd) {
15859 case CLI_INIT:
15860 e->command = "ss7 unblock linkset";
15861 e->usage =
15862 "Usage: ss7 unblock linkset <linkset number>\n"
15863 " Sends a remote unblocking request for all CICs on the specified linkset\n";
15864 return NULL;
15865 case CLI_GENERATE:
15866 return NULL;
15867 }
15868
15869 if (a->argc == 4)
15870 linkset = atoi(a->argv[3]);
15871 else
15872 return CLI_SHOWUSAGE;
15873
15874 if ((linkset < 1) || (linkset > NUM_SPANS)) {
15875 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
15876 return CLI_SUCCESS;
15877 }
15878
15879 if (!linksets[linkset-1].ss7) {
15880 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
15881 return CLI_SUCCESS;
15882 }
15883
15884 for (i = 0; i < linksets[linkset-1].numchans; i++) {
15885 ast_cli(a->fd, "Sending remote unblock request on CIC %d\n", linksets[linkset-1].pvts[i]->cic);
15886 ast_mutex_lock(&linksets[linkset-1].lock);
15887 isup_ubl(linksets[linkset-1].ss7, linksets[linkset-1].pvts[i]->cic, linksets[linkset-1].pvts[i]->dpc);
15888 ast_mutex_unlock(&linksets[linkset-1].lock);
15889 }
15890
15891
15892 pthread_kill(linksets[linkset-1].master, SIGURG);
15893
15894 return CLI_SUCCESS;
15895 }
15896 #endif
15897
15898 #if defined(HAVE_SS7)
15899 static char *handle_ss7_show_linkset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15900 {
15901 int linkset;
15902 struct dahdi_ss7 *ss7;
15903 switch (cmd) {
15904 case CLI_INIT:
15905 e->command = "ss7 show linkset";
15906 e->usage =
15907 "Usage: ss7 show linkset <span>\n"
15908 " Shows the status of an SS7 linkset.\n";
15909 return NULL;
15910 case CLI_GENERATE:
15911 return NULL;
15912 }
15913
15914 if (a->argc < 4)
15915 return CLI_SHOWUSAGE;
15916 linkset = atoi(a->argv[3]);
15917 if ((linkset < 1) || (linkset > NUM_SPANS)) {
15918 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
15919 return CLI_SUCCESS;
15920 }
15921 if (!linksets[linkset-1].ss7) {
15922 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
15923 return CLI_SUCCESS;
15924 }
15925 if (linksets[linkset-1].ss7)
15926 ss7 = &linksets[linkset-1];
15927
15928 ast_cli(a->fd, "SS7 linkset %d status: %s\n", linkset, (ss7->state == LINKSET_STATE_UP) ? "Up" : "Down");
15929
15930 return CLI_SUCCESS;
15931 }
15932 #endif
15933
15934 #if defined(HAVE_SS7)
15935 static char *handle_ss7_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15936 {
15937 switch (cmd) {
15938 case CLI_INIT:
15939 e->command = "ss7 show version";
15940 e->usage =
15941 "Usage: ss7 show version\n"
15942 " Show the libss7 version\n";
15943 return NULL;
15944 case CLI_GENERATE:
15945 return NULL;
15946 }
15947
15948 ast_cli(a->fd, "libss7 version: %s\n", ss7_get_version());
15949
15950 return CLI_SUCCESS;
15951 }
15952 #endif
15953
15954 #if defined(HAVE_SS7)
15955 static struct ast_cli_entry dahdi_ss7_cli[] = {
15956 AST_CLI_DEFINE(handle_ss7_debug, "Enables SS7 debugging on a linkset"),
15957 AST_CLI_DEFINE(handle_ss7_block_cic, "Blocks the given CIC"),
15958 AST_CLI_DEFINE(handle_ss7_unblock_cic, "Unblocks the given CIC"),
15959 AST_CLI_DEFINE(handle_ss7_block_linkset, "Blocks all CICs on a linkset"),
15960 AST_CLI_DEFINE(handle_ss7_unblock_linkset, "Unblocks all CICs on a linkset"),
15961 AST_CLI_DEFINE(handle_ss7_show_linkset, "Shows the status of a linkset"),
15962 AST_CLI_DEFINE(handle_ss7_version, "Displays libss7 version"),
15963 };
15964 #endif
15965
15966 static int __unload_module(void)
15967 {
15968 struct dahdi_pvt *p;
15969 #if defined(HAVE_PRI) || defined(HAVE_SS7)
15970 int i, j;
15971 #endif
15972
15973 #ifdef HAVE_PRI
15974 for (i = 0; i < NUM_SPANS; i++) {
15975 if (pris[i].master != AST_PTHREADT_NULL)
15976 pthread_cancel(pris[i].master);
15977 }
15978 ast_cli_unregister_multiple(dahdi_pri_cli, ARRAY_LEN(dahdi_pri_cli));
15979 ast_unregister_application(dahdi_send_keypad_facility_app);
15980 #ifdef HAVE_PRI_PROG_W_CAUSE
15981 ast_unregister_application(dahdi_send_callrerouting_facility_app);
15982 #endif
15983 #endif
15984 #if defined(HAVE_SS7)
15985 for (i = 0; i < NUM_SPANS; i++) {
15986 if (linksets[i].master != AST_PTHREADT_NULL)
15987 pthread_cancel(linksets[i].master);
15988 }
15989 ast_cli_unregister_multiple(dahdi_ss7_cli, ARRAY_LEN(dahdi_ss7_cli));
15990 #endif
15991 #if defined(HAVE_OPENR2)
15992 dahdi_r2_destroy_links();
15993 ast_cli_unregister_multiple(dahdi_mfcr2_cli, ARRAY_LEN(dahdi_mfcr2_cli));
15994 ast_unregister_application(dahdi_accept_r2_call_app);
15995 #endif
15996
15997 ast_cli_unregister_multiple(dahdi_cli, ARRAY_LEN(dahdi_cli));
15998 ast_manager_unregister( "DAHDIDialOffhook" );
15999 ast_manager_unregister( "DAHDIHangup" );
16000 ast_manager_unregister( "DAHDITransfer" );
16001 ast_manager_unregister( "DAHDIDNDoff" );
16002 ast_manager_unregister( "DAHDIDNDon" );
16003 ast_manager_unregister("DAHDIShowChannels");
16004 ast_manager_unregister("DAHDIRestart");
16005 ast_channel_unregister(&dahdi_tech);
16006 ast_mutex_lock(&iflock);
16007
16008 p = iflist;
16009 while (p) {
16010 if (p->owner)
16011 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
16012 p = p->next;
16013 }
16014 ast_mutex_unlock(&iflock);
16015 ast_mutex_lock(&monlock);
16016 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
16017 pthread_cancel(monitor_thread);
16018 pthread_kill(monitor_thread, SIGURG);
16019 pthread_join(monitor_thread, NULL);
16020 }
16021 monitor_thread = AST_PTHREADT_STOP;
16022 ast_mutex_unlock(&monlock);
16023
16024 destroy_all_channels();
16025
16026 #if defined(HAVE_PRI)
16027 for (i = 0; i < NUM_SPANS; i++) {
16028 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
16029 pthread_join(pris[i].master, NULL);
16030 for (j = 0; j < NUM_DCHANS; j++) {
16031 dahdi_close_pri_fd(&(pris[i]), j);
16032 }
16033 }
16034 #endif
16035
16036 #if defined(HAVE_SS7)
16037 for (i = 0; i < NUM_SPANS; i++) {
16038 if (linksets[i].master && (linksets[i].master != AST_PTHREADT_NULL))
16039 pthread_join(linksets[i].master, NULL);
16040 for (j = 0; j < NUM_DCHANS; j++) {
16041 dahdi_close_ss7_fd(&(linksets[i]), j);
16042 }
16043 }
16044 #endif
16045 ast_cond_destroy(&ss_thread_complete);
16046 return 0;
16047 }
16048
16049 static int unload_module(void)
16050 {
16051 #if defined(HAVE_PRI) || defined(HAVE_SS7)
16052 int y;
16053 #endif
16054 #ifdef HAVE_PRI
16055 for (y = 0; y < NUM_SPANS; y++)
16056 ast_mutex_destroy(&pris[y].lock);
16057 #endif
16058 #ifdef HAVE_SS7
16059 for (y = 0; y < NUM_SPANS; y++)
16060 ast_mutex_destroy(&linksets[y].lock);
16061 #endif
16062 return __unload_module();
16063 }
16064
16065 static int build_channels(struct dahdi_chan_conf *conf, int iscrv, const char *value, int reload, int lineno, int *found_pseudo)
16066 {
16067 char *c, *chan;
16068 int x, start, finish;
16069 struct dahdi_pvt *tmp;
16070 #ifdef HAVE_PRI
16071 struct dahdi_pri *pri;
16072 int trunkgroup, y;
16073 #endif
16074
16075 if ((reload == 0) && (conf->chan.sig < 0) && !conf->is_sig_auto) {
16076 ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
16077 return -1;
16078 }
16079
16080 c = ast_strdupa(value);
16081
16082 #ifdef HAVE_PRI
16083 pri = NULL;
16084 if (iscrv) {
16085 if (sscanf(c, "%30d:%n", &trunkgroup, &y) != 1) {
16086 ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d.\n", lineno);
16087 return -1;
16088 }
16089 if (trunkgroup < 1) {
16090 ast_log(LOG_WARNING, "CRV trunk group must be a positive number at line %d.\n", lineno);
16091 return -1;
16092 }
16093 c += y;
16094 for (y = 0; y < NUM_SPANS; y++) {
16095 if (pris[y].trunkgroup == trunkgroup) {
16096 pri = pris + y;
16097 break;
16098 }
16099 }
16100 if (!pri) {
16101 ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d.\n", trunkgroup, lineno);
16102 return -1;
16103 }
16104 }
16105 #endif
16106
16107 while ((chan = strsep(&c, ","))) {
16108 if (sscanf(chan, "%30d-%30d", &start, &finish) == 2) {
16109
16110 } else if (sscanf(chan, "%30d", &start)) {
16111
16112 finish = start;
16113 } else if (!strcasecmp(chan, "pseudo")) {
16114 finish = start = CHAN_PSEUDO;
16115 if (found_pseudo)
16116 *found_pseudo = 1;
16117 } else {
16118 ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", value, chan);
16119 return -1;
16120 }
16121 if (finish < start) {
16122 ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
16123 x = finish;
16124 finish = start;
16125 start = x;
16126 }
16127
16128 for (x = start; x <= finish; x++) {
16129 #ifdef HAVE_PRI
16130 tmp = mkintf(x, conf, pri, reload);
16131 #else
16132 tmp = mkintf(x, conf, NULL, reload);
16133 #endif
16134
16135 if (tmp) {
16136 #ifdef HAVE_PRI
16137 if (pri)
16138 ast_verb(3, "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup, x, sig2str(tmp->sig));
16139 else
16140 #endif
16141 ast_verb(3, "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig));
16142 } else {
16143 ast_log(LOG_ERROR, "Unable to %s channel '%s'\n",
16144 (reload == 1) ? "reconfigure" : "register", value);
16145 return -1;
16146 }
16147 }
16148 }
16149
16150 return 0;
16151 }
16152
16153
16154
16155 #define MAX_CHANLIST_LEN 80
16156
16157 static void process_echocancel(struct dahdi_chan_conf *confp, const char *data, unsigned int line)
16158 {
16159 char *parse = ast_strdupa(data);
16160 char *params[DAHDI_MAX_ECHOCANPARAMS + 1];
16161 unsigned int param_count;
16162 unsigned int x;
16163
16164 if (!(param_count = ast_app_separate_args(parse, ',', params, ARRAY_LEN(params))))
16165 return;
16166
16167 memset(&confp->chan.echocancel, 0, sizeof(confp->chan.echocancel));
16168
16169
16170
16171 x = ast_strlen_zero(params[0]) ? 0 : atoi(params[0]);
16172
16173 if ((x == 32) || (x == 64) || (x == 128) || (x == 256) || (x == 512) || (x == 1024))
16174 confp->chan.echocancel.head.tap_length = x;
16175 else if ((confp->chan.echocancel.head.tap_length = ast_true(params[0])))
16176 confp->chan.echocancel.head.tap_length = 128;
16177
16178
16179
16180 for (x = 1; x < param_count; x++) {
16181 struct {
16182 char *name;
16183 char *value;
16184 } param;
16185
16186 if (ast_app_separate_args(params[x], '=', (char **) ¶m, 2) < 1) {
16187 ast_log(LOG_WARNING, "Invalid echocancel parameter supplied at line %d: '%s'\n", line, params[x]);
16188 continue;
16189 }
16190
16191 if (ast_strlen_zero(param.name) || (strlen(param.name) > sizeof(confp->chan.echocancel.params[0].name)-1)) {
16192 ast_log(LOG_WARNING, "Invalid echocancel parameter supplied at line %d: '%s'\n", line, param.name);
16193 continue;
16194 }
16195
16196 strcpy(confp->chan.echocancel.params[confp->chan.echocancel.head.param_count].name, param.name);
16197
16198 if (param.value) {
16199 if (sscanf(param.value, "%30d", &confp->chan.echocancel.params[confp->chan.echocancel.head.param_count].value) != 1) {
16200 ast_log(LOG_WARNING, "Invalid echocancel parameter value supplied at line %d: '%s'\n", line, param.value);
16201 continue;
16202 }
16203 }
16204 confp->chan.echocancel.head.param_count++;
16205 }
16206 }
16207
16208
16209 #define PROC_DAHDI_OPT_NOCHAN (1 << 0)
16210
16211 #define PROC_DAHDI_OPT_NOWARN (1 << 1)
16212
16213 static int process_dahdi(struct dahdi_chan_conf *confp, const char *cat, struct ast_variable *v, int reload, int options)
16214 {
16215 struct dahdi_pvt *tmp;
16216 int y;
16217 int found_pseudo = 0;
16218 char dahdichan[MAX_CHANLIST_LEN] = {};
16219
16220 for (; v; v = v->next) {
16221 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
16222 continue;
16223
16224
16225 if (!strcasecmp(v->name, "parkinglot")) {
16226 ast_copy_string(confp->chan.parkinglot, v->value, sizeof(confp->chan.parkinglot));
16227 }
16228
16229
16230 if (!strcasecmp(v->name, "channel")
16231 #ifdef HAVE_PRI
16232 || !strcasecmp(v->name, "crv")
16233 #endif
16234 ) {
16235 int iscrv;
16236 if (options & PROC_DAHDI_OPT_NOCHAN) {
16237 ast_log(LOG_WARNING, "Channel '%s' ignored.\n", v->value);
16238 continue;
16239 }
16240 iscrv = !strcasecmp(v->name, "crv");
16241 if (build_channels(confp, iscrv, v->value, reload, v->lineno, &found_pseudo))
16242 return -1;
16243 ast_log(LOG_DEBUG, "Channel '%s' configured.\n", v->value);
16244 } else if (!strcasecmp(v->name, "buffers")) {
16245 if (parse_buffers_policy(v->value, &confp->chan.buf_no, &confp->chan.buf_policy)) {
16246 ast_log(LOG_WARNING, "Using default buffer policy.\n");
16247 confp->chan.buf_no = numbufs;
16248 confp->chan.buf_policy = DAHDI_POLICY_IMMEDIATE;
16249 }
16250 } else if (!strcasecmp(v->name, "faxbuffers")) {
16251 if (!parse_buffers_policy(v->value, &confp->chan.faxbuf_no, &confp->chan.faxbuf_policy)) {
16252 confp->chan.usefaxbuffers = 1;
16253 }
16254 } else if (!strcasecmp(v->name, "dahdichan")) {
16255 ast_copy_string(dahdichan, v->value, sizeof(dahdichan));
16256 } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
16257 usedistinctiveringdetection = ast_true(v->value);
16258 } else if (!strcasecmp(v->name, "distinctiveringaftercid")) {
16259 distinctiveringaftercid = ast_true(v->value);
16260 } else if (!strcasecmp(v->name, "dring1context")) {
16261 ast_copy_string(confp->chan.drings.ringContext[0].contextData,v->value,sizeof(confp->chan.drings.ringContext[0].contextData));
16262 } else if (!strcasecmp(v->name, "dring2context")) {
16263 ast_copy_string(confp->chan.drings.ringContext[1].contextData,v->value,sizeof(confp->chan.drings.ringContext[1].contextData));
16264 } else if (!strcasecmp(v->name, "dring3context")) {
16265 ast_copy_string(confp->chan.drings.ringContext[2].contextData,v->value,sizeof(confp->chan.drings.ringContext[2].contextData));
16266 } else if (!strcasecmp(v->name, "dring1range")) {
16267 confp->chan.drings.ringnum[0].range = atoi(v->value);
16268 } else if (!strcasecmp(v->name, "dring2range")) {
16269 confp->chan.drings.ringnum[1].range = atoi(v->value);
16270 } else if (!strcasecmp(v->name, "dring3range")) {
16271 confp->chan.drings.ringnum[2].range = atoi(v->value);
16272 } else if (!strcasecmp(v->name, "dring1")) {
16273 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]);
16274 } else if (!strcasecmp(v->name, "dring2")) {
16275 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]);
16276 } else if (!strcasecmp(v->name, "dring3")) {
16277 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]);
16278 } else if (!strcasecmp(v->name, "usecallerid")) {
16279 confp->chan.use_callerid = ast_true(v->value);
16280 } else if (!strcasecmp(v->name, "cidsignalling")) {
16281 if (!strcasecmp(v->value, "bell"))
16282 confp->chan.cid_signalling = CID_SIG_BELL;
16283 else if (!strcasecmp(v->value, "v23"))
16284 confp->chan.cid_signalling = CID_SIG_V23;
16285 else if (!strcasecmp(v->value, "dtmf"))
16286 confp->chan.cid_signalling = CID_SIG_DTMF;
16287 else if (!strcasecmp(v->value, "smdi"))
16288 confp->chan.cid_signalling = CID_SIG_SMDI;
16289 else if (!strcasecmp(v->value, "v23_jp"))
16290 confp->chan.cid_signalling = CID_SIG_V23_JP;
16291 else if (ast_true(v->value))
16292 confp->chan.cid_signalling = CID_SIG_BELL;
16293 } else if (!strcasecmp(v->name, "cidstart")) {
16294 if (!strcasecmp(v->value, "ring"))
16295 confp->chan.cid_start = CID_START_RING;
16296 else if (!strcasecmp(v->value, "polarity_in"))
16297 confp->chan.cid_start = CID_START_POLARITY_IN;
16298 else if (!strcasecmp(v->value, "polarity"))
16299 confp->chan.cid_start = CID_START_POLARITY;
16300 else if (ast_true(v->value))
16301 confp->chan.cid_start = CID_START_RING;
16302 } else if (!strcasecmp(v->name, "threewaycalling")) {
16303 confp->chan.threewaycalling = ast_true(v->value);
16304 } else if (!strcasecmp(v->name, "cancallforward")) {
16305 confp->chan.cancallforward = ast_true(v->value);
16306 } else if (!strcasecmp(v->name, "relaxdtmf")) {
16307 if (ast_true(v->value))
16308 confp->chan.dtmfrelax = DSP_DIGITMODE_RELAXDTMF;
16309 else
16310 confp->chan.dtmfrelax = 0;
16311 } else if (!strcasecmp(v->name, "mailbox")) {
16312 ast_copy_string(confp->chan.mailbox, v->value, sizeof(confp->chan.mailbox));
16313 } else if (!strcasecmp(v->name, "hasvoicemail")) {
16314 if (ast_true(v->value) && ast_strlen_zero(confp->chan.mailbox)) {
16315 ast_copy_string(confp->chan.mailbox, cat, sizeof(confp->chan.mailbox));
16316 }
16317 } else if (!strcasecmp(v->name, "adsi")) {
16318 confp->chan.adsi = ast_true(v->value);
16319 } else if (!strcasecmp(v->name, "usesmdi")) {
16320 confp->chan.use_smdi = ast_true(v->value);
16321 } else if (!strcasecmp(v->name, "smdiport")) {
16322 ast_copy_string(confp->smdi_port, v->value, sizeof(confp->smdi_port));
16323 } else if (!strcasecmp(v->name, "transfer")) {
16324 confp->chan.transfer = ast_true(v->value);
16325 } else if (!strcasecmp(v->name, "canpark")) {
16326 confp->chan.canpark = ast_true(v->value);
16327 } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
16328 confp->chan.echocanbridged = ast_true(v->value);
16329 } else if (!strcasecmp(v->name, "busydetect")) {
16330 confp->chan.busydetect = ast_true(v->value);
16331 } else if (!strcasecmp(v->name, "busycount")) {
16332 confp->chan.busycount = atoi(v->value);
16333 } else if (!strcasecmp(v->name, "busypattern")) {
16334 if (sscanf(v->value, "%30d,%30d", &confp->chan.busy_tonelength, &confp->chan.busy_quietlength) != 2) {
16335 ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength at line %d.\n", v->lineno);
16336 }
16337 } else if (!strcasecmp(v->name, "callprogress")) {
16338 confp->chan.callprogress &= ~CALLPROGRESS_PROGRESS;
16339 if (ast_true(v->value))
16340 confp->chan.callprogress |= CALLPROGRESS_PROGRESS;
16341 } else if (!strcasecmp(v->name, "waitfordialtone")) {
16342 confp->chan.waitfordialtone = atoi(v->value);
16343 } else if (!strcasecmp(v->name, "faxdetect")) {
16344 confp->chan.callprogress &= ~CALLPROGRESS_FAX;
16345 if (!strcasecmp(v->value, "incoming")) {
16346 confp->chan.callprogress |= CALLPROGRESS_FAX_INCOMING;
16347 } else if (!strcasecmp(v->value, "outgoing")) {
16348 confp->chan.callprogress |= CALLPROGRESS_FAX_OUTGOING;
16349 } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
16350 confp->chan.callprogress |= CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING;
16351 } else if (!strcasecmp(v->name, "echocancel")) {
16352 process_echocancel(confp, v->value, v->lineno);
16353 } else if (!strcasecmp(v->name, "echotraining")) {
16354 if (sscanf(v->value, "%30d", &y) == 1) {
16355 if ((y < 10) || (y > 4000)) {
16356 ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 4000 ms at line %d.\n", v->lineno);
16357 } else {
16358 confp->chan.echotraining = y;
16359 }
16360 } else if (ast_true(v->value)) {
16361 confp->chan.echotraining = 400;
16362 } else
16363 confp->chan.echotraining = 0;
16364 } else if (!strcasecmp(v->name, "hidecallerid")) {
16365 confp->chan.hidecallerid = ast_true(v->value);
16366 } else if (!strcasecmp(v->name, "hidecalleridname")) {
16367 confp->chan.hidecalleridname = ast_true(v->value);
16368 } else if (!strcasecmp(v->name, "pulsedial")) {
16369 confp->chan.pulse = ast_true(v->value);
16370 } else if (!strcasecmp(v->name, "callreturn")) {
16371 confp->chan.callreturn = ast_true(v->value);
16372 } else if (!strcasecmp(v->name, "callwaiting")) {
16373 confp->chan.callwaiting = ast_true(v->value);
16374 } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
16375 confp->chan.callwaitingcallerid = ast_true(v->value);
16376 } else if (!strcasecmp(v->name, "context")) {
16377 ast_copy_string(confp->chan.context, v->value, sizeof(confp->chan.context));
16378 } else if (!strcasecmp(v->name, "language")) {
16379 ast_copy_string(confp->chan.language, v->value, sizeof(confp->chan.language));
16380 } else if (!strcasecmp(v->name, "progzone")) {
16381 ast_copy_string(progzone, v->value, sizeof(progzone));
16382 } else if (!strcasecmp(v->name, "mohinterpret")
16383 ||!strcasecmp(v->name, "musiconhold") || !strcasecmp(v->name, "musicclass")) {
16384 ast_copy_string(confp->chan.mohinterpret, v->value, sizeof(confp->chan.mohinterpret));
16385 } else if (!strcasecmp(v->name, "mohsuggest")) {
16386 ast_copy_string(confp->chan.mohsuggest, v->value, sizeof(confp->chan.mohsuggest));
16387 } else if (!strcasecmp(v->name, "parkinglot")) {
16388 ast_copy_string(parkinglot, v->value, sizeof(parkinglot));
16389 } else if (!strcasecmp(v->name, "stripmsd")) {
16390 ast_log(LOG_NOTICE, "Configuration option \"%s\" has been deprecated. Please use dialplan instead\n", v->name);
16391 confp->chan.stripmsd = atoi(v->value);
16392 } else if (!strcasecmp(v->name, "jitterbuffers")) {
16393 numbufs = atoi(v->value);
16394 } else if (!strcasecmp(v->name, "group")) {
16395 confp->chan.group = ast_get_group(v->value);
16396 } else if (!strcasecmp(v->name, "callgroup")) {
16397 if (!strcasecmp(v->value, "none"))
16398 confp->chan.callgroup = 0;
16399 else
16400 confp->chan.callgroup = ast_get_group(v->value);
16401 } else if (!strcasecmp(v->name, "pickupgroup")) {
16402 if (!strcasecmp(v->value, "none"))
16403 confp->chan.pickupgroup = 0;
16404 else
16405 confp->chan.pickupgroup = ast_get_group(v->value);
16406 } else if (!strcasecmp(v->name, "setvar")) {
16407 char *varname = ast_strdupa(v->value), *varval = NULL;
16408 struct ast_variable *tmpvar;
16409 if (varname && (varval = strchr(varname, '='))) {
16410 *varval++ = '\0';
16411 if ((tmpvar = ast_variable_new(varname, varval, ""))) {
16412 tmpvar->next = confp->chan.vars;
16413 confp->chan.vars = tmpvar;
16414 }
16415 }
16416 } else if (!strcasecmp(v->name, "immediate")) {
16417 confp->chan.immediate = ast_true(v->value);
16418 } else if (!strcasecmp(v->name, "transfertobusy")) {
16419 confp->chan.transfertobusy = ast_true(v->value);
16420 } else if (!strcasecmp(v->name, "mwimonitor")) {
16421 confp->chan.mwimonitor_neon = 0;
16422 confp->chan.mwimonitor_fsk = 0;
16423 confp->chan.mwimonitor_rpas = 0;
16424 if (strcasestr(v->value, "fsk")) {
16425 confp->chan.mwimonitor_fsk = 1;
16426 }
16427 if (strcasestr(v->value, "rpas")) {
16428 confp->chan.mwimonitor_rpas = 1;
16429 }
16430 if (strcasestr(v->value, "neon")) {
16431 confp->chan.mwimonitor_neon = 1;
16432 }
16433
16434 if (ast_true(v->value)) {
16435 confp->chan.mwimonitor_fsk = 1;
16436 }
16437 } else if (!strcasecmp(v->name, "cid_rxgain")) {
16438 if (sscanf(v->value, "%30f", &confp->chan.cid_rxgain) != 1) {
16439 ast_log(LOG_WARNING, "Invalid cid_rxgain: %s at line %d.\n", v->value, v->lineno);
16440 }
16441 } else if (!strcasecmp(v->name, "rxgain")) {
16442 if (sscanf(v->value, "%30f", &confp->chan.rxgain) != 1) {
16443 ast_log(LOG_WARNING, "Invalid rxgain: %s at line %d.\n", v->value, v->lineno);
16444 }
16445 } else if (!strcasecmp(v->name, "txgain")) {
16446 if (sscanf(v->value, "%30f", &confp->chan.txgain) != 1) {
16447 ast_log(LOG_WARNING, "Invalid txgain: %s at line %d.\n", v->value, v->lineno);
16448 }
16449 } else if (!strcasecmp(v->name, "tonezone")) {
16450 if (sscanf(v->value, "%30d", &confp->chan.tonezone) != 1) {
16451 ast_log(LOG_WARNING, "Invalid tonezone: %s at line %d.\n", v->value, v->lineno);
16452 }
16453 } else if (!strcasecmp(v->name, "callerid")) {
16454 if (!strcasecmp(v->value, "asreceived")) {
16455 confp->chan.cid_num[0] = '\0';
16456 confp->chan.cid_name[0] = '\0';
16457 } else {
16458 ast_callerid_split(v->value, confp->chan.cid_name, sizeof(confp->chan.cid_name), confp->chan.cid_num, sizeof(confp->chan.cid_num));
16459 }
16460 } else if (!strcasecmp(v->name, "fullname")) {
16461 ast_copy_string(confp->chan.cid_name, v->value, sizeof(confp->chan.cid_name));
16462 } else if (!strcasecmp(v->name, "cid_number")) {
16463 ast_copy_string(confp->chan.cid_num, v->value, sizeof(confp->chan.cid_num));
16464 } else if (!strcasecmp(v->name, "useincomingcalleridondahditransfer")) {
16465 confp->chan.dahditrcallerid = ast_true(v->value);
16466 } else if (!strcasecmp(v->name, "restrictcid")) {
16467 confp->chan.restrictcid = ast_true(v->value);
16468 } else if (!strcasecmp(v->name, "usecallingpres")) {
16469 confp->chan.use_callingpres = ast_true(v->value);
16470 } else if (!strcasecmp(v->name, "accountcode")) {
16471 ast_copy_string(confp->chan.accountcode, v->value, sizeof(confp->chan.accountcode));
16472 } else if (!strcasecmp(v->name, "amaflags")) {
16473 y = ast_cdr_amaflags2int(v->value);
16474 if (y < 0)
16475 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d.\n", v->value, v->lineno);
16476 else
16477 confp->chan.amaflags = y;
16478 } else if (!strcasecmp(v->name, "polarityonanswerdelay")) {
16479 confp->chan.polarityonanswerdelay = atoi(v->value);
16480 } else if (!strcasecmp(v->name, "answeronpolarityswitch")) {
16481 confp->chan.answeronpolarityswitch = ast_true(v->value);
16482 } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) {
16483 confp->chan.hanguponpolarityswitch = ast_true(v->value);
16484 } else if (!strcasecmp(v->name, "sendcalleridafter")) {
16485 confp->chan.sendcalleridafter = atoi(v->value);
16486 } else if (!strcasecmp(v->name, "mwimonitornotify")) {
16487 ast_copy_string(mwimonitornotify, v->value, sizeof(mwimonitornotify));
16488 } else if (!strcasecmp(v->name, "mwisendtype")) {
16489 #ifndef HAVE_DAHDI_LINEREVERSE_VMWI
16490 if (!strcasecmp(v->value, "rpas")) {
16491 mwisend_rpas = 1;
16492 } else {
16493 mwisend_rpas = 0;
16494 }
16495 #else
16496
16497 memset(&confp->chan.mwisend_setting, 0, sizeof(confp->chan.mwisend_setting));
16498 if (strcasestr(v->value, "nofsk")) {
16499 confp->chan.mwisend_fsk = 0;
16500 } else {
16501 confp->chan.mwisend_fsk = 1;
16502 }
16503 if (strcasestr(v->value, "rpas")) {
16504 confp->chan.mwisend_rpas = 1;
16505 } else {
16506 confp->chan.mwisend_rpas = 0;
16507 }
16508 if (strcasestr(v->value, "lrev")) {
16509 confp->chan.mwisend_setting.vmwi_type |= DAHDI_VMWI_LREV;
16510 }
16511 if (strcasestr(v->value, "hvdc")) {
16512 confp->chan.mwisend_setting.vmwi_type |= DAHDI_VMWI_HVDC;
16513 }
16514 if ( (strcasestr(v->value, "neon")) || (strcasestr(v->value, "hvac")) ){
16515 confp->chan.mwisend_setting.vmwi_type |= DAHDI_VMWI_HVAC;
16516 }
16517 #endif
16518 } else if (reload != 1) {
16519 if (!strcasecmp(v->name, "signalling") || !strcasecmp(v->name, "signaling")) {
16520 int orig_radio = confp->chan.radio;
16521 int orig_outsigmod = confp->chan.outsigmod;
16522 int orig_auto = confp->is_sig_auto;
16523
16524 confp->chan.radio = 0;
16525 confp->chan.outsigmod = -1;
16526 confp->is_sig_auto = 0;
16527 if (!strcasecmp(v->value, "em")) {
16528 confp->chan.sig = SIG_EM;
16529 } else if (!strcasecmp(v->value, "em_e1")) {
16530 confp->chan.sig = SIG_EM_E1;
16531 } else if (!strcasecmp(v->value, "em_w")) {
16532 confp->chan.sig = SIG_EMWINK;
16533 } else if (!strcasecmp(v->value, "fxs_ls")) {
16534 confp->chan.sig = SIG_FXSLS;
16535 } else if (!strcasecmp(v->value, "fxs_gs")) {
16536 confp->chan.sig = SIG_FXSGS;
16537 } else if (!strcasecmp(v->value, "fxs_ks")) {
16538 confp->chan.sig = SIG_FXSKS;
16539 } else if (!strcasecmp(v->value, "fxo_ls")) {
16540 confp->chan.sig = SIG_FXOLS;
16541 } else if (!strcasecmp(v->value, "fxo_gs")) {
16542 confp->chan.sig = SIG_FXOGS;
16543 } else if (!strcasecmp(v->value, "fxo_ks")) {
16544 confp->chan.sig = SIG_FXOKS;
16545 } else if (!strcasecmp(v->value, "fxs_rx")) {
16546 confp->chan.sig = SIG_FXSKS;
16547 confp->chan.radio = 1;
16548 } else if (!strcasecmp(v->value, "fxo_rx")) {
16549 confp->chan.sig = SIG_FXOLS;
16550 confp->chan.radio = 1;
16551 } else if (!strcasecmp(v->value, "fxs_tx")) {
16552 confp->chan.sig = SIG_FXSLS;
16553 confp->chan.radio = 1;
16554 } else if (!strcasecmp(v->value, "fxo_tx")) {
16555 confp->chan.sig = SIG_FXOGS;
16556 confp->chan.radio = 1;
16557 } else if (!strcasecmp(v->value, "em_rx")) {
16558 confp->chan.sig = SIG_EM;
16559 confp->chan.radio = 1;
16560 } else if (!strcasecmp(v->value, "em_tx")) {
16561 confp->chan.sig = SIG_EM;
16562 confp->chan.radio = 1;
16563 } else if (!strcasecmp(v->value, "em_rxtx")) {
16564 confp->chan.sig = SIG_EM;
16565 confp->chan.radio = 2;
16566 } else if (!strcasecmp(v->value, "em_txrx")) {
16567 confp->chan.sig = SIG_EM;
16568 confp->chan.radio = 2;
16569 } else if (!strcasecmp(v->value, "sf")) {
16570 confp->chan.sig = SIG_SF;
16571 } else if (!strcasecmp(v->value, "sf_w")) {
16572 confp->chan.sig = SIG_SFWINK;
16573 } else if (!strcasecmp(v->value, "sf_featd")) {
16574 confp->chan.sig = SIG_FEATD;
16575 } else if (!strcasecmp(v->value, "sf_featdmf")) {
16576 confp->chan.sig = SIG_FEATDMF;
16577 } else if (!strcasecmp(v->value, "sf_featb")) {
16578 confp->chan.sig = SIG_SF_FEATB;
16579 } else if (!strcasecmp(v->value, "sf")) {
16580 confp->chan.sig = SIG_SF;
16581 } else if (!strcasecmp(v->value, "sf_rx")) {
16582 confp->chan.sig = SIG_SF;
16583 confp->chan.radio = 1;
16584 } else if (!strcasecmp(v->value, "sf_tx")) {
16585 confp->chan.sig = SIG_SF;
16586 confp->chan.radio = 1;
16587 } else if (!strcasecmp(v->value, "sf_rxtx")) {
16588 confp->chan.sig = SIG_SF;
16589 confp->chan.radio = 2;
16590 } else if (!strcasecmp(v->value, "sf_txrx")) {
16591 confp->chan.sig = SIG_SF;
16592 confp->chan.radio = 2;
16593 } else if (!strcasecmp(v->value, "featd")) {
16594 confp->chan.sig = SIG_FEATD;
16595 } else if (!strcasecmp(v->value, "featdmf")) {
16596 confp->chan.sig = SIG_FEATDMF;
16597 } else if (!strcasecmp(v->value, "featdmf_ta")) {
16598 confp->chan.sig = SIG_FEATDMF_TA;
16599 } else if (!strcasecmp(v->value, "e911")) {
16600 confp->chan.sig = SIG_E911;
16601 } else if (!strcasecmp(v->value, "fgccama")) {
16602 confp->chan.sig = SIG_FGC_CAMA;
16603 } else if (!strcasecmp(v->value, "fgccamamf")) {
16604 confp->chan.sig = SIG_FGC_CAMAMF;
16605 } else if (!strcasecmp(v->value, "featb")) {
16606 confp->chan.sig = SIG_FEATB;
16607 #ifdef HAVE_PRI
16608 } else if (!strcasecmp(v->value, "pri_net")) {
16609 confp->chan.sig = SIG_PRI;
16610 confp->pri.nodetype = PRI_NETWORK;
16611 } else if (!strcasecmp(v->value, "pri_cpe")) {
16612 confp->chan.sig = SIG_PRI;
16613 confp->pri.nodetype = PRI_CPE;
16614 } else if (!strcasecmp(v->value, "bri_cpe")) {
16615 confp->chan.sig = SIG_BRI;
16616 confp->pri.nodetype = PRI_CPE;
16617 } else if (!strcasecmp(v->value, "bri_net")) {
16618 confp->chan.sig = SIG_BRI;
16619 confp->pri.nodetype = PRI_NETWORK;
16620 } else if (!strcasecmp(v->value, "bri_cpe_ptmp")) {
16621 confp->chan.sig = SIG_BRI_PTMP;
16622 confp->pri.nodetype = PRI_CPE;
16623 } else if (!strcasecmp(v->value, "bri_net_ptmp")) {
16624 ast_log(LOG_WARNING, "How cool would it be if someone implemented this mode! For now, sucks for you. (line %d)\n", v->lineno);
16625 } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
16626 confp->chan.sig = SIG_GR303FXOKS;
16627 confp->pri.nodetype = PRI_NETWORK;
16628 } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
16629 confp->chan.sig = SIG_GR303FXSKS;
16630 confp->pri.nodetype = PRI_CPE;
16631 #endif
16632 #ifdef HAVE_SS7
16633 } else if (!strcasecmp(v->value, "ss7")) {
16634 confp->chan.sig = SIG_SS7;
16635 #endif
16636 #ifdef HAVE_OPENR2
16637 } else if (!strcasecmp(v->value, "mfcr2")) {
16638 confp->chan.sig = SIG_MFCR2;
16639 #endif
16640 } else if (!strcasecmp(v->value, "auto")) {
16641 confp->is_sig_auto = 1;
16642 } else {
16643 confp->chan.outsigmod = orig_outsigmod;
16644 confp->chan.radio = orig_radio;
16645 confp->is_sig_auto = orig_auto;
16646 ast_log(LOG_ERROR, "Unknown signalling method '%s' at line %d.\n", v->value, v->lineno);
16647 }
16648 } else if (!strcasecmp(v->name, "outsignalling") || !strcasecmp(v->name, "outsignaling")) {
16649 if (!strcasecmp(v->value, "em")) {
16650 confp->chan.outsigmod = SIG_EM;
16651 } else if (!strcasecmp(v->value, "em_e1")) {
16652 confp->chan.outsigmod = SIG_EM_E1;
16653 } else if (!strcasecmp(v->value, "em_w")) {
16654 confp->chan.outsigmod = SIG_EMWINK;
16655 } else if (!strcasecmp(v->value, "sf")) {
16656 confp->chan.outsigmod = SIG_SF;
16657 } else if (!strcasecmp(v->value, "sf_w")) {
16658 confp->chan.outsigmod = SIG_SFWINK;
16659 } else if (!strcasecmp(v->value, "sf_featd")) {
16660 confp->chan.outsigmod = SIG_FEATD;
16661 } else if (!strcasecmp(v->value, "sf_featdmf")) {
16662 confp->chan.outsigmod = SIG_FEATDMF;
16663 } else if (!strcasecmp(v->value, "sf_featb")) {
16664 confp->chan.outsigmod = SIG_SF_FEATB;
16665 } else if (!strcasecmp(v->value, "sf")) {
16666 confp->chan.outsigmod = SIG_SF;
16667 } else if (!strcasecmp(v->value, "featd")) {
16668 confp->chan.outsigmod = SIG_FEATD;
16669 } else if (!strcasecmp(v->value, "featdmf")) {
16670 confp->chan.outsigmod = SIG_FEATDMF;
16671 } else if (!strcasecmp(v->value, "featdmf_ta")) {
16672 confp->chan.outsigmod = SIG_FEATDMF_TA;
16673 } else if (!strcasecmp(v->value, "e911")) {
16674 confp->chan.outsigmod = SIG_E911;
16675 } else if (!strcasecmp(v->value, "fgccama")) {
16676 confp->chan.outsigmod = SIG_FGC_CAMA;
16677 } else if (!strcasecmp(v->value, "fgccamamf")) {
16678 confp->chan.outsigmod = SIG_FGC_CAMAMF;
16679 } else if (!strcasecmp(v->value, "featb")) {
16680 confp->chan.outsigmod = SIG_FEATB;
16681 } else {
16682 ast_log(LOG_ERROR, "Unknown signalling method '%s' at line %d.\n", v->value, v->lineno);
16683 }
16684 #ifdef HAVE_PRI
16685 } else if (!strcasecmp(v->name, "pridialplan")) {
16686 if (!strcasecmp(v->value, "national")) {
16687 confp->pri.dialplan = PRI_NATIONAL_ISDN + 1;
16688 } else if (!strcasecmp(v->value, "unknown")) {
16689 confp->pri.dialplan = PRI_UNKNOWN + 1;
16690 } else if (!strcasecmp(v->value, "private")) {
16691 confp->pri.dialplan = PRI_PRIVATE + 1;
16692 } else if (!strcasecmp(v->value, "international")) {
16693 confp->pri.dialplan = PRI_INTERNATIONAL_ISDN + 1;
16694 } else if (!strcasecmp(v->value, "local")) {
16695 confp->pri.dialplan = PRI_LOCAL_ISDN + 1;
16696 } else if (!strcasecmp(v->value, "dynamic")) {
16697 confp->pri.dialplan = -1;
16698 } else if (!strcasecmp(v->value, "redundant")) {
16699 confp->pri.dialplan = -2;
16700 } else {
16701 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
16702 }
16703 } else if (!strcasecmp(v->name, "prilocaldialplan")) {
16704 if (!strcasecmp(v->value, "national")) {
16705 confp->pri.localdialplan = PRI_NATIONAL_ISDN + 1;
16706 } else if (!strcasecmp(v->value, "unknown")) {
16707 confp->pri.localdialplan = PRI_UNKNOWN + 1;
16708 } else if (!strcasecmp(v->value, "private")) {
16709 confp->pri.localdialplan = PRI_PRIVATE + 1;
16710 } else if (!strcasecmp(v->value, "international")) {
16711 confp->pri.localdialplan = PRI_INTERNATIONAL_ISDN + 1;
16712 } else if (!strcasecmp(v->value, "local")) {
16713 confp->pri.localdialplan = PRI_LOCAL_ISDN + 1;
16714 } else if (!strcasecmp(v->value, "dynamic")) {
16715 confp->pri.localdialplan = -1;
16716 } else if (!strcasecmp(v->value, "redundant")) {
16717 confp->pri.localdialplan = -2;
16718 } else {
16719 ast_log(LOG_WARNING, "Unknown PRI localdialplan '%s' at line %d.\n", v->value, v->lineno);
16720 }
16721 } else if (!strcasecmp(v->name, "switchtype")) {
16722 if (!strcasecmp(v->value, "national"))
16723 confp->pri.switchtype = PRI_SWITCH_NI2;
16724 else if (!strcasecmp(v->value, "ni1"))
16725 confp->pri.switchtype = PRI_SWITCH_NI1;
16726 else if (!strcasecmp(v->value, "dms100"))
16727 confp->pri.switchtype = PRI_SWITCH_DMS100;
16728 else if (!strcasecmp(v->value, "4ess"))
16729 confp->pri.switchtype = PRI_SWITCH_ATT4ESS;
16730 else if (!strcasecmp(v->value, "5ess"))
16731 confp->pri.switchtype = PRI_SWITCH_LUCENT5E;
16732 else if (!strcasecmp(v->value, "euroisdn"))
16733 confp->pri.switchtype = PRI_SWITCH_EUROISDN_E1;
16734 else if (!strcasecmp(v->value, "qsig"))
16735 confp->pri.switchtype = PRI_SWITCH_QSIG;
16736 else {
16737 ast_log(LOG_ERROR, "Unknown switchtype '%s' at line %d.\n", v->value, v->lineno);
16738 return -1;
16739 }
16740 } else if (!strcasecmp(v->name, "nsf")) {
16741 if (!strcasecmp(v->value, "sdn"))
16742 confp->pri.nsf = PRI_NSF_SDN;
16743 else if (!strcasecmp(v->value, "megacom"))
16744 confp->pri.nsf = PRI_NSF_MEGACOM;
16745 else if (!strcasecmp(v->value, "tollfreemegacom"))
16746 confp->pri.nsf = PRI_NSF_TOLL_FREE_MEGACOM;
16747 else if (!strcasecmp(v->value, "accunet"))
16748 confp->pri.nsf = PRI_NSF_ACCUNET;
16749 else if (!strcasecmp(v->value, "none"))
16750 confp->pri.nsf = PRI_NSF_NONE;
16751 else {
16752 ast_log(LOG_WARNING, "Unknown network-specific facility '%s' at line %d.\n", v->value, v->lineno);
16753 confp->pri.nsf = PRI_NSF_NONE;
16754 }
16755 } else if (!strcasecmp(v->name, "priindication")) {
16756 if (!strcasecmp(v->value, "outofband"))
16757 confp->chan.priindication_oob = 1;
16758 else if (!strcasecmp(v->value, "inband"))
16759 confp->chan.priindication_oob = 0;
16760 else
16761 ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d.\n",
16762 v->value, v->lineno);
16763 } else if (!strcasecmp(v->name, "priexclusive")) {
16764 confp->chan.priexclusive = ast_true(v->value);
16765 } else if (!strcasecmp(v->name, "internationalprefix")) {
16766 ast_copy_string(confp->pri.internationalprefix, v->value, sizeof(confp->pri.internationalprefix));
16767 } else if (!strcasecmp(v->name, "nationalprefix")) {
16768 ast_copy_string(confp->pri.nationalprefix, v->value, sizeof(confp->pri.nationalprefix));
16769 } else if (!strcasecmp(v->name, "localprefix")) {
16770 ast_copy_string(confp->pri.localprefix, v->value, sizeof(confp->pri.localprefix));
16771 } else if (!strcasecmp(v->name, "privateprefix")) {
16772 ast_copy_string(confp->pri.privateprefix, v->value, sizeof(confp->pri.privateprefix));
16773 } else if (!strcasecmp(v->name, "unknownprefix")) {
16774 ast_copy_string(confp->pri.unknownprefix, v->value, sizeof(confp->pri.unknownprefix));
16775 } else if (!strcasecmp(v->name, "resetinterval")) {
16776 if (!strcasecmp(v->value, "never"))
16777 confp->pri.resetinterval = -1;
16778 else if (atoi(v->value) >= 60)
16779 confp->pri.resetinterval = atoi(v->value);
16780 else
16781 ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d.\n",
16782 v->value, v->lineno);
16783 } else if (!strcasecmp(v->name, "minunused")) {
16784 confp->pri.minunused = atoi(v->value);
16785 } else if (!strcasecmp(v->name, "minidle")) {
16786 confp->pri.minidle = atoi(v->value);
16787 } else if (!strcasecmp(v->name, "idleext")) {
16788 ast_copy_string(confp->pri.idleext, v->value, sizeof(confp->pri.idleext));
16789 } else if (!strcasecmp(v->name, "idledial")) {
16790 ast_copy_string(confp->pri.idledial, v->value, sizeof(confp->pri.idledial));
16791 } else if (!strcasecmp(v->name, "overlapdial")) {
16792 if (ast_true(v->value)) {
16793 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_BOTH;
16794 } else if (!strcasecmp(v->value, "incoming")) {
16795 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_INCOMING;
16796 } else if (!strcasecmp(v->value, "outgoing")) {
16797 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_OUTGOING;
16798 } else if (!strcasecmp(v->value, "both") || ast_true(v->value)) {
16799 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_BOTH;
16800 } else {
16801 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_NONE;
16802 }
16803 #ifdef HAVE_PRI_PROG_W_CAUSE
16804 } else if (!strcasecmp(v->name, "qsigchannelmapping")) {
16805 if (!strcasecmp(v->value, "logical")) {
16806 confp->pri.qsigchannelmapping = DAHDI_CHAN_MAPPING_LOGICAL;
16807 } else if (!strcasecmp(v->value, "physical")) {
16808 confp->pri.qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL;
16809 } else {
16810 confp->pri.qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL;
16811 }
16812 #endif
16813 } else if (!strcasecmp(v->name, "discardremoteholdretrieval")) {
16814 confp->pri.discardremoteholdretrieval = ast_true(v->value);
16815 #ifdef HAVE_PRI_INBANDDISCONNECT
16816 } else if (!strcasecmp(v->name, "inbanddisconnect")) {
16817 confp->pri.inbanddisconnect = ast_true(v->value);
16818 #endif
16819 } else if (!strcasecmp(v->name, "pritimer")) {
16820 #ifdef PRI_GETSET_TIMERS
16821 char tmp[20];
16822 char *timerc;
16823 char *c;
16824 int timer;
16825 int timeridx;
16826
16827 ast_copy_string(tmp, v->value, sizeof(tmp));
16828 c = tmp;
16829 timerc = strsep(&c, ",");
16830 if (!ast_strlen_zero(timerc) && !ast_strlen_zero(c)) {
16831 timeridx = pri_timer2idx(timerc);
16832 timer = atoi(c);
16833 if (timeridx < 0 || PRI_MAX_TIMERS <= timeridx) {
16834 ast_log(LOG_WARNING,
16835 "'%s' is not a valid ISDN timer at line %d.\n", timerc,
16836 v->lineno);
16837 } else if (!timer) {
16838 ast_log(LOG_WARNING,
16839 "'%s' is not a valid value for ISDN timer '%s' at line %d.\n",
16840 c, timerc, v->lineno);
16841 } else {
16842 pritimers[timeridx] = timer;
16843 }
16844 } else {
16845 ast_log(LOG_WARNING,
16846 "'%s' is not a valid ISDN timer configuration string at line %d.\n",
16847 v->value, v->lineno);
16848 }
16849
16850 } else if (!strcasecmp(v->name, "facilityenable")) {
16851 confp->pri.facilityenable = ast_true(v->value);
16852 #endif
16853 #endif
16854 #ifdef HAVE_SS7
16855 } else if (!strcasecmp(v->name, "ss7type")) {
16856 if (!strcasecmp(v->value, "itu")) {
16857 cur_ss7type = SS7_ITU;
16858 } else if (!strcasecmp(v->value, "ansi")) {
16859 cur_ss7type = SS7_ANSI;
16860 } else
16861 ast_log(LOG_WARNING, "'%s' is an unknown ss7 switch type at line %d.!\n", v->value, v->lineno);
16862 } else if (!strcasecmp(v->name, "linkset")) {
16863 cur_linkset = atoi(v->value);
16864 } else if (!strcasecmp(v->name, "pointcode")) {
16865 cur_pointcode = parse_pointcode(v->value);
16866 } else if (!strcasecmp(v->name, "adjpointcode")) {
16867 cur_adjpointcode = parse_pointcode(v->value);
16868 } else if (!strcasecmp(v->name, "defaultdpc")) {
16869 cur_defaultdpc = parse_pointcode(v->value);
16870 } else if (!strcasecmp(v->name, "cicbeginswith")) {
16871 cur_cicbeginswith = atoi(v->value);
16872 } else if (!strcasecmp(v->name, "networkindicator")) {
16873 if (!strcasecmp(v->value, "national"))
16874 cur_networkindicator = SS7_NI_NAT;
16875 else if (!strcasecmp(v->value, "national_spare"))
16876 cur_networkindicator = SS7_NI_NAT_SPARE;
16877 else if (!strcasecmp(v->value, "international"))
16878 cur_networkindicator = SS7_NI_INT;
16879 else if (!strcasecmp(v->value, "international_spare"))
16880 cur_networkindicator = SS7_NI_INT_SPARE;
16881 else
16882 cur_networkindicator = -1;
16883 } else if (!strcasecmp(v->name, "ss7_internationalprefix")) {
16884 ast_copy_string(confp->ss7.internationalprefix, v->value, sizeof(confp->ss7.internationalprefix));
16885 } else if (!strcasecmp(v->name, "ss7_nationalprefix")) {
16886 ast_copy_string(confp->ss7.nationalprefix, v->value, sizeof(confp->ss7.nationalprefix));
16887 } else if (!strcasecmp(v->name, "ss7_subscriberprefix")) {
16888 ast_copy_string(confp->ss7.subscriberprefix, v->value, sizeof(confp->ss7.subscriberprefix));
16889 } else if (!strcasecmp(v->name, "ss7_unknownprefix")) {
16890 ast_copy_string(confp->ss7.unknownprefix, v->value, sizeof(confp->ss7.unknownprefix));
16891 } else if (!strcasecmp(v->name, "ss7_called_nai")) {
16892 if (!strcasecmp(v->value, "national")) {
16893 confp->ss7.called_nai = SS7_NAI_NATIONAL;
16894 } else if (!strcasecmp(v->value, "international")) {
16895 confp->ss7.called_nai = SS7_NAI_INTERNATIONAL;
16896 } else if (!strcasecmp(v->value, "subscriber")) {
16897 confp->ss7.called_nai = SS7_NAI_SUBSCRIBER;
16898 } else if (!strcasecmp(v->value, "dynamic")) {
16899 confp->ss7.called_nai = SS7_NAI_DYNAMIC;
16900 } else {
16901 ast_log(LOG_WARNING, "Unknown SS7 called_nai '%s' at line %d.\n", v->value, v->lineno);
16902 }
16903 } else if (!strcasecmp(v->name, "ss7_calling_nai")) {
16904 if (!strcasecmp(v->value, "national")) {
16905 confp->ss7.calling_nai = SS7_NAI_NATIONAL;
16906 } else if (!strcasecmp(v->value, "international")) {
16907 confp->ss7.calling_nai = SS7_NAI_INTERNATIONAL;
16908 } else if (!strcasecmp(v->value, "subscriber")) {
16909 confp->ss7.calling_nai = SS7_NAI_SUBSCRIBER;
16910 } else if (!strcasecmp(v->value, "dynamic")) {
16911 confp->ss7.calling_nai = SS7_NAI_DYNAMIC;
16912 } else {
16913 ast_log(LOG_WARNING, "Unknown SS7 calling_nai '%s' at line %d.\n", v->value, v->lineno);
16914 }
16915 } else if (!strcasecmp(v->name, "sigchan")) {
16916 int sigchan, res;
16917 sigchan = atoi(v->value);
16918 res = linkset_addsigchan(sigchan);
16919 if (res < 0)
16920 return -1;
16921
16922 } else if (!strcasecmp(v->name, "ss7_explicitacm")) {
16923 struct dahdi_ss7 *link;
16924 link = ss7_resolve_linkset(cur_linkset);
16925 if (!link) {
16926 ast_log(LOG_ERROR, "Invalid linkset number. Must be between 1 and %d\n", NUM_SPANS + 1);
16927 return -1;
16928 }
16929 if (ast_true(v->value))
16930 link->flags |= LINKSET_FLAG_EXPLICITACM;
16931 #endif
16932 #ifdef HAVE_OPENR2
16933 } else if (!strcasecmp(v->name, "mfcr2_advanced_protocol_file")) {
16934 ast_copy_string(confp->mfcr2.r2proto_file, v->value, sizeof(confp->mfcr2.r2proto_file));
16935 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);
16936 } else if (!strcasecmp(v->name, "mfcr2_logdir")) {
16937 ast_copy_string(confp->mfcr2.logdir, v->value, sizeof(confp->mfcr2.logdir));
16938 } else if (!strcasecmp(v->name, "mfcr2_variant")) {
16939 confp->mfcr2.variant = openr2_proto_get_variant(v->value);
16940 if (OR2_VAR_UNKNOWN == confp->mfcr2.variant) {
16941 ast_log(LOG_WARNING, "Unknown MFC/R2 variant '%s' at line %d, defaulting to ITU.\n", v->value, v->lineno);
16942 confp->mfcr2.variant = OR2_VAR_ITU;
16943 }
16944 } else if (!strcasecmp(v->name, "mfcr2_mfback_timeout")) {
16945 confp->mfcr2.mfback_timeout = atoi(v->value);
16946 if (!confp->mfcr2.mfback_timeout) {
16947 ast_log(LOG_WARNING, "MF timeout of 0? hum, I will protect you from your ignorance. Setting default.\n");
16948 confp->mfcr2.mfback_timeout = -1;
16949 } else if (confp->mfcr2.mfback_timeout > 0 && confp->mfcr2.mfback_timeout < 500) {
16950 ast_log(LOG_WARNING, "MF timeout less than 500ms is not recommended, you have been warned!\n");
16951 }
16952 } else if (!strcasecmp(v->name, "mfcr2_metering_pulse_timeout")) {
16953 confp->mfcr2.metering_pulse_timeout = atoi(v->value);
16954 if (confp->mfcr2.metering_pulse_timeout > 500) {
16955 ast_log(LOG_WARNING, "Metering pulse timeout greater than 500ms is not recommended, you have been warned!\n");
16956 }
16957 } else if (!strcasecmp(v->name, "mfcr2_get_ani_first")) {
16958 confp->mfcr2.get_ani_first = ast_true(v->value) ? 1 : 0;
16959 } else if (!strcasecmp(v->name, "mfcr2_double_answer")) {
16960 confp->mfcr2.double_answer = ast_true(v->value) ? 1 : 0;
16961 } else if (!strcasecmp(v->name, "mfcr2_charge_calls")) {
16962 confp->mfcr2.charge_calls = ast_true(v->value) ? 1 : 0;
16963 } else if (!strcasecmp(v->name, "mfcr2_accept_on_offer")) {
16964 confp->mfcr2.accept_on_offer = ast_true(v->value) ? 1 : 0;
16965 } else if (!strcasecmp(v->name, "mfcr2_allow_collect_calls")) {
16966 confp->mfcr2.allow_collect_calls = ast_true(v->value) ? 1 : 0;
16967 } else if (!strcasecmp(v->name, "mfcr2_forced_release")) {
16968 confp->mfcr2.forced_release = ast_true(v->value) ? 1 : 0;
16969 } else if (!strcasecmp(v->name, "mfcr2_immediate_accept")) {
16970 confp->mfcr2.immediate_accept = ast_true(v->value) ? 1 : 0;
16971 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
16972 } else if (!strcasecmp(v->name, "mfcr2_skip_category")) {
16973 confp->mfcr2.skip_category_request = ast_true(v->value) ? 1 : 0;
16974 #endif
16975 } else if (!strcasecmp(v->name, "mfcr2_call_files")) {
16976 confp->mfcr2.call_files = ast_true(v->value) ? 1 : 0;
16977 } else if (!strcasecmp(v->name, "mfcr2_max_ani")) {
16978 confp->mfcr2.max_ani = atoi(v->value);
16979 if (confp->mfcr2.max_ani >= AST_MAX_EXTENSION){
16980 confp->mfcr2.max_ani = AST_MAX_EXTENSION - 1;
16981 }
16982 } else if (!strcasecmp(v->name, "mfcr2_max_dnis")) {
16983 confp->mfcr2.max_dnis = atoi(v->value);
16984 if (confp->mfcr2.max_dnis >= AST_MAX_EXTENSION){
16985 confp->mfcr2.max_dnis = AST_MAX_EXTENSION - 1;
16986 }
16987 } else if (!strcasecmp(v->name, "mfcr2_category")) {
16988 confp->mfcr2.category = openr2_proto_get_category(v->value);
16989 if (OR2_CALLING_PARTY_CATEGORY_UNKNOWN == confp->mfcr2.category) {
16990 confp->mfcr2.category = OR2_CALLING_PARTY_CATEGORY_NATIONAL_SUBSCRIBER;
16991 ast_log(LOG_WARNING, "Invalid MFC/R2 caller category '%s' at line %d. Using national subscriber as default.\n",
16992 v->value, v->lineno);
16993 }
16994 } else if (!strcasecmp(v->name, "mfcr2_logging")) {
16995 openr2_log_level_t tmplevel;
16996 char *clevel;
16997 char *logval = ast_strdupa(v->value);
16998 while (logval) {
16999 clevel = strsep(&logval,",");
17000 if (-1 == (tmplevel = openr2_log_get_level(clevel))) {
17001 ast_log(LOG_WARNING, "Ignoring invalid logging level: '%s' at line %d.\n", clevel, v->lineno);
17002 continue;
17003 }
17004 confp->mfcr2.loglevel |= tmplevel;
17005 }
17006 #endif
17007 } else if (!strcasecmp(v->name, "cadence")) {
17008
17009 int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
17010 int i;
17011 struct dahdi_ring_cadence new_cadence;
17012 int cid_location = -1;
17013 int firstcadencepos = 0;
17014 char original_args[80];
17015 int cadence_is_ok = 1;
17016
17017 ast_copy_string(original_args, v->value, sizeof(original_args));
17018
17019 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]);
17020
17021
17022 if (element_count % 2 == 1) {
17023 ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s at line %d.\n", original_args, v->lineno);
17024 cadence_is_ok = 0;
17025 }
17026
17027
17028 for (i = 0; i < element_count; i++) {
17029 if (c[i] == 0) {
17030 ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s at line %d.\n", original_args, v->lineno);
17031 cadence_is_ok = 0;
17032 break;
17033 } else if (c[i] < 0) {
17034 if (i % 2 == 1) {
17035
17036 if (cid_location == -1) {
17037 cid_location = i;
17038 c[i] *= -1;
17039 } else {
17040 ast_log(LOG_ERROR, "CID location specified twice: %s at line %d.\n", original_args, v->lineno);
17041 cadence_is_ok = 0;
17042 break;
17043 }
17044 } else {
17045 if (firstcadencepos == 0) {
17046 firstcadencepos = i;
17047
17048 } else {
17049 ast_log(LOG_ERROR, "First cadence position specified twice: %s at line %d.\n", original_args, v->lineno);
17050 cadence_is_ok = 0;
17051 break;
17052 }
17053 }
17054 }
17055 }
17056
17057
17058 for (i = 0; i < 16; i++) {
17059 new_cadence.ringcadence[i] = c[i];
17060 }
17061
17062 if (cadence_is_ok) {
17063
17064 if (element_count < 2) {
17065 ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s at line %d.\n", original_args, v->lineno);
17066 } else {
17067 if (cid_location == -1) {
17068
17069 cid_location = 1;
17070 } else {
17071
17072 cid_location = (cid_location + 1) / 2;
17073 }
17074
17075 if (!user_has_defined_cadences++)
17076
17077 num_cadence = 0;
17078 if ((num_cadence+1) >= NUM_CADENCE_MAX)
17079 ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s at line %d.\n", NUM_CADENCE_MAX, original_args, v->lineno);
17080 else {
17081 cadences[num_cadence] = new_cadence;
17082 cidrings[num_cadence++] = cid_location;
17083 ast_verb(3, "cadence 'r%d' added: %s\n",num_cadence,original_args);
17084 }
17085 }
17086 }
17087 } else if (!strcasecmp(v->name, "ringtimeout")) {
17088 ringt_base = (atoi(v->value) * 8) / READ_SIZE;
17089 } else if (!strcasecmp(v->name, "prewink")) {
17090 confp->timing.prewinktime = atoi(v->value);
17091 } else if (!strcasecmp(v->name, "preflash")) {
17092 confp->timing.preflashtime = atoi(v->value);
17093 } else if (!strcasecmp(v->name, "wink")) {
17094 confp->timing.winktime = atoi(v->value);
17095 } else if (!strcasecmp(v->name, "flash")) {
17096 confp->timing.flashtime = atoi(v->value);
17097 } else if (!strcasecmp(v->name, "start")) {
17098 confp->timing.starttime = atoi(v->value);
17099 } else if (!strcasecmp(v->name, "rxwink")) {
17100 confp->timing.rxwinktime = atoi(v->value);
17101 } else if (!strcasecmp(v->name, "rxflash")) {
17102 confp->timing.rxflashtime = atoi(v->value);
17103 } else if (!strcasecmp(v->name, "debounce")) {
17104 confp->timing.debouncetime = atoi(v->value);
17105 } else if (!strcasecmp(v->name, "toneduration")) {
17106 int toneduration;
17107 int ctlfd;
17108 int res;
17109 struct dahdi_dialparams dps;
17110
17111 ctlfd = open("/dev/dahdi/ctl", O_RDWR);
17112 if (ctlfd == -1) {
17113 ast_log(LOG_ERROR, "Unable to open /dev/dahdi/ctl to set toneduration at line %d.\n", v->lineno);
17114 return -1;
17115 }
17116
17117 toneduration = atoi(v->value);
17118 if (toneduration > -1) {
17119 memset(&dps, 0, sizeof(dps));
17120
17121 dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration;
17122 res = ioctl(ctlfd, DAHDI_SET_DIALPARAMS, &dps);
17123 if (res < 0) {
17124 ast_log(LOG_ERROR, "Invalid tone duration: %d ms at line %d: %s\n", toneduration, v->lineno, strerror(errno));
17125 close(ctlfd);
17126 return -1;
17127 }
17128 }
17129 close(ctlfd);
17130 } else if (!strcasecmp(v->name, "defaultcic")) {
17131 ast_copy_string(defaultcic, v->value, sizeof(defaultcic));
17132 } else if (!strcasecmp(v->name, "defaultozz")) {
17133 ast_copy_string(defaultozz, v->value, sizeof(defaultozz));
17134 } else if (!strcasecmp(v->name, "mwilevel")) {
17135 mwilevel = atoi(v->value);
17136 }
17137 } else if (!(options & PROC_DAHDI_OPT_NOWARN) )
17138 ast_log(LOG_WARNING, "Ignoring any changes to '%s' (on reload) at line %d.\n", v->name, v->lineno);
17139 }
17140 if (dahdichan[0]) {
17141
17142
17143 if (build_channels(confp, 0, dahdichan, reload, 0, &found_pseudo)) {
17144 return -1;
17145 }
17146 }
17147
17148
17149 if (!found_pseudo && reload != 1) {
17150
17151
17152
17153
17154 struct dahdi_chan_conf conf = dahdi_chan_conf_default();
17155
17156 tmp = mkintf(CHAN_PSEUDO, &conf, NULL, reload);
17157
17158 if (tmp) {
17159 ast_verb(3, "Automatically generated pseudo channel\n");
17160 } else {
17161 ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
17162 }
17163 }
17164 return 0;
17165 }
17166
17167 static int setup_dahdi(int reload)
17168 {
17169 struct ast_config *cfg, *ucfg;
17170 struct ast_variable *v;
17171 struct dahdi_chan_conf base_conf = dahdi_chan_conf_default();
17172 struct dahdi_chan_conf conf;
17173 struct ast_flags config_flags = { reload == 1 ? CONFIG_FLAG_FILEUNCHANGED : 0 };
17174 const char *cat;
17175 int res;
17176
17177 #ifdef HAVE_PRI
17178 char *c;
17179 int spanno;
17180 int i;
17181 int logicalspan;
17182 int trunkgroup;
17183 int dchannels[NUM_DCHANS];
17184 #endif
17185
17186 cfg = ast_config_load(config, config_flags);
17187
17188
17189 if (!cfg) {
17190 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
17191 return 0;
17192 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
17193 ucfg = ast_config_load("users.conf", config_flags);
17194 if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
17195 return 0;
17196 } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
17197 ast_log(LOG_ERROR, "File users.conf cannot be parsed. Aborting.\n");
17198 return 0;
17199 }
17200 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
17201 if ((cfg = ast_config_load(config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
17202 ast_log(LOG_ERROR, "File %s cannot be parsed. Aborting.\n", config);
17203 ast_config_destroy(ucfg);
17204 return 0;
17205 }
17206 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
17207 ast_log(LOG_ERROR, "File %s cannot be parsed. Aborting.\n", config);
17208 return 0;
17209 } else {
17210 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
17211 if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
17212 ast_log(LOG_ERROR, "File users.conf cannot be parsed. Aborting.\n");
17213 ast_config_destroy(cfg);
17214 return 0;
17215 }
17216 }
17217
17218
17219 ast_mutex_lock(&iflock);
17220 #ifdef HAVE_PRI
17221 if (reload != 1) {
17222
17223 v = ast_variable_browse(cfg, "trunkgroups");
17224 while (v) {
17225 if (!strcasecmp(v->name, "trunkgroup")) {
17226 trunkgroup = atoi(v->value);
17227 if (trunkgroup > 0) {
17228 if ((c = strchr(v->value, ','))) {
17229 i = 0;
17230 memset(dchannels, 0, sizeof(dchannels));
17231 while (c && (i < NUM_DCHANS)) {
17232 dchannels[i] = atoi(c + 1);
17233 if (dchannels[i] < 0) {
17234 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);
17235 } else
17236 i++;
17237 c = strchr(c + 1, ',');
17238 }
17239 if (i) {
17240 if (pri_create_trunkgroup(trunkgroup, dchannels)) {
17241 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);
17242 } else
17243 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");
17244 } else
17245 ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno);
17246 } else
17247 ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno);
17248 } else
17249 ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of chan_dahdi.conf\n", v->lineno);
17250 } else if (!strcasecmp(v->name, "spanmap")) {
17251 spanno = atoi(v->value);
17252 if (spanno > 0) {
17253 if ((c = strchr(v->value, ','))) {
17254 trunkgroup = atoi(c + 1);
17255 if (trunkgroup > 0) {
17256 if ((c = strchr(c + 1, ',')))
17257 logicalspan = atoi(c + 1);
17258 else
17259 logicalspan = 0;
17260 if (logicalspan >= 0) {
17261 if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
17262 ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
17263 } else
17264 ast_verb(2, "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
17265 } else
17266 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);
17267 } else
17268 ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of chan_dahdi.conf\n", v->lineno);
17269 } else
17270 ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of chan_dahdi.conf\n", v->lineno);
17271 } else
17272 ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of chan_dahdi.conf\n", v->lineno);
17273 } else {
17274 ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
17275 }
17276 v = v->next;
17277 }
17278 }
17279 #endif
17280
17281
17282 memcpy(&global_jbconf, &default_jbconf, sizeof(global_jbconf));
17283
17284 mwimonitornotify[0] = '\0';
17285
17286 v = ast_variable_browse(cfg, "channels");
17287 if ((res = process_dahdi(&base_conf, "", v, reload, 0))) {
17288 ast_mutex_unlock(&iflock);
17289 ast_config_destroy(cfg);
17290 if (ucfg) {
17291 ast_config_destroy(ucfg);
17292 }
17293 return res;
17294 }
17295
17296
17297 for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
17298
17299
17300
17301 if (!strcasecmp(cat, "general") ||
17302 !strcasecmp(cat, "trunkgroups") ||
17303 !strcasecmp(cat, "globals") ||
17304 !strcasecmp(cat, "channels")) {
17305 continue;
17306 }
17307
17308 memcpy(&conf, &base_conf, sizeof(conf));
17309
17310 if ((res = process_dahdi(&conf, cat, ast_variable_browse(cfg, cat), reload, PROC_DAHDI_OPT_NOCHAN))) {
17311 ast_mutex_unlock(&iflock);
17312 ast_config_destroy(cfg);
17313 if (ucfg) {
17314 ast_config_destroy(ucfg);
17315 }
17316 return res;
17317 }
17318 }
17319
17320 ast_config_destroy(cfg);
17321
17322 if (ucfg) {
17323 const char *chans;
17324
17325 process_dahdi(&base_conf, "", ast_variable_browse(ucfg, "general"), 1, 0);
17326
17327 for (cat = ast_category_browse(ucfg, NULL); cat ; cat = ast_category_browse(ucfg, cat)) {
17328 if (!strcasecmp(cat, "general")) {
17329 continue;
17330 }
17331
17332 chans = ast_variable_retrieve(ucfg, cat, "dahdichan");
17333
17334 if (ast_strlen_zero(chans)) {
17335 continue;
17336 }
17337
17338 memcpy(&conf, &base_conf, sizeof(conf));
17339
17340 if ((res = process_dahdi(&conf, cat, ast_variable_browse(ucfg, cat), reload, PROC_DAHDI_OPT_NOCHAN | PROC_DAHDI_OPT_NOWARN))) {
17341 ast_config_destroy(ucfg);
17342 ast_mutex_unlock(&iflock);
17343 return res;
17344 }
17345 }
17346 ast_config_destroy(ucfg);
17347 }
17348 ast_mutex_unlock(&iflock);
17349
17350 #ifdef HAVE_PRI
17351 if (reload != 1) {
17352 int x;
17353 for (x = 0; x < NUM_SPANS; x++) {
17354 if (pris[x].pvts[0]) {
17355 if (start_pri(pris + x)) {
17356 ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
17357 return -1;
17358 } else
17359 ast_verb(2, "Starting D-Channel on span %d\n", x + 1);
17360 }
17361 }
17362 }
17363 #endif
17364 #ifdef HAVE_SS7
17365 if (reload != 1) {
17366 int x;
17367 for (x = 0; x < NUM_SPANS; x++) {
17368 if (linksets[x].ss7) {
17369 if (ast_pthread_create(&linksets[x].master, NULL, ss7_linkset, &linksets[x])) {
17370 ast_log(LOG_ERROR, "Unable to start SS7 linkset on span %d\n", x + 1);
17371 return -1;
17372 } else
17373 ast_verb(2, "Starting SS7 linkset on span %d\n", x + 1);
17374 }
17375 }
17376 }
17377 #endif
17378 #ifdef HAVE_OPENR2
17379 if (reload != 1) {
17380 int x;
17381 for (x = 0; x < r2links_count; x++) {
17382 if (ast_pthread_create(&r2links[x]->r2master, NULL, mfcr2_monitor, r2links[x])) {
17383 ast_log(LOG_ERROR, "Unable to start R2 monitor on channel group %d\n", x + 1);
17384 return -1;
17385 } else {
17386 ast_verb(2, "Starting R2 monitor on channel group %d\n", x + 1);
17387 }
17388 }
17389 }
17390 #endif
17391
17392 restart_monitor();
17393 return 0;
17394 }
17395
17396 static int load_module(void)
17397 {
17398 int res;
17399 #if defined(HAVE_PRI) || defined(HAVE_SS7)
17400 int y, i;
17401 #endif
17402
17403 #ifdef HAVE_PRI
17404 memset(pris, 0, sizeof(pris));
17405 for (y = 0; y < NUM_SPANS; y++) {
17406 ast_mutex_init(&pris[y].lock);
17407 pris[y].offset = -1;
17408 pris[y].master = AST_PTHREADT_NULL;
17409 for (i = 0; i < NUM_DCHANS; i++)
17410 pris[y].fds[i] = -1;
17411 }
17412 pri_set_error(dahdi_pri_error);
17413 pri_set_message(dahdi_pri_message);
17414 ast_register_application_xml(dahdi_send_keypad_facility_app, dahdi_send_keypad_facility_exec);
17415 #ifdef HAVE_PRI_PROG_W_CAUSE
17416 ast_register_application_xml(dahdi_send_callrerouting_facility_app, dahdi_send_callrerouting_facility_exec);
17417 #endif
17418 #endif
17419 #ifdef HAVE_SS7
17420 memset(linksets, 0, sizeof(linksets));
17421 for (y = 0; y < NUM_SPANS; y++) {
17422 ast_mutex_init(&linksets[y].lock);
17423 linksets[y].master = AST_PTHREADT_NULL;
17424 for (i = 0; i < NUM_DCHANS; i++)
17425 linksets[y].fds[i] = -1;
17426 }
17427 ss7_set_error(dahdi_ss7_error);
17428 ss7_set_message(dahdi_ss7_message);
17429 #endif
17430 res = setup_dahdi(0);
17431
17432 if (res)
17433 return AST_MODULE_LOAD_DECLINE;
17434 if (ast_channel_register(&dahdi_tech)) {
17435 ast_log(LOG_ERROR, "Unable to register channel class 'DAHDI'\n");
17436 __unload_module();
17437 return AST_MODULE_LOAD_FAILURE;
17438 }
17439 #ifdef HAVE_PRI
17440 ast_string_field_init(&inuse, 16);
17441 ast_string_field_set(&inuse, name, "GR-303InUse");
17442 ast_cli_register_multiple(dahdi_pri_cli, ARRAY_LEN(dahdi_pri_cli));
17443 #endif
17444 #ifdef HAVE_SS7
17445 ast_cli_register_multiple(dahdi_ss7_cli, ARRAY_LEN(dahdi_ss7_cli));
17446 #endif
17447 #ifdef HAVE_OPENR2
17448 ast_cli_register_multiple(dahdi_mfcr2_cli, sizeof(dahdi_mfcr2_cli)/sizeof(dahdi_mfcr2_cli[0]));
17449 ast_register_application_xml(dahdi_accept_r2_call_app, dahdi_accept_r2_call_exec);
17450 #endif
17451
17452 ast_cli_register_multiple(dahdi_cli, ARRAY_LEN(dahdi_cli));
17453
17454 memset(round_robin, 0, sizeof(round_robin));
17455 ast_manager_register( "DAHDITransfer", 0, action_transfer, "Transfer DAHDI Channel" );
17456 ast_manager_register( "DAHDIHangup", 0, action_transferhangup, "Hangup DAHDI Channel" );
17457 ast_manager_register( "DAHDIDialOffhook", 0, action_dahdidialoffhook, "Dial over DAHDI channel while offhook" );
17458 ast_manager_register( "DAHDIDNDon", 0, action_dahdidndon, "Toggle DAHDI channel Do Not Disturb status ON" );
17459 ast_manager_register( "DAHDIDNDoff", 0, action_dahdidndoff, "Toggle DAHDI channel Do Not Disturb status OFF" );
17460 ast_manager_register("DAHDIShowChannels", 0, action_dahdishowchannels, "Show status DAHDI channels");
17461 ast_manager_register("DAHDIRestart", 0, action_dahdirestart, "Fully Restart DAHDI channels (terminates calls)");
17462
17463 ast_cond_init(&ss_thread_complete, NULL);
17464
17465 return res;
17466 }
17467
17468 static int dahdi_sendtext(struct ast_channel *c, const char *text)
17469 {
17470 #define END_SILENCE_LEN 400
17471 #define HEADER_MS 50
17472 #define TRAILER_MS 5
17473 #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
17474 #define ASCII_BYTES_PER_CHAR 80
17475
17476 unsigned char *buf,*mybuf;
17477 struct dahdi_pvt *p = c->tech_pvt;
17478 struct pollfd fds[1];
17479 int size,res,fd,len,x;
17480 int bytes=0;
17481
17482 float cr = 1.0;
17483 float ci = 0.0;
17484 float scont = 0.0;
17485 int idx;
17486
17487 idx = dahdi_get_index(c, p, 0);
17488 if (idx < 0) {
17489 ast_log(LOG_WARNING, "Huh? I don't exist?\n");
17490 return -1;
17491 }
17492 if (!text[0]) return(0);
17493 if ((!p->tdd) && (!p->mate)) return(0);
17494 if (p->mate)
17495 buf = ast_malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
17496 else
17497 buf = ast_malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
17498 if (!buf)
17499 return -1;
17500 mybuf = buf;
17501 if (p->mate) {
17502 int codec = AST_LAW(p);
17503 for (x = 0; x < HEADER_MS; x++) {
17504 PUT_CLID_MARKMS;
17505 }
17506
17507 for (x = 0; text[x]; x++) {
17508 PUT_CLID(text[x]);
17509 }
17510 for (x = 0; x < TRAILER_MS; x++) {
17511 PUT_CLID_MARKMS;
17512 }
17513 len = bytes;
17514 buf = mybuf;
17515 } else {
17516 len = tdd_generate(p->tdd, buf, text);
17517 if (len < 1) {
17518 ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n", (int)strlen(text));
17519 ast_free(mybuf);
17520 return -1;
17521 }
17522 }
17523 memset(buf + len, 0x7f, END_SILENCE_LEN);
17524 len += END_SILENCE_LEN;
17525 fd = p->subs[idx].dfd;
17526 while (len) {
17527 if (ast_check_hangup(c)) {
17528 ast_free(mybuf);
17529 return -1;
17530 }
17531 size = len;
17532 if (size > READ_SIZE)
17533 size = READ_SIZE;
17534 fds[0].fd = fd;
17535 fds[0].events = POLLOUT | POLLPRI;
17536 fds[0].revents = 0;
17537 res = poll(fds, 1, -1);
17538 if (!res) {
17539 ast_debug(1, "poll (for write) ret. 0 on channel %d\n", p->channel);
17540 continue;
17541 }
17542
17543 if (fds[0].revents & POLLPRI) {
17544 ast_free(mybuf);
17545 return -1;
17546 }
17547 if (!(fds[0].revents & POLLOUT)) {
17548 ast_debug(1, "write fd not ready on channel %d\n", p->channel);
17549 continue;
17550 }
17551 res = write(fd, buf, size);
17552 if (res != size) {
17553 if (res == -1) {
17554 ast_free(mybuf);
17555 return -1;
17556 }
17557 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
17558 break;
17559 }
17560 len -= size;
17561 buf += size;
17562 }
17563 ast_free(mybuf);
17564 return(0);
17565 }
17566
17567
17568 static int reload(void)
17569 {
17570 int res = 0;
17571
17572 res = setup_dahdi(1);
17573 if (res) {
17574 ast_log(LOG_WARNING, "Reload of chan_dahdi.so is unsuccessful!\n");
17575 return -1;
17576 }
17577 return 0;
17578 }
17579
17580
17581
17582
17583
17584 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, tdesc,
17585 .load = load_module,
17586 .unload = unload_module,
17587 .reload = reload,
17588 );