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