📄 isdn_lib.c
字号:
if (ret < 0) { cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret); return -1; } stinf = (stack_info_t *)&frm->data.p; cb_log(8, port, " --> Child %x\n",stinf->child[bidx]); } return 0;}struct misdn_stack* stack_init( int midev, int port, int ptp ){ int ret; unsigned char buff[1025]; iframe_t *frm = (iframe_t *)buff; stack_info_t *stinf; int i; layer_info_t li; struct misdn_stack *stack = malloc(sizeof(struct misdn_stack)); if (!stack ) return NULL; cb_log(8, port, "Init. Stack.\n"); memset(stack,0,sizeof(struct misdn_stack)); for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0; stack->port=port; stack->midev=midev; stack->ptp=ptp; stack->holding=NULL; stack->pri=0; msg_queue_init(&stack->downqueue); msg_queue_init(&stack->upqueue); /* query port's requirements */ ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff)); if (ret < 0) { cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret); return(NULL); } stinf = (stack_info_t *)&frm->data.p; stack->d_stid = stinf->id; stack->b_num = stinf->childcnt; for (i=0; i<=stinf->childcnt; i++) stack->b_stids[i] = stinf->child[i]; switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) { case ISDN_PID_L0_TE_S0: stack->nt=0; break; case ISDN_PID_L0_NT_S0: cb_log(8, port, "NT Stack\n"); stack->nt=1; break; case ISDN_PID_L0_TE_E1: cb_log(8, port, "TE S2M Stack\n"); stack->nt=0; stack->pri=1; break; case ISDN_PID_L0_NT_E1: cb_log(8, port, "TE S2M Stack\n"); stack->nt=1; stack->pri=1; break; default: cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]); } if (!stack->nt) { if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { stack->ptp = 1; } else { stack->ptp = 0; } } { int ret; int nt=stack->nt; cb_log(8, port, "Init. Stack.\n"); memset(&li, 0, sizeof(li)); { int l = sizeof(li.name); strncpy(li.name,nt?"net l2":"user l4", l); li.name[l-1] = 0; } li.object_id = -1; li.extentions = 0; li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20; li.pid.layermask = ISDN_LAYER((nt?2:4)); li.st = stack->d_stid; ret = mISDN_new_layer(midev, &li); if (ret) { cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4); return(NULL); } stack->upper_id = li.id; ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id); if (ret) { cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4); return(NULL); } stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3); if (stack->lower_id < 0) { cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3); return(NULL); } stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4); if (stack->upper_id < 0) { cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2); return(NULL); } cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id); /* create nst (nt-mode only) */ if (nt) { memset(&stack->nst, 0, sizeof(net_stack_t)); memset(&stack->mgr, 0, sizeof(manager_t)); stack->mgr.nst = &stack->nst; stack->nst.manager = &stack->mgr; stack->nst.l3_manager = handle_event_nt; stack->nst.device = midev; stack->nst.cardnr = port; stack->nst.d_stid = stack->d_stid; stack->nst.feature = FEATURE_NET_HOLD; if (stack->ptp) stack->nst.feature |= FEATURE_NET_PTP; if (stack->pri) stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID; stack->nst.l1_id = stack->lower_id; stack->nst.l2_id = stack->upper_id; msg_queue_init(&stack->nst.down_queue); Isdnl2Init(&stack->nst); Isdnl3Init(&stack->nst); } if (!stack->nt) { /*assume L1 is up, we'll get DEACTIVATES soon, for non * up L1s*/ stack->l1link=0; } stack->l1link=0; stack->l2link=0;#if 0 if (!stack->nt) { misdn_lib_get_short_status(stack); } else { misdn_lib_get_l1_up(stack); if (!stack->ptp) misdn_lib_get_l1_up(stack); misdn_lib_get_l2_up(stack); }#endif misdn_lib_get_short_status(stack); misdn_lib_get_l1_up(stack); misdn_lib_get_l2_up(stack); } cb_log(8,0,"stack_init: port:%d lowerId:%x upperId:%x\n",stack->port,stack->lower_id, stack->upper_id); return stack;}void stack_destroy(struct misdn_stack* stack){ char buf[1024]; if (!stack) return; if (stack->nt) { cleanup_Isdnl2(&stack->nst); cleanup_Isdnl3(&stack->nst); } if (stack->lower_id) mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); if (stack->upper_id) mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);}static struct misdn_stack * find_stack_by_addr(int addr){ struct misdn_stack *stack; for (stack=glob_mgr->stack_list; stack; stack=stack->next) { if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack; } return NULL;}static struct misdn_stack * find_stack_by_port(int port){ struct misdn_stack *stack; for (stack=glob_mgr->stack_list; stack; stack=stack->next) if (stack->port == port) return stack; return NULL;}static struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt){ struct misdn_stack *stack; for (stack=glob_mgr->stack_list; stack; stack=stack->next) if ( &stack->mgr == mgr_nt) return stack; return NULL;}static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask){ int i; for (i=0; i<=stack->b_num; i++) { if ( (stack->bc[i].l3_id & mask) == (l3id & mask)) return &stack->bc[i] ; } return stack_holder_find(stack,l3id);}struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id){ int i; for (i=0; i<=stack->b_num; i++) { if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ; } return stack_holder_find(stack,l3id);}static struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack){ int i; for (i=0; i<=stack->b_num; i++) { if (stack->bc[i].holded ) return &stack->bc[i] ; } return NULL;}static struct misdn_bchannel *find_bc_by_addr(unsigned long addr){ struct misdn_stack* stack; int i; for (stack=glob_mgr->stack_list; stack; stack=stack->next) { for (i=0; i<=stack->b_num; i++) { if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) || stack->bc[i].layer_id== addr ) { return &stack->bc[i]; } } } return NULL;}struct misdn_bchannel *find_bc_by_confid(unsigned long confid){ struct misdn_stack* stack; int i; for (stack=glob_mgr->stack_list; stack; stack=stack->next) { for (i=0; i<=stack->b_num; i++) { if ( stack->bc[i].conf_id==confid ) { return &stack->bc[i]; } } } return NULL;}static struct misdn_bchannel *find_bc_by_channel(int port, int channel){ struct misdn_stack* stack=find_stack_by_port(port); int i; if (!stack) return NULL; for (i=0; i<=stack->b_num; i++) { if ( stack->bc[i].channel== channel ) { return &stack->bc[i]; } } return NULL;}static int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm){ struct misdn_stack *stack=get_stack_by_bc(bc); if (!stack->nt) { switch (event) { case EVENT_CONNECT_ACKNOWLEDGE: setup_bc(bc); if ( *bc->crypt_key ) { 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); manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) ); } if (misdn_cap_is_speech(bc->capability)) { if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0); manager_ec_enable(bc); if ( bc->txgain != 0 ) { cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain); manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain); } if ( bc->rxgain != 0 ) { cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain); manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain); } } break; case EVENT_CONNECT: if ( *bc->crypt_key ) { 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); manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) ); } case EVENT_ALERTING: case EVENT_PROGRESS: case EVENT_PROCEEDING: case EVENT_SETUP_ACKNOWLEDGE: case EVENT_SETUP: { if (bc->channel == 0xff || bc->channel<=0) bc->channel=0; if (find_free_chan_in_stack(stack, bc, bc->channel, 0)<0){ if (!stack->pri && !stack->ptp) { bc->cw=1; break; } if (!bc->channel) cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n"); else cb_log(0, stack->port, "Requested Channel Already in Use releasing this call with cause 34!!!!\n"); /* when the channel is already in use, we can't * simply clear it, we need to make sure that * it will still be marked as in_use in the * available channels list.*/ bc->channel=0; misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); return -1; } } setup_bc(bc); break; case EVENT_RELEASE_COMPLETE: case EVENT_RELEASE: break; default: break; } } else { /** NT MODE **/ } return 0;}static int handle_cr ( struct misdn_stack *stack, iframe_t *frm){ struct misdn_bchannel* bc; if (!stack) return -1; switch (frm->prim) { case CC_NEW_CR|INDICATION: cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo); bc=misdn_lib_get_free_bc(stack->port, 0, 1, 0); if (!bc) { cb_log(0, stack->port, " --> !! lib: No free channel!\n"); return -1; } cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo); bc->l3_id=frm->dinfo; return 1; case CC_NEW_CR|CONFIRM: return 1; case CC_NEW_CR|REQUEST: return 1; case CC_RELEASE_CR|REQUEST: return 1; case CC_RELEASE_CR|CONFIRM: break; case CC_RELEASE_CR|INDICATION: cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo); { struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo); struct misdn_bchannel dummybc; if (!bc) { cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo); make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0); bc=&dummybc; } if (bc) { int channel = bc->channel; cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo); /*bc->pid = 0;*/ bc->need_disconnect=0; bc->need_release=0; bc->need_release_complete=0; cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data); empty_bc(bc); clean_up_bc(bc); if (channel>0) empty_chan_in_stack(stack,channel); bc->in_use=0; dump_chan_list(stack); if (bc->stack_holder) { cb_log(4,stack->port, "REMOVEING Holder\n"); stack_holder_remove( stack, bc); free(bc); } } else { if (stack->nt) cb_log(4, stack->port, "BC with dinfo: %x not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr); } return 1; } break; } return 0;}/*Emptys bc if it's reserved (no SETUP out yet)*/void misdn_lib_release(struct misdn_bchannel *bc){ struct misdn_stack *stack=get_stack_by_bc(bc); if (!stack) { cb_log(1,0,"misdn_release: No Stack found\n"); return; } if (bc->channel>0) empty_chan_in_stack(stack,bc->channel); empty_bc(bc); clean_up_bc(bc); bc->in_use=0;}int misdn_lib_get_port_up (int port) { /* Pull Up L1 */ struct misdn_stack *stack; for (stack=glob_mgr->stack_list; stack; stack=stack->next) { if (stack->port == port) { if (!stack->l1link) misdn_lib_get_l1_up(stack); if (!stack->l2link) misdn_lib_get_l2_up(stack); return 0; } } return 0;}int misdn_lib_get_port_down (int port) { /* Pull Down L1 */ struct misdn_stack *stack; for (stack=glob_mgr->stack_list; stack; stack=stack->next) { if (stack->port == port) { if (stack->l2link) misdn_lib_get_l2_down(stack); misdn_lib_get_l1_down(stack); return 0; } } return 0;}int misdn_lib_port_up(int port, int check){ struct misdn_stack *stack; for (stack=glob_mgr->stack_list; stack; stack=stack->next) { if (stack->port == port) { if (stack->blocked) { cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link); return -1; } if (stack->ptp ) { if (stack->l1link && stack->l2link) { return 1; } else { cb_log(1,port, "Port Down L2:%d L1:%d\n", stack->l2link, stack->l1link); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -