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