2 * Chan_Misdn -- Channel Driver for Asterisk
6 * Copyright (C) 2004, Christian Richter
8 * Christian Richter <crich@beronet.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #include <mISDNuser/isdn_debug.h>
18 #include "isdn_lib_intern.h"
21 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id);
22 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id);
24 int queue_cleanup_bc(struct misdn_bchannel *bc) ;
26 int misdn_lib_get_l2_up(struct misdn_stack *stack);
28 struct misdn_stack* get_misdn_stack( void );
30 int misdn_lib_port_is_pri(int port)
32 struct misdn_stack *stack=get_misdn_stack();
33 for ( ; stack; stack=stack->next) {
34 if (stack->port == port) {
43 int misdn_lib_port_block(int port)
45 struct misdn_stack *stack=get_misdn_stack();
46 for ( ; stack; stack=stack->next) {
47 if (stack->port == port) {
56 int misdn_lib_port_unblock(int port)
58 struct misdn_stack *stack=get_misdn_stack();
59 for ( ; stack; stack=stack->next) {
60 if (stack->port == port) {
69 int misdn_lib_is_port_blocked(int port)
71 struct misdn_stack *stack=get_misdn_stack();
72 for ( ; stack; stack=stack->next) {
73 if (stack->port == port) {
74 return stack->blocked;
80 int misdn_lib_is_ptp(int port)
82 struct misdn_stack *stack=get_misdn_stack();
83 for ( ; stack; stack=stack->next) {
84 if (stack->port == port) return stack->ptp;
89 int misdn_lib_get_maxchans(int port)
91 struct misdn_stack *stack=get_misdn_stack();
92 for ( ; stack; stack=stack->next) {
93 if (stack->port == port) {
104 struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc)
106 struct misdn_stack *stack=get_misdn_stack();
108 if (!bc) return NULL;
110 for ( ; stack; stack=stack->next) {
112 for (i=0; i <=stack->b_num; i++) {
113 if ( bc->port == stack->port) return stack;
121 void get_show_stack_details(int port, char *buf)
123 struct misdn_stack *stack=get_misdn_stack();
125 for ( ; stack; stack=stack->next) {
126 if (stack->port == port) break;
130 sprintf(buf, "* Port %d Type %s Prot. %s L2Link %s L1Link:%s Blocked:%d", stack->port, stack->nt?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN",stack->blocked);
139 static int nt_err_cnt =0 ;
146 static enum global_states global_state=MISDN_INITIALIZING;
149 #include <mISDNuser/net_l2.h>
150 #include <mISDNuser/tone.h>
152 #include <semaphore.h>
156 #include "isdn_lib.h"
163 pthread_t event_thread;
164 pthread_t event_handler_thread;
169 msg_queue_t activatequeue;
173 struct misdn_stack *stack_list;
177 #define ECHOCAN_ON 123
178 #define ECHOCAN_OFF 124
181 #define MISDN_DEBUG 0
183 void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
185 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
187 struct misdn_bchannel *find_bc_by_confid(unsigned long confid);
189 struct misdn_bchannel *stack_holder_find_bychan(struct misdn_stack *stack, int chan);
191 int setup_bc(struct misdn_bchannel *bc);
193 int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
195 int misdn_lib_port_restart(int port);
196 int misdn_lib_pid_restart(int pid);
198 extern struct isdn_msg msgs_g[];
200 #define ISDN_PID_L3_B_USER 0x430000ff
201 #define ISDN_PID_L4_B_USER 0x440000ff
203 /* #define MISDN_IBUF_SIZE 1024 */
204 #define MISDN_IBUF_SIZE 512
206 /* Fine Tuning of Inband Signalling time */
207 #define TONE_ALERT_CNT 41 /* 1 Sec */
208 #define TONE_ALERT_SILENCE_CNT 200 /* 4 Sec */
210 #define TONE_BUSY_CNT 20 /* ? */
211 #define TONE_BUSY_SILENCE_CNT 48 /* ? */
215 static struct misdn_lib *glob_mgr;
217 char tone_425_flip[TONE_425_SIZE];
218 char tone_silence_flip[TONE_SILENCE_SIZE];
220 static void misdn_lib_isdn_event_catcher(void *arg);
221 static int handle_event_nt(void *dat, void *arg);
224 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
225 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
226 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
228 /* from isdn_lib.h */
229 int init_bc(struct misdn_stack * stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime);
230 struct misdn_stack* stack_init(int midev, int port, int ptp);
231 void stack_destroy(struct misdn_stack* stack);
233 int te_lib_init( void ) ; /* returns midev */
234 void te_lib_destroy(int midev) ;
235 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
236 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc);
237 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
238 void manager_clean_bc(struct misdn_bchannel *bc );
239 void manager_bchannel_setup (struct misdn_bchannel *bc);
240 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
242 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
244 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
245 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
249 static char *bearer2str(int cap) {
250 static char *bearers[]={
259 case INFO_CAPABILITY_SPEECH:
262 case INFO_CAPABILITY_AUDIO_3_1K:
265 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
268 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
278 static char flip_table[256];
280 static void init_flip_bits(void)
284 for (i = 0 ; i < 256 ; i++) {
285 unsigned char sample = 0 ;
286 for (k = 0; k<8; k++) {
287 if ( i & 1 << k ) sample |= 0x80 >> k;
289 flip_table[i] = sample;
293 static char * flip_buf_bits ( char * buf , int len)
298 for (i = 0 ; i < len; i++) {
299 buf[i] = flip_table[(unsigned char)buf[i]];
308 static msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
315 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
320 printf("cannot allocate memory, trying again...\n");
324 printf("cannot allocate memory, system overloaded.\n");
330 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
338 size = sizeof(Q931_info_t)+2;
342 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
347 dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
350 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
351 frm = (iframe_t *)dmsg->data;
354 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
360 if (!i) printf("cannot allocate memory, trying again...\n");
364 printf("cannot allocate memory, system overloaded.\n");
369 static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
372 frm = (iframe_t *)dmsg->data;
373 struct misdn_stack *stack=get_stack_by_bc(bc);
376 cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
380 frm->addr = (stack->upper_id | FLG_MSG_DOWN);
381 frm->dinfo = bc->l3_id;
382 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
384 cb_log(4,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo);
386 mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
396 int misdn_cap_is_speech(int cap)
397 /** Poor mans version **/
399 if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
400 (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
404 int misdn_inband_avail(struct misdn_bchannel *bc)
407 /*if ! early_bconnect we have never inband available*/
408 if ( ! bc->early_bconnect ) return 0;
410 switch (bc->progress_indicator) {
411 case INFO_PI_INBAND_AVAILABLE:
412 case INFO_PI_CALL_NOT_E2E_ISDN:
413 case INFO_PI_CALLED_NOT_ISDN:
422 static void dump_chan_list(struct misdn_stack *stack)
426 for (i=0; i <= stack->b_num; i++) {
427 cb_log(6, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
434 static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel)
438 cb_log(5,stack->port,"find_free_chan: req_chan:%d\n",channel);
440 if (channel < 0 || channel > MAX_BCHANS) {
441 cb_log(0, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
447 for (i = 0; i < stack->b_num; i++) {
448 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
449 if (!stack->channels[i]) {
450 cb_log (3, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
452 cb_event(EVENT_NEW_CHANNEL, bc, NULL);
458 cb_log (1, stack->port, " !! NO FREE CHAN IN STACK\n");
459 dump_chan_list(stack);
464 static int empty_chan_in_stack(struct misdn_stack *stack, int channel)
466 if (channel<=0 || channel>=MAX_BCHANS) {
467 cb_log(0,stack?stack->port:0, "empty_chan_in_stack: cannot empty channel %d\n",channel);
471 cb_log (4, stack?stack->port:0, "empty_chan_in_stack: %d\n",channel);
472 stack->channels[channel-1] = 0;
473 dump_chan_list(stack);
477 char *bc_state2str(enum bchannel_state state) {
480 struct bchan_state_s {
482 enum bchannel_state s;
484 {"BCHAN_CLEANED", BCHAN_CLEANED },
485 {"BCHAN_EMPTY", BCHAN_EMPTY},
486 {"BCHAN_SETUP", BCHAN_SETUP},
487 {"BCHAN_SETUPED", BCHAN_SETUPED},
488 {"BCHAN_ACTIVE", BCHAN_ACTIVE},
489 {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
490 {"BCHAN_BRIDGE", BCHAN_BRIDGE},
491 {"BCHAN_BRIDGED", BCHAN_BRIDGED},
492 {"BCHAN_RELEASE", BCHAN_RELEASE},
493 {"BCHAN_RELEASED", BCHAN_RELEASED},
494 {"BCHAN_CLEAN", BCHAN_CLEAN},
495 {"BCHAN_CLEAN_REQUEST", BCHAN_CLEAN_REQUEST},
496 {"BCHAN_ERROR", BCHAN_ERROR}
499 for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
500 if ( states[i].s == state)
506 void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
508 cb_log(5,bc->port,"BC_STATE_CHANGE: l3id:%x from:%s to:%s\n",
510 bc_state2str(bc->bc_state),
511 bc_state2str(state) );
514 case BCHAN_ACTIVATED:
515 if (bc->next_bc_state == BCHAN_BRIDGED) {
516 misdn_join_conf(bc, bc->conf_id);
517 bc->next_bc_state = BCHAN_EMPTY;
526 static void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
528 cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
529 bc_state2str(bc->next_bc_state),
530 bc_state2str(state) );
532 bc->next_bc_state=state;
536 static void empty_bc(struct misdn_bchannel *bc)
546 bc->sending_complete = 0;
548 bc->restart_channel=0;
552 bc->need_more_infos = 0;
564 bc->curptx=0; bc->curprx=0;
566 bc->crypt_key[0] = 0;
571 bc->dnumplan=NUMPLAN_UNKNOWN;
572 bc->onumplan=NUMPLAN_UNKNOWN;
573 bc->rnumplan=NUMPLAN_UNKNOWN;
574 bc->cpnnumplan=NUMPLAN_UNKNOWN;
579 bc->early_bconnect = 1;
582 bc->ec_deftaps = 128;
583 bc->ec_whenbridged = 0;
593 bc->pres=0 ; /* screened */
595 bc->evq=EVENT_NOTHING;
597 bc->progress_coding=0;
598 bc->progress_location=0;
599 bc->progress_indicator=0;
601 /** Set Default Bearer Caps **/
602 bc->capability=INFO_CAPABILITY_SPEECH;
603 bc->law=INFO_CODEC_ALAW;
614 bc->infos_pending[0] = 0;
621 bc->fac_in.Function = Fac_None;
622 bc->fac_out.Function = Fac_None;
624 bc->te_choose_channel = 0;
628 static int clean_up_bc(struct misdn_bchannel *bc)
631 unsigned char buff[32];
632 struct misdn_stack * stack;
634 cb_log(3, bc?bc->port:0, "$$$ CLEANUP CALLED pid:%d\n", bc?bc->pid:-1);
637 stack=get_stack_by_bc(bc);
639 if (!stack) return -1;
641 switch (bc->bc_state ) {
643 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
650 cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
652 manager_ec_disable(bc);
654 manager_bchannel_deactivate(bc);
656 mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
659 bc_state_change(bc, BCHAN_CLEANED);
666 static void clear_l3(struct misdn_stack *stack)
670 for (i=0; i<=stack->b_num; i++) {
671 if (global_state == MISDN_INITIALIZED) {
672 cb_event(EVENT_CLEANUP, &stack->bc[i], NULL);
673 empty_chan_in_stack(stack,i+1);
674 empty_bc(&stack->bc[i]);
675 clean_up_bc(&stack->bc[i]);
681 static int set_chan_in_stack(struct misdn_stack *stack, int channel)
684 cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
685 dump_chan_list(stack);
686 if (channel >=1 && channel <= MAX_BCHANS) {
687 if (!stack->channels[channel-1])
688 stack->channels[channel-1] = 1;
690 cb_log(0,stack->port,"channel already in use:%d\n", channel );
694 cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
702 static int chan_in_stack_free(struct misdn_stack *stack, int channel)
704 if (stack->channels[channel-1])
712 static int newteid=0;
714 #define MAXPROCS 0x100
716 static int misdn_lib_get_l1_down(struct misdn_stack *stack)
720 act.prim = PH_DEACTIVATE | REQUEST;
721 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
727 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
733 static int misdn_lib_get_l2_down(struct misdn_stack *stack)
736 if (stack->ptp && (stack->nt) ) {
739 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
741 if (stack->nst.manager_l3(&stack->nst, dmsg))
747 act.prim = DL_RELEASE| REQUEST;
748 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
752 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
759 static int misdn_lib_get_l1_up(struct misdn_stack *stack)
763 act.prim = PH_ACTIVATE | REQUEST;
764 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
770 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
774 int misdn_lib_get_l2_up(struct misdn_stack *stack)
777 if (stack->ptp && (stack->nt) ) {
780 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
782 if (stack->nst.manager_l3(&stack->nst, dmsg))
788 act.prim = DL_ESTABLISH | REQUEST;
789 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
793 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
800 static int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack)
804 act.prim = DL_ESTABLISH | REQUEST;
805 act.addr = (stack->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
809 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
814 static int misdn_lib_get_short_status(struct misdn_stack *stack)
819 act.prim = MGR_SHORTSTATUS | REQUEST;
821 act.addr = (stack->upper_id | MSG_BROADCAST) ;
823 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
826 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
831 static int create_process (int midev, struct misdn_bchannel *bc) {
835 struct misdn_stack *stack=get_stack_by_bc(bc);
838 if (!find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0))
840 /*bc->channel=free_chan;*/
842 if (set_chan_in_stack(stack ,bc->channel)<0) return -1;
844 cb_log(4,stack->port, " --> found channel: %d\n", bc->channel);
846 for (i=0; i <= MAXPROCS; i++)
847 if (stack->procids[i]==0) break;
850 cb_log(0, stack->port, "Couldnt Create New ProcId.\n");
857 ncr.prim = CC_NEW_CR | REQUEST;
859 ncr.addr = (stack->upper_id | FLG_MSG_DOWN) ;
865 cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
868 if (stack->ptp || bc->te_choose_channel) {
869 /* we know exactly which channels are in use */
870 if (!find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0))
872 /*bc->channel=free_chan;*/
873 cb_log(2,stack->port, " --> found channel: %d\n", bc->channel);
874 if (set_chan_in_stack(stack ,bc->channel)<0) return -1;
876 /* other phones could have made a call also on this port (ptmp) */
881 /* if we are in te-mode, we need to create a process first */
882 if (newteid++ > 0xffff)
885 l3_id = (entity<<16) | newteid;
886 /* preparing message */
887 ncr.prim = CC_NEW_CR | REQUEST;
889 ncr.addr = (stack->upper_id | FLG_MSG_DOWN) ;
896 cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
898 mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
905 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
912 int setup_bc(struct misdn_bchannel *bc)
914 unsigned char buff[1025];
920 struct misdn_stack *stack=get_stack_by_bc(bc);
923 cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
927 int midev=stack->midev;
928 int channel=bc->channel-1-(bc->channel>16);
929 int b_stid=stack->b_stids[channel>=0?channel:0];
932 switch (bc->bc_state) {
936 cb_log(4, stack->port, "$$$ bc already upsetted stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
940 cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
943 cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
945 bc_state_change(bc,BCHAN_ERROR);
954 memset(&li, 0, sizeof(li));
959 li.st = bc->b_stid; /* given idx */
966 if ( bc->hdlc || bc->nodsp) {
967 cb_log(4, stack->port,"setup_bc: without dsp\n");
969 int l = sizeof(li.name);
970 strncpy(li.name, "B L3", l);
973 li.pid.layermask = ISDN_LAYER((3));
974 li.pid.protocol[3] = ISDN_PID_L3_B_USER;
978 cb_log(4, stack->port,"setup_bc: with dsp\n");
980 int l = sizeof(li.name);
981 strncpy(li.name, "B L4", l);
984 li.pid.layermask = ISDN_LAYER((4));
985 li.pid.protocol[4] = ISDN_PID_L4_B_USER
991 ret = mISDN_new_layer(midev, &li);
993 cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
995 bc_state_change(bc,BCHAN_ERROR);
999 bc->layer_id = li.id;
1002 memset(&pid, 0, sizeof(pid));
1006 cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
1009 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
1010 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1011 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1012 pid.protocol[3] = ISDN_PID_L3_B_USER;
1013 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
1015 } else if ( bc->hdlc ) {
1016 cb_log(2, stack->port," --> HDLC Mode\n");
1017 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
1018 pid.protocol[2] = ISDN_PID_L2_B_TRANS ;
1019 pid.protocol[3] = ISDN_PID_L3_B_USER;
1020 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
1022 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
1023 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1024 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1025 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1026 pid.protocol[4] = ISDN_PID_L4_B_USER;
1027 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1031 ret = mISDN_set_stack(midev, bc->b_stid, &pid);
1034 cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
1036 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1038 bc_state_change(bc,BCHAN_ERROR);
1039 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1043 ret = mISDN_get_setstack_ind(midev, bc->layer_id);
1046 cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
1047 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1049 bc_state_change(bc,BCHAN_ERROR);
1050 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1054 ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
1056 bc->addr = ret>0? ret : 0;
1059 cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
1060 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1062 bc_state_change(bc,BCHAN_ERROR);
1063 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1067 manager_bchannel_activate(bc);
1069 bc_state_change(bc,BCHAN_ACTIVATED);
1077 int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime)
1079 unsigned char buff[1025];
1080 iframe_t *frm = (iframe_t *)buff;
1085 cb_log(8, port, "Init.BC %d.\n",bidx);
1087 memset(bc, 0,sizeof(struct misdn_bchannel));
1090 int l = sizeof(bc->msn);
1091 strncpy(bc->msn,msn, l);
1097 bc_state_change(bc, BCHAN_CLEANED);
1099 bc->port=stack->port;
1100 bc->nt=stack->nt?1:0;
1103 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
1105 if (!ibuf) return -1;
1107 clear_ibuffer( ibuf);
1109 ibuf->rsem=malloc(sizeof(sem_t));
1113 if (sem_init(ibuf->rsem,1,0)<0)
1114 sem_init(ibuf->rsem,0,0);
1122 stack_info_t *stinf;
1123 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
1125 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1129 stinf = (stack_info_t *)&frm->data.p;
1131 cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
1139 struct misdn_stack* stack_init( int midev, int port, int ptp )
1142 unsigned char buff[1025];
1143 iframe_t *frm = (iframe_t *)buff;
1144 stack_info_t *stinf;
1148 struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
1149 if (!stack ) return NULL;
1152 cb_log(8, port, "Init. Stack.\n");
1154 memset(stack,0,sizeof(struct misdn_stack));
1156 for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
1162 stack->holding=NULL;
1165 msg_queue_init(&stack->downqueue);
1166 msg_queue_init(&stack->upqueue);
1168 /* query port's requirements */
1169 ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1171 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1175 stinf = (stack_info_t *)&frm->data.p;
1177 stack->d_stid = stinf->id;
1178 stack->b_num = stinf->childcnt;
1180 for (i=0; i<=stinf->childcnt; i++)
1181 stack->b_stids[i] = stinf->child[i];
1183 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1184 case ISDN_PID_L0_TE_S0:
1187 case ISDN_PID_L0_NT_S0:
1188 cb_log(8, port, "NT Stack\n");
1193 case ISDN_PID_L0_TE_U:
1195 case ISDN_PID_L0_NT_U:
1197 case ISDN_PID_L0_TE_UP2:
1199 case ISDN_PID_L0_NT_UP2:
1201 case ISDN_PID_L0_TE_E1:
1202 cb_log(8, port, "TE S2M Stack\n");
1206 case ISDN_PID_L0_NT_E1:
1207 cb_log(8, port, "TE S2M Stack\n");
1213 cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1218 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) {
1229 cb_log(8, port, "Init. Stack.\n");
1231 memset(&li, 0, sizeof(li));
1233 int l = sizeof(li.name);
1234 strncpy(li.name,nt?"net l2":"user l4", l);
1239 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1240 li.pid.layermask = ISDN_LAYER((nt?2:4));
1241 li.st = stack->d_stid;
1244 ret = mISDN_new_layer(midev, &li);
1246 cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1251 stack->upper_id = li.id;
1252 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
1255 cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
1259 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3);
1260 if (stack->lower_id < 0) {
1261 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1265 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1266 if (stack->upper_id < 0) {
1267 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
1271 cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id);
1274 /* create nst (nt-mode only) */
1277 memset(&stack->nst, 0, sizeof(net_stack_t));
1278 memset(&stack->mgr, 0, sizeof(manager_t));
1280 stack->mgr.nst = &stack->nst;
1281 stack->nst.manager = &stack->mgr;
1283 stack->nst.l3_manager = handle_event_nt;
1284 stack->nst.device = midev;
1285 stack->nst.cardnr = port;
1286 stack->nst.d_stid = stack->d_stid;
1288 stack->nst.feature = FEATURE_NET_HOLD;
1290 stack->nst.feature |= FEATURE_NET_PTP;
1292 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1294 stack->nst.l1_id = stack->lower_id;
1295 stack->nst.l2_id = stack->upper_id;
1297 msg_queue_init(&stack->nst.down_queue);
1299 Isdnl2Init(&stack->nst);
1300 Isdnl3Init(&stack->nst);
1305 /*assume L1 is up, we'll get DEACTIVATES soon, for non
1313 misdn_lib_get_short_status(stack);
1315 misdn_lib_get_l1_up(stack);
1316 if (!stack->ptp) misdn_lib_get_l1_up(stack);
1317 misdn_lib_get_l2_up(stack);
1321 misdn_lib_get_short_status(stack);
1322 misdn_lib_get_l1_up(stack);
1323 misdn_lib_get_l2_up(stack);
1327 cb_log(8,0,"stack_init: port:%d lowerId:%x upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
1333 void stack_destroy(struct misdn_stack* stack)
1339 cleanup_Isdnl2(&stack->nst);
1340 cleanup_Isdnl3(&stack->nst);
1343 if (stack->lower_id)
1344 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1346 if (stack->upper_id)
1347 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1351 static struct misdn_stack * find_stack_by_addr(int addr)
1353 struct misdn_stack *stack;
1355 for (stack=glob_mgr->stack_list;
1357 stack=stack->next) {
1358 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
1366 static struct misdn_stack * find_stack_by_port(int port)
1368 struct misdn_stack *stack;
1370 for (stack=glob_mgr->stack_list;
1373 if (stack->port == port) return stack;
1378 static struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
1380 struct misdn_stack *stack;
1382 for (stack=glob_mgr->stack_list;
1385 if ( &stack->mgr == mgr_nt) return stack;
1390 static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1393 for (i=0; i<=stack->b_num; i++) {
1394 if ( (stack->bc[i].l3_id & mask) == (l3id & mask)) return &stack->bc[i] ;
1396 return stack_holder_find(stack,l3id);
1400 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1403 for (i=0; i<=stack->b_num; i++) {
1404 if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
1406 return stack_holder_find(stack,l3id);
1409 static struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
1412 for (i=0; i<=stack->b_num; i++) {
1413 if (stack->bc[i].holded ) return &stack->bc[i] ;
1419 static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1421 struct misdn_stack* stack;
1424 for (stack=glob_mgr->stack_list;
1426 stack=stack->next) {
1427 for (i=0; i<=stack->b_num; i++) {
1428 if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) || stack->bc[i].layer_id== addr ) {
1429 return &stack->bc[i];
1437 struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
1439 struct misdn_stack* stack;
1442 for (stack=glob_mgr->stack_list;
1444 stack=stack->next) {
1445 for (i=0; i<=stack->b_num; i++) {
1446 if ( stack->bc[i].conf_id==confid ) {
1447 return &stack->bc[i];
1455 static struct misdn_bchannel *find_bc_by_channel(int port, int channel)
1457 struct misdn_stack* stack=find_stack_by_port(port);
1460 if (!stack) return NULL;
1462 for (i=0; i<=stack->b_num; i++) {
1463 if ( stack->bc[i].channel== channel ) {
1464 return &stack->bc[i];
1475 static int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1477 struct misdn_stack *stack=get_stack_by_bc(bc);
1483 case EVENT_CONNECT_ACKNOWLEDGE:
1485 if ( !misdn_cap_is_speech(bc->capability)) {
1486 int ret=setup_bc(bc);
1487 if (ret == -EINVAL){
1488 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1495 if ( *bc->crypt_key ) {
1496 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);
1497 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1499 case EVENT_ALERTING:
1500 case EVENT_PROGRESS:
1501 case EVENT_PROCEEDING:
1502 case EVENT_SETUP_ACKNOWLEDGE:
1508 if (bc->channel == 0xff) {
1509 if (!find_free_chan_in_stack(stack, bc, 0)) {
1510 cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
1512 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1517 if (bc->channel >0 && bc->channel<255) {
1518 int ret=set_chan_in_stack(stack ,bc->channel);
1519 if (event == EVENT_SETUP && ret<0){
1520 /* empty bchannel */
1523 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1530 case EVENT_RELEASE_COMPLETE:
1533 empty_chan_in_stack(stack,bc->channel);
1534 int tmpcause=bc->cause;
1542 } else { /** NT MODE **/
1548 static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1550 if (!stack) return -1;
1552 switch (frm->prim) {
1553 case CC_NEW_CR|INDICATION:
1554 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1556 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1);
1558 cb_log(0, stack->port, " --> !! lib: No free channel!\n");
1562 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1563 bc->l3_id=frm->dinfo;
1565 case CC_NEW_CR|CONFIRM:
1567 case CC_NEW_CR|REQUEST:
1569 case CC_RELEASE_CR|REQUEST:
1571 case CC_RELEASE_CR|CONFIRM:
1573 case CC_RELEASE_CR|INDICATION:
1574 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
1576 struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
1577 struct misdn_bchannel dummybc;
1580 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1581 memset (&dummybc,0,sizeof(dummybc));
1582 dummybc.port=stack->port;
1583 dummybc.l3_id=frm->dinfo;
1584 dummybc.nt=stack->nt;
1589 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
1591 empty_chan_in_stack(stack,bc->channel);
1596 dump_chan_list(stack);
1598 bc->need_disconnect=0;
1600 bc->need_release_complete=0;
1602 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1603 if (bc->stack_holder) {
1604 cb_log(4,stack->port, "REMOVEING Holder\n");
1605 stack_holder_remove( stack, bc);
1611 cb_log(4, stack->port, "BC with dinfo: %x not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
1623 /*Emptys bc if it's reserved (no SETUP out yet)*/
1624 void misdn_lib_release(struct misdn_bchannel *bc)
1626 struct misdn_stack *stack=get_stack_by_bc(bc);
1629 cb_log(1,0,"misdn_release: No Stack found\n");
1633 if (bc->channel>0) {
1634 empty_chan_in_stack(stack,bc->channel);
1643 int misdn_lib_get_port_up (int port)
1645 struct misdn_stack *stack;
1647 for (stack=glob_mgr->stack_list;
1649 stack=stack->next) {
1651 if (stack->port == port) {
1654 misdn_lib_get_l1_up(stack);
1656 misdn_lib_get_l2_up(stack);
1665 int misdn_lib_get_port_down (int port)
1666 { /* Pull Down L1 */
1667 struct misdn_stack *stack;
1668 for (stack=glob_mgr->stack_list;
1670 stack=stack->next) {
1671 if (stack->port == port) {
1673 misdn_lib_get_l2_down(stack);
1674 misdn_lib_get_l1_down(stack);
1681 int misdn_lib_port_up(int port, int check)
1683 struct misdn_stack *stack;
1686 for (stack=glob_mgr->stack_list;
1688 stack=stack->next) {
1690 if (stack->port == port) {
1692 if (stack->blocked) {
1693 cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
1699 if (stack->l1link && stack->l2link) {
1702 cb_log(0,port, "Port Down L2:%d L1:%d\n",
1703 stack->l2link, stack->l1link);
1707 if ( !check || stack->l1link )
1710 cb_log(0,port, "Port down PMP\n");
1722 handle_event_nt(void *dat, void *arg)
1724 manager_t *mgr = (manager_t *)dat;
1725 msg_t *msg = (msg_t *)arg;
1726 mISDNuser_head_t *hh;
1729 struct misdn_stack *stack=find_stack_by_mgr(mgr);
1735 hh=(mISDNuser_head_t*)msg->data;
1738 cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1741 case CC_RETRIEVE|INDICATION:
1743 iframe_t frm; /* fake te frm to add callref to global callreflist */
1744 frm.dinfo = hh->dinfo;
1746 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1748 frm.prim = CC_NEW_CR|INDICATION;
1750 if (handle_cr( stack, &frm)< 0) {
1752 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1753 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1754 stack->nst.manager_l3(&stack->nst, dmsg);
1759 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1760 struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
1761 cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
1764 cb_log(4, stack->port, "REMOVEING Holder\n");
1766 /*swap the backup to our new channel back*/
1767 stack_holder_remove(stack, hold_bc);
1768 memcpy(bc,hold_bc,sizeof(struct misdn_bchannel));
1778 case CC_SETUP|CONFIRM:
1780 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1781 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1782 cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n",l3id );
1784 if (!bc) { cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
1785 cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1787 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1792 case CC_SETUP|INDICATION:
1794 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1);
1799 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1800 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1801 stack->nst.manager_l3(&stack->nst, dmsg);
1806 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
1807 bc->l3_id=hh->dinfo;
1811 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
1814 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1816 if ( !misdn_cap_is_speech(bc->capability)) {
1817 int ret=setup_bc(bc);
1818 if (ret == -EINVAL){
1819 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1828 case CC_ALERTING|INDICATION:
1829 case CC_PROCEEDING|INDICATION:
1830 case CC_SETUP_ACKNOWLEDGE|INDICATION:
1831 if(!stack->ptp) break;
1832 case CC_CONNECT|INDICATION:
1835 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1839 cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
1841 cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
1842 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1843 stack->nst.manager_l3(&stack->nst, dmsg);
1848 int ret=setup_bc(bc);
1849 if (ret == -EINVAL){
1850 cb_log(0,bc->port,"handle_event_nt: setup_bc failed\n");
1851 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1856 case CC_DISCONNECT|INDICATION:
1858 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1860 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1862 int myprocid=bc->l3_id&0x0000ffff;
1863 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1864 cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
1871 case CC_FACILITY|INDICATION:
1873 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1875 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1877 int myprocid=bc->l3_id&0x0000ffff;
1878 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1879 cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1885 case CC_RELEASE_COMPLETE|INDICATION:
1888 case CC_SUSPEND|INDICATION:
1891 cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
1892 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1893 stack->nst.manager_l3(&stack->nst, dmsg);
1898 case CC_RESUME|INDICATION:
1901 case CC_RELEASE|CONFIRM:
1904 case CC_RELEASE|INDICATION:
1907 case CC_RELEASE_CR|INDICATION:
1909 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1910 struct misdn_bchannel dummybc;
1911 iframe_t frm; /* fake te frm to remove callref from global callreflist */
1912 frm.dinfo = hh->dinfo;
1914 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1916 frm.prim = CC_RELEASE_CR|INDICATION;
1917 cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
1918 /** removing procid **/
1920 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1921 memset (&dummybc,0,sizeof(dummybc));
1922 dummybc.port=stack->port;
1923 dummybc.l3_id=hh->dinfo;
1924 dummybc.nt=stack->nt;
1929 if ( (bc->l3_id & 0xff00) == 0xff00) {
1930 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
1931 stack->procids[bc->l3_id&0xff] = 0 ;
1934 else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
1936 if (handle_cr(stack, &frm)<0) {
1944 case CC_NEW_CR|INDICATION:
1945 /* Got New CR for bchan, for now I handle this one in */
1946 /* connect_ack, Need to be changed */
1948 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1949 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1950 if (!bc) { cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
1951 if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
1952 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
1953 stack->procids[bc->l3_id&0xff] = 0 ;
1955 cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID is %x\n",l3id );
1958 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1964 case DL_ESTABLISH | INDICATION:
1965 case DL_ESTABLISH | CONFIRM:
1967 cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
1969 if (stack->ptp && stack->l2link) {
1970 cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
1971 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
1983 case DL_RELEASE | INDICATION:
1984 case DL_RELEASE | CONFIRM:
1987 cb_log(3 , stack->port, "%% GOT L2 DeActivate Info.\n");
1989 if (stack->l2upcnt>3) {
1990 cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attemps!!!\n");
1993 if (stack->nt) misdn_lib_reinit_nt_stack(stack->port);
1995 if (stack->l1link) {
1996 misdn_lib_get_l2_up(stack);
2002 cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
2013 /* Parse Events and fire_up to App. */
2014 struct misdn_bchannel *bc;
2015 struct misdn_bchannel dummybc;
2017 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
2019 bc=find_bc_by_l3id(stack, hh->dinfo);
2023 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
2024 memset (&dummybc,0,sizeof(dummybc));
2025 dummybc.port=stack->port;
2026 dummybc.l3_id=hh->dinfo;
2027 dummybc.nt=stack->nt;
2031 isdn_msg_parse_event(msgs_g,msg,bc, 1);
2035 if (bc->channel<=0 || bc->channel==0xff) {
2036 bc->channel=find_free_chan_in_stack(stack,bc, 0);
2039 goto ERR_NO_CHANNEL;
2040 } else if (!stack->ptp)
2041 cb_log(3,stack->port," --> PTMP but channel requested\n");
2043 int ret=set_chan_in_stack(stack, bc->channel);
2044 if (event==EVENT_SETUP && ret<0){
2045 /* empty bchannel */
2049 goto ERR_NO_CHANNEL;
2053 case EVENT_RELEASE_COMPLETE:
2055 empty_chan_in_stack(stack, bc->channel);
2056 int tmpcause=bc->cause;
2066 if(!isdn_get_info(msgs_g,event,1)) {
2067 cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
2072 cb_log(1, stack->port, "Siemens Busy reject..\n");
2079 cb_event(event, bc, glob_mgr->user_data);
2083 cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo);
2094 static int handle_timers(msg_t* msg)
2096 iframe_t *frm= (iframe_t*)msg->data;
2097 struct misdn_stack *stack;
2100 switch (frm->prim) {
2101 case MGR_INITTIMER | CONFIRM:
2102 case MGR_ADDTIMER | CONFIRM:
2103 case MGR_DELTIMER | CONFIRM:
2104 case MGR_REMOVETIMER | CONFIRM:
2111 if (frm->prim==(MGR_TIMER | INDICATION) ) {
2112 for (stack = glob_mgr->stack_list;
2114 stack = stack->next) {
2117 if (!stack->nt) continue;
2119 it = stack->nst.tlist;
2121 for(it=stack->nst.tlist;
2124 if (it->id == (int)frm->addr)
2129 ret = mISDN_write_frame(stack->midev, msg->data, frm->addr,
2130 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
2131 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
2132 ret = it->function(it->data);
2138 cb_log(0, 0, "Timer Msg without Timer ??\n");
2148 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
2150 bc->generate_tone=1;
2153 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
2155 bc->generate_tone=0;
2159 static int do_tone(struct misdn_bchannel *bc, int len)
2163 if (bc->generate_tone) {
2164 cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
2166 if ( !bc->nojitter ) {
2167 misdn_tx_jitter(bc,len);
2177 #ifdef MISDN_SAVE_DATA
2178 static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2)
2182 sprintf(n1,"/tmp/misdn-rx-%d.raw",id);
2183 sprintf(n2,"/tmp/misdn-tx-%d.raw",id);
2185 FILE *rx=fopen(n1,"a+");
2186 FILE *tx=fopen(n2,"a+");
2189 cb_log(0,0,"Couldn't open files: %s\n",strerror(errno));
2202 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
2204 char buf[4096 + mISDN_HEADER_LEN];
2205 char *data=&buf[mISDN_HEADER_LEN];
2206 iframe_t *txfrm= (iframe_t*)buf;
2209 jlen=cb_jb_empty(bc,data,len);
2212 #ifdef MISDN_SAVE_DATA
2213 misdn_save_data((bc->port*100+bc->channel), data, jlen, bc->bframe, bc->bframe_len);
2215 flip_buf_bits( data, jlen);
2218 cb_log(7,bc->port,"Jitterbuffer Underrun.\n");
2221 txfrm->prim = DL_DATA|REQUEST;
2225 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2228 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2230 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2232 #define MISDN_GEN_SILENCE
2233 #ifdef MISDN_GEN_SILENCE
2234 int cnt=len/TONE_SILENCE_SIZE;
2235 int rest=len%TONE_SILENCE_SIZE;
2238 for (i=0; i<cnt; i++) {
2239 memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE );
2240 data +=TONE_SILENCE_SIZE;
2244 memcpy(data, tone_silence_flip, rest);
2247 txfrm->prim = DL_DATA|REQUEST;
2251 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2254 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2256 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2262 static int handle_bchan(msg_t *msg)
2264 iframe_t *frm= (iframe_t*)msg->data;
2267 struct misdn_bchannel *bc=find_bc_by_addr(frm->addr);
2270 cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
2274 struct misdn_stack *stack=get_stack_by_bc(bc);
2277 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);
2281 switch (frm->prim) {
2283 case MGR_SETSTACK| CONFIRM:
2284 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid);
2287 case MGR_SETSTACK| INDICATION:
2288 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid);
2292 bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer);
2295 if (errno == EAGAIN) {
2300 cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno));
2302 /* we kill the channel later, when we received some
2304 bc->addr= frm->addr;
2305 } else if ( bc->addr < 0) {
2306 cb_log(0, stack->port,"$$$ bc->addr <0 Error:%s\n",strerror(errno));
2310 cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
2315 switch(bc->bc_state) {
2317 bc_state_change(bc,BCHAN_SETUPED);
2320 case BCHAN_CLEAN_REQUEST:
2322 cb_log(0, stack->port," --> STATE WASN'T SETUP (but %s) in SETSTACK|IND pid:%d\n",bc_state2str(bc->bc_state), bc->pid);
2328 case MGR_DELLAYER| INDICATION:
2329 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid);
2332 case MGR_DELLAYER| CONFIRM:
2333 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid);
2341 case PH_ACTIVATE | INDICATION:
2342 case DL_ESTABLISH | INDICATION:
2343 cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid);
2348 case PH_ACTIVATE | CONFIRM:
2349 case DL_ESTABLISH | CONFIRM:
2351 cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid);
2356 case DL_ESTABLISH | REQUEST:
2359 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2364 case DL_RELEASE|REQUEST:
2367 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2372 case PH_DEACTIVATE | INDICATION:
2373 case DL_RELEASE | INDICATION:
2374 cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid);
2379 case PH_DEACTIVATE | CONFIRM:
2380 case DL_RELEASE | CONFIRM:
2381 cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid);
2386 case PH_CONTROL|INDICATION:
2388 unsigned int cont = *((unsigned int *)&frm->data.p);
2390 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);
2392 if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
2393 int dtmf = cont & DTMF_TONE_MASK;
2394 cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
2396 cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
2401 if (cont == BF_REJECT) {
2402 cb_log(4, stack->port, " --> BF REJECT\n");
2406 if (cont == BF_ACCEPT) {
2407 cb_log(4, stack->port, " --> BF ACCEPT\n");
2414 case PH_DATA|REQUEST:
2415 case DL_DATA|REQUEST:
2416 cb_log(0, stack->port, "DL_DATA REQUEST \n");
2423 case PH_DATA|INDICATION:
2424 case DL_DATA|INDICATION:
2426 bc->bframe = (void*)&frm->data.i;
2427 bc->bframe_len = frm->len;
2429 /** Anyway flip the bufbits **/
2430 if ( misdn_cap_is_speech(bc->capability) )
2431 flip_buf_bits(bc->bframe, bc->bframe_len);
2434 if (!bc->bframe_len) {
2435 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2440 if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
2441 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2447 cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
2451 if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
2454 #ifdef MISDN_B_DEBUG
2455 cb_log(0,bc->port,"do_tone START\n");
2457 t=do_tone(bc,frm->len);
2459 #ifdef MISDN_B_DEBUG
2460 cb_log(0,bc->port,"do_tone STOP (%d)\n",t);
2464 if ( misdn_cap_is_speech(bc->capability)) {
2465 if ( !bc->nojitter ) {
2466 #ifdef MISDN_B_DEBUG
2467 cb_log(0,bc->port,"tx_jitter START\n");
2469 misdn_tx_jitter(bc,frm->len);
2470 #ifdef MISDN_B_DEBUG
2471 cb_log(0,bc->port,"tx_jitter STOP\n");
2476 #ifdef MISDN_B_DEBUG
2477 cb_log(0,bc->port,"EVENT_B_DATA START\n");
2480 int i=cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
2481 #ifdef MISDN_B_DEBUG
2482 cb_log(0,bc->port,"EVENT_B_DATA STOP\n");
2486 cb_log(10,stack->port,"cb_event returned <0\n");
2487 /*clean_up_bc(bc);*/
2496 case PH_CONTROL | CONFIRM:
2497 cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
2501 case PH_DATA | CONFIRM:
2502 case DL_DATA|CONFIRM:
2505 cb_log(0, stack->port, "Data confirmed\n");
2510 case DL_DATA|RESPONSE:
2512 cb_log(0, stack->port, "Data response\n");
2523 static int handle_frm_nt(msg_t *msg)
2525 iframe_t *frm= (iframe_t*)msg->data;
2526 struct misdn_stack *stack;
2529 stack=find_stack_by_addr( frm->addr );
2533 if (!stack || !stack->nt) {
2538 if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
2540 if (nt_err_cnt > 0 ) {
2541 if (nt_err_cnt < 100) {
2543 cb_log(0, stack->port, "NT Stack sends us error: %d \n", err);
2544 } else if (nt_err_cnt < 105){
2545 cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
2558 static int handle_frm(msg_t *msg)
2560 iframe_t *frm = (iframe_t*) msg->data;
2562 struct misdn_stack *stack=find_stack_by_addr(frm->addr);
2564 if (!stack || stack->nt) {
2568 cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim);
2571 struct misdn_bchannel *bc;
2572 int ret=handle_cr(stack, frm);
2575 cb_log(3,stack?stack->port:0,"handle_frm: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
2585 bc=find_bc_by_l3id(stack, frm->dinfo);
2589 enum event_e event = isdn_msg_get_event(msgs_g, msg, 0);
2590 enum event_response_e response=RESPONSE_OK;
2592 isdn_msg_parse_event(msgs_g,msg,bc, 0);
2594 /** Preprocess some Events **/
2595 int ret=handle_event(bc, event, frm);
2597 cb_log(0,stack->port,"couldn't handle event\n");
2601 /* shoot up event to App: */
2602 cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2604 if(!isdn_get_info(msgs_g,event,0))
2605 cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2607 response=cb_event(event, bc, glob_mgr->user_data);
2609 if (event == EVENT_SETUP) {
2611 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2613 cb_log(0, stack->port, "TOTALY IGNORING SETUP \n");
2616 case RESPONSE_IGNORE_SETUP:
2617 /* I think we should send CC_RELEASE_CR, but am not sure*/
2620 case RESPONSE_RELEASE_SETUP:
2621 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
2623 empty_chan_in_stack(stack, bc->channel);
2625 bc_state_change(bc,BCHAN_CLEANED);
2627 cb_log(0, stack->port, "GOT IGNORE SETUP\n");
2632 cb_log(4, stack->port, "GOT SETUP OK\n");
2641 cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
2649 cb_log(0, stack->port, " --> Didn't find BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
2650 struct misdn_bchannel dummybc;
2651 memset (&dummybc,0,sizeof(dummybc));
2652 dummybc.port=stack->port;
2653 dummybc.l3_id=frm->dinfo;
2659 cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
2664 static int handle_l1(msg_t *msg)
2666 iframe_t *frm = (iframe_t*) msg->data;
2667 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2670 if (!stack) return 0 ;
2672 switch (frm->prim) {
2673 case PH_ACTIVATE | CONFIRM:
2674 case PH_ACTIVATE | INDICATION:
2675 cb_log (3, stack->port, "L1: PH L1Link Up!\n");
2680 if (stack->nst.l1_l2(&stack->nst, msg))
2684 misdn_lib_get_l2_up(stack);
2689 for (i=0;i<=stack->b_num; i++) {
2690 if (stack->bc[i].evq != EVENT_NOTHING) {
2691 cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
2692 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
2693 stack->bc[i].evq=EVENT_NOTHING;
2699 case PH_ACTIVATE | REQUEST:
2701 cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2704 case PH_DEACTIVATE | REQUEST:
2706 cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2709 case PH_DEACTIVATE | CONFIRM:
2710 case PH_DEACTIVATE | INDICATION:
2711 cb_log (3, stack->port, "L1: PH L1Link Down! \n");
2713 for (i=0; i<=stack->b_num; i++) {
2714 if (global_state == MISDN_INITIALIZED) {
2715 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2720 if (stack->nst.l1_l2(&stack->nst, msg))
2734 static int handle_l2(msg_t *msg)
2736 iframe_t *frm = (iframe_t*) msg->data;
2738 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2746 case DL_ESTABLISH | REQUEST:
2747 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2749 case DL_RELEASE | REQUEST:
2750 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2753 case DL_ESTABLISH | INDICATION:
2754 case DL_ESTABLISH | CONFIRM:
2756 cb_log (3, stack->port, "L2: L2Link Up! \n");
2757 if (stack->ptp && stack->l2link) {
2758 cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n");
2759 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2767 case DL_RELEASE | INDICATION:
2768 case DL_RELEASE | CONFIRM:
2770 cb_log (3, stack->port, "L2: L2Link Down! \n");
2781 static int handle_mgmt(msg_t *msg)
2783 iframe_t *frm = (iframe_t*) msg->data;
2785 if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2786 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2791 struct misdn_stack * stack=find_stack_by_addr(frm->addr);
2794 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
2795 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
2805 case MGR_SHORTSTATUS | INDICATION:
2806 case MGR_SHORTSTATUS | CONFIRM:
2807 cb_log(5, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
2809 switch (frm->dinfo) {
2810 case SSTATUS_L1_ACTIVATED:
2811 cb_log(3, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
2815 case SSTATUS_L1_DEACTIVATED:
2816 cb_log(3, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2822 case SSTATUS_L2_ESTABLISHED:
2823 cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
2827 case SSTATUS_L2_RELEASED:
2828 cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
2836 case MGR_SETSTACK | INDICATION:
2837 cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2840 case MGR_DELLAYER | CONFIRM:
2841 cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
2848 if ( (frm->prim & 0x0f0000) == 0x0f0000) {
2849 cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
2858 static msg_t *fetch_msg(int midev)
2860 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2864 cb_log(0, 0, "fetch_msg: alloc msg failed !!");
2869 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
2873 free_msg(msg); /* danger, cauz usualy freeing in main_loop */
2874 cb_log(6,0,"Got empty Msg..\n");
2879 if (errno == EAGAIN) {
2880 /*we wait for mISDN here*/
2881 cb_log(4,0,"mISDN_read wants us to wait\n");
2886 cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno);
2890 if (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION)))
2891 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 );
2896 void misdn_lib_isdn_l1watcher(int port)
2898 struct misdn_stack *stack;
2900 for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
2904 cb_log(4, port, "Checking L1 State\n");
2905 if (!stack->l1link) {
2906 cb_log(4, port, "L1 State Down, trying to get it up again\n");
2907 misdn_lib_get_short_status(stack);
2908 misdn_lib_get_l1_up(stack);
2909 misdn_lib_get_l2_up(stack);
2914 static void misdn_lib_isdn_event_catcher(void *arg)
2916 struct misdn_lib *mgr = arg;
2917 int zero_frm=0 , fff_frm=0 ;
2918 int midev= mgr->midev;
2922 msg_t *msg = fetch_msg(midev);
2928 frm = (iframe_t*) msg->data;
2930 /** When we make a call from NT2Ast we get this frames **/
2931 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
2937 cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
2942 /** I get this sometimes after setup_bc **/
2943 if (frm->len == 0 && frm->dinfo == 0 && frm->prim == 0xffffffff ) {
2949 cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
2954 manager_isdn_handler(frm, msg);
2960 /** App Interface **/
2964 iframe_t *frm=(iframe_t*)buff;
2965 int midev=mISDN_open();
2968 memset(buff,0,1025);
2970 if (midev<=0) return midev;
2972 /* create entity for layer 3 TE-mode */
2973 mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
2974 ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
2976 if (ret < mISDN_HEADER_LEN) {
2978 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
2982 entity = frm->dinfo & 0xffff ;
2991 void te_lib_destroy(int midev)
2994 mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
2996 cb_log(4, 0, "Entetity deleted\n");
2998 cb_log(4, 0, "midev closed\n");
3003 void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
3005 holded_bc->holded=0;
3008 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
3010 struct misdn_stack *stack;
3013 for (stack=glob_mgr->stack_list;
3015 stack=stack->next) {
3016 for (i=0; i<=stack->b_num; i++)
3017 if (stack->bc[i].pid == pid) return &stack->bc[i];
3023 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
3025 struct misdn_stack *stack=get_stack_by_bc(bc);
3026 return find_bc_holded(stack);
3031 static void prepare_bc(struct misdn_bchannel*bc, int channel)
3033 bc->channel = channel;
3034 bc->channel_preselected = channel?1:0;
3036 bc->need_disconnect=1;
3038 bc->need_release_complete=1;
3041 if (++mypid>5000) mypid=1;
3051 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout)
3053 struct misdn_stack *stack;
3056 if (channel < 0 || channel > MAX_BCHANS) {
3057 cb_log(0,port,"Requested channel out of bounds (%d)\n",channel);
3061 for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
3063 if (stack->port == port) {
3064 if (stack->blocked) {
3065 cb_log(0,port,"Port is blocked\n");
3070 if (channel <= stack->b_num) {
3071 for (i = 0; i < stack->b_num; i++) {
3072 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
3073 cb_log(0,port,"Requested channel:%d on port:%d is already in use\n",channel, port);
3078 cb_log(0,port,"Requested channel:%d is out of bounds on port:%d\n",channel, port);
3083 int maxnum=inout&&!stack->pri&&!stack->ptp?stack->b_num+1:stack->b_num;
3084 for (i = 0; i <maxnum; i++) {
3085 if (!stack->bc[i].in_use) {
3086 /* 3. channel on bri means CW*/
3087 if (!stack->pri && i==stack->b_num)
3090 prepare_bc(&stack->bc[i], channel);
3091 return &stack->bc[i];
3095 cb_log(1,port,"There is no free channel on port (%d)\n",port);
3100 cb_log(0,port,"Port is not configured (%d)\n",port);
3105 static char *fac2str (enum FacFunction func)
3111 { Fac_None, "Fac_None" },
3112 { Fac_CD, "Fac_CD"},
3117 for (i=0; i < sizeof(arr)/sizeof( struct arr_el) ; i ++)