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, int dec)
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 int bnums=stack->pri?stack->b_num:stack->b_num-1;
450 for (i = bnums; i >=0; i--) {
451 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
452 if (!stack->channels[i]) {
453 cb_log (3, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
460 for (i = 0; i <= bnums; i++) {
461 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
462 if (!stack->channels[i]) {
463 cb_log (3, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
471 cb_log (1, stack->port, " !! NO FREE CHAN IN STACK\n");
472 dump_chan_list(stack);
477 static int empty_chan_in_stack(struct misdn_stack *stack, int channel)
479 if (channel<=0 || channel>MAX_BCHANS) {
480 cb_log(0,stack?stack->port:0, "empty_chan_in_stack: cannot empty channel %d\n",channel);
484 cb_log (4, stack?stack->port:0, "empty_chan_in_stack: %d\n",channel);
485 stack->channels[channel-1] = 0;
486 dump_chan_list(stack);
490 char *bc_state2str(enum bchannel_state state) {
493 struct bchan_state_s {
495 enum bchannel_state s;
497 {"BCHAN_CLEANED", BCHAN_CLEANED },
498 {"BCHAN_EMPTY", BCHAN_EMPTY},
499 {"BCHAN_SETUP", BCHAN_SETUP},
500 {"BCHAN_SETUPED", BCHAN_SETUPED},
501 {"BCHAN_ACTIVE", BCHAN_ACTIVE},
502 {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
503 {"BCHAN_BRIDGE", BCHAN_BRIDGE},
504 {"BCHAN_BRIDGED", BCHAN_BRIDGED},
505 {"BCHAN_RELEASE", BCHAN_RELEASE},
506 {"BCHAN_RELEASED", BCHAN_RELEASED},
507 {"BCHAN_CLEAN", BCHAN_CLEAN},
508 {"BCHAN_CLEAN_REQUEST", BCHAN_CLEAN_REQUEST},
509 {"BCHAN_ERROR", BCHAN_ERROR}
512 for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
513 if ( states[i].s == state)
519 void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
521 cb_log(5,bc->port,"BC_STATE_CHANGE: l3id:%x from:%s to:%s\n",
523 bc_state2str(bc->bc_state),
524 bc_state2str(state) );
527 case BCHAN_ACTIVATED:
528 if (bc->next_bc_state == BCHAN_BRIDGED) {
529 misdn_join_conf(bc, bc->conf_id);
530 bc->next_bc_state = BCHAN_EMPTY;
539 static void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
541 cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
542 bc_state2str(bc->next_bc_state),
543 bc_state2str(state) );
545 bc->next_bc_state=state;
549 static void empty_bc(struct misdn_bchannel *bc)
560 bc->sending_complete = 0;
562 bc->restart_channel=0;
566 bc->need_more_infos = 0;
578 bc->curptx=0; bc->curprx=0;
580 bc->crypt_key[0] = 0;
585 bc->dnumplan=NUMPLAN_UNKNOWN;
586 bc->onumplan=NUMPLAN_UNKNOWN;
587 bc->rnumplan=NUMPLAN_UNKNOWN;
588 bc->cpnnumplan=NUMPLAN_UNKNOWN;
593 bc->early_bconnect = 1;
599 bc->ec_deftaps = 128;
602 bc->AOCD_need_export = 0;
608 bc->pres=0 ; /* screened */
610 bc->evq=EVENT_NOTHING;
612 bc->progress_coding=0;
613 bc->progress_location=0;
614 bc->progress_indicator=0;
616 /** Set Default Bearer Caps **/
617 bc->capability=INFO_CAPABILITY_SPEECH;
618 bc->law=INFO_CODEC_ALAW;
629 bc->infos_pending[0] = 0;
638 bc->fac_in.Function = Fac_None;
639 bc->fac_out.Function = Fac_None;
641 bc->te_choose_channel = 0;
645 static int clean_up_bc(struct misdn_bchannel *bc)
648 unsigned char buff[32];
649 struct misdn_stack * stack;
651 cb_log(3, bc?bc->port:0, "$$$ CLEANUP CALLED pid:%d\n", bc?bc->pid:-1);
654 stack=get_stack_by_bc(bc);
656 if (!stack) return -1;
658 switch (bc->bc_state ) {
660 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
667 cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
669 manager_ec_disable(bc);
671 manager_bchannel_deactivate(bc);
673 mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
676 bc_state_change(bc, BCHAN_CLEANED);
683 static void clear_l3(struct misdn_stack *stack)
687 for (i=0; i<=stack->b_num; i++) {
688 if (global_state == MISDN_INITIALIZED) {
689 cb_event(EVENT_CLEANUP, &stack->bc[i], NULL);
690 empty_chan_in_stack(stack,i+1);
691 empty_bc(&stack->bc[i]);
692 clean_up_bc(&stack->bc[i]);
698 static int set_chan_in_stack(struct misdn_stack *stack, int channel)
701 cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
702 dump_chan_list(stack);
703 if (channel >=1 && channel <= MAX_BCHANS) {
704 if (!stack->channels[channel-1])
705 stack->channels[channel-1] = 1;
707 cb_log(4,stack->port,"channel already in use:%d\n", channel );
711 cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
719 static int chan_in_stack_free(struct misdn_stack *stack, int channel)
721 if (stack->channels[channel-1])
729 static int newteid=0;
731 #define MAXPROCS 0x100
733 static int misdn_lib_get_l1_down(struct misdn_stack *stack)
737 act.prim = PH_DEACTIVATE | REQUEST;
738 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
744 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
750 static int misdn_lib_get_l2_down(struct misdn_stack *stack)
753 if (stack->ptp && (stack->nt) ) {
756 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
758 if (stack->nst.manager_l3(&stack->nst, dmsg))
764 act.prim = DL_RELEASE| REQUEST;
765 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
769 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
776 static int misdn_lib_get_l1_up(struct misdn_stack *stack)
780 act.prim = PH_ACTIVATE | REQUEST;
781 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
787 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
791 int misdn_lib_get_l2_up(struct misdn_stack *stack)
794 if (stack->ptp && (stack->nt) ) {
797 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
799 if (stack->nst.manager_l3(&stack->nst, dmsg))
805 act.prim = DL_ESTABLISH | REQUEST;
806 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
810 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
817 static int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack)
821 act.prim = DL_ESTABLISH | REQUEST;
822 act.addr = (stack->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
826 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
831 static int misdn_lib_get_short_status(struct misdn_stack *stack)
836 act.prim = MGR_SHORTSTATUS | REQUEST;
838 act.addr = (stack->upper_id | MSG_BROADCAST) ;
840 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
843 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
848 static int create_process (int midev, struct misdn_bchannel *bc) {
852 struct misdn_stack *stack=get_stack_by_bc(bc);
855 if (!find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, 0))
857 /*bc->channel=free_chan;*/
859 if (set_chan_in_stack(stack ,bc->channel)<0) return -1;
861 cb_log(4,stack->port, " --> found channel: %d\n", bc->channel);
863 for (i=0; i <= MAXPROCS; i++)
864 if (stack->procids[i]==0) break;
867 cb_log(0, stack->port, "Couldnt Create New ProcId.\n");
874 ncr.prim = CC_NEW_CR | REQUEST;
876 ncr.addr = (stack->upper_id | FLG_MSG_DOWN) ;
882 cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
885 if (stack->ptp || bc->te_choose_channel) {
886 /* we know exactly which channels are in use */
887 if (!find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, 0))
889 /*bc->channel=free_chan;*/
890 cb_log(2,stack->port, " --> found channel: %d\n", bc->channel);
892 if (set_chan_in_stack(stack ,bc->channel)<0) return -1;
894 /* other phones could have made a call also on this port (ptmp) */
899 /* if we are in te-mode, we need to create a process first */
900 if (newteid++ > 0xffff)
903 l3_id = (entity<<16) | newteid;
904 /* preparing message */
905 ncr.prim = CC_NEW_CR | REQUEST;
907 ncr.addr = (stack->upper_id | FLG_MSG_DOWN) ;
914 cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
916 mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
923 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
930 int setup_bc(struct misdn_bchannel *bc)
932 unsigned char buff[1025];
938 struct misdn_stack *stack=get_stack_by_bc(bc);
941 cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
945 int midev=stack->midev;
946 int channel=bc->channel-1-(bc->channel>16);
947 int b_stid=stack->b_stids[channel>=0?channel:0];
950 switch (bc->bc_state) {
954 cb_log(4, stack->port, "$$$ bc already upsetted stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
958 cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
961 cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
963 bc_state_change(bc,BCHAN_ERROR);
972 memset(&li, 0, sizeof(li));
977 li.st = bc->b_stid; /* given idx */
984 if ( bc->hdlc || bc->nodsp) {
985 cb_log(4, stack->port,"setup_bc: without dsp\n");
987 int l = sizeof(li.name);
988 strncpy(li.name, "B L3", l);
991 li.pid.layermask = ISDN_LAYER((3));
992 li.pid.protocol[3] = ISDN_PID_L3_B_USER;
996 cb_log(4, stack->port,"setup_bc: with dsp\n");
998 int l = sizeof(li.name);
999 strncpy(li.name, "B L4", l);
1002 li.pid.layermask = ISDN_LAYER((4));
1003 li.pid.protocol[4] = ISDN_PID_L4_B_USER
1009 ret = mISDN_new_layer(midev, &li);
1011 cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
1013 bc_state_change(bc,BCHAN_ERROR);
1017 bc->layer_id = li.id;
1020 memset(&pid, 0, sizeof(pid));
1024 cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
1027 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
1028 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1029 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1030 pid.protocol[3] = ISDN_PID_L3_B_USER;
1031 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
1033 } else if ( bc->hdlc ) {
1034 cb_log(2, stack->port," --> HDLC Mode\n");
1035 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
1036 pid.protocol[2] = ISDN_PID_L2_B_TRANS ;
1037 pid.protocol[3] = ISDN_PID_L3_B_USER;
1038 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
1040 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
1041 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1042 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1043 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1044 pid.protocol[4] = ISDN_PID_L4_B_USER;
1045 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1049 ret = mISDN_set_stack(midev, bc->b_stid, &pid);
1052 cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
1054 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1056 bc_state_change(bc,BCHAN_ERROR);
1057 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1061 ret = mISDN_get_setstack_ind(midev, bc->layer_id);
1064 cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
1065 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1067 bc_state_change(bc,BCHAN_ERROR);
1068 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1072 ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
1074 bc->addr = ret>0? ret : 0;
1077 cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
1078 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1080 bc_state_change(bc,BCHAN_ERROR);
1081 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1085 manager_bchannel_activate(bc);
1087 bc_state_change(bc,BCHAN_ACTIVATED);
1095 int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime)
1097 unsigned char buff[1025];
1098 iframe_t *frm = (iframe_t *)buff;
1103 cb_log(8, port, "Init.BC %d.\n",bidx);
1105 memset(bc, 0,sizeof(struct misdn_bchannel));
1107 bc->send_lock=malloc(sizeof(struct send_lock));
1109 pthread_mutex_init(&bc->send_lock->lock, NULL);
1112 int l = sizeof(bc->msn);
1113 strncpy(bc->msn,msn, l);
1119 bc_state_change(bc, BCHAN_CLEANED);
1121 bc->port=stack->port;
1122 bc->nt=stack->nt?1:0;
1125 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
1127 if (!ibuf) return -1;
1129 clear_ibuffer( ibuf);
1131 ibuf->rsem=malloc(sizeof(sem_t));
1135 if (sem_init(ibuf->rsem,1,0)<0)
1136 sem_init(ibuf->rsem,0,0);
1144 stack_info_t *stinf;
1145 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
1147 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1151 stinf = (stack_info_t *)&frm->data.p;
1153 cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
1161 struct misdn_stack* stack_init( int midev, int port, int ptp )
1164 unsigned char buff[1025];
1165 iframe_t *frm = (iframe_t *)buff;
1166 stack_info_t *stinf;
1170 struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
1171 if (!stack ) return NULL;
1174 cb_log(8, port, "Init. Stack.\n");
1176 memset(stack,0,sizeof(struct misdn_stack));
1178 for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
1184 stack->holding=NULL;
1187 msg_queue_init(&stack->downqueue);
1188 msg_queue_init(&stack->upqueue);
1190 /* query port's requirements */
1191 ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1193 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1197 stinf = (stack_info_t *)&frm->data.p;
1199 stack->d_stid = stinf->id;
1200 stack->b_num = stinf->childcnt;
1202 for (i=0; i<=stinf->childcnt; i++)
1203 stack->b_stids[i] = stinf->child[i];
1205 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1206 case ISDN_PID_L0_TE_S0:
1209 case ISDN_PID_L0_NT_S0:
1210 cb_log(8, port, "NT Stack\n");
1216 case ISDN_PID_L0_TE_U:
1218 case ISDN_PID_L0_NT_U:
1220 case ISDN_PID_L0_TE_UP2:
1222 case ISDN_PID_L0_NT_UP2:
1225 case ISDN_PID_L0_TE_E1:
1226 cb_log(8, port, "TE S2M Stack\n");
1230 case ISDN_PID_L0_NT_E1:
1231 cb_log(8, port, "TE S2M Stack\n");
1237 cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1242 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) {
1253 cb_log(8, port, "Init. Stack.\n");
1255 memset(&li, 0, sizeof(li));
1257 int l = sizeof(li.name);
1258 strncpy(li.name,nt?"net l2":"user l4", l);
1263 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1264 li.pid.layermask = ISDN_LAYER((nt?2:4));
1265 li.st = stack->d_stid;
1268 ret = mISDN_new_layer(midev, &li);
1270 cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1275 stack->upper_id = li.id;
1276 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
1279 cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
1283 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3);
1284 if (stack->lower_id < 0) {
1285 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1289 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1290 if (stack->upper_id < 0) {
1291 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
1295 cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id);
1298 /* create nst (nt-mode only) */
1301 memset(&stack->nst, 0, sizeof(net_stack_t));
1302 memset(&stack->mgr, 0, sizeof(manager_t));
1304 stack->mgr.nst = &stack->nst;
1305 stack->nst.manager = &stack->mgr;
1307 stack->nst.l3_manager = handle_event_nt;
1308 stack->nst.device = midev;
1309 stack->nst.cardnr = port;
1310 stack->nst.d_stid = stack->d_stid;
1312 stack->nst.feature = FEATURE_NET_HOLD;
1314 stack->nst.feature |= FEATURE_NET_PTP;
1316 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1318 stack->nst.l1_id = stack->lower_id;
1319 stack->nst.l2_id = stack->upper_id;
1321 msg_queue_init(&stack->nst.down_queue);
1323 Isdnl2Init(&stack->nst);
1324 Isdnl3Init(&stack->nst);
1329 /*assume L1 is up, we'll get DEACTIVATES soon, for non
1337 misdn_lib_get_short_status(stack);
1339 misdn_lib_get_l1_up(stack);
1340 if (!stack->ptp) misdn_lib_get_l1_up(stack);
1341 misdn_lib_get_l2_up(stack);
1345 misdn_lib_get_short_status(stack);
1346 misdn_lib_get_l1_up(stack);
1347 misdn_lib_get_l2_up(stack);
1351 cb_log(8,0,"stack_init: port:%d lowerId:%x upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
1357 void stack_destroy(struct misdn_stack* stack)
1363 cleanup_Isdnl2(&stack->nst);
1364 cleanup_Isdnl3(&stack->nst);
1367 if (stack->lower_id)
1368 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1370 if (stack->upper_id)
1371 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1375 static struct misdn_stack * find_stack_by_addr(int addr)
1377 struct misdn_stack *stack;
1379 for (stack=glob_mgr->stack_list;
1381 stack=stack->next) {
1382 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
1390 static struct misdn_stack * find_stack_by_port(int port)
1392 struct misdn_stack *stack;
1394 for (stack=glob_mgr->stack_list;
1397 if (stack->port == port) return stack;
1402 static struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
1404 struct misdn_stack *stack;
1406 for (stack=glob_mgr->stack_list;
1409 if ( &stack->mgr == mgr_nt) return stack;
1414 static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1417 for (i=0; i<=stack->b_num; i++) {
1418 if ( (stack->bc[i].l3_id & mask) == (l3id & mask)) return &stack->bc[i] ;
1420 return stack_holder_find(stack,l3id);
1424 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1427 for (i=0; i<=stack->b_num; i++) {
1428 if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
1430 return stack_holder_find(stack,l3id);
1433 static struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
1436 for (i=0; i<=stack->b_num; i++) {
1437 if (stack->bc[i].holded ) return &stack->bc[i] ;
1443 static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1445 struct misdn_stack* stack;
1448 for (stack=glob_mgr->stack_list;
1450 stack=stack->next) {
1451 for (i=0; i<=stack->b_num; i++) {
1452 if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) || stack->bc[i].layer_id== addr ) {
1453 return &stack->bc[i];
1461 struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
1463 struct misdn_stack* stack;
1466 for (stack=glob_mgr->stack_list;
1468 stack=stack->next) {
1469 for (i=0; i<=stack->b_num; i++) {
1470 if ( stack->bc[i].conf_id==confid ) {
1471 return &stack->bc[i];
1479 static struct misdn_bchannel *find_bc_by_channel(int port, int channel)
1481 struct misdn_stack* stack=find_stack_by_port(port);
1484 if (!stack) return NULL;
1486 for (i=0; i<=stack->b_num; i++) {
1487 if ( stack->bc[i].channel== channel ) {
1488 return &stack->bc[i];
1499 static int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1501 struct misdn_stack *stack=get_stack_by_bc(bc);
1507 case EVENT_CONNECT_ACKNOWLEDGE:
1509 if ( !misdn_cap_is_speech(bc->capability)) {
1510 int ret=setup_bc(bc);
1511 if (ret == -EINVAL){
1512 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1519 if ( *bc->crypt_key ) {
1520 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);
1521 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1523 case EVENT_ALERTING:
1524 case EVENT_PROGRESS:
1525 case EVENT_PROCEEDING:
1526 case EVENT_SETUP_ACKNOWLEDGE:
1532 if (bc->channel == 0xff) {
1533 if (!find_free_chan_in_stack(stack, bc, 0, 0)) {
1534 cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
1536 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1541 if (bc->channel >0 && bc->channel<255) {
1542 int ret=set_chan_in_stack(stack ,bc->channel);
1543 if (event == EVENT_SETUP && ret<0){
1544 /* empty bchannel */
1547 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1554 case EVENT_RELEASE_COMPLETE:
1560 } else { /** NT MODE **/
1566 static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1568 if (!stack) return -1;
1570 switch (frm->prim) {
1571 case CC_NEW_CR|INDICATION:
1572 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1574 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1, 0);
1576 cb_log(0, stack->port, " --> !! lib: No free channel!\n");
1580 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1581 bc->l3_id=frm->dinfo;
1583 case CC_NEW_CR|CONFIRM:
1585 case CC_NEW_CR|REQUEST:
1587 case CC_RELEASE_CR|REQUEST:
1589 case CC_RELEASE_CR|CONFIRM:
1591 case CC_RELEASE_CR|INDICATION:
1592 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
1594 struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
1595 struct misdn_bchannel dummybc;
1598 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1599 memset (&dummybc,0,sizeof(dummybc));
1600 dummybc.port=stack->port;
1601 dummybc.l3_id=frm->dinfo;
1602 dummybc.nt=stack->nt;
1607 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
1609 empty_chan_in_stack(stack,bc->channel);
1612 bc->need_disconnect=0;
1614 bc->need_release_complete=0;
1616 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1620 dump_chan_list(stack);
1622 if (bc->stack_holder) {
1623 cb_log(4,stack->port, "REMOVEING Holder\n");
1624 stack_holder_remove( stack, bc);
1630 cb_log(4, stack->port, "BC with dinfo: %x not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
1642 /*Emptys bc if it's reserved (no SETUP out yet)*/
1643 void misdn_lib_release(struct misdn_bchannel *bc)
1645 struct misdn_stack *stack=get_stack_by_bc(bc);
1648 cb_log(1,0,"misdn_release: No Stack found\n");
1652 if (bc->channel>0) {
1653 empty_chan_in_stack(stack,bc->channel);
1662 int misdn_lib_get_port_up (int port)
1664 struct misdn_stack *stack;
1666 for (stack=glob_mgr->stack_list;
1668 stack=stack->next) {
1670 if (stack->port == port) {
1673 misdn_lib_get_l1_up(stack);
1675 misdn_lib_get_l2_up(stack);
1684 int misdn_lib_get_port_down (int port)
1685 { /* Pull Down L1 */
1686 struct misdn_stack *stack;
1687 for (stack=glob_mgr->stack_list;
1689 stack=stack->next) {
1690 if (stack->port == port) {
1692 misdn_lib_get_l2_down(stack);
1693 misdn_lib_get_l1_down(stack);
1700 int misdn_lib_port_up(int port, int check)
1702 struct misdn_stack *stack;
1705 for (stack=glob_mgr->stack_list;
1707 stack=stack->next) {
1709 if (stack->port == port) {
1711 if (stack->blocked) {
1712 cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
1718 if (stack->l1link && stack->l2link) {
1721 cb_log(1,port, "Port Down L2:%d L1:%d\n",
1722 stack->l2link, stack->l1link);
1726 if ( !check || stack->l1link )
1729 cb_log(1,port, "Port down PMP\n");
1741 handle_event_nt(void *dat, void *arg)
1743 manager_t *mgr = (manager_t *)dat;
1744 msg_t *msg = (msg_t *)arg;
1745 mISDNuser_head_t *hh;
1748 struct misdn_stack *stack=find_stack_by_mgr(mgr);
1754 hh=(mISDNuser_head_t*)msg->data;
1757 cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1760 case CC_RETRIEVE|INDICATION:
1762 iframe_t frm; /* fake te frm to add callref to global callreflist */
1763 frm.dinfo = hh->dinfo;
1765 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1767 frm.prim = CC_NEW_CR|INDICATION;
1769 if (handle_cr( stack, &frm)< 0) {
1771 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1772 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1773 stack->nst.manager_l3(&stack->nst, dmsg);
1778 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1779 struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
1780 cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
1783 cb_log(4, stack->port, "REMOVEING Holder\n");
1785 /*swap the backup to our new channel back*/
1786 stack_holder_remove(stack, hold_bc);
1787 memcpy(bc,hold_bc,sizeof(struct misdn_bchannel));
1797 case CC_SETUP|CONFIRM:
1799 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1800 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1801 cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n",l3id );
1803 if (!bc) { cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
1804 cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1806 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1811 case CC_SETUP|INDICATION:
1813 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1, 0);
1818 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1819 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1820 stack->nst.manager_l3(&stack->nst, dmsg);
1825 cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
1826 bc->l3_id=hh->dinfo;
1830 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
1833 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1835 if ( !misdn_cap_is_speech(bc->capability)) {
1836 int ret=setup_bc(bc);
1837 if (ret == -EINVAL){
1838 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1847 case CC_ALERTING|INDICATION:
1848 case CC_PROCEEDING|INDICATION:
1849 case CC_SETUP_ACKNOWLEDGE|INDICATION:
1850 if(!stack->ptp) break;
1851 case CC_CONNECT|INDICATION:
1854 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1858 cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
1860 cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
1861 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1862 stack->nst.manager_l3(&stack->nst, dmsg);
1867 int ret=setup_bc(bc);
1868 if (ret == -EINVAL){
1869 cb_log(0,bc->port,"handle_event_nt: setup_bc failed\n");
1870 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1875 case CC_DISCONNECT|INDICATION:
1877 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1879 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1881 int myprocid=bc->l3_id&0x0000ffff;
1882 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1883 cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
1890 case CC_FACILITY|INDICATION:
1892 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1894 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1896 int myprocid=bc->l3_id&0x0000ffff;
1897 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1898 cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1904 case CC_RELEASE_COMPLETE|INDICATION:
1907 case CC_SUSPEND|INDICATION:
1910 cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
1911 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1912 stack->nst.manager_l3(&stack->nst, dmsg);
1917 case CC_RESUME|INDICATION:
1920 case CC_RELEASE|CONFIRM:
1923 case CC_RELEASE|INDICATION:
1926 case CC_RELEASE_CR|INDICATION:
1928 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1929 struct misdn_bchannel dummybc;
1930 iframe_t frm; /* fake te frm to remove callref from global callreflist */
1931 frm.dinfo = hh->dinfo;
1933 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1935 frm.prim = CC_RELEASE_CR|INDICATION;
1936 cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
1937 /** removing procid **/
1939 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1940 memset (&dummybc,0,sizeof(dummybc));
1941 dummybc.port=stack->port;
1942 dummybc.l3_id=hh->dinfo;
1943 dummybc.nt=stack->nt;
1948 if ( (bc->l3_id & 0xff00) == 0xff00) {
1949 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
1950 stack->procids[bc->l3_id&0xff] = 0 ;
1953 else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
1955 if (handle_cr(stack, &frm)<0) {
1963 case CC_NEW_CR|INDICATION:
1964 /* Got New CR for bchan, for now I handle this one in */
1965 /* connect_ack, Need to be changed */
1967 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1968 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1969 if (!bc) { cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
1970 if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
1971 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
1972 stack->procids[bc->l3_id&0xff] = 0 ;
1974 cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID is %x\n",l3id );
1977 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1983 case DL_ESTABLISH | INDICATION:
1984 case DL_ESTABLISH | CONFIRM:
1986 cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
1988 if (stack->ptp && stack->l2link) {
1989 cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
1990 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2002 case DL_RELEASE | INDICATION:
2003 case DL_RELEASE | CONFIRM:
2006 cb_log(3 , stack->port, "%% GOT L2 DeActivate Info.\n");
2008 if (stack->l2upcnt>3) {
2009 cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attemps!!!\n");
2012 if (stack->nt) misdn_lib_reinit_nt_stack(stack->port);
2014 if (stack->l1link) {
2015 misdn_lib_get_l2_up(stack);
2021 cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
2032 /* Parse Events and fire_up to App. */
2033 struct misdn_bchannel *bc;
2034 struct misdn_bchannel dummybc;
2036 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
2038 bc=find_bc_by_l3id(stack, hh->dinfo);
2042 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
2043 memset (&dummybc,0,sizeof(dummybc));
2044 dummybc.port=stack->port;
2045 dummybc.l3_id=hh->dinfo;
2046 dummybc.nt=stack->nt;
2050 isdn_msg_parse_event(msgs_g,msg,bc, 1);
2054 if (bc->channel<=0 || bc->channel==0xff) {
2055 bc->channel=find_free_chan_in_stack(stack,bc, 0,0);
2057 goto ERR_NO_CHANNEL;
2058 } else if (!stack->ptp)
2059 cb_log(3,stack->port," --> PTMP but channel requested\n");
2061 int ret=set_chan_in_stack(stack, bc->channel);
2062 if (event==EVENT_SETUP && ret<0){
2063 /* empty bchannel */
2067 goto ERR_NO_CHANNEL;
2071 case EVENT_RELEASE_COMPLETE:
2073 empty_chan_in_stack(stack, bc->channel);
2074 int tmpcause=bc->cause;
2084 if(!isdn_get_info(msgs_g,event,1)) {
2085 cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
2090 cb_log(1, stack->port, "Siemens Busy reject..\n");
2097 cb_event(event, bc, glob_mgr->user_data);
2101 cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo);
2112 static int handle_timers(msg_t* msg)
2114 iframe_t *frm= (iframe_t*)msg->data;
2115 struct misdn_stack *stack;
2118 switch (frm->prim) {
2119 case MGR_INITTIMER | CONFIRM:
2120 case MGR_ADDTIMER | CONFIRM:
2121 case MGR_DELTIMER | CONFIRM:
2122 case MGR_REMOVETIMER | CONFIRM:
2129 if (frm->prim==(MGR_TIMER | INDICATION) ) {
2130 for (stack = glob_mgr->stack_list;
2132 stack = stack->next) {
2135 if (!stack->nt) continue;
2137 it = stack->nst.tlist;
2139 for(it=stack->nst.tlist;
2142 if (it->id == (int)frm->addr)
2147 ret = mISDN_write_frame(stack->midev, msg->data, frm->addr,
2148 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
2149 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
2150 ret = it->function(it->data);
2156 cb_log(0, 0, "Timer Msg without Timer ??\n");
2166 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
2168 bc->generate_tone=1;
2171 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
2173 bc->generate_tone=0;
2177 static int do_tone(struct misdn_bchannel *bc, int len)
2181 if (bc->generate_tone) {
2182 cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
2184 if ( !bc->nojitter ) {
2185 misdn_tx_jitter(bc,len);
2195 #ifdef MISDN_SAVE_DATA
2196 static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2)
2200 sprintf(n1,"/tmp/misdn-rx-%d.raw",id);
2201 sprintf(n2,"/tmp/misdn-tx-%d.raw",id);
2203 FILE *rx=fopen(n1,"a+");
2204 FILE *tx=fopen(n2,"a+");
2207 cb_log(0,0,"Couldn't open files: %s\n",strerror(errno));
2220 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
2222 char buf[4096 + mISDN_HEADER_LEN];
2223 char *data=&buf[mISDN_HEADER_LEN];
2224 iframe_t *txfrm= (iframe_t*)buf;
2227 jlen=cb_jb_empty(bc,data,len);
2230 #ifdef MISDN_SAVE_DATA
2231 misdn_save_data((bc->port*100+bc->channel), data, jlen, bc->bframe, bc->bframe_len);
2233 flip_buf_bits( data, jlen);
2236 cb_log(7,bc->port,"Jitterbuffer Underrun.\n");
2239 txfrm->prim = DL_DATA|REQUEST;
2243 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2246 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2248 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2250 #define MISDN_GEN_SILENCE
2251 #ifdef MISDN_GEN_SILENCE
2252 int cnt=len/TONE_SILENCE_SIZE;
2253 int rest=len%TONE_SILENCE_SIZE;
2256 for (i=0; i<cnt; i++) {
2257 memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE );
2258 data +=TONE_SILENCE_SIZE;
2262 memcpy(data, tone_silence_flip, rest);
2265 txfrm->prim = DL_DATA|REQUEST;
2269 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2272 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2274 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2280 static int handle_bchan(msg_t *msg)
2282 iframe_t *frm= (iframe_t*)msg->data;
2285 struct misdn_bchannel *bc=find_bc_by_addr(frm->addr);
2288 cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
2292 struct misdn_stack *stack=get_stack_by_bc(bc);
2295 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);
2299 switch (frm->prim) {
2301 case MGR_SETSTACK| CONFIRM:
2302 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid);
2305 case MGR_SETSTACK| INDICATION:
2306 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid);
2310 bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer);
2313 if (errno == EAGAIN) {
2318 cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno));
2320 /* we kill the channel later, when we received some
2322 bc->addr= frm->addr;
2323 } else if ( bc->addr < 0) {
2324 cb_log(0, stack->port,"$$$ bc->addr <0 Error:%s\n",strerror(errno));
2328 cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
2333 switch(bc->bc_state) {
2335 bc_state_change(bc,BCHAN_SETUPED);
2338 case BCHAN_CLEAN_REQUEST:
2340 cb_log(0, stack->port," --> STATE WASN'T SETUP (but %s) in SETSTACK|IND pid:%d\n",bc_state2str(bc->bc_state), bc->pid);
2346 case MGR_DELLAYER| INDICATION:
2347 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid);
2350 case MGR_DELLAYER| CONFIRM:
2351 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid);
2359 case PH_ACTIVATE | INDICATION:
2360 case DL_ESTABLISH | INDICATION:
2361 cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid);
2366 case PH_ACTIVATE | CONFIRM:
2367 case DL_ESTABLISH | CONFIRM:
2369 cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid);
2374 case DL_ESTABLISH | REQUEST:
2377 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2382 case DL_RELEASE|REQUEST:
2385 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2390 case PH_DEACTIVATE | INDICATION:
2391 case DL_RELEASE | INDICATION:
2392 cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid);
2397 case PH_DEACTIVATE | CONFIRM:
2398 case DL_RELEASE | CONFIRM:
2399 cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid);
2404 case PH_CONTROL|INDICATION:
2406 unsigned int cont = *((unsigned int *)&frm->data.p);
2408 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);
2410 if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
2411 int dtmf = cont & DTMF_TONE_MASK;
2412 cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
2414 cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
2419 if (cont == BF_REJECT) {
2420 cb_log(4, stack->port, " --> BF REJECT\n");
2424 if (cont == BF_ACCEPT) {
2425 cb_log(4, stack->port, " --> BF ACCEPT\n");
2432 case PH_DATA|REQUEST:
2433 case DL_DATA|REQUEST:
2434 cb_log(0, stack->port, "DL_DATA REQUEST \n");
2441 case PH_DATA|INDICATION:
2442 case DL_DATA|INDICATION:
2444 bc->bframe = (void*)&frm->data.i;
2445 bc->bframe_len = frm->len;
2447 /** Anyway flip the bufbits **/
2448 if ( misdn_cap_is_speech(bc->capability) )
2449 flip_buf_bits(bc->bframe, bc->bframe_len);
2452 if (!bc->bframe_len) {
2453 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2458 if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
2459 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2465 cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
2469 if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
2472 #ifdef MISDN_B_DEBUG
2473 cb_log(0,bc->port,"do_tone START\n");
2475 t=do_tone(bc,frm->len);
2477 #ifdef MISDN_B_DEBUG
2478 cb_log(0,bc->port,"do_tone STOP (%d)\n",t);
2482 if ( misdn_cap_is_speech(bc->capability)) {
2483 if ( !bc->nojitter ) {
2484 #ifdef MISDN_B_DEBUG
2485 cb_log(0,bc->port,"tx_jitter START\n");
2487 misdn_tx_jitter(bc,frm->len);
2488 #ifdef MISDN_B_DEBUG
2489 cb_log(0,bc->port,"tx_jitter STOP\n");
2494 #ifdef MISDN_B_DEBUG
2495 cb_log(0,bc->port,"EVENT_B_DATA START\n");
2498 int i=cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
2499 #ifdef MISDN_B_DEBUG
2500 cb_log(0,bc->port,"EVENT_B_DATA STOP\n");
2504 cb_log(10,stack->port,"cb_event returned <0\n");
2505 /*clean_up_bc(bc);*/
2514 case PH_CONTROL | CONFIRM:
2515 cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
2519 case PH_DATA | CONFIRM:
2520 case DL_DATA|CONFIRM:
2523 cb_log(0, stack->port, "Data confirmed\n");
2528 case DL_DATA|RESPONSE:
2530 cb_log(0, stack->port, "Data response\n");
2541 static int handle_frm_nt(msg_t *msg)
2543 iframe_t *frm= (iframe_t*)msg->data;
2544 struct misdn_stack *stack;
2547 stack=find_stack_by_addr( frm->addr );
2551 if (!stack || !stack->nt) {
2556 if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
2558 if (nt_err_cnt > 0 ) {
2559 if (nt_err_cnt < 100) {
2561 cb_log(0, stack->port, "NT Stack sends us error: %d \n", err);
2562 } else if (nt_err_cnt < 105){
2563 cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
2576 static int handle_frm(msg_t *msg)
2578 iframe_t *frm = (iframe_t*) msg->data;
2580 struct misdn_stack *stack=find_stack_by_addr(frm->addr);
2582 if (!stack || stack->nt) {
2586 cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim);
2589 struct misdn_bchannel *bc;
2590 int ret=handle_cr(stack, frm);
2593 cb_log(3,stack?stack->port:0,"handle_frm: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
2603 bc=find_bc_by_l3id(stack, frm->dinfo);
2607 enum event_e event = isdn_msg_get_event(msgs_g, msg, 0);
2608 enum event_response_e response=RESPONSE_OK;
2610 isdn_msg_parse_event(msgs_g,msg,bc, 0);
2612 /** Preprocess some Events **/
2613 int ret=handle_event(bc, event, frm);
2615 cb_log(0,stack->port,"couldn't handle event\n");
2619 /* shoot up event to App: */
2620 cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2622 if(!isdn_get_info(msgs_g,event,0))
2623 cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2625 response=cb_event(event, bc, glob_mgr->user_data);
2627 if (event == EVENT_SETUP) {
2629 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2631 cb_log(0, stack->port, "TOTALY IGNORING SETUP \n");
2634 case RESPONSE_IGNORE_SETUP:
2635 /* I think we should send CC_RELEASE_CR, but am not sure*/
2638 case RESPONSE_RELEASE_SETUP:
2639 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
2641 empty_chan_in_stack(stack, bc->channel);
2643 bc_state_change(bc,BCHAN_CLEANED);
2645 cb_log(0, stack->port, "GOT IGNORE SETUP\n");
2650 cb_log(4, stack->port, "GOT SETUP OK\n");
2659 if (event == EVENT_RELEASE_COMPLETE) {
2660 /* release bchannel only after we've anounced the RELEASE_COMPLETE */
2662 empty_chan_in_stack(stack,bc->channel);
2663 int tmpcause=bc->cause;
2664 int tmp_out_cause=bc->out_cause;
2667 bc->out_cause=tmp_out_cause;
2671 cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
2679 cb_log(0, stack->port, " --> Didn't find BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
2680 struct misdn_bchannel dummybc;
2681 memset (&dummybc,0,sizeof(dummybc));
2682 dummybc.port=stack->port;
2683 dummybc.l3_id=frm->dinfo;
2689 cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
2694 static int handle_l1(msg_t *msg)
2696 iframe_t *frm = (iframe_t*) msg->data;
2697 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2700 if (!stack) return 0 ;
2702 switch (frm->prim) {
2703 case PH_ACTIVATE | CONFIRM:
2704 case PH_ACTIVATE | INDICATION:
2705 cb_log (3, stack->port, "L1: PH L1Link Up!\n");
2710 if (stack->nst.l1_l2(&stack->nst, msg))
2714 misdn_lib_get_l2_up(stack);
2719 for (i=0;i<=stack->b_num; i++) {
2720 if (stack->bc[i].evq != EVENT_NOTHING) {
2721 cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
2722 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
2723 stack->bc[i].evq=EVENT_NOTHING;
2729 case PH_ACTIVATE | REQUEST:
2731 cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2734 case PH_DEACTIVATE | REQUEST:
2736 cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2739 case PH_DEACTIVATE | CONFIRM:
2740 case PH_DEACTIVATE | INDICATION:
2741 cb_log (3, stack->port, "L1: PH L1Link Down! \n");
2743 for (i=0; i<=stack->b_num; i++) {
2744 if (global_state == MISDN_INITIALIZED) {
2745 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2750 if (stack->nst.l1_l2(&stack->nst, msg))
2764 static int handle_l2(msg_t *msg)
2766 iframe_t *frm = (iframe_t*) msg->data;
2768 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2776 case DL_ESTABLISH | REQUEST:
2777 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2779 case DL_RELEASE | REQUEST:
2780 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2783 case DL_ESTABLISH | INDICATION:
2784 case DL_ESTABLISH | CONFIRM:
2786 cb_log (3, stack->port, "L2: L2Link Up! \n");
2787 if (stack->ptp && stack->l2link) {
2788 cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n");
2789 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2797 case DL_RELEASE | INDICATION:
2798 case DL_RELEASE | CONFIRM:
2800 cb_log (3, stack->port, "L2: L2Link Down! \n");
2811 static int handle_mgmt(msg_t *msg)
2813 iframe_t *frm = (iframe_t*) msg->data;
2815 if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2816 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2821 struct misdn_stack * stack=find_stack_by_addr(frm->addr);
2824 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
2825 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
2835 case MGR_SHORTSTATUS | INDICATION:
2836 case MGR_SHORTSTATUS | CONFIRM:
2837 cb_log(5, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
2839 switch (frm->dinfo) {
2840 case SSTATUS_L1_ACTIVATED:
2841 cb_log(3, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
2845 case SSTATUS_L1_DEACTIVATED:
2846 cb_log(3, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2852 case SSTATUS_L2_ESTABLISHED:
2853 cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
2857 case SSTATUS_L2_RELEASED:
2858 cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
2866 case MGR_SETSTACK | INDICATION:
2867 cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2870 case MGR_DELLAYER | CONFIRM:
2871 cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
2878 if ( (frm->prim & 0x0f0000) == 0x0f0000) {
2879 cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
2888 static msg_t *fetch_msg(int midev)
2890 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2894 cb_log(0, 0, "fetch_msg: alloc msg failed !!");
2899 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
2903 free_msg(msg); /* danger, cauz usualy freeing in main_loop */
2904 cb_log(6,0,"Got empty Msg..\n");
2909 if (errno == EAGAIN) {
2910 /*we wait for mISDN here*/
2911 cb_log(4,0,"mISDN_read wants us to wait\n");
2916 cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno);
2920 if (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION)))
2921 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 );
2926 void misdn_lib_isdn_l1watcher(int port)
2928 struct misdn_stack *stack;
2930 for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
2934 cb_log(4, port, "Checking L1 State\n");
2935 if (!stack->l1link) {
2936 cb_log(4, port, "L1 State Down, trying to get it up again\n");
2937 misdn_lib_get_short_status(stack);
2938 misdn_lib_get_l1_up(stack);
2939 misdn_lib_get_l2_up(stack);
2944 static void misdn_lib_isdn_event_catcher(void *arg)
2946 struct misdn_lib *mgr = arg;
2947 int zero_frm=0 , fff_frm=0 ;
2948 int midev= mgr->midev;
2952 msg_t *msg = fetch_msg(midev);
2958 frm = (iframe_t*) msg->data;
2960 /** When we make a call from NT2Ast we get this frames **/
2961 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
2967 cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
2972 /** I get this sometimes after setup_bc **/
2973 if (frm->len == 0 && frm->dinfo == 0 && frm->prim == 0xffffffff ) {
2979 cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
2984 manager_isdn_handler(frm, msg);
2990 /** App Interface **/
2994 iframe_t *frm=(iframe_t*)buff;
2995 int midev=mISDN_open();
2998 memset(buff,0,1025);
3000 if (midev<=0) return midev;
3002 /* create entity for layer 3 TE-mode */
3003 mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
3004 ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
3006 if (ret < mISDN_HEADER_LEN) {
3008 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
3012 entity = frm->dinfo & 0xffff ;
3021 void te_lib_destroy(int midev)
3024 mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
3026 cb_log(4, 0, "Entetity deleted\n");
3028 cb_log(4, 0, "midev closed\n");
3033 void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
3035 holded_bc->holded=0;
3038 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
3040 struct misdn_stack *stack;
3043 for (stack=glob_mgr->stack_list;
3045 stack=stack->next) {
3046 for (i=0; i<=stack->b_num; i++)
3047 if (stack->bc[i].pid == pid) return &stack->bc[i];
3053 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
3055 struct misdn_stack *stack=get_stack_by_bc(bc);
3056 return find_bc_holded(stack);
3061 static void prepare_bc(struct misdn_bchannel*bc, int channel)
3063 bc->channel = channel;
3064 bc->channel_preselected = channel?1:0;
3066 bc->need_disconnect=1;
3068 bc->need_release_complete=1;
3071 if (++mypid>5000) mypid=1;
3081 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout, int dec)
3083 struct misdn_stack *stack;
3086 if (channel < 0 || channel > MAX_BCHANS) {
3087 cb_log(0,port,"Requested channel out of bounds (%d)\n",channel);
3091 for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
3093 if (stack->port == port) {
3094 if (stack->blocked) {
3095 cb_log(0,port,"Port is blocked\n");
3100 if (channel <= stack->b_num) {
3101 for (i = 0; i < stack->b_num; i++) {
3102 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
3103 cb_log(0,port,"Requested channel:%d on port:%d is already in use\n",channel, port);
3108 cb_log(0,port,"Requested channel:%d is out of bounds on port:%d\n",channel, port);
3113 int maxnum=inout&&!stack->pri&&!stack->ptp?stack->b_num+1:stack->b_num;
3116 for (i = maxnum-1; i>=0; i--) {