00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <syslog.h>
00022 #include <sys/time.h>
00023 #include <mISDNuser/isdn_debug.h>
00024
00025 #include "isdn_lib_intern.h"
00026 #include "isdn_lib.h"
00027
00028
00029
00030
00031
00032 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
00033
00034 #include "asterisk/causes.h"
00035
00036 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id);
00037 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id);
00038
00039 int queue_cleanup_bc(struct misdn_bchannel *bc) ;
00040
00041 int misdn_lib_get_l2_up(struct misdn_stack *stack);
00042
00043 struct misdn_stack* get_misdn_stack( void );
00044
00045 int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh);
00046
00047 int misdn_lib_port_is_pri(int port)
00048 {
00049 struct misdn_stack *stack=get_misdn_stack();
00050 for ( ; stack; stack=stack->next) {
00051 if (stack->port == port) {
00052 return stack->pri;
00053 }
00054 }
00055
00056 return -1;
00057 }
00058
00059 static void misdn_make_dummy(struct misdn_bchannel *dummybc, int port, int l3id, int nt, int channel)
00060 {
00061 memset (dummybc,0,sizeof(struct misdn_bchannel));
00062 dummybc->port=port;
00063 dummybc->l3_id=l3id;
00064 dummybc->nt=nt;
00065 dummybc->dummy=1;
00066 dummybc->channel=channel;
00067 }
00068
00069 int misdn_lib_port_block(int port)
00070 {
00071 struct misdn_stack *stack=get_misdn_stack();
00072 for ( ; stack; stack=stack->next) {
00073 if (stack->port == port) {
00074 stack->blocked=1;
00075 return 0;
00076 }
00077 }
00078 return -1;
00079
00080 }
00081
00082 int misdn_lib_port_unblock(int port)
00083 {
00084 struct misdn_stack *stack=get_misdn_stack();
00085 for ( ; stack; stack=stack->next) {
00086 if (stack->port == port) {
00087 stack->blocked=0;
00088 return 0;
00089 }
00090 }
00091 return -1;
00092
00093 }
00094
00095 int misdn_lib_is_port_blocked(int port)
00096 {
00097 struct misdn_stack *stack=get_misdn_stack();
00098 for ( ; stack; stack=stack->next) {
00099 if (stack->port == port) {
00100 return stack->blocked;
00101 }
00102 }
00103 return -1;
00104 }
00105
00106 int misdn_lib_is_ptp(int port)
00107 {
00108 struct misdn_stack *stack=get_misdn_stack();
00109 for ( ; stack; stack=stack->next) {
00110 if (stack->port == port) return stack->ptp;
00111 }
00112 return -1;
00113 }
00114
00115 int misdn_lib_get_maxchans(int port)
00116 {
00117 struct misdn_stack *stack=get_misdn_stack();
00118 for ( ; stack; stack=stack->next) {
00119 if (stack->port == port) {
00120 if (stack->pri)
00121 return 30;
00122 else
00123 return 2;
00124 }
00125 }
00126 return -1;
00127 }
00128
00129
00130 struct misdn_stack *get_stack_by_bc(struct misdn_bchannel *bc)
00131 {
00132 struct misdn_stack *stack = get_misdn_stack();
00133
00134 if (!bc)
00135 return NULL;
00136
00137 for ( ; stack; stack = stack->next) {
00138 if (bc->port == stack->port)
00139 return stack;
00140 }
00141
00142 return NULL;
00143 }
00144
00145
00146 void get_show_stack_details(int port, char *buf)
00147 {
00148 struct misdn_stack *stack=get_misdn_stack();
00149
00150 for ( ; stack; stack=stack->next) {
00151 if (stack->port == port) break;
00152 }
00153
00154 if (stack) {
00155 sprintf(buf, "* Port %d Type %s Prot. %s L2Link %s L1Link:%s Blocked:%d",
00156 stack->port, stack->nt ? "NT" : "TE", stack->ptp ? "PTP" : "PMP",
00157 stack->l2link ? "UP" : "DOWN", stack->l1link ? "UP" : "DOWN",
00158 stack->blocked);
00159 } else {
00160 buf[0]=0;
00161 }
00162 }
00163
00164
00165 static int nt_err_cnt =0 ;
00166
00167 enum global_states {
00168 MISDN_INITIALIZING,
00169 MISDN_INITIALIZED
00170 } ;
00171
00172 static enum global_states global_state=MISDN_INITIALIZING;
00173
00174
00175 #include <mISDNuser/net_l2.h>
00176 #include <mISDNuser/tone.h>
00177 #include <unistd.h>
00178 #include <semaphore.h>
00179 #include <pthread.h>
00180 #include <signal.h>
00181
00182 #include "isdn_lib.h"
00183
00184
00185 struct misdn_lib {
00186
00187 int midev;
00188 int midev_nt;
00189
00190 pthread_t event_thread;
00191 pthread_t event_handler_thread;
00192
00193 void *user_data;
00194
00195 msg_queue_t upqueue;
00196 msg_queue_t activatequeue;
00197
00198 sem_t new_msg;
00199
00200 struct misdn_stack *stack_list;
00201 } ;
00202
00203 #ifndef ECHOCAN_ON
00204 #define ECHOCAN_ON 123
00205 #define ECHOCAN_OFF 124
00206 #endif
00207
00208 #define MISDN_DEBUG 0
00209
00210 void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
00211
00212 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
00213
00214 int setup_bc(struct misdn_bchannel *bc);
00215
00216 int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
00217
00218 int misdn_lib_port_restart(int port);
00219 int misdn_lib_pid_restart(int pid);
00220
00221 extern struct isdn_msg msgs_g[];
00222
00223 #define ISDN_PID_L3_B_USER 0x430000ff
00224 #define ISDN_PID_L4_B_USER 0x440000ff
00225
00226
00227 #define MISDN_IBUF_SIZE 512
00228
00229
00230 #define TONE_ALERT_CNT 41
00231 #define TONE_ALERT_SILENCE_CNT 200
00232
00233 #define TONE_BUSY_CNT 20
00234 #define TONE_BUSY_SILENCE_CNT 48
00235
00236 static int entity;
00237
00238 static struct misdn_lib *glob_mgr;
00239
00240 char tone_425_flip[TONE_425_SIZE];
00241 char tone_silence_flip[TONE_SILENCE_SIZE];
00242
00243 static void misdn_lib_isdn_event_catcher(void *arg);
00244 static int handle_event_nt(void *dat, void *arg);
00245
00246
00247 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
00248 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
00249 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
00250
00251
00252
00253 int te_lib_init( void ) ;
00254 void te_lib_destroy(int midev) ;
00255 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
00256 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
00257 void manager_clean_bc(struct misdn_bchannel *bc );
00258 void manager_bchannel_setup (struct misdn_bchannel *bc);
00259 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
00260
00261 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
00262
00263 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
00264 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
00265
00266
00267
00268 static char *bearer2str(int cap) {
00269 static char *bearers[]={
00270 "Speech",
00271 "Audio 3.1k",
00272 "Unres Digital",
00273 "Res Digital",
00274 "Unknown Bearer"
00275 };
00276
00277 switch (cap) {
00278 case INFO_CAPABILITY_SPEECH:
00279 return bearers[0];
00280 break;
00281 case INFO_CAPABILITY_AUDIO_3_1K:
00282 return bearers[1];
00283 break;
00284 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
00285 return bearers[2];
00286 break;
00287 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
00288 return bearers[3];
00289 break;
00290 default:
00291 return bearers[4];
00292 break;
00293 }
00294 }
00295
00296
00297 static char flip_table[256];
00298
00299 static void init_flip_bits(void)
00300 {
00301 int i,k;
00302
00303 for (i = 0 ; i < 256 ; i++) {
00304 unsigned char sample = 0 ;
00305 for (k = 0; k<8; k++) {
00306 if ( i & 1 << k ) sample |= 0x80 >> k;
00307 }
00308 flip_table[i] = sample;
00309 }
00310 }
00311
00312 static char * flip_buf_bits ( char * buf , int len)
00313 {
00314 int i;
00315 char * start = buf;
00316
00317 for (i = 0 ; i < len; i++) {
00318 buf[i] = flip_table[(unsigned char)buf[i]];
00319 }
00320
00321 return start;
00322 }
00323
00324
00325
00326
00327 static msg_t *create_l2msg(int prim, int dinfo, int size)
00328 {
00329 int i = 0;
00330 msg_t *dmsg;
00331
00332 while(i < 10)
00333 {
00334 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
00335 if (dmsg)
00336 return(dmsg);
00337
00338 if (!i)
00339 printf("cannot allocate memory, trying again...\n");
00340 i++;
00341 usleep(300000);
00342 }
00343 printf("cannot allocate memory, system overloaded.\n");
00344 exit(-1);
00345 }
00346
00347
00348
00349 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
00350 {
00351 int i = 0;
00352 msg_t *dmsg;
00353 Q931_info_t *qi;
00354 iframe_t *frm;
00355
00356 if (!ntmode)
00357 size = sizeof(Q931_info_t)+2;
00358
00359 while(i < 10) {
00360 if (ntmode) {
00361 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
00362 if (dmsg) {
00363 return(dmsg);
00364 }
00365 } else {
00366 dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
00367 if (dmsg)
00368 {
00369 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
00370 frm = (iframe_t *)dmsg->data;
00371 frm->prim = prim;
00372 frm->dinfo = dinfo;
00373 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
00374 qi->type = mt;
00375 return(dmsg);
00376 }
00377 }
00378
00379 if (!i) printf("cannot allocate memory, trying again...\n");
00380 i++;
00381 usleep(300000);
00382 }
00383 printf("cannot allocate memory, system overloaded.\n");
00384 exit(-1);
00385 }
00386
00387
00388 static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
00389 {
00390 iframe_t *frm = (iframe_t *)dmsg->data;
00391 struct misdn_stack *stack=get_stack_by_bc(bc);
00392
00393 if (!stack) {
00394 cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
00395 return -1;
00396 }
00397
00398 frm->addr = (stack->upper_id | FLG_MSG_DOWN);
00399 frm->dinfo = bc->l3_id;
00400 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
00401
00402 cb_log(4,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo);
00403
00404 mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
00405 free_msg(dmsg);
00406
00407 return 0;
00408 }
00409
00410
00411 static int mypid=1;
00412
00413
00414 int misdn_cap_is_speech(int cap)
00415
00416 {
00417 if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
00418 (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
00419 return 0;
00420 }
00421
00422 int misdn_inband_avail(struct misdn_bchannel *bc)
00423 {
00424
00425 if (!bc->early_bconnect) {
00426
00427 return 0;
00428 }
00429
00430 switch (bc->progress_indicator) {
00431 case INFO_PI_INBAND_AVAILABLE:
00432 case INFO_PI_CALL_NOT_E2E_ISDN:
00433 case INFO_PI_CALLED_NOT_ISDN:
00434 return 1;
00435 default:
00436 return 0;
00437 }
00438 return 0;
00439 }
00440
00441
00442 static void dump_chan_list(struct misdn_stack *stack)
00443 {
00444 int i;
00445
00446 for (i=0; i <= stack->b_num; i++) {
00447 cb_log(6, stack->port, "Idx:%d stack->cchan:%d in_use:%d Chan:%d\n",i,stack->channels[i], stack->bc[i].in_use, i+1);
00448 }
00449 }
00450
00451
00452 void misdn_dump_chanlist()
00453 {
00454 struct misdn_stack *stack=get_misdn_stack();
00455 for ( ; stack; stack=stack->next) {
00456 dump_chan_list(stack);
00457 }
00458
00459 }
00460
00461 static int set_chan_in_stack(struct misdn_stack *stack, int channel)
00462 {
00463
00464 cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
00465 dump_chan_list(stack);
00466 if (channel >=1 && channel <= MAX_BCHANS) {
00467 if (!stack->channels[channel-1])
00468 stack->channels[channel-1] = 1;
00469 else {
00470 cb_log(4,stack->port,"channel already in use:%d\n", channel );
00471 return -1;
00472 }
00473 } else {
00474 cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
00475 return -1;
00476 }
00477
00478 return 0;
00479 }
00480
00481
00482
00483 static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel, int dec)
00484 {
00485 int i;
00486 int chan=0;
00487 int bnums = stack->pri ? stack->b_num : stack->b_num - 1;
00488
00489 if (bc->channel_found)
00490 return 0;
00491
00492 bc->channel_found=1;
00493
00494 cb_log(5,stack->port,"find_free_chan: req_chan:%d\n",channel);
00495
00496 if (channel < 0 || channel > MAX_BCHANS) {
00497 cb_log(0, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
00498 return 0;
00499 }
00500
00501 channel--;
00502
00503 pthread_mutex_lock(&stack->st_lock);
00504 if (dec) {
00505 for (i = bnums; i >=0; i--) {
00506 if (i != 15 && (channel < 0 || i == channel)) {
00507 if (!stack->channels[i]) {
00508 cb_log (3, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
00509 chan=i+1;
00510 break;
00511 }
00512 }
00513 }
00514 } else {
00515 for (i = 0; i <= bnums; i++) {
00516 if (i != 15 && (channel < 0 || i == channel)) {
00517 if (!stack->channels[i]) {
00518 cb_log (3, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
00519 chan=i+1;
00520 break;
00521 }
00522 }
00523 }
00524 }
00525
00526 if (!chan) {
00527 pthread_mutex_unlock(&stack->st_lock);
00528 cb_log (1, stack->port, " !! NO FREE CHAN IN STACK\n");
00529 dump_chan_list(stack);
00530 bc->out_cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
00531 return -1;
00532 }
00533
00534 if (set_chan_in_stack(stack, chan)<0) {
00535 pthread_mutex_unlock(&stack->st_lock);
00536 cb_log (0, stack->port, "Channel Already in use:%d\n", chan);
00537 bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
00538 return -1;
00539 }
00540 pthread_mutex_unlock(&stack->st_lock);
00541
00542 bc->channel=chan;
00543 return 0;
00544 }
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 static int empty_chan_in_stack(struct misdn_stack *stack, int channel)
00561 {
00562 if (channel<=0 || channel>MAX_BCHANS) {
00563 cb_log(0,stack?stack->port:0, "empty_chan_in_stack: cannot empty channel %d\n",channel);
00564 return -1;
00565 }
00566
00567 cb_log (4, stack?stack->port:0, "empty_chan_in_stack: %d\n",channel);
00568 stack->channels[channel-1] = 0;
00569 dump_chan_list(stack);
00570 return 0;
00571 }
00572
00573 char *bc_state2str(enum bchannel_state state) {
00574 int i;
00575
00576 struct bchan_state_s {
00577 char *n;
00578 enum bchannel_state s;
00579 } states[] = {
00580 {"BCHAN_CLEANED", BCHAN_CLEANED },
00581 {"BCHAN_EMPTY", BCHAN_EMPTY},
00582 {"BCHAN_SETUP", BCHAN_SETUP},
00583 {"BCHAN_SETUPED", BCHAN_SETUPED},
00584 {"BCHAN_ACTIVE", BCHAN_ACTIVE},
00585 {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
00586 {"BCHAN_BRIDGE", BCHAN_BRIDGE},
00587 {"BCHAN_BRIDGED", BCHAN_BRIDGED},
00588 {"BCHAN_RELEASE", BCHAN_RELEASE},
00589 {"BCHAN_RELEASED", BCHAN_RELEASED},
00590 {"BCHAN_CLEAN", BCHAN_CLEAN},
00591 {"BCHAN_CLEAN_REQUEST", BCHAN_CLEAN_REQUEST},
00592 {"BCHAN_ERROR", BCHAN_ERROR}
00593 };
00594
00595 for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
00596 if ( states[i].s == state)
00597 return states[i].n;
00598
00599 return "UNKNOWN";
00600 }
00601
00602 void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
00603 {
00604 cb_log(5,bc->port,"BC_STATE_CHANGE: l3id:%x from:%s to:%s\n",
00605 bc->l3_id,
00606 bc_state2str(bc->bc_state),
00607 bc_state2str(state) );
00608
00609 switch (state) {
00610 case BCHAN_ACTIVATED:
00611 if (bc->next_bc_state == BCHAN_BRIDGED) {
00612 misdn_join_conf(bc, bc->conf_id);
00613 bc->next_bc_state = BCHAN_EMPTY;
00614 return;
00615 }
00616 default:
00617 bc->bc_state=state;
00618 break;
00619 }
00620 }
00621
00622 static void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
00623 {
00624 cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
00625 bc_state2str(bc->next_bc_state),
00626 bc_state2str(state) );
00627
00628 bc->next_bc_state=state;
00629 }
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642 static void empty_bc(struct misdn_bchannel *bc)
00643 {
00644 bc->dummy=0;
00645
00646 bc->bframe_len=0;
00647
00648 bc->cw= 0;
00649
00650 bc->dec=0;
00651 bc->channel = 0;
00652
00653 bc->sending_complete = 0;
00654
00655 bc->restart_channel=0;
00656
00657 bc->conf_id = 0;
00658
00659 bc->need_more_infos = 0;
00660
00661 bc->send_dtmf=0;
00662 bc->nodsp=0;
00663 bc->nojitter=0;
00664
00665 bc->time_usec=0;
00666
00667 bc->rxgain=0;
00668 bc->txgain=0;
00669
00670 bc->crypt=0;
00671 bc->curptx=0; bc->curprx=0;
00672
00673 bc->crypt_key[0] = 0;
00674
00675 bc->generate_tone=0;
00676 bc->tone_cnt=0;
00677
00678 bc->dnumplan=NUMPLAN_UNKNOWN;
00679 bc->onumplan=NUMPLAN_UNKNOWN;
00680 bc->rnumplan=NUMPLAN_UNKNOWN;
00681 bc->cpnnumplan=NUMPLAN_UNKNOWN;
00682
00683
00684 bc->active = 0;
00685
00686 bc->early_bconnect = 1;
00687
00688 #ifdef MISDN_1_2
00689 *bc->pipeline = 0;
00690 #else
00691 bc->ec_enable = 0;
00692 bc->ec_deftaps = 128;
00693 #endif
00694
00695 bc->orig=0;
00696
00697 bc->cause = AST_CAUSE_NORMAL_CLEARING;
00698 bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
00699 bc->pres = 0;
00700
00701 bc->evq=EVENT_NOTHING;
00702
00703 bc->progress_coding=0;
00704 bc->progress_location=0;
00705 bc->progress_indicator=0;
00706
00707
00708 bc->capability=INFO_CAPABILITY_SPEECH;
00709 bc->law=INFO_CODEC_ALAW;
00710 bc->mode=0;
00711 bc->rate=0x10;
00712 bc->user1=0;
00713 bc->urate=0;
00714
00715 bc->hdlc=0;
00716
00717
00718 bc->info_dad[0] = 0;
00719 bc->display[0] = 0;
00720 bc->infos_pending[0] = 0;
00721 bc->cad[0] = 0;
00722 bc->oad[0] = 0;
00723 bc->dad[0] = 0;
00724 bc->rad[0] = 0;
00725 bc->orig_dad[0] = 0;
00726 bc->uu[0]=0;
00727 bc->uulen=0;
00728
00729 bc->fac_in.Function = Fac_None;
00730 bc->fac_out.Function = Fac_None;
00731
00732 bc->te_choose_channel = 0;
00733 bc->channel_found= 0;
00734
00735 gettimeofday(&bc->last_used, NULL);
00736 }
00737
00738
00739 static int clean_up_bc(struct misdn_bchannel *bc)
00740 {
00741 int ret=0;
00742 unsigned char buff[32];
00743 struct misdn_stack * stack;
00744
00745 cb_log(3, bc?bc->port:0, "$$$ CLEANUP CALLED pid:%d\n", bc?bc->pid:-1);
00746
00747 if (!bc ) return -1;
00748 stack=get_stack_by_bc(bc);
00749
00750 if (!stack) return -1;
00751
00752 switch (bc->bc_state ) {
00753 case BCHAN_CLEANED:
00754 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
00755 return -1;
00756
00757 default:
00758 break;
00759 }
00760
00761 cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
00762
00763 manager_ec_disable(bc);
00764
00765 manager_bchannel_deactivate(bc);
00766
00767 mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
00768
00769 bc->b_stid = 0;
00770 bc_state_change(bc, BCHAN_CLEANED);
00771
00772 return ret;
00773 }
00774
00775
00776
00777 static void clear_l3(struct misdn_stack *stack)
00778 {
00779 int i;
00780
00781 for (i=0; i<=stack->b_num; i++) {
00782 if (global_state == MISDN_INITIALIZED) {
00783 cb_event(EVENT_CLEANUP, &stack->bc[i], NULL);
00784 empty_bc(&stack->bc[i]);
00785 clean_up_bc(&stack->bc[i]);
00786 empty_chan_in_stack(stack, i + 1);
00787 stack->bc[i].in_use = 0;
00788 }
00789
00790 }
00791 }
00792
00793 static int new_te_id = 0;
00794
00795 #define MAXPROCS 0x100
00796
00797 static int misdn_lib_get_l1_down(struct misdn_stack *stack)
00798 {
00799
00800 iframe_t act;
00801 act.prim = PH_DEACTIVATE | REQUEST;
00802 act.addr = stack->lower_id|FLG_MSG_DOWN;
00803 act.dinfo = 0;
00804 act.len = 0;
00805
00806 cb_log(1, stack->port, "SENDING PH_DEACTIVATE | REQ\n");
00807 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00808 }
00809
00810
00811 static int misdn_lib_get_l2_down(struct misdn_stack *stack)
00812 {
00813
00814 if (stack->ptp && (stack->nt) ) {
00815 msg_t *dmsg;
00816
00817 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
00818
00819 pthread_mutex_lock(&stack->nstlock);
00820 if (stack->nst.manager_l3(&stack->nst, dmsg))
00821 free_msg(dmsg);
00822 pthread_mutex_unlock(&stack->nstlock);
00823 } else {
00824 iframe_t act;
00825
00826 act.prim = DL_RELEASE| REQUEST;
00827 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
00828
00829 act.dinfo = 0;
00830 act.len = 0;
00831 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00832 }
00833
00834 return 0;
00835 }
00836
00837
00838 static int misdn_lib_get_l1_up(struct misdn_stack *stack)
00839 {
00840
00841 iframe_t act;
00842 act.prim = PH_ACTIVATE | REQUEST;
00843 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
00844
00845
00846 act.dinfo = 0;
00847 act.len = 0;
00848
00849 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00850
00851 }
00852
00853 int misdn_lib_get_l2_up(struct misdn_stack *stack)
00854 {
00855
00856 if (stack->ptp && (stack->nt) ) {
00857 msg_t *dmsg;
00858
00859 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
00860
00861 pthread_mutex_lock(&stack->nstlock);
00862 if (stack->nst.manager_l3(&stack->nst, dmsg))
00863 free_msg(dmsg);
00864 pthread_mutex_unlock(&stack->nstlock);
00865 } else {
00866 iframe_t act;
00867
00868 act.prim = DL_ESTABLISH | REQUEST;
00869 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
00870
00871 act.dinfo = 0;
00872 act.len = 0;
00873 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00874 }
00875
00876 return 0;
00877 }
00878
00879 #if 0
00880 static int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack)
00881 {
00882 iframe_t act;
00883
00884 act.prim = DL_ESTABLISH | REQUEST;
00885 act.addr = (stack->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
00886
00887 act.dinfo = 0;
00888 act.len = 0;
00889 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00890 return 0;
00891 }
00892 #endif
00893
00894 static int misdn_lib_get_short_status(struct misdn_stack *stack)
00895 {
00896 iframe_t act;
00897
00898
00899 act.prim = MGR_SHORTSTATUS | REQUEST;
00900
00901 act.addr = (stack->upper_id | MSG_BROADCAST) ;
00902
00903 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
00904
00905 act.len = 0;
00906 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00907 }
00908
00909
00910
00911 static int create_process(int midev, struct misdn_bchannel *bc)
00912 {
00913 iframe_t ncr;
00914 int l3_id;
00915 int proc_id;
00916 struct misdn_stack *stack;
00917
00918 stack = get_stack_by_bc(bc);
00919 if (stack->nt) {
00920 if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, 0) < 0) {
00921 return -1;
00922 }
00923 cb_log(4, stack->port, " --> found channel: %d\n", bc->channel);
00924
00925 for (proc_id = 0; proc_id < MAXPROCS; ++proc_id) {
00926 if (stack->procids[proc_id] == 0) {
00927 break;
00928 }
00929 }
00930 if (proc_id == MAXPROCS) {
00931 cb_log(0, stack->port, "Couldn't Create New ProcId.\n");
00932 return -1;
00933 }
00934
00935 stack->procids[proc_id] = 1;
00936
00937 l3_id = 0xff00 | proc_id;
00938 bc->l3_id = l3_id;
00939 cb_log(3, stack->port, " --> new_l3id %x\n", l3_id);
00940 } else {
00941 if ((stack->pri && stack->ptp) || bc->te_choose_channel) {
00942
00943 if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, bc->dec) < 0) {
00944 return -1;
00945 }
00946 cb_log(2, stack->port, " --> found channel: %d\n", bc->channel);
00947 } else {
00948
00949 bc->channel = 0xff;
00950 }
00951
00952
00953 if (++new_te_id > 0xffff) {
00954 new_te_id = 0x0001;
00955 }
00956
00957 l3_id = (entity << 16) | new_te_id;
00958 bc->l3_id = l3_id;
00959 cb_log(3, stack->port, "--> new_l3id %x\n", l3_id);
00960
00961
00962 ncr.prim = CC_NEW_CR | REQUEST;
00963 ncr.addr = (stack->upper_id | FLG_MSG_DOWN);
00964 ncr.dinfo = l3_id;
00965 ncr.len = 0;
00966 mISDN_write(midev, &ncr, mISDN_HEADER_LEN + ncr.len, TIMEOUT_1SEC);
00967 }
00968
00969 return l3_id;
00970 }
00971
00972
00973 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
00974 {
00975 clean_up_bc(bc);
00976 setup_bc(bc);
00977 }
00978
00979
00980 int setup_bc(struct misdn_bchannel *bc)
00981 {
00982 unsigned char buff[1025];
00983 int midev;
00984 int channel;
00985 int b_stid;
00986 int i;
00987 mISDN_pid_t pid;
00988 int ret;
00989
00990 struct misdn_stack *stack=get_stack_by_bc(bc);
00991
00992 if (!stack) {
00993 cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
00994 return -1;
00995 }
00996
00997 midev = stack->midev;
00998 channel = bc->channel - 1 - (bc->channel > 16);
00999 b_stid = stack->b_stids[channel >= 0 ? channel : 0];
01000
01001 switch (bc->bc_state) {
01002 case BCHAN_CLEANED:
01003 break;
01004 default:
01005 cb_log(4, stack->port, "$$$ bc already setup stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
01006 return -1;
01007 }
01008
01009 cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
01010
01011
01012 for (i=0; i <= stack->b_num; i++) {
01013 if (stack->bc[i].b_stid == b_stid) {
01014 cb_log(0, bc->port, "setup_bc: b_stid:%x already in use !!!\n", b_stid);
01015 return -1;
01016 }
01017 }
01018
01019 if (b_stid <= 0) {
01020 cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
01021
01022 bc_state_change(bc,BCHAN_ERROR);
01023 return 1;
01024 }
01025
01026 bc->b_stid = b_stid;
01027
01028 {
01029 layer_info_t li;
01030 memset(&li, 0, sizeof(li));
01031
01032 li.object_id = -1;
01033 li.extentions = 0;
01034
01035 li.st = bc->b_stid;
01036
01037
01038 #define MISDN_DSP
01039 #ifndef MISDN_DSP
01040 bc->nodsp=1;
01041 #endif
01042 if ( bc->hdlc || bc->nodsp) {
01043 cb_log(4, stack->port,"setup_bc: without dsp\n");
01044 {
01045 int l = sizeof(li.name);
01046 strncpy(li.name, "B L3", l);
01047 li.name[l-1] = 0;
01048 }
01049 li.pid.layermask = ISDN_LAYER((3));
01050 li.pid.protocol[3] = ISDN_PID_L3_B_USER;
01051
01052 bc->layer=3;
01053 } else {
01054 cb_log(4, stack->port,"setup_bc: with dsp\n");
01055 {
01056 int l = sizeof(li.name);
01057 strncpy(li.name, "B L4", l);
01058 li.name[l-1] = 0;
01059 }
01060 li.pid.layermask = ISDN_LAYER((4));
01061 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
01062
01063 bc->layer=4;
01064 }
01065
01066 ret = mISDN_new_layer(midev, &li);
01067 if (ret ) {
01068 cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
01069
01070 bc_state_change(bc,BCHAN_ERROR);
01071 return(-EINVAL);
01072 }
01073
01074 bc->layer_id = li.id;
01075 }
01076
01077 memset(&pid, 0, sizeof(pid));
01078
01079
01080
01081 cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
01082
01083 if (bc->nodsp) {
01084 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
01085 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
01086 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
01087 pid.protocol[3] = ISDN_PID_L3_B_USER;
01088 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
01089
01090 } else if ( bc->hdlc ) {
01091 cb_log(2, stack->port," --> HDLC Mode\n");
01092 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
01093 pid.protocol[2] = ISDN_PID_L2_B_TRANS ;
01094 pid.protocol[3] = ISDN_PID_L3_B_USER;
01095 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
01096 } else {
01097 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
01098 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
01099 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
01100 pid.protocol[3] = ISDN_PID_L3_B_DSP;
01101 pid.protocol[4] = ISDN_PID_L4_B_USER;
01102 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
01103
01104 }
01105
01106 ret = mISDN_set_stack(midev, bc->b_stid, &pid);
01107
01108 if (ret){
01109 cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
01110
01111 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01112
01113 bc_state_change(bc,BCHAN_ERROR);
01114 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
01115 return(-EINVAL);
01116 }
01117
01118 ret = mISDN_get_setstack_ind(midev, bc->layer_id);
01119
01120 if (ret) {
01121 cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
01122 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01123
01124 bc_state_change(bc,BCHAN_ERROR);
01125 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
01126 return(-EINVAL);
01127 }
01128
01129 ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
01130
01131 bc->addr = ret>0? ret : 0;
01132
01133 if (!bc->addr) {
01134 cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
01135 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01136
01137 bc_state_change(bc,BCHAN_ERROR);
01138 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
01139 return (-EINVAL);
01140 }
01141
01142 manager_bchannel_activate(bc);
01143
01144 bc_state_change(bc,BCHAN_ACTIVATED);
01145
01146 return 0;
01147 }
01148
01149
01150
01151
01152 static int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime)
01153 {
01154 unsigned char buff[1025] = "";
01155 iframe_t *frm = (iframe_t *)buff;
01156 int ret;
01157
01158 if (!bc) return -1;
01159
01160 cb_log(8, port, "Init.BC %d.\n",bidx);
01161
01162 memset(bc, 0,sizeof(struct misdn_bchannel));
01163
01164 bc->send_lock=malloc(sizeof(struct send_lock));
01165 if (!bc->send_lock) {
01166 return -1;
01167 }
01168 pthread_mutex_init(&bc->send_lock->lock, NULL);
01169
01170 if (msn) {
01171 int l = sizeof(bc->msn);
01172 strncpy(bc->msn,msn, l);
01173 bc->msn[l-1] = 0;
01174 }
01175
01176
01177 empty_bc(bc);
01178 bc_state_change(bc, BCHAN_CLEANED);
01179
01180 bc->port=stack->port;
01181 bc->nt=stack->nt?1:0;
01182 bc->pri=stack->pri;
01183
01184 {
01185 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
01186
01187 if (!ibuf) return -1;
01188
01189 clear_ibuffer( ibuf);
01190
01191 ibuf->rsem=malloc(sizeof(sem_t));
01192 if (!ibuf->rsem) {
01193 return -1;
01194 }
01195
01196 bc->astbuf=ibuf;
01197
01198 if (sem_init(ibuf->rsem,1,0)<0)
01199 sem_init(ibuf->rsem,0,0);
01200
01201 }
01202
01203 {
01204 stack_info_t *stinf;
01205 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
01206 if (ret < 0) {
01207 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
01208 return -1;
01209 }
01210
01211 stinf = (stack_info_t *)&frm->data.p;
01212
01213 cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
01214 }
01215
01216 return 0;
01217 }
01218
01219
01220
01221 static struct misdn_stack *stack_init(int midev, int port, int ptp)
01222 {
01223 int ret;
01224 unsigned char buff[1025];
01225 iframe_t *frm = (iframe_t *)buff;
01226 stack_info_t *stinf;
01227 int i;
01228 layer_info_t li;
01229
01230 struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
01231 if (!stack ) return NULL;
01232
01233
01234 cb_log(8, port, "Init. Stack.\n");
01235
01236 memset(stack,0,sizeof(struct misdn_stack));
01237
01238 for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
01239
01240 stack->port=port;
01241 stack->midev=midev;
01242 stack->ptp=ptp;
01243
01244 stack->holding=NULL;
01245 stack->pri=0;
01246
01247 msg_queue_init(&stack->downqueue);
01248 msg_queue_init(&stack->upqueue);
01249
01250 pthread_mutex_init(&stack->st_lock, NULL);
01251
01252
01253 ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
01254 if (ret < 0) {
01255 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
01256 return(NULL);
01257 }
01258
01259 stinf = (stack_info_t *)&frm->data.p;
01260
01261 stack->d_stid = stinf->id;
01262 stack->b_num = stinf->childcnt;
01263
01264 for (i=0; i<=stinf->childcnt; i++)
01265 stack->b_stids[i] = stinf->child[i];
01266
01267 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
01268 case ISDN_PID_L0_TE_S0:
01269 stack->nt=0;
01270 break;
01271 case ISDN_PID_L0_NT_S0:
01272 cb_log(8, port, "NT Stack\n");
01273
01274 stack->nt=1;
01275 break;
01276 case ISDN_PID_L0_TE_E1:
01277 cb_log(8, port, "TE S2M Stack\n");
01278 stack->nt=0;
01279 stack->pri=1;
01280 break;
01281 case ISDN_PID_L0_NT_E1:
01282 cb_log(8, port, "TE S2M Stack\n");
01283 stack->nt=1;
01284 stack->pri=1;
01285
01286 break;
01287 default:
01288 cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
01289
01290 }
01291
01292 if (!stack->nt) {
01293 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) {
01294 stack->ptp = 1;
01295 } else {
01296 stack->ptp = 0;
01297 }
01298 }
01299
01300 {
01301 int ret;
01302 int nt=stack->nt;
01303
01304 cb_log(8, port, "Init. Stack.\n");
01305
01306 memset(&li, 0, sizeof(li));
01307 {
01308 int l = sizeof(li.name);
01309 strncpy(li.name,nt?"net l2":"user l4", l);
01310 li.name[l-1] = 0;
01311 }
01312 li.object_id = -1;
01313 li.extentions = 0;
01314 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
01315 li.pid.layermask = ISDN_LAYER((nt?2:4));
01316 li.st = stack->d_stid;
01317
01318
01319 ret = mISDN_new_layer(midev, &li);
01320 if (ret) {
01321 cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
01322 return(NULL);
01323 }
01324
01325
01326 stack->upper_id = li.id;
01327 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
01328 if (ret)
01329 {
01330 cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
01331 return(NULL);
01332 }
01333
01334 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3);
01335 if (stack->lower_id < 0) {
01336 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
01337 return(NULL);
01338 }
01339
01340 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
01341 if (stack->upper_id < 0) {
01342 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
01343 return(NULL);
01344 }
01345
01346 cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id);
01347
01348
01349
01350 if (nt) {
01351
01352 memset(&stack->nst, 0, sizeof(net_stack_t));
01353 memset(&stack->mgr, 0, sizeof(manager_t));
01354
01355 stack->mgr.nst = &stack->nst;
01356 stack->nst.manager = &stack->mgr;
01357
01358 stack->nst.l3_manager = handle_event_nt;
01359 stack->nst.device = midev;
01360 stack->nst.cardnr = port;
01361 stack->nst.d_stid = stack->d_stid;
01362
01363 stack->nst.feature = FEATURE_NET_HOLD;
01364 if (stack->ptp)
01365 stack->nst.feature |= FEATURE_NET_PTP;
01366 if (stack->pri)
01367 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
01368
01369 stack->nst.l1_id = stack->lower_id;
01370 stack->nst.l2_id = stack->upper_id;
01371
01372 msg_queue_init(&stack->nst.down_queue);
01373 pthread_mutex_init(&stack->nstlock, NULL);
01374
01375 Isdnl2Init(&stack->nst);
01376 Isdnl3Init(&stack->nst);
01377
01378 }
01379
01380 if (!stack->nt) {
01381
01382
01383 stack->l1link=0;
01384 }
01385 stack->l1link=0;
01386 stack->l2link=0;
01387 #if 0
01388 if (!stack->nt) {
01389 misdn_lib_get_short_status(stack);
01390 } else {
01391 misdn_lib_get_l1_up(stack);
01392 if (!stack->ptp) misdn_lib_get_l1_up(stack);
01393 misdn_lib_get_l2_up(stack);
01394 }
01395 #endif
01396
01397 misdn_lib_get_short_status(stack);
01398 misdn_lib_get_l1_up(stack);
01399 misdn_lib_get_l2_up(stack);
01400
01401 }
01402
01403 cb_log(8,0,"stack_init: port:%d lowerId:%x upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
01404
01405 return stack;
01406 }
01407
01408
01409 static void stack_destroy(struct misdn_stack *stack)
01410 {
01411 char buf[1024];
01412 if (!stack) return;
01413
01414 if (stack->nt) {
01415 pthread_mutex_destroy(&stack->nstlock);
01416 cleanup_Isdnl2(&stack->nst);
01417 cleanup_Isdnl3(&stack->nst);
01418 }
01419
01420 if (stack->lower_id)
01421 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01422
01423 if (stack->upper_id)
01424 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01425
01426 pthread_mutex_destroy(&stack->st_lock);
01427 }
01428
01429
01430 static struct misdn_stack * find_stack_by_addr(int addr)
01431 {
01432 struct misdn_stack *stack;
01433
01434 for (stack=glob_mgr->stack_list;
01435 stack;
01436 stack=stack->next) {
01437 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
01438
01439 }
01440
01441 return NULL;
01442 }
01443
01444
01445 static struct misdn_stack * find_stack_by_port(int port)
01446 {
01447 struct misdn_stack *stack;
01448
01449 for (stack=glob_mgr->stack_list;
01450 stack;
01451 stack=stack->next)
01452 if (stack->port == port) return stack;
01453
01454 return NULL;
01455 }
01456
01457 static struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
01458 {
01459 struct misdn_stack *stack;
01460
01461 for (stack=glob_mgr->stack_list;
01462 stack;
01463 stack=stack->next)
01464 if ( &stack->mgr == mgr_nt) return stack;
01465
01466 return NULL;
01467 }
01468
01469 static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
01470 {
01471 int i;
01472
01473 for (i = 0; i <= stack->b_num; i++) {
01474 if (stack->bc[i].in_use && (stack->bc[i].l3_id & mask) == (l3id & mask)) {
01475 return &stack->bc[i];
01476 }
01477 }
01478 return stack_holder_find(stack,l3id);
01479 }
01480
01481
01482 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
01483 {
01484 int i;
01485
01486 for (i = 0; i <= stack->b_num; i++) {
01487 if (stack->bc[i].in_use && stack->bc[i].l3_id == l3id) {
01488 return &stack->bc[i];
01489 }
01490 }
01491 return stack_holder_find(stack,l3id);
01492 }
01493
01494 static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
01495 {
01496 struct misdn_stack *stack;
01497 int i;
01498
01499 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
01500 for (i = 0; i <= stack->b_num; i++) {
01501 if (stack->bc[i].in_use
01502 && ((stack->bc[i].addr & STACK_ID_MASK) == (addr & STACK_ID_MASK)
01503 || stack->bc[i].layer_id == addr)) {
01504 return &stack->bc[i];
01505 }
01506 }
01507 }
01508
01509 return NULL;
01510 }
01511
01512 static struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
01513 {
01514 struct misdn_stack *stack;
01515 int i;
01516
01517 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
01518 for (i = 0; i <= stack->b_num; i++) {
01519 if (stack->bc[i].in_use && stack->bc[i].conf_id == confid) {
01520 return &stack->bc[i];
01521 }
01522 }
01523 }
01524 return NULL;
01525 }
01526
01527
01528 static struct misdn_bchannel *find_bc_by_channel(int port, int channel)
01529 {
01530 struct misdn_stack *stack = find_stack_by_port(port);
01531 int i;
01532
01533 if (!stack) {
01534 return NULL;
01535 }
01536
01537 for (i = 0; i <= stack->b_num; i++) {
01538 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
01539 return &stack->bc[i];
01540 }
01541 }
01542
01543 return NULL;
01544 }
01545
01546
01547
01548
01549
01550 static int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
01551 {
01552 struct misdn_stack *stack=get_stack_by_bc(bc);
01553
01554 if (!stack->nt) {
01555
01556 switch (event) {
01557
01558 case EVENT_CONNECT_ACKNOWLEDGE:
01559 setup_bc(bc);
01560
01561 if ( *bc->crypt_key ) {
01562 cb_log(4, stack->port, "ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s\n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
01563 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
01564 }
01565
01566 if (misdn_cap_is_speech(bc->capability)) {
01567 if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
01568 manager_ec_enable(bc);
01569
01570 if ( bc->txgain != 0 ) {
01571 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
01572 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
01573 }
01574 if ( bc->rxgain != 0 ) {
01575 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
01576 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
01577 }
01578 }
01579
01580 break;
01581 case EVENT_CONNECT:
01582
01583 if ( *bc->crypt_key ) {
01584 cb_log(4, stack->port, "ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s\n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
01585 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
01586 }
01587 case EVENT_ALERTING:
01588 case EVENT_PROGRESS:
01589 case EVENT_PROCEEDING:
01590 case EVENT_SETUP_ACKNOWLEDGE:
01591 case EVENT_SETUP:
01592 {
01593 if (bc->channel == 0xff || bc->channel<=0)
01594 bc->channel=0;
01595
01596 if (find_free_chan_in_stack(stack, bc, bc->channel, 0)<0){
01597 if (!stack->pri && !stack->ptp) {
01598 bc->cw=1;
01599 break;
01600 }
01601
01602 if (!bc->channel)
01603 cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
01604 else
01605 cb_log(0, stack->port,
01606 "Requested Channel Already in Use releasing this call with cause %d!!!!\n",
01607 bc->out_cause);
01608
01609
01610
01611
01612
01613 bc->channel=0;
01614
01615 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
01616 return -1;
01617 }
01618 }
01619
01620 setup_bc(bc);
01621 break;
01622
01623 case EVENT_RELEASE_COMPLETE:
01624 case EVENT_RELEASE:
01625 break;
01626 default:
01627 break;
01628 }
01629 } else {
01630
01631 }
01632 return 0;
01633 }
01634
01635 static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
01636 {
01637 struct misdn_bchannel *bc;
01638
01639 if (!stack) return -1;
01640
01641 switch (frm->prim) {
01642 case CC_NEW_CR|INDICATION:
01643 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
01644
01645 bc = misdn_lib_get_free_bc(stack->port, 0, 1, 0);
01646 if (!bc) {
01647 cb_log(0, stack->port, " --> !! lib: No free channel!\n");
01648 return -1;
01649 }
01650
01651 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
01652 bc->l3_id=frm->dinfo;
01653 return 1;
01654 case CC_NEW_CR|CONFIRM:
01655 return 1;
01656 case CC_NEW_CR|REQUEST:
01657 return 1;
01658 case CC_RELEASE_CR|REQUEST:
01659 return 1;
01660 case CC_RELEASE_CR|CONFIRM:
01661 break;
01662 case CC_RELEASE_CR|INDICATION:
01663 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
01664 {
01665 struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
01666 struct misdn_bchannel dummybc;
01667
01668 if (!bc) {
01669 cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
01670 misdn_make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0);
01671
01672 bc=&dummybc;
01673 }
01674
01675 if (bc) {
01676 int channel = bc->channel;
01677 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
01678
01679
01680 bc->need_disconnect=0;
01681 bc->need_release=0;
01682 bc->need_release_complete=0;
01683
01684 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
01685
01686 empty_bc(bc);
01687 clean_up_bc(bc);
01688
01689 if (channel>0)
01690 empty_chan_in_stack(stack,channel);
01691 bc->in_use=0;
01692
01693 dump_chan_list(stack);
01694
01695 if (bc->stack_holder) {
01696 cb_log(4,stack->port, "REMOVING Holder\n");
01697 stack_holder_remove( stack, bc);
01698 free(bc);
01699 }
01700 }
01701 else {
01702 if (stack->nt)
01703 cb_log(4, stack->port, "BC with dinfo: %x not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
01704 }
01705
01706 return 1;
01707 }
01708 break;
01709 }
01710
01711 return 0;
01712 }
01713
01714
01715
01716 void misdn_lib_release(struct misdn_bchannel *bc)
01717 {
01718 int channel;
01719 struct misdn_stack *stack=get_stack_by_bc(bc);
01720
01721 if (!stack) {
01722 cb_log(1,0,"misdn_release: No Stack found\n");
01723 return;
01724 }
01725
01726 channel = bc->channel;
01727 empty_bc(bc);
01728 clean_up_bc(bc);
01729 if (channel > 0) {
01730 empty_chan_in_stack(stack, channel);
01731 }
01732 bc->in_use=0;
01733 }
01734
01735
01736
01737
01738 int misdn_lib_get_port_up (int port)
01739 {
01740 struct misdn_stack *stack;
01741
01742 for (stack=glob_mgr->stack_list;
01743 stack;
01744 stack=stack->next) {
01745
01746 if (stack->port == port) {
01747
01748 if (!stack->l1link)
01749 misdn_lib_get_l1_up(stack);
01750 if (!stack->l2link)
01751 misdn_lib_get_l2_up(stack);
01752
01753 return 0;
01754 }
01755 }
01756 return 0;
01757 }
01758
01759
01760 int misdn_lib_get_port_down (int port)
01761 {
01762 struct misdn_stack *stack;
01763 for (stack=glob_mgr->stack_list;
01764 stack;
01765 stack=stack->next) {
01766 if (stack->port == port) {
01767 if (stack->l2link)
01768 misdn_lib_get_l2_down(stack);
01769 misdn_lib_get_l1_down(stack);
01770 return 0;
01771 }
01772 }
01773 return 0;
01774 }
01775
01776 int misdn_lib_port_up(int port, int check)
01777 {
01778 struct misdn_stack *stack;
01779
01780
01781 for (stack=glob_mgr->stack_list;
01782 stack;
01783 stack=stack->next) {
01784
01785 if (stack->port == port) {
01786
01787 if (stack->blocked) {
01788 cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
01789 return -1;
01790 }
01791
01792 if (stack->ptp ) {
01793
01794 if (stack->l1link && stack->l2link) {
01795 return 1;
01796 } else {
01797 cb_log(1,port, "Port Down L2:%d L1:%d\n",
01798 stack->l2link, stack->l1link);
01799 return 0;
01800 }
01801 } else {
01802 if ( !check || stack->l1link )
01803 return 1;
01804 else {
01805 cb_log(1,port, "Port down PMP\n");
01806 return 0;
01807 }
01808 }
01809 }
01810 }
01811
01812 return -1;
01813 }
01814
01815
01816 int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh)
01817 {
01818 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
01819 struct misdn_bchannel dummybc;
01820 iframe_t frm;
01821 frm.dinfo = hh->dinfo;
01822
01823 frm.addr=stack->upper_id | FLG_MSG_DOWN;
01824
01825 frm.prim = CC_RELEASE_CR|INDICATION;
01826 cb_log(4, stack->port, " --> CC_RELEASE_CR: Faking Release_cr for %x l3id:%x\n",frm.addr, frm.dinfo);
01827
01828 if (!bc) {
01829 cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
01830 misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0);
01831 bc=&dummybc;
01832 }
01833
01834 if (bc) {
01835 if ( (bc->l3_id & 0xff00) == 0xff00) {
01836 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
01837 stack->procids[bc->l3_id&0xff] = 0 ;
01838 }
01839 }
01840 else cb_log(0, stack->port, "Couldn't find BC so I couldn't remove the Process!!!! this is a bad port.\n");
01841
01842 if (handle_cr(stack, &frm)<0) {
01843 }
01844
01845 return 0 ;
01846 }
01847
01848 int
01849 handle_event_nt(void *dat, void *arg)
01850 {
01851 manager_t *mgr = (manager_t *)dat;
01852 msg_t *msg = (msg_t *)arg;
01853 mISDNuser_head_t *hh;
01854 struct misdn_stack *stack;
01855 int reject=0;
01856
01857 if (!msg || !mgr)
01858 return(-EINVAL);
01859
01860 stack = find_stack_by_mgr(mgr);
01861 hh=(mISDNuser_head_t*)msg->data;
01862
01863
01864
01865
01866
01867
01868 pthread_mutex_unlock(&stack->nstlock);
01869
01870 cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
01871 {
01872 switch(hh->prim){
01873 case CC_RETRIEVE|INDICATION:
01874 {
01875 struct misdn_bchannel *bc;
01876 struct misdn_bchannel *hold_bc;
01877
01878 iframe_t frm;
01879 frm.dinfo = hh->dinfo;
01880
01881 frm.addr=stack->upper_id | FLG_MSG_DOWN;
01882
01883 frm.prim = CC_NEW_CR|INDICATION;
01884
01885 if (handle_cr( stack, &frm)< 0) {
01886 msg_t *dmsg;
01887 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
01888 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
01889 pthread_mutex_lock(&stack->nstlock);
01890 stack->nst.manager_l3(&stack->nst, dmsg);
01891 free_msg(msg);
01892 return 0;
01893 }
01894
01895 bc = find_bc_by_l3id(stack, hh->dinfo);
01896 hold_bc = stack_holder_find(stack, bc->l3_id);
01897 cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
01898
01899 if (hold_bc) {
01900 cb_log(4, stack->port, "REMOVING Holder\n");
01901
01902
01903 stack_holder_remove(stack, hold_bc);
01904 memcpy(bc, hold_bc, sizeof(struct misdn_bchannel));
01905 free(hold_bc);
01906
01907 bc->holded=0;
01908 bc->b_stid=0;
01909 }
01910
01911 }
01912
01913 break;
01914
01915 case CC_SETUP|CONFIRM:
01916 {
01917 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
01918 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
01919 cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n",l3id );
01920
01921 if (bc) {
01922 cb_log (2, bc->port, "I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
01923 bc->l3_id = l3id;
01924 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
01925 } else {
01926 cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n");
01927 }
01928 }
01929 free_msg(msg);
01930 pthread_mutex_lock(&stack->nstlock);
01931 return 0;
01932
01933 case CC_SETUP|INDICATION:
01934 {
01935 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1, 0);
01936 if (!bc) {
01937 msg_t *dmsg;
01938 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
01939 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
01940 pthread_mutex_lock(&stack->nstlock);
01941 stack->nst.manager_l3(&stack->nst, dmsg);
01942 free_msg(msg);
01943 return 0;
01944 }
01945
01946 cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
01947 bc->l3_id=hh->dinfo;
01948 }
01949 break;
01950
01951 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
01952 break;
01953
01954 case CC_ALERTING|INDICATION:
01955 case CC_PROCEEDING|INDICATION:
01956 case CC_SETUP_ACKNOWLEDGE|INDICATION:
01957 if(!stack->ptp) break;
01958 case CC_CONNECT|INDICATION:
01959 break;
01960 case CC_DISCONNECT|INDICATION:
01961 {
01962 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
01963 if (!bc) {
01964 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
01965 if (bc) {
01966 int myprocid=bc->l3_id&0x0000ffff;
01967 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
01968 cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
01969 reject=1;
01970 }
01971 }
01972 }
01973 break;
01974
01975 case CC_FACILITY|INDICATION:
01976 {
01977 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
01978 if (!bc) {
01979 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
01980 if (bc) {
01981 int myprocid=bc->l3_id&0x0000ffff;
01982 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
01983 cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
01984 }
01985 }
01986 }
01987 break;
01988
01989 case CC_RELEASE_COMPLETE|INDICATION:
01990 break;
01991
01992 case CC_SUSPEND|INDICATION:
01993 {
01994 msg_t *dmsg;
01995 cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
01996 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
01997 pthread_mutex_lock(&stack->nstlock);
01998 stack->nst.manager_l3(&stack->nst, dmsg);
01999 free_msg(msg);
02000 return 0;
02001 }
02002 break;
02003 case CC_RESUME|INDICATION:
02004 break;
02005
02006 case CC_RELEASE|CONFIRM:
02007 {
02008 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
02009
02010 if (bc) {
02011 cb_log(1, stack->port, "CC_RELEASE|CONFIRM (l3id:%x), sending RELEASE_COMPLETE\n", hh->dinfo);
02012 misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
02013 }
02014 }
02015 break;
02016
02017 case CC_RELEASE|INDICATION:
02018 break;
02019
02020 case CC_RELEASE_CR|INDICATION:
02021 release_cr(stack, hh);
02022 free_msg(msg);
02023 pthread_mutex_lock(&stack->nstlock);
02024 return 0 ;
02025 break;
02026
02027 case CC_NEW_CR|INDICATION:
02028
02029
02030 {
02031 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
02032 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
02033 if (!bc) {
02034 cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n");
02035 pthread_mutex_lock(&stack->nstlock);
02036 return -1;
02037 }
02038 if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
02039 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
02040 stack->procids[bc->l3_id&0xff] = 0 ;
02041 }
02042 cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID is %x\n",l3id );
02043
02044 bc->l3_id =l3id;
02045 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
02046
02047 free_msg(msg);
02048 pthread_mutex_lock(&stack->nstlock);
02049 return 0;
02050 }
02051
02052 case DL_ESTABLISH | INDICATION:
02053 case DL_ESTABLISH | CONFIRM:
02054 {
02055 cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
02056
02057 if (stack->ptp && stack->l2link) {
02058 cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
02059 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
02060 }
02061
02062 if (stack->ptp && !stack->restart_sent) {
02063
02064
02065 stack->restart_sent=1;
02066 misdn_lib_send_restart(stack->port, -1);
02067
02068 }
02069
02070
02071 stack->l1link = 1;
02072 stack->l2link = 1;
02073 stack->l2upcnt=0;
02074
02075 free_msg(msg);
02076 pthread_mutex_lock(&stack->nstlock);
02077 return 0;
02078 }
02079 break;
02080
02081
02082 case DL_RELEASE | INDICATION:
02083 case DL_RELEASE | CONFIRM:
02084 {
02085 if (stack->ptp) {
02086 cb_log(3 , stack->port, "%% GOT L2 DeActivate Info.\n");
02087
02088 if (stack->l2upcnt>3) {
02089 cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attempts!!!\n");
02090 } else {
02091 #if 0
02092 if (stack->nt) misdn_lib_reinit_nt_stack(stack->port);
02093 #endif
02094 if (stack->l1link) {
02095 misdn_lib_get_l2_up(stack);
02096 stack->l2upcnt++;
02097 }
02098 }
02099
02100 } else
02101 cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
02102
02103 stack->l2link = 0;
02104 free_msg(msg);
02105 pthread_mutex_lock(&stack->nstlock);
02106 return 0;
02107 }
02108 break;
02109 }
02110 }
02111
02112 {
02113
02114 struct misdn_bchannel *bc;
02115 struct misdn_bchannel dummybc;
02116
02117 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
02118
02119 bc=find_bc_by_l3id(stack, hh->dinfo);
02120
02121 if (!bc) {
02122 cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x).\n", hh->dinfo);
02123 misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0);
02124 bc=&dummybc;
02125 }
02126 if (bc ) {
02127 isdn_msg_parse_event(msgs_g,msg,bc, 1);
02128
02129 switch (event) {
02130 case EVENT_SETUP:
02131 if (bc->channel<=0 || bc->channel==0xff)
02132 bc->channel=0;
02133
02134 if (find_free_chan_in_stack(stack,bc, bc->channel,0)<0) {
02135 msg_t *dmsg;
02136 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
02137 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
02138 pthread_mutex_lock(&stack->nstlock);
02139 stack->nst.manager_l3(&stack->nst, dmsg);
02140 free_msg(msg);
02141 return 0;
02142 }
02143 break;
02144 case EVENT_RELEASE:
02145 case EVENT_RELEASE_COMPLETE:
02146 {
02147 int channel=bc->channel;
02148 int tmpcause=bc->cause;
02149 empty_bc(bc);
02150 bc->cause=tmpcause;
02151 clean_up_bc(bc);
02152
02153 if (channel>0)
02154 empty_chan_in_stack(stack,channel);
02155 bc->in_use=0;
02156 }
02157 break;
02158
02159 default:
02160 break;
02161 }
02162
02163 if(!isdn_get_info(msgs_g,event,1)) {
02164 cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
02165 } else {
02166 if (reject) {
02167 switch(bc->cause){
02168 case AST_CAUSE_USER_BUSY:
02169 cb_log(1, stack->port, "Siemens Busy reject..\n");
02170
02171 break;
02172 default:
02173 break;
02174 }
02175 }
02176 cb_event(event, bc, glob_mgr->user_data);
02177 }
02178 } else {
02179 cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo);
02180 }
02181
02182 free_msg(msg);
02183 }
02184
02185
02186 pthread_mutex_lock(&stack->nstlock);
02187 return 0;
02188 }
02189
02190
02191 static int handle_timers(msg_t* msg)
02192 {
02193 iframe_t *frm= (iframe_t*)msg->data;
02194 struct misdn_stack *stack;
02195
02196
02197 switch (frm->prim) {
02198 case MGR_INITTIMER | CONFIRM:
02199 case MGR_ADDTIMER | CONFIRM:
02200 case MGR_DELTIMER | CONFIRM:
02201 case MGR_REMOVETIMER | CONFIRM:
02202 free_msg(msg);
02203 return(1);
02204 }
02205
02206
02207
02208 if (frm->prim==(MGR_TIMER | INDICATION) ) {
02209 for (stack = glob_mgr->stack_list;
02210 stack;
02211 stack = stack->next) {
02212 itimer_t *it;
02213
02214 if (!stack->nt) continue;
02215
02216 it = stack->nst.tlist;
02217
02218 for(it=stack->nst.tlist;
02219 it;
02220 it=it->next) {
02221 if (it->id == (int)frm->addr)
02222 break;
02223 }
02224 if (it) {
02225 int ret;
02226 ret = mISDN_write_frame(stack->midev, msg->data, frm->addr,
02227 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
02228 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
02229 pthread_mutex_lock(&stack->nstlock);
02230 ret = it->function(it->data);
02231 pthread_mutex_unlock(&stack->nstlock);
02232 free_msg(msg);
02233 return 1;
02234 }
02235 }
02236
02237 cb_log(0, 0, "Timer Msg without Timer ??\n");
02238 free_msg(msg);
02239 return 1;
02240 }
02241
02242 return 0;
02243 }
02244
02245
02246
02247 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
02248 {
02249 bc->generate_tone=1;
02250 }
02251
02252 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
02253 {
02254 bc->generate_tone=0;
02255 }
02256
02257
02258 static int do_tone(struct misdn_bchannel *bc, int len)
02259 {
02260 bc->tone_cnt=len;
02261
02262 if (bc->generate_tone) {
02263 cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
02264
02265 if ( !bc->nojitter ) {
02266 misdn_tx_jitter(bc,len);
02267 }
02268
02269 return 1;
02270 }
02271
02272 return 0;
02273 }
02274
02275
02276 #ifdef MISDN_SAVE_DATA
02277 static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2)
02278 {
02279 char n1[32],n2[32];
02280 FILE *rx, *tx;
02281
02282 sprintf(n1,"/tmp/misdn-rx-%d.raw",id);
02283 sprintf(n2,"/tmp/misdn-tx-%d.raw",id);
02284
02285 rx = fopen(n1,"a+");
02286 tx = fopen(n2,"a+");
02287
02288 if (!rx || !tx) {
02289 cb_log(0,0,"Couldn't open files: %s\n",strerror(errno));
02290 return ;
02291 }
02292
02293 fwrite(p1,1,l1,rx);
02294 fwrite(p2,1,l2,tx);
02295
02296 fclose(rx);
02297 fclose(tx);
02298
02299 }
02300 #endif
02301
02302 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
02303 {
02304 char buf[4096 + mISDN_HEADER_LEN];
02305 char *data=&buf[mISDN_HEADER_LEN];
02306 iframe_t *txfrm= (iframe_t*)buf;
02307 int jlen, r;
02308
02309 jlen=cb_jb_empty(bc,data,len);
02310
02311 if (jlen) {
02312 #ifdef MISDN_SAVE_DATA
02313 misdn_save_data((bc->port*100+bc->channel), data, jlen, bc->bframe, bc->bframe_len);
02314 #endif
02315 flip_buf_bits( data, jlen);
02316
02317 if (jlen < len) {
02318 cb_log(1, bc->port, "Jitterbuffer Underrun. Got %d of expected %d\n", jlen, len);
02319 }
02320
02321 txfrm->prim = DL_DATA|REQUEST;
02322
02323 txfrm->dinfo = 0;
02324
02325 txfrm->addr = bc->addr|FLG_MSG_DOWN;
02326
02327 txfrm->len =jlen;
02328 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
02329
02330 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
02331 } else {
02332 #define MISDN_GEN_SILENCE
02333 #ifdef MISDN_GEN_SILENCE
02334 int cnt=len/TONE_SILENCE_SIZE;
02335 int rest=len%TONE_SILENCE_SIZE;
02336 int i;
02337
02338 for (i=0; i<cnt; i++) {
02339 memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE );
02340 data +=TONE_SILENCE_SIZE;
02341 }
02342
02343 if (rest) {
02344 memcpy(data, tone_silence_flip, rest);
02345 }
02346
02347 txfrm->prim = DL_DATA|REQUEST;
02348
02349 txfrm->dinfo = 0;
02350
02351 txfrm->addr = bc->addr|FLG_MSG_DOWN;
02352
02353 txfrm->len =len;
02354 cb_log(5, bc->port, "Transmitting %d samples of silence to misdn\n", len);
02355
02356 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
02357 #else
02358 r = 0;
02359 #endif
02360 }
02361
02362 if (r < 0) {
02363 cb_log(1, bc->port, "Error in mISDN_write (%s)\n", strerror(errno));
02364 }
02365 }
02366
02367 static int handle_bchan(msg_t *msg)
02368 {
02369 iframe_t *frm= (iframe_t*)msg->data;
02370 struct misdn_bchannel *bc=find_bc_by_addr(frm->addr);
02371 struct misdn_stack *stack;
02372
02373 if (!bc) {
02374 cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
02375 return 0 ;
02376 }
02377
02378 stack = get_stack_by_bc(bc);
02379
02380 if (!stack) {
02381 cb_log(0, bc->port,"handle_bchan: STACK not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
02382 return 0;
02383 }
02384
02385 switch (frm->prim) {
02386
02387 case MGR_SETSTACK| CONFIRM:
02388 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid);
02389 break;
02390
02391 case MGR_SETSTACK| INDICATION:
02392 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid);
02393 break;
02394 #if 0
02395 AGAIN:
02396 bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer);
02397 if (!bc->addr) {
02398
02399 if (errno == EAGAIN) {
02400 usleep(1000);
02401 goto AGAIN;
02402 }
02403
02404 cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno));
02405
02406
02407
02408 bc->addr= frm->addr;
02409 } else if ( bc->addr < 0) {
02410 cb_log(0, stack->port,"$$$ bc->addr <0 Error:%s\n",strerror(errno));
02411 bc->addr=0;
02412 }
02413
02414 cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
02415
02416 free_msg(msg);
02417
02418
02419 switch(bc->bc_state) {
02420 case BCHAN_SETUP:
02421 bc_state_change(bc,BCHAN_SETUPED);
02422 break;
02423
02424 case BCHAN_CLEAN_REQUEST:
02425 default:
02426 cb_log(0, stack->port," --> STATE WASN'T SETUP (but %s) in SETSTACK|IND pid:%d\n",bc_state2str(bc->bc_state), bc->pid);
02427 clean_up_bc(bc);
02428 }
02429 return 1;
02430 #endif
02431
02432 case MGR_DELLAYER| INDICATION:
02433 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid);
02434 break;
02435
02436 case MGR_DELLAYER| CONFIRM:
02437 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid);
02438
02439 bc->pid=0;
02440 bc->addr=0;
02441
02442 free_msg(msg);
02443 return 1;
02444
02445 case PH_ACTIVATE | INDICATION:
02446 case DL_ESTABLISH | INDICATION:
02447 cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid);
02448
02449 free_msg(msg);
02450 return 1;
02451
02452 case PH_ACTIVATE | CONFIRM:
02453 case DL_ESTABLISH | CONFIRM:
02454
02455 cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid);
02456 free_msg(msg);
02457
02458 return 1;
02459
02460 case DL_ESTABLISH | REQUEST:
02461 {
02462 char buf[128];
02463 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
02464 }
02465 free_msg(msg);
02466 return 1;
02467
02468 case DL_RELEASE|REQUEST:
02469 {
02470 char buf[128];
02471 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
02472 }
02473 free_msg(msg);
02474 return 1;
02475
02476 case PH_DEACTIVATE | INDICATION:
02477 case DL_RELEASE | INDICATION:
02478 cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid);
02479
02480 free_msg(msg);
02481 return 1;
02482
02483 case PH_DEACTIVATE | CONFIRM:
02484 case DL_RELEASE | CONFIRM:
02485 cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid);
02486
02487 free_msg(msg);
02488 return 1;
02489
02490 case PH_CONTROL|INDICATION:
02491 {
02492 unsigned int *cont = (unsigned int *) &frm->data.p;
02493
02494 cb_log(4, stack->port, "PH_CONTROL: channel:%d oad%d:%s dad%d:%s \n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
02495
02496 if ((*cont & ~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
02497 int dtmf = *cont & DTMF_TONE_MASK;
02498 cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
02499 bc->dtmf=dtmf;
02500 cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
02501
02502 free_msg(msg);
02503 return 1;
02504 }
02505 if (*cont == BF_REJECT) {
02506 cb_log(4, stack->port, " --> BF REJECT\n");
02507 free_msg(msg);
02508 return 1;
02509 }
02510 if (*cont == BF_ACCEPT) {
02511 cb_log(4, stack->port, " --> BF ACCEPT\n");
02512 free_msg(msg);
02513 return 1;
02514 }
02515 }
02516 break;
02517
02518 case PH_DATA|REQUEST:
02519 case DL_DATA|REQUEST:
02520 cb_log(0, stack->port, "DL_DATA REQUEST \n");
02521 do_tone(bc, 64);
02522
02523 free_msg(msg);
02524 return 1;
02525
02526
02527 case PH_DATA|INDICATION:
02528 case DL_DATA|INDICATION:
02529 {
02530 bc->bframe = (void*)&frm->data.i;
02531 bc->bframe_len = frm->len;
02532
02533
02534 if ( misdn_cap_is_speech(bc->capability) )
02535 flip_buf_bits(bc->bframe, bc->bframe_len);
02536
02537
02538 if (!bc->bframe_len) {
02539 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
02540 free_msg(msg);
02541 return 1;
02542 }
02543
02544 if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
02545 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
02546 free_msg(msg);
02547 return 1;
02548 }
02549
02550 #if MISDN_DEBUG
02551 cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
02552
02553 #endif
02554
02555 if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
02556 int t;
02557
02558 #ifdef MISDN_B_DEBUG
02559 cb_log(0,bc->port,"do_tone START\n");
02560 #endif
02561 t=do_tone(bc,frm->len);
02562
02563 #ifdef MISDN_B_DEBUG
02564 cb_log(0,bc->port,"do_tone STOP (%d)\n",t);
02565 #endif
02566 if ( !t ) {
02567 int i;
02568
02569 if ( misdn_cap_is_speech(bc->capability)) {
02570 if ( !bc->nojitter ) {
02571 #ifdef MISDN_B_DEBUG
02572 cb_log(0,bc->port,"tx_jitter START\n");
02573 #endif
02574 misdn_tx_jitter(bc,frm->len);
02575 #ifdef MISDN_B_DEBUG
02576 cb_log(0,bc->port,"tx_jitter STOP\n");
02577 #endif
02578 }
02579 }
02580
02581 #ifdef MISDN_B_DEBUG
02582 cb_log(0,bc->port,"EVENT_B_DATA START\n");
02583 #endif
02584
02585 i = cb_event(EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
02586 #ifdef MISDN_B_DEBUG
02587 cb_log(0,bc->port,"EVENT_B_DATA STOP\n");
02588 #endif
02589
02590 if (i<0) {
02591 cb_log(10,stack->port,"cb_event returned <0\n");
02592
02593 }
02594 }
02595 }
02596 free_msg(msg);
02597 return 1;
02598 }
02599
02600
02601 case PH_CONTROL | CONFIRM:
02602 cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
02603 free_msg(msg);
02604 return 1;
02605
02606 case PH_DATA | CONFIRM:
02607 case DL_DATA|CONFIRM:
02608 #if MISDN_DEBUG
02609
02610 cb_log(0, stack->port, "Data confirmed\n");
02611
02612 #endif
02613 free_msg(msg);
02614 return 1;
02615 case DL_DATA|RESPONSE:
02616 #if MISDN_DEBUG
02617 cb_log(0, stack->port, "Data response\n");
02618
02619 #endif
02620 break;
02621 }
02622
02623 return 0;
02624 }
02625
02626
02627
02628 static int handle_frm_nt(msg_t *msg)
02629 {
02630 iframe_t *frm= (iframe_t*)msg->data;
02631 struct misdn_stack *stack;
02632 int err=0;
02633
02634 stack=find_stack_by_addr( frm->addr );
02635
02636
02637
02638 if (!stack || !stack->nt) {
02639 return 0;
02640 }
02641
02642
02643 pthread_mutex_lock(&stack->nstlock);
02644 if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
02645 pthread_mutex_unlock(&stack->nstlock);
02646 if (nt_err_cnt > 0 ) {
02647 if (nt_err_cnt < 100) {
02648 nt_err_cnt++;
02649 cb_log(0, stack->port, "NT Stack sends us error: %d \n", err);
02650 } else if (nt_err_cnt < 105){
02651 cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
02652 nt_err_cnt = - 1;
02653 }
02654 }
02655 free_msg(msg);
02656 return 1;
02657 }
02658 pthread_mutex_unlock(&stack->nstlock);
02659 return 1;
02660 }
02661
02662
02663 static int handle_frm(msg_t *msg)
02664 {
02665 iframe_t *frm = (iframe_t*) msg->data;
02666
02667 struct misdn_stack *stack=find_stack_by_addr(frm->addr);
02668
02669 if (!stack || stack->nt) {
02670 return 0;
02671 }
02672
02673 cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim);
02674
02675 {
02676 struct misdn_bchannel dummybc;
02677 struct misdn_bchannel *bc;
02678 int ret=handle_cr(stack, frm);
02679
02680 if (ret<0) {
02681 cb_log(3,stack?stack->port:0,"handle_frm: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
02682
02683
02684 }
02685
02686 if(ret) {
02687 free_msg(msg);
02688 return 1;
02689 }
02690
02691 bc=find_bc_by_l3id(stack, frm->dinfo);
02692
02693 if (!bc && (frm->prim==(CC_RESTART|CONFIRM)) ) {
02694 misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0);
02695 bc=&dummybc;
02696 }
02697
02698 if (!bc && (frm->prim==(CC_SETUP|INDICATION)) ) {
02699 misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0);
02700 dummybc.port=stack->port;
02701 dummybc.l3_id=frm->dinfo;
02702 bc=&dummybc;
02703
02704 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
02705
02706 free_msg(msg);
02707 return 1;
02708 }
02709
02710
02711 handle_frm_bc:
02712 if (bc ) {
02713 enum event_e event = isdn_msg_get_event(msgs_g, msg, 0);
02714 enum event_response_e response=RESPONSE_OK;
02715 int ret;
02716
02717 isdn_msg_parse_event(msgs_g,msg,bc, 0);
02718
02719
02720 ret = handle_event(bc, event, frm);
02721 if (ret<0) {
02722 cb_log(0,stack->port,"couldn't handle event\n");
02723 free_msg(msg);
02724 return 1;
02725 }
02726
02727 cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
02728
02729 if(!isdn_get_info(msgs_g,event,0))
02730 cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
02731 else
02732 response=cb_event(event, bc, glob_mgr->user_data);
02733 #if 1
02734 if (event == EVENT_SETUP) {
02735 switch (response) {
02736 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
02737
02738 cb_log(0, stack->port, "TOTALLY IGNORING SETUP\n");
02739
02740 break;
02741 case RESPONSE_IGNORE_SETUP:
02742
02743 bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
02744
02745 case RESPONSE_RELEASE_SETUP:
02746 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
02747 if (bc->channel>0)
02748 empty_chan_in_stack(stack, bc->channel);
02749 empty_bc(bc);
02750 bc_state_change(bc,BCHAN_CLEANED);
02751 bc->in_use=0;
02752
02753 cb_log(0, stack->port, "GOT IGNORE SETUP\n");
02754 break;
02755 case RESPONSE_OK:
02756 cb_log(4, stack->port, "GOT SETUP OK\n");
02757
02758
02759 break;
02760 default:
02761 break;
02762 }
02763 }
02764
02765 if (event == EVENT_RELEASE_COMPLETE) {
02766
02767 int channel=bc->channel;
02768 int tmpcause=bc->cause;
02769 int tmp_out_cause=bc->out_cause;
02770 empty_bc(bc);
02771 bc->cause=tmpcause;
02772 bc->out_cause=tmp_out_cause;
02773 clean_up_bc(bc);
02774 bc->in_use = 0;
02775
02776 if (tmpcause == AST_CAUSE_REQUESTED_CHAN_UNAVAIL) {
02777 cb_log(0, stack->port, "**** Received CAUSE:%d, restarting channel %d\n", AST_CAUSE_REQUESTED_CHAN_UNAVAIL, channel);
02778 misdn_lib_send_restart(stack->port, channel);
02779 }
02780 if (channel > 0) {
02781 empty_chan_in_stack(stack, channel);
02782 }
02783 }
02784
02785 if (event == EVENT_RESTART) {
02786 cb_log(0, stack->port, "**** Received RESTART channel:%d\n", bc->restart_channel);
02787 empty_chan_in_stack(stack, bc->restart_channel);
02788 }
02789
02790 cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
02791
02792
02793 free_msg(msg);
02794 return 1;
02795 #endif
02796
02797 } else {
02798 struct misdn_bchannel dummybc;
02799 cb_log(0, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
02800 memset (&dummybc,0,sizeof(dummybc));
02801 dummybc.port=stack->port;
02802 dummybc.l3_id=frm->dinfo;
02803 bc=&dummybc;
02804 goto handle_frm_bc;
02805 }
02806 }
02807
02808 cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
02809 return 0;
02810 }
02811
02812
02813 static int handle_l1(msg_t *msg)
02814 {
02815 iframe_t *frm = (iframe_t*) msg->data;
02816 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
02817 int i ;
02818
02819 if (!stack) return 0 ;
02820
02821 switch (frm->prim) {
02822 case PH_ACTIVATE | CONFIRM:
02823 case PH_ACTIVATE | INDICATION:
02824 cb_log (3, stack->port, "L1: PH L1Link Up!\n");
02825 stack->l1link=1;
02826
02827 if (stack->nt) {
02828
02829 pthread_mutex_lock(&stack->nstlock);
02830 if (stack->nst.l1_l2(&stack->nst, msg))
02831 free_msg(msg);
02832 pthread_mutex_unlock(&stack->nstlock);
02833
02834 if (stack->ptp)
02835 misdn_lib_get_l2_up(stack);
02836 } else {
02837 free_msg(msg);
02838 }
02839
02840 for (i=0;i<=stack->b_num; i++) {
02841 if (stack->bc[i].evq != EVENT_NOTHING) {
02842 cb_log(4, stack->port, "Firing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
02843 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
02844 stack->bc[i].evq=EVENT_NOTHING;
02845 }
02846
02847 }
02848 return 1;
02849
02850 case PH_ACTIVATE | REQUEST:
02851 free_msg(msg);
02852 cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
02853 return 1;
02854
02855 case PH_DEACTIVATE | REQUEST:
02856 free_msg(msg);
02857 cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
02858 return 1;
02859
02860 case PH_DEACTIVATE | CONFIRM:
02861 case PH_DEACTIVATE | INDICATION:
02862 cb_log (3, stack->port, "L1: PH L1Link Down! \n");
02863
02864 #if 0
02865 for (i=0; i<=stack->b_num; i++) {
02866 if (global_state == MISDN_INITIALIZED) {
02867 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
02868 }
02869 }
02870 #endif
02871
02872 if (stack->nt) {
02873 pthread_mutex_lock(&stack->nstlock);
02874 if (stack->nst.l1_l2(&stack->nst, msg))
02875 free_msg(msg);
02876 pthread_mutex_unlock(&stack->nstlock);
02877 } else {
02878 free_msg(msg);
02879 }
02880
02881 stack->l1link=0;
02882 stack->l2link=0;
02883 return 1;
02884 }
02885
02886 return 0;
02887 }
02888
02889 static int handle_l2(msg_t *msg)
02890 {
02891 iframe_t *frm = (iframe_t*) msg->data;
02892
02893 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
02894
02895 if (!stack) {
02896 return 0 ;
02897 }
02898
02899 switch(frm->prim) {
02900
02901 case DL_ESTABLISH | REQUEST:
02902 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
02903 free_msg(msg);
02904 return 1;
02905 case DL_RELEASE | REQUEST:
02906 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
02907 free_msg(msg);
02908 return 1;
02909
02910 case DL_ESTABLISH | INDICATION:
02911 case DL_ESTABLISH | CONFIRM:
02912 {
02913 cb_log (3, stack->port, "L2: L2Link Up! \n");
02914 if (stack->ptp && stack->l2link) {
02915 cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n");
02916 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
02917 }
02918 stack->l2link=1;
02919 free_msg(msg);
02920 return 1;
02921 }
02922 break;
02923
02924 case DL_RELEASE | INDICATION:
02925 case DL_RELEASE | CONFIRM:
02926 {
02927 cb_log (3, stack->port, "L2: L2Link Down! \n");
02928 stack->l2link=0;
02929
02930 free_msg(msg);
02931 return 1;
02932 }
02933 break;
02934 }
02935 return 0;
02936 }
02937
02938 static int handle_mgmt(msg_t *msg)
02939 {
02940 iframe_t *frm = (iframe_t*) msg->data;
02941 struct misdn_stack *stack;
02942
02943 if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
02944 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
02945 free_msg(msg);
02946 return 1;
02947 }
02948
02949 stack = find_stack_by_addr(frm->addr);
02950
02951 if (!stack) {
02952 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
02953 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
02954 frm->addr) ;
02955 free_msg(msg);
02956 return 1;
02957 }
02958
02959 return 0;
02960 }
02961
02962 switch(frm->prim) {
02963 case MGR_SHORTSTATUS | INDICATION:
02964 case MGR_SHORTSTATUS | CONFIRM:
02965 cb_log(5, stack->port, "MGMT: Short status dinfo %x\n",frm->dinfo);
02966
02967 switch (frm->dinfo) {
02968 case SSTATUS_L1_ACTIVATED:
02969 cb_log(3, stack->port, "MGMT: SSTATUS: L1_ACTIVATED \n");
02970 stack->l1link=1;
02971
02972 break;
02973 case SSTATUS_L1_DEACTIVATED:
02974 cb_log(3, stack->port, "MGMT: SSTATUS: L1_DEACTIVATED \n");
02975 stack->l1link=0;
02976 #if 0
02977 clear_l3(stack);
02978 #endif
02979 break;
02980
02981 case SSTATUS_L2_ESTABLISHED:
02982 cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
02983
02984
02985 stack->l1link=1;
02986 stack->l2link=1;
02987 break;
02988
02989 case SSTATUS_L2_RELEASED:
02990 cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
02991 stack->l2link=0;
02992 break;
02993 }
02994
02995 free_msg(msg);
02996 return 1;
02997
02998 case MGR_SETSTACK | INDICATION:
02999 cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
03000 free_msg(msg);
03001 return 1;
03002 case MGR_DELLAYER | CONFIRM:
03003 cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
03004 free_msg(msg);
03005 return 1;
03006
03007 }
03008
03009
03010
03011
03012
03013
03014
03015
03016 return 0;
03017 }
03018
03019
03020 static msg_t *fetch_msg(int midev)
03021 {
03022 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
03023 int r;
03024
03025 if (!msg) {
03026 cb_log(0, 0, "fetch_msg: alloc msg failed !!");
03027 return NULL;
03028 }
03029
03030 AGAIN:
03031 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
03032 msg->len=r;
03033
03034 if (r==0) {
03035 free_msg(msg);
03036 cb_log(6,0,"Got empty Msg..\n");
03037 return NULL;
03038 }
03039
03040 if (r<0) {
03041 if (errno == EAGAIN) {
03042
03043 cb_log(4,0,"mISDN_read wants us to wait\n");
03044 usleep(5000);
03045 goto AGAIN;
03046 }
03047
03048 cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno);
03049 }
03050
03051 #if 0
03052 if (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION)))
03053 cb_log(0,0,"prim: %x dinfo:%x addr:%x msglen:%d frm->len:%d\n",frm->prim, frm->dinfo, frm->addr, msg->len,frm->len );
03054 #endif
03055 return msg;
03056 }
03057
03058 void misdn_lib_isdn_l1watcher(int port)
03059 {
03060 struct misdn_stack *stack;
03061
03062 for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
03063 ;
03064
03065 if (stack) {
03066 cb_log(4, port, "Checking L1 State\n");
03067 if (!stack->l1link) {
03068 cb_log(4, port, "L1 State Down, trying to get it up again\n");
03069 misdn_lib_get_short_status(stack);
03070 misdn_lib_get_l1_up(stack);
03071 misdn_lib_get_l2_up(stack);
03072 }
03073 }
03074 }
03075
03076
03077 static void misdn_lib_isdn_event_catcher(void *arg)
03078 {
03079 struct misdn_lib *mgr = arg;
03080 int zero_frm=0 , fff_frm=0 ;
03081 int midev= mgr->midev;
03082 int port=0;
03083
03084 while (1) {
03085 msg_t *msg = fetch_msg(midev);
03086 iframe_t *frm;
03087
03088
03089 if (!msg) continue;
03090
03091 frm = (iframe_t*) msg->data;
03092
03093
03094 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
03095 zero_frm++;
03096 free_msg(msg);
03097 continue;
03098 } else {
03099 if (zero_frm) {
03100 cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
03101 zero_frm = 0 ;
03102 }
03103 }
03104
03105
03106 if (frm->len == 0 && frm->dinfo == 0 && frm->prim == 0xffffffff ) {
03107 fff_frm++;
03108 free_msg(msg);
03109 continue;
03110 } else {
03111 if (fff_frm) {
03112 cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
03113 fff_frm = 0 ;
03114 }
03115 }
03116
03117 manager_isdn_handler(frm, msg);
03118 }
03119
03120 }
03121
03122
03123
03124
03125 int te_lib_init() {
03126 char buff[1025] = "";
03127 iframe_t *frm=(iframe_t*)buff;
03128 int midev=mISDN_open();
03129 int ret;
03130
03131 if (midev<=0) return midev;
03132
03133
03134 mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
03135 ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
03136
03137 if (ret < mISDN_HEADER_LEN) {
03138 noentity:
03139 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
03140 exit(-1);
03141 }
03142
03143 entity = frm->dinfo & 0xffff ;
03144
03145 if (!entity)
03146 goto noentity;
03147
03148 return midev;
03149
03150 }
03151
03152 void te_lib_destroy(int midev)
03153 {
03154 char buf[1024];
03155 mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
03156
03157 cb_log(4, 0, "Entity deleted\n");
03158 mISDN_close(midev);
03159 cb_log(4, 0, "midev closed\n");
03160 }
03161
03162 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
03163 {
03164 struct misdn_stack *stack;
03165 int i;
03166
03167 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
03168 for (i = 0; i <= stack->b_num; i++) {
03169 if (stack->bc[i].in_use && stack->bc[i].pid == pid) {
03170 return &stack->bc[i];
03171 }
03172 }
03173 }
03174
03175 return NULL;
03176 }
03177
03178 static int test_inuse(struct misdn_bchannel *bc)
03179 {
03180 struct timeval now;
03181 gettimeofday(&now, NULL);
03182 if (!bc->in_use) {
03183 if (misdn_lib_port_is_pri(bc->port) && bc->last_used.tv_sec == now.tv_sec ) {
03184 cb_log(2,bc->port, "channel with stid:%x for one second still in use! (n:%d lu:%d)\n", bc->b_stid, (int) now.tv_sec, (int) bc->last_used.tv_sec);
03185 return 1;
03186 }
03187
03188
03189 cb_log(3,bc->port, "channel with stid:%x not in use!\n", bc->b_stid);
03190 return 0;
03191 }
03192
03193 cb_log(2,bc->port, "channel with stid:%x in use!\n", bc->b_stid);
03194 return 1;
03195 }
03196
03197
03198 static void prepare_bc(struct misdn_bchannel*bc, int channel)
03199 {
03200 bc->channel = channel;
03201 bc->channel_preselected = channel?1:0;
03202 bc->need_disconnect=1;
03203 bc->need_release=1;
03204 bc->need_release_complete=1;
03205 bc->cause = AST_CAUSE_NORMAL_CLEARING;
03206
03207 if (++mypid>5000) mypid=1;
03208 bc->pid=mypid;
03209
03210 #if 0
03211 bc->addr=0;
03212 bc->b_stid=0;
03213 bc->layer_id=0;
03214 #endif
03215
03216 bc->in_use = 1;
03217 }
03218
03219 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout, int dec)
03220 {
03221 struct misdn_stack *stack;
03222 int i;
03223
03224 if (channel < 0 || channel > MAX_BCHANS) {
03225 cb_log(0,port,"Requested channel out of bounds (%d)\n",channel);
03226 return NULL;
03227 }
03228
03229 for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
03230
03231 if (stack->port == port) {
03232 int maxnum;
03233
03234 if (stack->blocked) {
03235 cb_log(0,port,"Port is blocked\n");
03236 return NULL;
03237 }
03238
03239 pthread_mutex_lock(&stack->st_lock);
03240 if (channel > 0) {
03241 if (channel <= stack->b_num) {
03242 for (i = 0; i < stack->b_num; i++) {
03243 if ( stack->bc[i].channel == channel) {
03244 if (test_inuse(&stack->bc[i])) {
03245 pthread_mutex_unlock(&stack->st_lock);
03246 cb_log(0,port,"Requested channel:%d on port:%d is already in use\n",channel, port);
03247 return NULL;
03248
03249 } else {
03250 prepare_bc(&stack->bc[i], channel);
03251 pthread_mutex_unlock(&stack->st_lock);
03252 return &stack->bc[i];
03253 }
03254 }
03255 }
03256 } else {
03257 pthread_mutex_unlock(&stack->st_lock);
03258 cb_log(0,port,"Requested channel:%d is out of bounds on port:%d\n",channel, port);
03259 return NULL;
03260 }
03261 }
03262
03263 maxnum = inout && !stack->pri && !stack->ptp ? stack->b_num + 1 : stack->b_num;
03264
03265 if (dec) {
03266 for (i = maxnum-1; i>=0; i--) {
03267 if (!test_inuse(&stack->bc[i])) {
03268
03269 if (!stack->pri && i==stack->b_num)
03270 stack->bc[i].cw=1;
03271
03272 prepare_bc(&stack->bc[i], channel);
03273 stack->bc[i].dec=1;
03274 pthread_mutex_unlock(&stack->st_lock);
03275 return &stack->bc[i];
03276 }
03277 }
03278 } else {
03279 for (i = 0; i <maxnum; i++) {
03280 if (!test_inuse(&stack->bc[i])) {
03281
03282 if (!stack->pri && i==stack->b_num)
03283 stack->bc[i].cw=1;
03284
03285 prepare_bc(&stack->bc[i], channel);
03286 pthread_mutex_unlock(&stack->st_lock);
03287 return &stack->bc[i];
03288 }
03289 }
03290 }
03291 pthread_mutex_unlock(&stack->st_lock);
03292
03293 cb_log(1,port,"There is no free channel on port (%d)\n",port);
03294 return NULL;
03295 }
03296 }
03297
03298 cb_log(0,port,"Port is not configured (%d)\n",port);
03299 return NULL;
03300 }
03301
03302
03303
03304
03305
03306
03307
03308
03309
03310
03311
03312 static const char *fac2str(enum FacFunction facility)
03313 {
03314 static const struct {
03315 enum FacFunction facility;
03316 char *name;
03317 } arr[] = {
03318
03319 { Fac_None, "Fac_None" },
03320 { Fac_GetSupportedServices, "Fac_GetSupportedServices" },
03321 { Fac_Listen, "Fac_Listen" },
03322 { Fac_Suspend, "Fac_Suspend" },
03323 { Fac_Resume, "Fac_Resume" },
03324 { Fac_CFActivate, "Fac_CFActivate" },
03325 { Fac_CFDeactivate, "Fac_CFDeactivate" },
03326 { Fac_CFInterrogateParameters, "Fac_CFInterrogateParameters" },
03327 { Fac_CFInterrogateNumbers, "Fac_CFInterrogateNumbers" },
03328 { Fac_CD, "Fac_CD" },
03329 { Fac_AOCDCurrency, "Fac_AOCDCurrency" },
03330 { Fac_AOCDChargingUnit, "Fac_AOCDChargingUnit" },
03331
03332 };
03333
03334 unsigned index;
03335
03336 for (index = 0; index < ARRAY_LEN(arr); ++index) {
03337 if (arr[index].facility == facility) {
03338 return arr[index].name;
03339 }
03340 }
03341
03342 return "unknown";
03343 }
03344
03345 void misdn_lib_log_ies(struct misdn_bchannel *bc)
03346 {
03347 struct misdn_stack *stack;
03348
03349 if (!bc) return;
03350
03351 stack = get_stack_by_bc(bc);
03352
03353 if (!stack) return;
03354
03355 cb_log(2, stack->port, " --> channel:%d mode:%s cause:%d ocause:%d rad:%s cad:%s\n", bc->channel, stack->nt?"NT":"TE", bc->cause, bc->out_cause, bc->rad, bc->cad);
03356
03357 cb_log(2, stack->port,
03358 " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c cpnnumplan:%c\n",
03359 bc->info_dad,
03360 bc->onumplan>=0?'0'+bc->onumplan:' ',
03361 bc->dnumplan>=0?'0'+bc->dnumplan:' ',
03362 bc->rnumplan>=0?'0'+bc->rnumplan:' ',
03363 bc->cpnnumplan>=0?'0'+bc->cpnnumplan:' '
03364 );
03365
03366 cb_log(3, stack->port, " --> caps:%s pi:%x keypad:%s sending_complete:%d\n", bearer2str(bc->capability),bc->progress_indicator, bc->keypad, bc->sending_complete);
03367 cb_log(4, stack->port, " --> screen:%d --> pres:%d\n",
03368 bc->screen, bc->pres);
03369
03370 cb_log(4, stack->port, " --> addr:%x l3id:%x b_stid:%x layer_id:%x\n", bc->addr, bc->l3_id, bc->b_stid, bc->layer_id);
03371
03372 cb_log(4, stack->port, " --> facility:%s out_facility:%s\n",fac2str(bc->fac_in.Function),fac2str(bc->fac_out.Function));
03373
03374 cb_log(5, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
03375
03376 cb_log(5, stack->port, " --> bc:%p h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
03377 }
03378
03379
03380 #define RETURN(a,b) {retval=a; goto b;}
03381
03382 static void misdn_send_lock(struct misdn_bchannel *bc)
03383 {
03384
03385 if (bc->send_lock)
03386 pthread_mutex_lock(&bc->send_lock->lock);
03387 }
03388
03389 static void misdn_send_unlock(struct misdn_bchannel *bc)
03390 {
03391
03392 if (bc->send_lock)
03393 pthread_mutex_unlock(&bc->send_lock->lock);
03394 }
03395
03396 int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
03397 {
03398 msg_t *msg;
03399 int retval=0;
03400 struct misdn_stack *stack;
03401 struct misdn_bchannel *held_bc;
03402
03403 if (!bc) RETURN(-1,OUT_POST_UNLOCK);
03404
03405 stack = get_stack_by_bc(bc);
03406
03407 if (!stack) {
03408 cb_log(0,bc->port,"SENDEVENT: no Stack for event:%s oad:%s dad:%s \n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad);
03409 RETURN(-1,OUT);
03410 }
03411
03412 misdn_send_lock(bc);
03413
03414
03415 cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->upperid:%x\n",stack->nt, stack->upper_id);
03416
03417 if ( stack->nt && !stack->l1link) {
03418
03419 bc->evq=event;
03420 cb_log(1, stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
03421 misdn_lib_get_l1_up(stack);
03422 RETURN(0,OUT);
03423 }
03424
03425 cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s pid:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, bc->pid);
03426 cb_log(4, stack->port, " --> bc_state:%s\n",bc_state2str(bc->bc_state));
03427 misdn_lib_log_ies(bc);
03428
03429 switch (event) {
03430 case EVENT_SETUP:
03431 if (create_process(glob_mgr->midev, bc)<0) {
03432 cb_log(0, stack->port, " No free channel at the moment @ send_event\n");
03433
03434 RETURN(-ENOCHAN,OUT);
03435 }
03436 break;
03437
03438 case EVENT_PROGRESS:
03439 case EVENT_ALERTING:
03440 case EVENT_PROCEEDING:
03441 case EVENT_SETUP_ACKNOWLEDGE:
03442 case EVENT_CONNECT:
03443 if (!stack->nt) break;
03444
03445 case EVENT_RETRIEVE_ACKNOWLEDGE:
03446
03447 if (stack->nt) {
03448 if (bc->channel <=0 ) {
03449 if (find_free_chan_in_stack(stack, bc, 0, 0)<0) {
03450 cb_log(0, stack->port, " No free channel at the moment\n");
03451
03452 RETURN(-ENOCHAN,OUT);
03453 }
03454 }
03455
03456 }
03457
03458 retval=setup_bc(bc);
03459 if (retval == -EINVAL) {
03460 cb_log(0,bc->port,"send_event: setup_bc failed\n");
03461 }
03462
03463 if (misdn_cap_is_speech(bc->capability)) {
03464 if ((event==EVENT_CONNECT)||(event==EVENT_RETRIEVE_ACKNOWLEDGE)) {
03465 if ( *bc->crypt_key ) {
03466 cb_log(4, stack->port, " --> ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s \n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
03467
03468 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
03469 }
03470
03471 if (!bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
03472 manager_ec_enable(bc);
03473
03474 if (bc->txgain != 0) {
03475 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
03476 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
03477 }
03478
03479 if ( bc->rxgain != 0 ) {
03480 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
03481 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
03482 }
03483 }
03484 }
03485 break;
03486
03487 case EVENT_HOLD_ACKNOWLEDGE:
03488 held_bc = malloc(sizeof(struct misdn_bchannel));
03489 if (!held_bc) {
03490 cb_log(0, bc->port, "Could not allocate held_bc!!!\n");
03491 RETURN(-1,OUT);
03492 }
03493
03494
03495 *held_bc = *bc;
03496 held_bc->holded = 1;
03497 held_bc->channel = 0;
03498 held_bc->channel_preselected = 0;
03499 held_bc->channel_found = 0;
03500 bc_state_change(held_bc, BCHAN_CLEANED);
03501 stack_holder_add(stack, held_bc);
03502
03503
03504 if (stack->nt) {
03505 int channel;
03506 if (bc->bc_state == BCHAN_BRIDGED) {
03507 struct misdn_bchannel *bc2;
03508
03509 misdn_split_conf(bc,bc->conf_id);
03510 bc2 = find_bc_by_confid(bc->conf_id);
03511 if (!bc2) {
03512 cb_log(0,bc->port,"We have no second bc in bridge???\n");
03513 } else {
03514 misdn_split_conf(bc2,bc->conf_id);
03515 }
03516 }
03517
03518 channel = bc->channel;
03519
03520 empty_bc(bc);
03521 clean_up_bc(bc);
03522
03523 if (channel>0)
03524 empty_chan_in_stack(stack,channel);
03525
03526 bc->in_use=0;
03527 }
03528 break;
03529
03530
03531 case EVENT_DISCONNECT:
03532 if (!bc->need_disconnect) {
03533 cb_log(0,bc->port," --> we have already send Disconnect\n");
03534 RETURN(-1,OUT);
03535 }
03536
03537 bc->need_disconnect=0;
03538 break;
03539 case EVENT_RELEASE:
03540 if (!bc->need_release) {
03541 cb_log(0,bc->port," --> we have already send Release\n");
03542 RETURN(-1,OUT);
03543 }
03544 bc->need_disconnect=0;
03545 bc->need_release=0;
03546 break;
03547 case EVENT_RELEASE_COMPLETE:
03548 if (!bc->need_release_complete) {
03549 cb_log(0,bc->port," --> we have already send Release_complete\n");
03550 RETURN(-1,OUT);
03551 }
03552 bc->need_disconnect=0;
03553 bc->need_release=0;
03554 bc->need_release_complete=0;
03555
03556 if (!stack->nt) {
03557
03558 int channel=bc->channel;
03559
03560 int tmpcause=bc->cause;
03561 int tmp_out_cause=bc->out_cause;
03562 empty_bc(bc);
03563 bc->cause=tmpcause;
03564 bc->out_cause=tmp_out_cause;
03565 clean_up_bc(bc);
03566
03567 if (channel>0)
03568 empty_chan_in_stack(stack,channel);
03569
03570 bc->in_use=0;
03571 }
03572 break;
03573
03574 case EVENT_CONNECT_ACKNOWLEDGE:
03575
03576 if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
03577 int retval=setup_bc(bc);
03578 if (retval == -EINVAL){
03579 cb_log(0,bc->port,"send_event: setup_bc failed\n");
03580
03581 }
03582 }
03583
03584 if (misdn_cap_is_speech(bc->capability)) {
03585 if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
03586 manager_ec_enable(bc);
03587
03588 if ( bc->txgain != 0 ) {
03589 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
03590 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
03591 }
03592 if ( bc->rxgain != 0 ) {
03593 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
03594 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
03595 }
03596 }
03597 break;
03598
03599 default:
03600 break;
03601 }
03602
03603
03604 msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
03605 msg_queue_tail(&stack->downqueue, msg);
03606 sem_post(&glob_mgr->new_msg);
03607
03608 OUT:
03609 misdn_send_unlock(bc);
03610
03611 OUT_POST_UNLOCK:
03612 return retval;
03613 }
03614
03615
03616 static int handle_err(msg_t *msg)
03617 {
03618 iframe_t *frm = (iframe_t*) msg->data;
03619
03620
03621 if (!frm->addr) {
03622 static int cnt=0;
03623 if (!cnt)
03624 cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x\n",frm->prim,frm->dinfo);
03625 cnt++;
03626 if (cnt>100) {
03627 cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x (already more than 100 of them)\n",frm->prim,frm->dinfo);
03628 cnt=0;
03629 }
03630
03631 free_msg(msg);
03632 return 1;
03633
03634 }
03635
03636 switch (frm->prim) {
03637 case MGR_SETSTACK|INDICATION:
03638 return handle_bchan(msg);
03639 break;
03640
03641 case MGR_SETSTACK|CONFIRM:
03642 case MGR_CLEARSTACK|CONFIRM:
03643 free_msg(msg) ;
03644 return 1;
03645 break;
03646
03647 case DL_DATA|CONFIRM:
03648 cb_log(4,0,"DL_DATA|CONFIRM\n");
03649 free_msg(msg);
03650 return 1;
03651
03652 case PH_CONTROL|CONFIRM:
03653 cb_log(4,0,"PH_CONTROL|CONFIRM\n");
03654 free_msg(msg);
03655 return 1;
03656
03657 case DL_DATA|INDICATION:
03658 {
03659 int port=(frm->addr&MASTER_ID_MASK) >> 8;
03660 int channel=(frm->addr&CHILD_ID_MASK) >> 16;
03661 struct misdn_bchannel *bc;
03662
03663
03664
03665 cb_log(9,0,"BCHAN DATA without BC: addr:%x port:%d channel:%d\n",frm->addr, port,channel);
03666
03667 free_msg(msg);
03668 return 1;
03669
03670
03671 bc = find_bc_by_channel(port, channel);
03672
03673 if (!bc) {
03674 struct misdn_stack *stack = find_stack_by_port(port);
03675
03676 if (!stack) {
03677 cb_log(0,0," --> stack not found\n");
03678 free_msg(msg);
03679 return 1;
03680 }
03681
03682 cb_log(0,0," --> bc not found by channel\n");
03683 if (stack->l2link)
03684 misdn_lib_get_l2_down(stack);
03685
03686 if (stack->l1link)
03687 misdn_lib_get_l1_down(stack);
03688
03689 free_msg(msg);
03690 return 1;
03691 }
03692
03693 cb_log(3,port," --> BC in state:%s\n", bc_state2str(bc->bc_state));
03694 }
03695 }
03696
03697 return 0;
03698 }
03699
03700 #if 0
03701 static int queue_l2l3(msg_t *msg)
03702 {
03703 iframe_t *frm= (iframe_t*)msg->data;
03704 struct misdn_stack *stack;
03705 stack=find_stack_by_addr( frm->addr );
03706
03707
03708 if (!stack) {
03709 return 0;
03710 }
03711
03712 msg_queue_tail(&stack->upqueue, msg);
03713 sem_post(&glob_mgr->new_msg);
03714 return 1;
03715 }
03716 #endif
03717
03718 int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
03719 {
03720
03721 if (frm->dinfo==0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
03722 cb_log(0,0,"SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
03723 }
03724
03725 if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
03726 static int unhandled_bmsg_count=1000;
03727 if (handle_bchan(msg)) {
03728 return 0 ;
03729 }
03730
03731 if (unhandled_bmsg_count==1000) {
03732 cb_log(0, 0, "received 1k Unhandled Bchannel Messages: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);
03733 unhandled_bmsg_count=0;
03734 }
03735
03736 unhandled_bmsg_count++;
03737 free_msg(msg);
03738 return 0;
03739 }
03740
03741 #ifdef RECV_FRM_SYSLOG_DEBUG
03742 syslog(LOG_NOTICE,"mISDN recv: P(%02d): ADDR:%x PRIM:%x DINFO:%x\n",stack->port, frm->addr, frm->prim, frm->dinfo);
03743 #endif
03744
03745 if (handle_timers(msg))
03746 return 0 ;
03747
03748
03749 if (handle_mgmt(msg))
03750 return 0 ;
03751
03752 if (handle_l2(msg))
03753 return 0 ;
03754
03755
03756 if (handle_l1(msg))
03757 return 0 ;
03758
03759 if (handle_frm_nt(msg)) {
03760 return 0;
03761 }
03762
03763 if (handle_frm(msg)) {
03764 return 0;
03765 }
03766
03767 if (handle_err(msg)) {
03768 return 0 ;
03769 }
03770
03771
03772 free_msg(msg);
03773
03774
03775 return 0;
03776 }
03777
03778
03779
03780
03781 int misdn_lib_get_port_info(int port)
03782 {
03783 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
03784 iframe_t *frm;
03785 struct misdn_stack *stack=find_stack_by_port(port);
03786 if (!msg) {
03787 cb_log(0, port, "misdn_lib_get_port_info: alloc_msg failed!\n");
03788 return -1;
03789 }
03790 frm=(iframe_t*)msg->data;
03791 if (!stack ) {
03792 cb_log(0, port, "There is no Stack for this port.\n");
03793 return -1;
03794 }
03795
03796 frm->prim = CC_STATUS_ENQUIRY | REQUEST;
03797
03798 frm->addr = stack->upper_id| FLG_MSG_DOWN;
03799
03800 frm->dinfo = 0;
03801 frm->len = 0;
03802
03803 msg_queue_tail(&glob_mgr->activatequeue, msg);
03804 sem_post(&glob_mgr->new_msg);
03805
03806
03807 return 0;
03808 }
03809
03810
03811 int queue_cleanup_bc(struct misdn_bchannel *bc)
03812 {
03813 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
03814 iframe_t *frm;
03815 if (!msg) {
03816 cb_log(0, bc->port, "queue_cleanup_bc: alloc_msg failed!\n");
03817 return -1;
03818 }
03819 frm=(iframe_t*)msg->data;
03820
03821
03822 frm->prim = MGR_CLEARSTACK| REQUEST;
03823
03824 frm->addr = bc->l3_id;
03825
03826 frm->dinfo = bc->port;
03827 frm->len = 0;
03828
03829 msg_queue_tail(&glob_mgr->activatequeue, msg);
03830 sem_post(&glob_mgr->new_msg);
03831
03832 return 0;
03833
03834 }
03835
03836 int misdn_lib_pid_restart(int pid)
03837 {
03838 struct misdn_bchannel *bc=manager_find_bc_by_pid(pid);
03839
03840 if (bc) {
03841 manager_clean_bc(bc);
03842 }
03843 return 0;
03844 }
03845
03846
03847 int misdn_lib_send_restart(int port, int channel)
03848 {
03849 struct misdn_stack *stack=find_stack_by_port(port);
03850 struct misdn_bchannel dummybc;
03851
03852 cb_log(0, port, "Sending Restarts on this port.\n");
03853
03854 misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0);
03855
03856
03857 if (channel <0) {
03858 dummybc.channel=-1;
03859 cb_log(0, port, "Restarting and all Interfaces\n");
03860 misdn_lib_send_event(&dummybc, EVENT_RESTART);
03861
03862 return 0;
03863 }
03864
03865
03866 if (channel >0) {
03867 int cnt;
03868 dummybc.channel=channel;
03869 cb_log(0, port, "Restarting and cleaning channel %d\n",channel);
03870 misdn_lib_send_event(&dummybc, EVENT_RESTART);
03871
03872
03873 for (cnt=0; cnt<=stack->b_num; cnt++) {
03874 if (stack->bc[cnt].in_use && stack->bc[cnt].channel == channel) {
03875 empty_bc(&stack->bc[cnt]);
03876 clean_up_bc(&stack->bc[cnt]);
03877 stack->bc[cnt].in_use=0;
03878 }
03879 }
03880 }
03881
03882 return 0;
03883 }
03884
03885
03886 int misdn_lib_port_restart(int port)
03887 {
03888 struct misdn_stack *stack=find_stack_by_port(port);
03889
03890 cb_log(0, port, "Restarting this port.\n");
03891 if (stack) {
03892 cb_log(0, port, "Stack:%p\n",stack);
03893
03894 clear_l3(stack);
03895 {
03896 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
03897 iframe_t *frm;
03898
03899 if (!msg) {
03900 cb_log(0, port, "port_restart: alloc_msg failed\n");
03901 return -1;
03902 }
03903
03904 frm=(iframe_t*)msg->data;
03905
03906
03907 frm->prim = DL_RELEASE | REQUEST;
03908 frm->addr = stack->upper_id | FLG_MSG_DOWN;
03909
03910 frm->dinfo = 0;
03911 frm->len = 0;
03912 msg_queue_tail(&glob_mgr->activatequeue, msg);
03913 sem_post(&glob_mgr->new_msg);
03914 }
03915
03916 if (stack->nt)
03917 misdn_lib_reinit_nt_stack(stack->port);
03918
03919 }
03920
03921 return 0;
03922 }
03923
03924
03925
03926 sem_t handler_started;
03927
03928
03929 static void manager_event_handler(void *arg)
03930 {
03931 sem_post(&handler_started);
03932 while (1) {
03933 struct misdn_stack *stack;
03934 msg_t *msg;
03935
03936
03937 sem_wait(&glob_mgr->new_msg);
03938
03939 for (msg=msg_dequeue(&glob_mgr->activatequeue);
03940 msg;
03941 msg=msg_dequeue(&glob_mgr->activatequeue)
03942 )
03943 {
03944
03945 iframe_t *frm = (iframe_t*) msg->data ;
03946
03947 switch ( frm->prim) {
03948
03949 case MGR_CLEARSTACK | REQUEST:
03950
03951 {
03952 struct misdn_stack *stack=find_stack_by_port(frm->dinfo);
03953 struct misdn_bchannel *bc;
03954 if (!stack) {
03955 cb_log(0,0,"no stack found with port [%d]!! so we cannot cleanup the bc\n",frm->dinfo);
03956 free_msg(msg);
03957 break;
03958 }
03959
03960 bc = find_bc_by_l3id(stack, frm->addr);
03961 if (bc) {
03962 cb_log(1,bc->port,"CLEARSTACK queued, cleaning up\n");
03963 clean_up_bc(bc);
03964 } else {
03965 cb_log(0,stack->port,"bc could not be cleaned correctly !! addr [%x]\n",frm->addr);
03966 }
03967 }
03968 free_msg(msg);
03969 break;
03970 case MGR_SETSTACK | REQUEST :
03971 free_msg(msg);
03972 break;
03973 default:
03974 mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
03975 free_msg(msg);
03976 }
03977 }
03978
03979 for (stack=glob_mgr->stack_list;
03980 stack;
03981 stack=stack->next ) {
03982
03983 while ( (msg=msg_dequeue(&stack->upqueue)) ) {
03984
03985 if (!handle_frm_nt(msg)) {
03986
03987 if (!handle_frm(msg)) {
03988
03989 cb_log(0,stack->port,"Wow we've got a strange issue while dequeueing a Frame\n");
03990 }
03991 }
03992 }
03993
03994
03995
03996
03997
03998
03999 while ( (msg=msg_dequeue(&stack->downqueue)) ) {
04000 if (stack->nt ) {
04001 pthread_mutex_lock(&stack->nstlock);
04002 if (stack->nst.manager_l3(&stack->nst, msg))
04003 cb_log(0, stack->port, "Error@ Sending Message in NT-Stack.\n");
04004 pthread_mutex_unlock(&stack->nstlock);
04005 } else {
04006 iframe_t *frm = (iframe_t *)msg->data;
04007 struct misdn_bchannel *bc = find_bc_by_l3id(stack, frm->dinfo);
04008 if (bc)
04009 send_msg(glob_mgr->midev, bc, msg);
04010 else {
04011 if (frm->dinfo == MISDN_ID_GLOBAL) {
04012 struct misdn_bchannel dummybc;
04013 misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0);
04014 send_msg(glob_mgr->midev, &dummybc, msg);
04015 }
04016 }
04017 }
04018 }
04019 }
04020 }
04021 }
04022
04023
04024 int misdn_lib_maxports_get(void)
04025 {
04026
04027
04028 int i = mISDN_open();
04029 int max=0;
04030
04031 if (i<0)
04032 return -1;
04033
04034 max = mISDN_get_stack_count(i);
04035
04036 mISDN_close(i);
04037
04038 return max;
04039 }
04040
04041
04042 void misdn_lib_nt_keepcalls( int kc)
04043 {
04044 #ifdef FEATURE_NET_KEEPCALLS
04045 if (kc) {
04046 struct misdn_stack *stack=get_misdn_stack();
04047 for ( ; stack; stack=stack->next) {
04048 stack->nst.feature |= FEATURE_NET_KEEPCALLS;
04049 }
04050 }
04051 #endif
04052 }
04053
04054 void misdn_lib_nt_debug_init( int flags, char *file )
04055 {
04056 static int init=0;
04057 char *f;
04058
04059 if (!flags)
04060 f=NULL;
04061 else
04062 f=file;
04063
04064 if (!init) {
04065 debug_init( flags , f, f, f);
04066 init=1;
04067 } else {
04068 debug_close();
04069 debug_init( flags , f, f, f);
04070 }
04071 }
04072
04073
04074 int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_data)
04075 {
04076 struct misdn_lib *mgr=calloc(1, sizeof(struct misdn_lib));
04077 char *tok, *tokb;
04078 char plist[1024];
04079 int midev;
04080 int port_count=0;
04081
04082 cb_log = iface->cb_log;
04083 cb_event = iface->cb_event;
04084 cb_jb_empty = iface->cb_jb_empty;
04085
04086 glob_mgr = mgr;
04087
04088 msg_init();
04089
04090 misdn_lib_nt_debug_init(0,NULL);
04091
04092 if (!portlist || (*portlist == 0) ) return 1;
04093
04094 init_flip_bits();
04095
04096 {
04097 strncpy(plist,portlist, 1024);
04098 plist[1023] = 0;
04099 }
04100
04101 memcpy(tone_425_flip,tone_425,TONE_425_SIZE);
04102 flip_buf_bits(tone_425_flip,TONE_425_SIZE);
04103
04104 memcpy(tone_silence_flip,tone_SILENCE,TONE_SILENCE_SIZE);
04105 flip_buf_bits(tone_silence_flip,TONE_SILENCE_SIZE);
04106
04107 midev=te_lib_init();
04108 mgr->midev=midev;
04109
04110 port_count=mISDN_get_stack_count(midev);
04111
04112 msg_queue_init(&mgr->activatequeue);
04113
04114 if (sem_init(&mgr->new_msg, 1, 0)<0)
04115 sem_init(&mgr->new_msg, 0, 0);
04116
04117 for (tok=strtok_r(plist," ,",&tokb );
04118 tok;
04119 tok=strtok_r(NULL," ,",&tokb)) {
04120 int port = atoi(tok);
04121 struct misdn_stack *stack;
04122 static int first=1;
04123 int ptp=0;
04124
04125 if (strstr(tok, "ptp"))
04126 ptp=1;
04127
04128 if (port > port_count) {
04129 cb_log(0, port, "Couldn't Initialize this port since we have only %d ports\n", port_count);
04130 exit(1);
04131 }
04132 stack=stack_init(midev, port, ptp);
04133
04134 if (!stack) {
04135 perror("stack_init");
04136 exit(1);
04137 }
04138
04139 {
04140 int i;
04141 for(i=0;i<=stack->b_num; i++) {
04142 int r;
04143 if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "", 1))<0) {
04144 cb_log(0, port, "Got Err @ init_bc :%d\n",r);
04145 exit(1);
04146 }
04147 }
04148 }
04149
04150 if (stack && first) {
04151 mgr->stack_list=stack;
04152 first=0;
04153 continue;
04154 }
04155
04156 if (stack) {
04157 struct misdn_stack * help;
04158 for ( help=mgr->stack_list; help; help=help->next )
04159 if (help->next == NULL) break;
04160 help->next=stack;
04161 }
04162
04163 }
04164
04165 if (sem_init(&handler_started, 1, 0)<0)
04166 sem_init(&handler_started, 0, 0);
04167
04168 cb_log(8, 0, "Starting Event Handler\n");
04169 pthread_create( &mgr->event_handler_thread, NULL,(void*)manager_event_handler, mgr);
04170
04171 sem_wait(&handler_started) ;
04172 cb_log(8, 0, "Starting Event Catcher\n");
04173 pthread_create( &mgr->event_thread, NULL, (void*)misdn_lib_isdn_event_catcher, mgr);
04174
04175 cb_log(8, 0, "Event Catcher started\n");
04176
04177 global_state= MISDN_INITIALIZED;
04178
04179 return (mgr == NULL);
04180 }
04181
04182 void misdn_lib_destroy(void)
04183 {
04184 struct misdn_stack *help;
04185 int i;
04186
04187 for ( help=glob_mgr->stack_list; help; help=help->next ) {
04188 for(i=0;i<=help->b_num; i++) {
04189 char buf[1024];
04190 mISDN_write_frame(help->midev, buf, help->bc[i].addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
04191 help->bc[i].addr = 0;
04192 }
04193 cb_log (1, help->port, "Destroying this port.\n");
04194 stack_destroy(help);
04195 }
04196
04197 if (global_state == MISDN_INITIALIZED) {
04198 cb_log(4, 0, "Killing Handler Thread\n");
04199 if ( pthread_cancel(glob_mgr->event_handler_thread) == 0 ) {
04200 cb_log(4, 0, "Joining Handler Thread\n");
04201 pthread_join(glob_mgr->event_handler_thread, NULL);
04202 }
04203
04204 cb_log(4, 0, "Killing Main Thread\n");
04205 if ( pthread_cancel(glob_mgr->event_thread) == 0 ) {
04206 cb_log(4, 0, "Joining Main Thread\n");
04207 pthread_join(glob_mgr->event_thread, NULL);
04208 }
04209 }
04210
04211 cb_log(1, 0, "Closing mISDN device\n");
04212 te_lib_destroy(glob_mgr->midev);
04213 }
04214
04215 char *manager_isdn_get_info(enum event_e event)
04216 {
04217 return isdn_get_info(msgs_g , event, 0);
04218 }
04219
04220 void manager_bchannel_activate(struct misdn_bchannel *bc)
04221 {
04222 char buf[128];
04223
04224 struct misdn_stack *stack=get_stack_by_bc(bc);
04225
04226 if (!stack) {
04227 cb_log(0, bc->port, "bchannel_activate: Stack not found !");
04228 return ;
04229 }
04230
04231
04232 clear_ibuffer(bc->astbuf);
04233
04234 cb_log(5, stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
04235
04236 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_DOWN, DL_ESTABLISH | REQUEST, 0,0, NULL, TIMEOUT_1SEC);
04237
04238 return ;
04239 }
04240
04241
04242 void manager_bchannel_deactivate(struct misdn_bchannel * bc)
04243 {
04244 struct misdn_stack *stack=get_stack_by_bc(bc);
04245 iframe_t dact;
04246 char buf[128];
04247
04248 switch (bc->bc_state) {
04249 case BCHAN_ACTIVATED:
04250 break;
04251 case BCHAN_BRIDGED:
04252 misdn_split_conf(bc,bc->conf_id);
04253 break;
04254 default:
04255 cb_log( 4, bc->port,"bchan_deactivate: called but not activated\n");
04256 return ;
04257
04258 }
04259
04260 cb_log(5, stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
04261
04262 bc->generate_tone=0;
04263
04264 dact.prim = DL_RELEASE | REQUEST;
04265 dact.addr = bc->addr | FLG_MSG_DOWN;
04266 dact.dinfo = 0;
04267 dact.len = 0;
04268 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_DOWN, DL_RELEASE|REQUEST,0,0,NULL, TIMEOUT_1SEC);
04269
04270 clear_ibuffer(bc->astbuf);
04271
04272 bc_state_change(bc,BCHAN_RELEASE);
04273
04274 return;
04275 }
04276
04277
04278 int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
04279 {
04280 struct misdn_stack *stack=get_stack_by_bc(bc);
04281 char buf[4096 + mISDN_HEADER_LEN];
04282 iframe_t *frm = (iframe_t*)buf;
04283 int r;
04284
04285 switch (bc->bc_state) {
04286 case BCHAN_ACTIVATED:
04287 case BCHAN_BRIDGED:
04288 break;
04289 default:
04290 cb_log(3, bc->port, "BC not yet activated (state:%s)\n",bc_state2str(bc->bc_state));
04291 return -1;
04292 }
04293
04294 frm->prim = DL_DATA|REQUEST;
04295 frm->dinfo = 0;
04296 frm->addr = bc->addr | FLG_MSG_DOWN ;
04297
04298 frm->len = len;
04299 memcpy(&buf[mISDN_HEADER_LEN], data,len);
04300
04301 if ( misdn_cap_is_speech(bc->capability) )
04302 flip_buf_bits( &buf[mISDN_HEADER_LEN], len);
04303 else
04304 cb_log(6, stack->port, "Writing %d data bytes\n",len);
04305
04306 cb_log(9, stack->port, "Writing %d bytes 2 mISDN\n",len);
04307 r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
04308 return 0;
04309 }
04310
04311
04312
04313
04314
04315
04316 void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2)
04317 {
04318 unsigned char buffer[mISDN_HEADER_LEN+2*sizeof(int)];
04319 iframe_t *ctrl = (iframe_t *)buffer;
04320 unsigned int *d = (unsigned int*)&ctrl->data.p;
04321
04322
04323 cb_log(4,bc->port,"ph_control: c1:%x c2:%x\n",c1,c2);
04324
04325 ctrl->prim = PH_CONTROL | REQUEST;
04326 ctrl->addr = bc->addr | FLG_MSG_DOWN;
04327 ctrl->dinfo = 0;
04328 ctrl->len = sizeof(unsigned int)*2;
04329 *d++ = c1;
04330 *d++ = c2;
04331 mISDN_write(glob_mgr->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
04332 }
04333
04334
04335
04336
04337 void isdn_lib_update_rxgain (struct misdn_bchannel *bc)
04338 {
04339 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
04340 }
04341
04342 void isdn_lib_update_txgain (struct misdn_bchannel *bc)
04343 {
04344 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
04345 }
04346
04347 void isdn_lib_update_ec (struct misdn_bchannel *bc)
04348 {
04349 #ifdef MISDN_1_2
04350 if (*bc->pipeline)
04351 #else
04352 if (bc->ec_enable)
04353 #endif
04354 manager_ec_enable(bc);
04355 else
04356 manager_ec_disable(bc);
04357 }
04358
04359 void isdn_lib_stop_dtmf (struct misdn_bchannel *bc)
04360 {
04361 manager_ph_control(bc, DTMF_TONE_STOP, 0);
04362 }
04363
04364
04365
04366
04367 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len)
04368 {
04369 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
04370 iframe_t *ctrl = (iframe_t *)buffer;
04371 unsigned int *d = (unsigned int *)&ctrl->data.p;
04372
04373
04374 ctrl->prim = PH_CONTROL | REQUEST;
04375 ctrl->addr = bc->addr | FLG_MSG_DOWN;
04376 ctrl->dinfo = 0;
04377 ctrl->len = sizeof(unsigned int) + c2_len;
04378 *d++ = c1;
04379 memcpy(d, c2, c2_len);
04380 mISDN_write(glob_mgr->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
04381 }
04382
04383
04384
04385
04386 void manager_clean_bc(struct misdn_bchannel *bc )
04387 {
04388 struct misdn_stack *stack=get_stack_by_bc(bc);
04389
04390 if (bc->channel>0)
04391 empty_chan_in_stack(stack, bc->channel);
04392 empty_bc(bc);
04393 bc->in_use=0;
04394
04395 cb_event(EVENT_CLEANUP, bc, NULL);
04396 }
04397
04398
04399 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder)
04400 {
04401 struct misdn_bchannel *help;
04402 cb_log(4,stack->port, "*HOLDER: add %x\n",holder->l3_id);
04403
04404 holder->stack_holder=1;
04405 holder->next=NULL;
04406
04407 if (!stack->holding) {
04408 stack->holding = holder;
04409 return;
04410 }
04411
04412 for (help=stack->holding;
04413 help;
04414 help=help->next) {
04415 if (!help->next) {
04416 help->next=holder;
04417 break;
04418 }
04419 }
04420
04421 }
04422
04423 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder)
04424 {
04425 struct misdn_bchannel *h1;
04426
04427 if (!holder->stack_holder) return;
04428
04429 holder->stack_holder=0;
04430
04431 cb_log(4,stack->port, "*HOLDER: remove %x\n",holder->l3_id);
04432 if (!stack || ! stack->holding) return;
04433
04434 if (holder == stack->holding) {
04435 stack->holding = stack->holding->next;
04436 return;
04437 }
04438
04439 for (h1=stack->holding;
04440 h1;
04441 h1=h1->next) {
04442 if (h1->next == holder) {
04443 h1->next=h1->next->next;
04444 return ;
04445 }
04446 }
04447 }
04448
04449 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id)
04450 {
04451 struct misdn_bchannel *help;
04452
04453 cb_log(4,stack?stack->port:0, "*HOLDER: find %lx\n",l3id);
04454
04455 if (!stack) return NULL;
04456
04457 for (help=stack->holding;
04458 help;
04459 help=help->next) {
04460 if (help->l3_id == l3id) {
04461 cb_log(4,stack->port, "*HOLDER: found bc\n");
04462 return help;
04463 }
04464 }
04465
04466 cb_log(4,stack->port, "*HOLDER: find nothing\n");
04467 return NULL;
04468 }
04469
04470
04471
04472
04473
04474
04475
04476
04477
04478 struct misdn_bchannel *misdn_lib_find_held_bc(int port, int l3_id)
04479 {
04480 struct misdn_bchannel *bc;
04481 struct misdn_stack *stack;
04482
04483 bc = NULL;
04484 for (stack = get_misdn_stack(); stack; stack = stack->next) {
04485 if (stack->port == port) {
04486 bc = stack_holder_find(stack, l3_id);
04487 break;
04488 }
04489 }
04490
04491 return bc;
04492 }
04493
04494 void misdn_lib_send_tone(struct misdn_bchannel *bc, enum tone_e tone)
04495 {
04496 char buf[mISDN_HEADER_LEN + 128] = "";
04497 iframe_t *frm = (iframe_t*)buf;
04498
04499 switch(tone) {
04500 case TONE_DIAL:
04501 manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_DIALTONE);
04502 break;
04503
04504 case TONE_ALERTING:
04505 manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_RINGING);
04506 break;
04507
04508 case TONE_HANGUP:
04509 manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_HANGUP);
04510 break;
04511
04512 case TONE_NONE:
04513 default:
04514 manager_ph_control(bc, TONE_PATT_OFF, TONE_GERMAN_HANGUP);
04515 }
04516
04517 frm->prim=DL_DATA|REQUEST;
04518 frm->addr=bc->addr|FLG_MSG_DOWN;
04519 frm->dinfo=0;
04520 frm->len=128;
04521
04522 mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
04523 }
04524
04525
04526 void manager_ec_enable(struct misdn_bchannel *bc)
04527 {
04528 struct misdn_stack *stack=get_stack_by_bc(bc);
04529
04530 cb_log(4, stack?stack->port:0,"ec_enable\n");
04531
04532 if (!misdn_cap_is_speech(bc->capability)) {
04533 cb_log(1, stack?stack->port:0, " --> no speech? cannot enable EC\n");
04534 } else {
04535
04536 #ifdef MISDN_1_2
04537 if (*bc->pipeline) {
04538 cb_log(3, stack?stack->port:0,"Sending Control PIPELINE_CFG %s\n",bc->pipeline);
04539 manager_ph_control_block(bc, PIPELINE_CFG, bc->pipeline, strlen(bc->pipeline) + 1);
04540 }
04541 #else
04542 int ec_arr[2];
04543
04544 if (bc->ec_enable) {
04545 cb_log(3, stack?stack->port:0,"Sending Control ECHOCAN_ON taps:%d\n",bc->ec_deftaps);
04546
04547 switch (bc->ec_deftaps) {
04548 case 4:
04549 case 8:
04550 case 16:
04551 case 32:
04552 case 64:
04553 case 128:
04554 case 256:
04555 case 512:
04556 case 1024:
04557 cb_log(4, stack->port, "Taps is %d\n",bc->ec_deftaps);
04558 break;
04559 default:
04560 cb_log(0, stack->port, "Taps should be power of 2\n");
04561 bc->ec_deftaps=128;
04562 }
04563
04564 ec_arr[0]=bc->ec_deftaps;
04565 ec_arr[1]=0;
04566
04567 manager_ph_control_block(bc, ECHOCAN_ON, ec_arr, sizeof(ec_arr));
04568 }
04569 #endif
04570 }
04571 }
04572
04573
04574
04575 void manager_ec_disable(struct misdn_bchannel *bc)
04576 {
04577 struct misdn_stack *stack=get_stack_by_bc(bc);
04578
04579 cb_log(4, stack?stack->port:0," --> ec_disable\n");
04580
04581 if (!misdn_cap_is_speech(bc->capability)) {
04582 cb_log(1, stack?stack->port:0, " --> no speech? cannot disable EC\n");
04583 return;
04584 }
04585
04586 #ifdef MISDN_1_2
04587 manager_ph_control_block(bc, PIPELINE_CFG, "", 0);
04588 #else
04589 if ( ! bc->ec_enable) {
04590 cb_log(3, stack?stack->port:0, "Sending Control ECHOCAN_OFF\n");
04591 manager_ph_control(bc, ECHOCAN_OFF, 0);
04592 }
04593 #endif
04594 }
04595
04596 struct misdn_stack* get_misdn_stack() {
04597 return glob_mgr->stack_list;
04598 }
04599
04600
04601
04602 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id)
04603 {
04604 char data[16] = "";
04605
04606 bc_state_change(bc,BCHAN_BRIDGED);
04607 manager_ph_control(bc, CMX_RECEIVE_OFF, 0);
04608 manager_ph_control(bc, CMX_CONF_JOIN, conf_id);
04609
04610 cb_log(3,bc->port, "Joining bc:%x in conf:%d\n",bc->addr,conf_id);
04611
04612 misdn_lib_tx2misdn_frm(bc, data, sizeof(data) - 1);
04613 }
04614
04615
04616 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id)
04617 {
04618 bc_state_change(bc,BCHAN_ACTIVATED);
04619 manager_ph_control(bc, CMX_RECEIVE_ON, 0);
04620 manager_ph_control(bc, CMX_CONF_SPLIT, conf_id);
04621
04622 cb_log(4,bc->port, "Splitting bc:%x in conf:%d\n",bc->addr,conf_id);
04623 }
04624
04625 void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2)
04626 {
04627 int conf_id = bc1->pid + 1;
04628 struct misdn_bchannel *bc_list[] = { bc1, bc2, NULL };
04629 struct misdn_bchannel **bc;
04630
04631 cb_log(4, bc1->port, "I Send: BRIDGE from:%d to:%d\n",bc1->port,bc2->port);
04632
04633 for (bc=bc_list; *bc; bc++) {
04634 (*bc)->conf_id=conf_id;
04635 cb_log(4, (*bc)->port, " --> bc_addr:%x\n",(*bc)->addr);
04636
04637 switch((*bc)->bc_state) {
04638 case BCHAN_ACTIVATED:
04639 misdn_join_conf(*bc,conf_id);
04640 break;
04641 default:
04642 bc_next_state_change(*bc,BCHAN_BRIDGED);
04643 break;
04644 }
04645 }
04646 }
04647
04648 void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2)
04649 {
04650
04651 struct misdn_bchannel *bc_list[]={
04652 bc1,bc2,NULL
04653 };
04654 struct misdn_bchannel **bc;
04655
04656 for (bc=bc_list; *bc; bc++) {
04657 if ( (*bc)->bc_state == BCHAN_BRIDGED){
04658 misdn_split_conf( *bc, (*bc)->conf_id);
04659 } else {
04660 cb_log( 2, (*bc)->port, "BC not bridged (state:%s) so not splitting it\n",bc_state2str((*bc)->bc_state));
04661 }
04662 }
04663
04664 }
04665
04666
04667
04668 void misdn_lib_echo(struct misdn_bchannel *bc, int onoff)
04669 {
04670 cb_log(3,bc->port, " --> ECHO %s\n", onoff?"ON":"OFF");
04671 manager_ph_control(bc, onoff?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
04672 }
04673
04674
04675
04676 void misdn_lib_reinit_nt_stack(int port)
04677 {
04678 struct misdn_stack *stack=find_stack_by_port(port);
04679
04680 if (stack) {
04681 stack->l2link=0;
04682 stack->blocked=0;
04683
04684 cleanup_Isdnl3(&stack->nst);
04685 cleanup_Isdnl2(&stack->nst);
04686
04687
04688 memset(&stack->nst, 0, sizeof(net_stack_t));
04689 memset(&stack->mgr, 0, sizeof(manager_t));
04690
04691 stack->mgr.nst = &stack->nst;
04692 stack->nst.manager = &stack->mgr;
04693
04694 stack->nst.l3_manager = handle_event_nt;
04695 stack->nst.device = glob_mgr->midev;
04696 stack->nst.cardnr = port;
04697 stack->nst.d_stid = stack->d_stid;
04698
04699 stack->nst.feature = FEATURE_NET_HOLD;
04700 if (stack->ptp)
04701 stack->nst.feature |= FEATURE_NET_PTP;
04702 if (stack->pri)
04703 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
04704
04705 stack->nst.l1_id = stack->lower_id;
04706 stack->nst.l2_id = stack->upper_id;
04707
04708 msg_queue_init(&stack->nst.down_queue);
04709
04710 Isdnl2Init(&stack->nst);
04711 Isdnl3Init(&stack->nst);
04712
04713 if (!stack->ptp)
04714 misdn_lib_get_l1_up(stack);
04715 misdn_lib_get_l2_up(stack);
04716 }
04717 }
04718
04719