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