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