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