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
14 #include "isdn_lib_intern.h"
15 #include <mISDNuser/isdn_debug.h>
18 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id);
19 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id);
22 void misdn_free_ibuffer(void *ibuf)
24 free_ibuffer((ibuffer_t*)ibuf);
28 void misdn_clear_ibuffer(void *ibuf)
30 clear_ibuffer( (ibuffer_t*)ibuf);
33 void *misdn_init_ibuffer(int len)
35 return init_ibuffer(len);
38 int misdn_ibuf_freecount(void *buf)
40 return ibuf_freecount( (ibuffer_t*)buf);
43 int misdn_ibuf_usedcount(void *buf)
45 return ibuf_usedcount( (ibuffer_t*)buf);
48 void misdn_ibuf_memcpy_r(char *to, void *buf, int len)
50 ibuf_memcpy_r( to, (ibuffer_t*)buf, len);
53 void misdn_ibuf_memcpy_w(void *buf, char *from, int len)
55 ibuf_memcpy_w((ibuffer_t*)buf, from, len);
58 struct misdn_stack* get_misdn_stack( void );
61 int misdn_lib_is_ptp(int port)
63 struct misdn_stack *stack=get_misdn_stack();
64 for ( ; stack; stack=stack->next) {
65 if (stack->port == port) return stack->ptp;
71 struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc)
73 struct misdn_stack *stack=get_misdn_stack();
77 for ( ; stack; stack=stack->next) {
79 for (i=0; i <stack->b_num; i++) {
80 if ( bc->port == stack->port) return stack;
88 void get_show_stack_details(int port, char *buf)
90 struct misdn_stack *stack=get_misdn_stack();
92 for ( ; stack; stack=stack->next) {
93 if (stack->port == port) break;
97 sprintf(buf, "* Stack Addr:%x Port %d Type %s Prot. %s L2Link %s L1Link:%s", stack->upper_id, stack->port, stack->nt?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN");
106 static int nt_err_cnt =0 ;
113 static enum global_states global_state=MISDN_INITIALIZING;
116 #include <mISDNuser/net_l2.h>
117 #include <mISDNuser/tone.h>
119 #include <semaphore.h>
123 #include "isdn_lib.h"
130 pthread_t event_thread;
131 pthread_t event_handler_thread;
136 msg_queue_t activatequeue;
140 struct misdn_stack *stack_list;
144 #define ECHOCAN_ON 123
145 #define ECHOCAN_OFF 124
148 #define MISDN_DEBUG 0
150 void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
152 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
154 int setup_bc(struct misdn_bchannel *bc);
156 int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
158 int misdn_lib_port_restart(int port);
160 extern struct isdn_msg msgs_g[];
162 #define ISDN_PID_L3_B_USER 0x430000ff
163 #define ISDN_PID_L4_B_USER 0x440000ff
165 /* #define MISDN_IBUF_SIZE 1024 */
166 #define MISDN_IBUF_SIZE 512
168 /* Fine Tuning of Inband Signalling time */
169 #define TONE_ALERT_CNT 41 /* 1 Sec */
170 #define TONE_ALERT_SILENCE_CNT 200 /* 4 Sec */
172 #define TONE_BUSY_CNT 20 /* ? */
173 #define TONE_BUSY_SILENCE_CNT 48 /* ? */
177 static struct misdn_lib *glob_mgr;
179 unsigned char tone_425_flip[TONE_425_SIZE];
180 unsigned char tone_silence_flip[TONE_SILENCE_SIZE];
182 static void misdn_lib_isdn_event_catcher(void *arg);
183 static int handle_event_nt(void *dat, void *arg);
186 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
187 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
188 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
190 /* from isdn_lib.h */
191 int init_bc(struct misdn_stack * stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime);
192 struct misdn_stack* stack_init(int midev, int port, int ptp);
193 void stack_te_destroy(struct misdn_stack* stack);
195 int te_lib_init( void ) ; /* returns midev */
196 void te_lib_destroy(int midev) ;
197 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
198 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc);
199 unsigned char * manager_flip_buf_bits ( unsigned char * buf , int len);
200 void manager_ph_control_block(struct misdn_bchannel *bc, long c1, void *c2, int c2_len);
201 void manager_clean_bc(struct misdn_bchannel *bc );
202 void manager_bchannel_setup (struct misdn_bchannel *bc);
203 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
205 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *frm, int nt);
206 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *frm, int nt);
207 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *frm, struct misdn_bchannel *bc, int nt);
208 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt);
209 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt);
210 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
212 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
213 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
217 static char *bearer2str(int cap) {
218 static char *bearers[]={
227 case INFO_CAPABILITY_SPEECH:
230 case INFO_CAPABILITY_AUDIO_3_1K:
233 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
236 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
246 static char flip_table[256];
248 void init_flip_bits(void)
252 for (i = 0 ; i < 256 ; i++) {
253 unsigned char sample = 0 ;
254 for (k = 0; k<8; k++) {
255 if ( i & 1 << k ) sample |= 0x80 >> k;
257 flip_table[i] = sample;
261 unsigned char * flip_buf_bits ( unsigned char * buf , int len)
266 for (i = 0 ; i < len; i++) {
267 buf[i] = flip_table[buf[i]];
276 msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
283 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
288 printf("cannot allocate memory, trying again...\n");
292 printf("cannot allocate memory, system overloaded.\n");
298 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
306 size = sizeof(Q931_info_t)+2;
310 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
315 dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
318 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
319 frm = (iframe_t *)dmsg->data;
322 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
328 if (!i) printf("cannot allocate memory, trying again...\n");
332 printf("cannot allocate memory, system overloaded.\n");
337 int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
340 frm = (iframe_t *)dmsg->data;
341 struct misdn_stack *stack=get_stack_by_bc(bc);
344 cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
348 frm->addr = (stack->upper_id | FLG_MSG_DOWN);
351 frm->dinfo = bc->l3_id;
353 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
355 mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
366 int misdn_cap_is_speech(int cap)
367 /** Poor mans version **/
369 if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
370 (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
374 int misdn_inband_avail(struct misdn_bchannel *bc)
377 /*if ! early_bconnect we have never inband available*/
378 if ( ! bc->early_bconnect ) return 0;
380 switch (bc->progress_indicator) {
381 case INFO_PI_INBAND_AVAILABLE:
382 case INFO_PI_CALL_NOT_E2E_ISDN:
383 case INFO_PI_CALLED_NOT_ISDN:
392 void dump_chan_list(struct misdn_stack *stack)
396 for (i=0; i <stack->b_num; i++) {
397 cb_log(8, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
404 static int find_free_chan_in_stack(struct misdn_stack *stack, int channel)
408 if (channel < 0 || channel > MAX_BCHANS) {
409 cb_log(4, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
415 for (i = 0; i < stack->b_num; i++) {
416 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
417 if (!stack->channels[i]) {
418 cb_log (4, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
419 stack->channels[i] = 1;
425 cb_log (4, stack->port, " !! NO FREE CHAN IN STACK\n");
426 dump_chan_list(stack);
431 int empty_chan_in_stack(struct misdn_stack *stack, int channel)
433 cb_log (4, stack?stack->port:0, " --> empty chan %d\n",channel);
434 stack->channels[channel-1] = 0;
435 dump_chan_list(stack);
439 char *bc_state2str(enum bchannel_state state) {
442 struct bchan_state_s {
444 enum bchannel_state s;
446 {"BCHAN_CLEANED", BCHAN_CLEANED },
447 {"BCHAN_EMPTY", BCHAN_EMPTY},
448 {"BCHAN_SETUP", BCHAN_SETUP},
449 {"BCHAN_SETUPED", BCHAN_SETUPED},
450 {"BCHAN_ACTIVE", BCHAN_ACTIVE},
451 {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
452 {"BCHAN_BRIDGE", BCHAN_BRIDGE},
453 {"BCHAN_BRIDGED", BCHAN_BRIDGED},
454 {"BCHAN_RELEASE", BCHAN_RELEASE},
455 {"BCHAN_RELEASED", BCHAN_RELEASED},
456 {"BCHAN_CLEAN", BCHAN_CLEAN},
457 {"BCHAN_ERROR", BCHAN_ERROR}
460 for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
461 if ( states[i].s == state)
467 void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
469 cb_log(3,bc->port,"BC_STATE_CHANGE: from:%s to:%s\n",
470 bc_state2str(bc->bc_state),
471 bc_state2str(state) );
474 case BCHAN_ACTIVATED:
475 if (bc->next_bc_state == BCHAN_BRIDGED) {
476 misdn_join_conf(bc, bc->conf_id);
477 bc->next_bc_state = BCHAN_EMPTY;
486 void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
488 cb_log(3,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
489 bc_state2str(bc->next_bc_state),
490 bc_state2str(state) );
492 bc->next_bc_state=state;
496 void empty_bc(struct misdn_bchannel *bc)
505 bc->need_more_infos = 0;
517 bc->curptx=0; bc->curprx=0;
519 bc->crypt_key[0] = 0;
524 bc->dnumplan=NUMPLAN_UNKNOWN;
525 bc->onumplan=NUMPLAN_UNKNOWN;
526 bc->rnumplan=NUMPLAN_UNKNOWN;
527 bc->cpnnumplan=NUMPLAN_UNKNOWN;
532 bc->early_bconnect = 1;
535 bc->ec_deftaps = 128;
536 bc->ec_whenbridged = 0;
544 bc->pres=0 ; /* screened */
546 bc->evq=EVENT_NOTHING;
548 bc->progress_coding=0;
549 bc->progress_location=0;
550 bc->progress_indicator=0;
552 /** Set Default Bearer Caps **/
553 bc->capability=INFO_CAPABILITY_SPEECH;
554 bc->law=INFO_CODEC_ALAW;
565 bc->infos_pending[0] = 0;
572 bc->fac_type=FACILITY_NONE;
574 bc->te_choose_channel = 0;
578 bc_state_change(bc,BCHAN_EMPTY);
579 bc_next_state_change(bc,BCHAN_EMPTY);
583 int clean_up_bc(struct misdn_bchannel *bc)
586 unsigned char buff[32];
587 struct misdn_stack * stack;
589 cb_log(2, 0, "$$$ CLEANUP CALLED\n");
592 stack=get_stack_by_bc(bc);
594 if (!stack) return -1;
596 switch (bc->bc_state ) {
599 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
602 case BCHAN_ACTIVATED:
603 cb_log(2, stack->port, "$$$ bc still active, deactivatiing .. stid :%x\n", bc->b_stid);
604 manager_bchannel_deactivate(bc);
608 cb_log(2, stack->port, "$$$ bc still bridged\n");
613 cb_log(5, stack->port, "$$$ Cleaning up bc with stid :%x\n", bc->b_stid);
615 if ( misdn_cap_is_speech(bc->capability) && bc->ec_enable) {
616 manager_ec_disable(bc);
619 cb_log(2, stack->port, "$$$ CLEARING STACK\n");
620 /*mISDN_clear_stack(stack->midev,bc->b_stid);*/
622 mISDN_write_frame(stack->midev, buff, bc->addr|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
625 bc_state_change(bc, BCHAN_CLEANED);
632 void clear_l3(struct misdn_stack *stack)
635 for (i=0; i<stack->b_num; i++) {
636 if (global_state == MISDN_INITIALIZED) {
637 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
638 empty_chan_in_stack(stack,i+1);
639 empty_bc(&stack->bc[i]);
640 clean_up_bc(&stack->bc[i]);
647 int set_chan_in_stack(struct misdn_stack *stack, int channel)
649 stack->channels[channel-1] = 1;
654 int chan_in_stack_free(struct misdn_stack *stack, int channel)
656 if (stack->channels[channel-1])
664 static int newteid=0;
666 #define MAXPROCS 0x100
668 int misdn_lib_get_l1_down(struct misdn_stack *stack)
672 act.prim = PH_DEACTIVATE | REQUEST;
673 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
679 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
685 int misdn_lib_get_l2_down(struct misdn_stack *stack)
688 if (stack->ptp && (stack->nt) ) {
691 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
693 if (stack->nst.manager_l3(&stack->nst, dmsg))
699 act.prim = DL_RELEASE| REQUEST;
700 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
704 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
711 int misdn_lib_get_l1_up(struct misdn_stack *stack)
715 act.prim = PH_ACTIVATE | REQUEST;
716 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
722 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
726 int misdn_lib_get_l2_up(struct misdn_stack *stack)
729 if (stack->ptp && (stack->nt) ) {
732 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
734 if (stack->nst.manager_l3(&stack->nst, dmsg))
740 act.prim = DL_ESTABLISH | REQUEST;
741 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
745 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
751 int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack)
755 act.prim = DL_ESTABLISH | REQUEST;
756 act.addr = (stack->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
760 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
764 int misdn_lib_get_l2_status(struct misdn_stack *stack)
768 act.prim = DL_ESTABLISH | REQUEST;
770 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
774 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
777 int misdn_lib_get_short_status(struct misdn_stack *stack)
782 act.prim = MGR_SHORTSTATUS | REQUEST;
784 act.addr = (stack->upper_id | MSG_BROADCAST) ;
786 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
789 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
794 static int create_process (int midev, struct misdn_bchannel *bc) {
798 struct misdn_stack *stack=get_stack_by_bc(bc);
802 free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0);
803 if (!free_chan) return -1;
804 bc->channel=free_chan;
806 cb_log(0,stack->port, " --> found channel: %d\n",free_chan);
808 for (i=0; i <= MAXPROCS; i++)
809 if (stack->procids[i]==0) break;
812 cb_log(-1, stack->port, "Couldnt Create New ProcId.\n");
819 ncr.prim = CC_NEW_CR | REQUEST;
821 ncr.addr = (stack->upper_id | FLG_MSG_DOWN) ;
827 if (mypid>5000) mypid=0;
830 cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
833 if (stack->ptp || bc->te_choose_channel) {
834 /* we know exactly which channels are in use */
835 free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0);
836 if (!free_chan) return -1;
837 bc->channel=free_chan;
838 cb_log(0,stack->port, " --> found channel: %d\n",free_chan);
840 /* other phones could have made a call also on this port (ptmp) */
845 /* if we are in te-mode, we need to create a process first */
846 if (newteid++ > 0xffff)
849 l3_id = (entity<<16) | newteid;
850 /* preparing message */
851 ncr.prim = CC_NEW_CR | REQUEST;
853 ncr.addr = (stack->upper_id | FLG_MSG_DOWN) ;
860 if (mypid>5000) mypid=0;
863 cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
865 mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
872 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
878 int setup_bc(struct misdn_bchannel *bc)
880 unsigned char buff[1025];
886 struct misdn_stack *stack=get_stack_by_bc(bc);
889 cb_log(-1, bc->port, "setup_bc: NO STACK FOUND!!\n");
893 int midev=stack->midev;
894 int channel=bc->channel-1-(bc->channel>16);
895 int b_stid=stack->b_stids[channel>=0?channel:0];
898 if ( bc->bc_state != BCHAN_CLEANED) {
899 cb_log(4, stack->port, "$$$ bc already upsetted stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
903 cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
906 cb_log(-1, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
908 bc_state_change(bc,BCHAN_ERROR);
917 memset(&li, 0, sizeof(li));
922 li.st = bc->b_stid; /* given idx */
929 if ( bc->hdlc || bc->nodsp) {
930 cb_log(4, stack->port,"setup_bc: without dsp\n");
932 int l = sizeof(li.name);
933 strncpy(li.name, "B L3", l);
936 li.pid.layermask = ISDN_LAYER((3));
937 li.pid.protocol[3] = ISDN_PID_L3_B_USER;
941 cb_log(4, stack->port,"setup_bc: with dsp\n");
943 int l = sizeof(li.name);
944 strncpy(li.name, "B L4", l);
947 li.pid.layermask = ISDN_LAYER((4));
948 li.pid.protocol[4] = ISDN_PID_L4_B_USER
954 ret = mISDN_new_layer(midev, &li);
956 cb_log(-1, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
958 bc_state_change(bc,BCHAN_ERROR);
962 bc->layer_id = li.id;
965 memset(&pid, 0, sizeof(pid));
969 cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
972 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
973 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
974 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
975 pid.protocol[3] = ISDN_PID_L3_B_USER;
976 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
978 } else if ( bc->hdlc ) {
979 cb_log(2, stack->port," --> HDLC Mode\n");
981 bc->ack_hdlc=(sem_t*)malloc(sizeof(sem_t));
982 if ( sem_init((sem_t*)bc->ack_hdlc, 1, 0)<0 )
983 sem_init((sem_t*)bc->ack_hdlc, 0, 0);
986 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
987 pid.protocol[2] = ISDN_PID_L2_B_TRANS ;
988 pid.protocol[3] = ISDN_PID_L3_B_USER;
989 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
991 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
992 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
993 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
994 pid.protocol[3] = ISDN_PID_L3_B_DSP;
995 pid.protocol[4] = ISDN_PID_L4_B_USER;
996 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1000 ret = mISDN_set_stack(midev, bc->b_stid, &pid);
1003 cb_log(5, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
1005 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1007 bc_state_change(bc,BCHAN_ERROR);
1012 bc_state_change(bc,BCHAN_SETUP);
1015 /*manager_bchannel_deactivate(bc);*/
1024 int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime)
1026 unsigned char buff[1025];
1027 iframe_t *frm = (iframe_t *)buff;
1032 cb_log(4, port, "Init.BC %d.\n",bidx);
1034 memset(bc, 0,sizeof(struct misdn_bchannel));
1037 int l = sizeof(bc->msn);
1038 strncpy(bc->msn,msn, l);
1044 bc_state_change(bc, BCHAN_CLEANED);
1046 bc->port=stack->port;
1047 bc->nt=stack->nt?1:0;
1050 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
1052 if (!ibuf) return -1;
1054 clear_ibuffer( ibuf);
1056 ibuf->rsem=malloc(sizeof(sem_t));
1060 if (sem_init(ibuf->rsem,1,0)<0)
1061 sem_init(ibuf->rsem,0,0);
1069 stack_info_t *stinf;
1070 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
1072 cb_log(-1, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1076 stinf = (stack_info_t *)&frm->data.p;
1078 cb_log(4, port, " --> Child %x\n",stinf->child[bidx]);
1086 struct misdn_stack* stack_init( int midev, int port, int ptp )
1089 unsigned char buff[1025];
1090 iframe_t *frm = (iframe_t *)buff;
1091 stack_info_t *stinf;
1095 struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
1096 if (!stack ) return NULL;
1099 cb_log(4, port, "Init. Stack.\n");
1101 memset(stack,0,sizeof(struct misdn_stack));
1103 for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
1109 stack->holding=NULL;
1112 msg_queue_init(&stack->downqueue);
1113 msg_queue_init(&stack->upqueue);
1115 /* query port's requirements */
1116 ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1118 cb_log(-1, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1122 stinf = (stack_info_t *)&frm->data.p;
1124 stack->d_stid = stinf->id;
1125 stack->b_num = stinf->childcnt;
1127 for (i=0; i<stinf->childcnt; i++)
1128 stack->b_stids[i] = stinf->child[i];
1130 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1131 case ISDN_PID_L0_TE_S0:
1134 case ISDN_PID_L0_NT_S0:
1135 cb_log(4, port, "NT Stack\n");
1140 case ISDN_PID_L0_TE_U:
1142 case ISDN_PID_L0_NT_U:
1144 case ISDN_PID_L0_TE_UP2:
1146 case ISDN_PID_L0_NT_UP2:
1148 case ISDN_PID_L0_TE_E1:
1149 cb_log(4, port, "TE S2M Stack\n");
1153 case ISDN_PID_L0_NT_E1:
1154 cb_log(4, port, "TE S2M Stack\n");
1160 cb_log(-1, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1165 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) {
1176 cb_log(4, port, "Init. Stack.\n");
1178 memset(&li, 0, sizeof(li));
1180 int l = sizeof(li.name);
1181 strncpy(li.name,nt?"net l2":"user l4", l);
1186 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1187 li.pid.layermask = ISDN_LAYER((nt?2:4));
1188 li.st = stack->d_stid;
1191 ret = mISDN_new_layer(midev, &li);
1193 cb_log(-1, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1198 stack->upper_id = li.id;
1199 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
1202 cb_log(-1,port,"Cannot register layer %d of this port.\n", nt?2:4);
1206 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3);
1207 if (stack->lower_id < 0) {
1208 cb_log(-1, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1212 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1213 if (stack->upper_id < 0) {
1214 cb_log(-1, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
1218 cb_log(4, port, "NT Stacks upper_id %x\n",stack->upper_id);
1221 /* create nst (nt-mode only) */
1224 memset(&stack->nst, 0, sizeof(net_stack_t));
1225 memset(&stack->mgr, 0, sizeof(manager_t));
1227 stack->mgr.nst = &stack->nst;
1228 stack->nst.manager = &stack->mgr;
1230 stack->nst.l3_manager = handle_event_nt;
1231 stack->nst.device = midev;
1232 stack->nst.cardnr = port;
1233 stack->nst.d_stid = stack->d_stid;
1235 stack->nst.feature = FEATURE_NET_HOLD;
1237 stack->nst.feature |= FEATURE_NET_PTP;
1239 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1241 stack->nst.l1_id = stack->lower_id;
1242 stack->nst.l2_id = stack->upper_id;
1244 msg_queue_init(&stack->nst.down_queue);
1246 Isdnl2Init(&stack->nst);
1247 Isdnl3Init(&stack->nst);
1252 /*assume L1 is up, we'll get DEACTIVATES soon, for non
1257 misdn_lib_get_short_status(stack);
1258 misdn_lib_get_l1_up(stack);
1259 misdn_lib_get_l2_up(stack);
1263 cb_log(1,0,"stack_init: port:%d lowerId:%x upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
1269 void stack_te_destroy(struct misdn_stack* stack)
1274 if (stack->lower_id)
1275 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1277 if (stack->upper_id)
1278 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1282 struct misdn_stack * find_stack_by_addr(int addr)
1284 struct misdn_stack *stack;
1286 for (stack=glob_mgr->stack_list;
1288 stack=stack->next) {
1289 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
1297 struct misdn_stack * find_stack_by_port(int port)
1299 struct misdn_stack *stack;
1301 for (stack=glob_mgr->stack_list;
1304 if (stack->port == port) return stack;
1309 struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
1311 struct misdn_stack *stack;
1313 for (stack=glob_mgr->stack_list;
1316 if ( &stack->mgr == mgr_nt) return stack;
1321 struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1324 for (i=0; i<stack->b_num; i++) {
1325 if ( (stack->bc[i].l3_id & mask) == (l3id & mask)) return &stack->bc[i] ;
1327 return stack_holder_find(stack,l3id);
1331 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1334 for (i=0; i<stack->b_num; i++) {
1335 if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
1337 return stack_holder_find(stack,l3id);
1340 struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
1343 for (i=0; i<stack->b_num; i++) {
1344 if (stack->bc[i].holded ) return &stack->bc[i] ;
1350 struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1352 struct misdn_stack* stack;
1356 for (stack=glob_mgr->stack_list;
1358 stack=stack->next) {
1360 for (i=0; i< stack->b_num; i++) {
1362 if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) || stack->bc[i].layer_id== addr ) {
1363 return &stack->bc[i];
1374 struct misdn_bchannel *find_bc_by_channel(int port, int channel)
1376 struct misdn_stack* stack=find_stack_by_port(port);
1379 if (!stack) return NULL;
1381 for (i=0; i< stack->b_num; i++) {
1382 if ( stack->bc[i].channel== channel ) {
1383 return &stack->bc[i];
1394 int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1396 struct misdn_stack *stack=get_stack_by_bc(bc);
1402 case EVENT_CONNECT_ACKNOWLEDGE:
1404 if ( !misdn_cap_is_speech(bc->capability)) {
1405 int ret=setup_bc(bc);
1406 if (ret == -EINVAL){
1407 cb_log(-1,bc->port,"send_event: setup_bc failed\n");
1411 manager_bchannel_activate(bc);
1415 if ( *bc->crypt_key ) {
1416 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);
1417 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1420 if (bc->channel>0 && bc->channel<255)
1421 set_chan_in_stack(stack, bc->channel);
1423 case EVENT_ALERTING:
1424 case EVENT_PROGRESS:
1425 case EVENT_PROCEEDING:
1426 case EVENT_SETUP_ACKNOWLEDGE:
1429 if (bc->channel == 0xff) {
1430 bc->channel=find_free_chan_in_stack(stack, 0);
1432 cb_log(-1, stack->port, "Any Channel Requested, but we have no more!!\n");
1438 int ret=setup_bc(bc);
1439 if (ret == -EINVAL){
1440 cb_log(-1,bc->port,"handle_event: setup_bc failed\n");
1441 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1449 } else { /** NT MODE **/
1455 int handle_new_process(struct misdn_stack *stack, iframe_t *frm)
1458 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0);
1462 cb_log(-1, stack->port, " --> !! lib: No free channel!\n");
1466 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1467 bc->l3_id=frm->dinfo;
1469 if (mypid>5000) mypid=0;
1474 int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1476 if (!stack) return -1;
1478 switch (frm->prim) {
1479 case CC_NEW_CR|INDICATION:
1480 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1481 if (handle_new_process(stack, frm) <0)
1484 case CC_NEW_CR|CONFIRM:
1486 case CC_NEW_CR|REQUEST:
1488 case CC_RELEASE_CR|REQUEST:
1490 case CC_RELEASE_CR|CONFIRM:
1492 case CC_RELEASE_CR|INDICATION:
1493 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
1495 struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
1496 struct misdn_bchannel dummybc;
1499 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1500 memset (&dummybc,0,sizeof(dummybc));
1501 dummybc.port=stack->port;
1502 dummybc.l3_id=frm->dinfo;
1507 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
1508 empty_chan_in_stack(stack,bc->channel);
1511 dump_chan_list(stack);
1513 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1514 if (bc->stack_holder) {
1515 cb_log(4,stack->port, "REMOVEING Holder\n");
1516 stack_holder_remove( stack, bc);
1522 cb_log(4, stack->port, "BC with dinfo: %x not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
1534 /*Emptys bc if it's reserved (no SETUP out yet)*/
1535 void misdn_lib_release(struct misdn_bchannel *bc)
1537 struct misdn_stack *stack=get_stack_by_bc(bc);
1540 cb_log(1,0,"misdn_release: No Stack found\n");
1544 if (bc->channel>=0) {
1545 empty_chan_in_stack(stack,bc->channel);
1554 int misdn_lib_get_port_up (int port)
1556 struct misdn_stack *stack;
1558 for (stack=glob_mgr->stack_list;
1560 stack=stack->next) {
1562 if (stack->port == port) {
1565 misdn_lib_get_l1_up(stack);
1567 misdn_lib_get_l2_up(stack);
1576 int misdn_lib_get_port_down (int port)
1577 { /* Pull Down L1 */
1578 struct misdn_stack *stack;
1579 for (stack=glob_mgr->stack_list;
1581 stack=stack->next) {
1582 if (stack->port == port) {
1584 misdn_lib_get_l2_down(stack);
1585 misdn_lib_get_l1_down(stack);
1592 int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data)
1595 case FACILITY_CALLDEFLECT:
1596 strcpy(bc->out_fac.calldeflect_nr,(char*)data);
1599 cb_log(1,bc?bc->port:0,"We don't handle this facility yet: %d\n",fac);
1603 bc->out_fac_type=fac;
1605 misdn_lib_send_event(bc,EVENT_FACILITY);
1610 int misdn_lib_port_up(int port, int check)
1612 struct misdn_stack *stack;
1615 for (stack=glob_mgr->stack_list;
1617 stack=stack->next) {
1619 if ( !stack->ptp && !check) return 1;
1621 if (stack->port == port) {
1625 cb_log(-1,port, "Port down [%s]\n",
1626 stack->ptp?"PP":"PMP");
1637 handle_event_nt(void *dat, void *arg)
1639 manager_t *mgr = (manager_t *)dat;
1640 msg_t *msg = (msg_t *)arg;
1641 mISDNuser_head_t *hh;
1644 struct misdn_stack *stack=find_stack_by_mgr(mgr);
1650 hh=(mISDNuser_head_t*)msg->data;
1653 cb_log(4, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1656 case CC_RETRIEVE|INDICATION:
1658 iframe_t frm; /* fake te frm to add callref to global callreflist */
1659 frm.dinfo = hh->dinfo;
1661 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1663 frm.prim = CC_NEW_CR|INDICATION;
1665 if (handle_cr( stack, &frm)< 0) {
1667 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1668 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1669 stack->nst.manager_l3(&stack->nst, dmsg);
1674 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1675 cb_event(EVENT_NEW_BC, bc, glob_mgr->user_data);
1676 struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
1678 cb_log(4, stack->port, "REMOVEING Holder\n");
1679 stack_holder_remove(stack, hold_bc);
1687 case CC_SETUP|CONFIRM:
1689 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1690 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1691 cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n",l3id );
1693 if (!bc) { cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
1694 cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1696 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1701 case CC_SETUP|INDICATION:
1703 iframe_t frm; /* fake te frm to add callref to global callreflist */
1704 frm.dinfo = hh->dinfo;
1705 frm.addr=stack->upper_id;
1706 frm.prim = CC_NEW_CR|INDICATION;
1708 if (handle_cr(stack, &frm)< 0) {
1710 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1711 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1712 stack->nst.manager_l3(&stack->nst, dmsg);
1719 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
1722 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1724 if ( !misdn_cap_is_speech(bc->capability)) {
1725 int ret=setup_bc(bc);
1726 if (ret == -EINVAL){
1727 cb_log(-1,bc->port,"send_event: setup_bc failed\n");
1732 manager_bchannel_activate(bc);
1738 case CC_CONNECT|INDICATION:
1739 case CC_ALERTING|INDICATION:
1740 case CC_PROCEEDING|INDICATION:
1741 case CC_SETUP_ACKNOWLEDGE|INDICATION:
1744 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1748 cb_log(-1, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
1750 cb_log(-1, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
1751 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1752 stack->nst.manager_l3(&stack->nst, dmsg);
1757 int ret=setup_bc(bc);
1758 if (ret == -EINVAL){
1759 cb_log(-1,bc->port,"handle_event_nt: setup_bc failed\n");
1760 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1764 case CC_DISCONNECT|INDICATION:
1766 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1768 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1770 int myprocid=bc->l3_id&0x0000ffff;
1771 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1772 cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
1779 case CC_FACILITY|INDICATION:
1781 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1783 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1785 int myprocid=bc->l3_id&0x0000ffff;
1786 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1787 cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1793 case CC_RELEASE_COMPLETE|INDICATION:
1796 case CC_SUSPEND|INDICATION:
1799 cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
1800 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1801 stack->nst.manager_l3(&stack->nst, dmsg);
1806 case CC_RESUME|INDICATION:
1809 case CC_RELEASE|CONFIRM:
1812 case CC_RELEASE|INDICATION:
1815 case CC_RELEASE_CR|INDICATION:
1817 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1818 struct misdn_bchannel dummybc;
1819 iframe_t frm; /* fake te frm to remove callref from global callreflist */
1820 frm.dinfo = hh->dinfo;
1822 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1824 frm.prim = CC_RELEASE_CR|INDICATION;
1825 cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
1826 /** removing procid **/
1828 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1829 memset (&dummybc,0,sizeof(dummybc));
1830 dummybc.port=stack->port;
1831 dummybc.l3_id=hh->dinfo;
1836 if ( (bc->l3_id & 0xff00) == 0xff00) {
1837 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
1838 stack->procids[bc->l3_id&0xff] = 0 ;
1841 else cb_log(-1, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
1843 handle_cr(stack, &frm);
1849 case CC_NEW_CR|INDICATION:
1850 /* Got New CR for bchan, for now I handle this one in */
1851 /* connect_ack, Need to be changed */
1853 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1854 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1855 if (!bc) { cb_log(-1, stack->port, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
1856 if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
1857 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
1858 stack->procids[bc->l3_id&0xff] = 0 ;
1860 cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID is %x\n",l3id );
1863 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1869 case DL_ESTABLISH | INDICATION:
1870 case DL_ESTABLISH | CONFIRM:
1872 cb_log(4, stack->port, "%% GOT L2 Activate Info.\n");
1880 case DL_RELEASE | INDICATION:
1881 case DL_RELEASE | CONFIRM:
1883 cb_log(4, stack->port, "%% GOT L2 DeActivate Info.\n");
1894 /* Parse Events and fire_up to App. */
1895 struct misdn_bchannel *bc;
1896 struct misdn_bchannel dummybc;
1898 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
1900 bc=find_bc_by_l3id(stack, hh->dinfo);
1904 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
1905 memset (&dummybc,0,sizeof(dummybc));
1906 dummybc.port=stack->port;
1907 dummybc.l3_id=hh->dinfo;
1911 isdn_msg_parse_event(msgs_g,msg,bc, 1);
1913 if(!isdn_get_info(msgs_g,event,1)) {
1914 cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1919 cb_log(1, stack->port, "Siemens Busy reject..\n");
1926 cb_event(event, bc, glob_mgr->user_data);
1930 cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1941 int handle_timers(msg_t* msg)
1943 iframe_t *frm= (iframe_t*)msg->data;
1944 struct misdn_stack *stack;
1947 switch (frm->prim) {
1948 case MGR_INITTIMER | CONFIRM:
1949 case MGR_ADDTIMER | CONFIRM:
1950 case MGR_DELTIMER | CONFIRM:
1951 case MGR_REMOVETIMER | CONFIRM:
1958 if (frm->prim==(MGR_TIMER | INDICATION) ) {
1959 for (stack = glob_mgr->stack_list;
1961 stack = stack->next) {
1964 if (!stack->nt) continue;
1966 it = stack->nst.tlist;
1968 for(it=stack->nst.tlist;
1971 if (it->id == (int)frm->addr)
1976 ret = mISDN_write_frame(stack->midev, msg->data, frm->addr,
1977 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
1978 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
1979 ret = it->function(it->data);
1985 cb_log(-1, 0, "Timer Msg without Timer ??\n");
1995 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
1997 bc->generate_tone=1;
2000 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
2002 bc->generate_tone=0;
2006 static int do_tone(struct misdn_bchannel *bc, int len)
2010 if (bc->generate_tone) {
2011 cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
2013 if ( !bc->nojitter ) {
2014 misdn_tx_jitter(bc,len);
2025 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
2027 char buf[4096 + mISDN_HEADER_LEN];
2028 iframe_t *txfrm= (iframe_t*)buf;
2031 jlen=cb_jb_empty(bc,&buf[mISDN_HEADER_LEN],len);
2036 cb_log(5,bc->port,"Jitterbuffer Underrun.\n");
2039 txfrm->prim = DL_DATA|REQUEST;
2043 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2046 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2048 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2052 int handle_bchan(msg_t *msg)
2054 iframe_t *frm= (iframe_t*)msg->data;
2055 struct misdn_bchannel *bc;
2057 bc=find_bc_by_addr(frm->addr);
2060 cb_log(0,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
2064 struct misdn_stack *stack=get_stack_by_bc(bc);
2067 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);
2071 switch (frm->prim) {
2073 case MGR_SETSTACK| CONFIRM:
2074 cb_log(2, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM \n");
2077 case MGR_SETSTACK| INDICATION:
2078 cb_log(2, stack->port, "BCHAN: MGR_SETSTACK|IND \n");
2081 bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer);
2084 if (errno == EAGAIN) {
2089 cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno));
2091 /* we kill the channel later, when we received some
2093 bc->addr= frm->addr;
2096 cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
2100 if (bc->bc_state != BCHAN_SETUP) {
2101 cb_log(4, stack->port," --> STATE WASN'T SETUP in SETSTACK|IND\n");
2104 bc_state_change(bc,BCHAN_SETUPED);
2107 manager_bchannel_activate(bc);
2111 case MGR_DELLAYER| INDICATION:
2112 cb_log(2, stack->port, "BCHAN: MGR_DELLAYER|IND\n");
2115 case MGR_DELLAYER| CONFIRM:
2116 cb_log(2, stack->port, "BCHAN: MGR_DELLAYER|CNF \n");
2118 bc_state_change(bc,BCHAN_CLEANED);
2123 case PH_ACTIVATE | INDICATION:
2124 case DL_ESTABLISH | INDICATION:
2125 cb_log(4, stack->port, "BCHAN: ACT Ind\n");
2127 bc_state_change(bc,BCHAN_ACTIVATED);
2132 case PH_ACTIVATE | CONFIRM:
2133 case DL_ESTABLISH | CONFIRM:
2135 bc_state_change(bc,BCHAN_ACTIVATED);
2137 cb_log(4, stack->port, "BCHAN: bchan ACT Confirm\n");
2142 case PH_DEACTIVATE | INDICATION:
2143 case DL_RELEASE | INDICATION:
2144 cb_log (4, stack->port, "BCHAN: DeACT Ind\n");
2146 bc_state_change(bc,BCHAN_RELEASED);
2150 case PH_DEACTIVATE | CONFIRM:
2151 case DL_RELEASE | CONFIRM:
2152 cb_log(4, stack->port, "BCHAN: DeACT Conf\n");
2154 bc_state_change(bc,BCHAN_RELEASED);
2158 case PH_CONTROL|INDICATION:
2160 unsigned int cont = *((unsigned int *)&frm->data.p);
2162 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);
2164 if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
2165 int dtmf = cont & DTMF_TONE_MASK;
2166 cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
2168 cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
2173 if (cont == BF_REJECT) {
2174 cb_log(4, stack->port, " --> BF REJECT\n");
2178 if (cont == BF_ACCEPT) {
2179 cb_log(4, stack->port, " --> BF ACCEPT\n");
2186 case PH_DATA|REQUEST:
2187 case DL_DATA|REQUEST:
2188 cb_log(0, stack->port, "DL_DATA REQUEST \n");
2194 case PH_DATA|INDICATION:
2195 case DL_DATA|INDICATION:
2197 bc->bframe = (void*)&frm->data.i;
2198 bc->bframe_len = frm->len;
2200 /** Anyway flip the bufbits **/
2201 if ( misdn_cap_is_speech(bc->capability) )
2202 flip_buf_bits(bc->bframe, bc->bframe_len);
2205 if (!bc->bframe_len) {
2206 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2211 if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
2212 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2218 cb_log(-1, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
2222 if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
2223 if ( !do_tone(bc, frm->len) ) {
2225 if ( misdn_cap_is_speech(bc->capability)) {
2226 if ( !bc->nojitter ) {
2227 misdn_tx_jitter(bc,frm->len);
2231 int i=cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
2233 cb_log(10,stack->port,"cb_event returned <0\n");
2234 /*clean_up_bc(bc);*/
2243 case PH_CONTROL | CONFIRM:
2244 cb_log(2, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
2248 case PH_DATA | CONFIRM:
2249 case DL_DATA|CONFIRM:
2252 cb_log(-1, stack->port, "Data confirmed\n");
2258 case DL_DATA|RESPONSE:
2260 cb_log(-1, stack->port, "Data response\n");
2271 int handle_frm_nt(msg_t *msg)
2273 iframe_t *frm= (iframe_t*)msg->data;
2274 struct misdn_stack *stack;
2277 stack=find_stack_by_addr( frm->addr );
2281 if (!stack || !stack->nt) {
2285 if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
2287 if (nt_err_cnt > 0 ) {
2288 if (nt_err_cnt < 100) {
2290 cb_log(-1, stack->port, "NT Stack sends us error: %d \n", err);
2291 } else if (nt_err_cnt < 105){
2292 cb_log(-1, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
2305 int handle_frm(msg_t *msg)
2307 iframe_t *frm = (iframe_t*) msg->data;
2309 struct misdn_stack *stack=find_stack_by_addr(frm->addr);
2312 cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim);
2315 if (!stack || stack->nt) {
2320 struct misdn_bchannel *bc;
2321 int ret=handle_cr(stack, frm);
2324 cb_log(3,stack?stack->port:0,"handle_frm: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
2332 bc=find_bc_by_l3id(stack, frm->dinfo);
2335 enum event_e event = isdn_msg_get_event(msgs_g, msg, 0);
2336 enum event_response_e response=RESPONSE_OK;
2338 isdn_msg_parse_event(msgs_g,msg,bc, 0);
2340 /** Preprocess some Events **/
2341 handle_event(bc, event, frm);
2342 /* shoot up event to App: */
2343 cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2345 if(!isdn_get_info(msgs_g,event,0))
2346 cb_log(-1, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2348 response=cb_event(event, bc, glob_mgr->user_data);
2350 if (event == EVENT_SETUP) {
2352 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2354 cb_log(-1, stack->port, "TOTALY IGNORING SETUP \n");
2357 case RESPONSE_IGNORE_SETUP:
2358 /* I think we should send CC_RELEASE_CR, but am not sure*/
2359 empty_chan_in_stack(stack, bc->channel);
2361 cb_log(-1, stack->port, "GOT IGNORE SETUP\n");
2366 cb_log(4, stack->port, "GOT SETUP OK\n");
2375 cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
2383 cb_log(-1, stack->port, "NO BC FOR STACK\n");
2387 cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
2392 int handle_l1(msg_t *msg)
2394 iframe_t *frm = (iframe_t*) msg->data;
2395 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2398 if (!stack) return 0 ;
2400 switch (frm->prim) {
2401 case PH_ACTIVATE | CONFIRM:
2402 case PH_ACTIVATE | INDICATION:
2403 cb_log (1, stack->port, "L1: PH L1Link Up!\n");
2408 if (stack->nst.l1_l2(&stack->nst, msg))
2414 for (i=0;i<stack->b_num; i++) {
2415 if (stack->bc[i].evq != EVENT_NOTHING) {
2416 cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
2417 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
2418 stack->bc[i].evq=EVENT_NOTHING;
2424 case PH_ACTIVATE | REQUEST:
2426 cb_log(1,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2429 case PH_DEACTIVATE | REQUEST:
2431 cb_log(1,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2434 case PH_DEACTIVATE | CONFIRM:
2435 case PH_DEACTIVATE | INDICATION:
2436 cb_log (1, stack->port, "L1: PH L1Link Down! \n");
2438 for (i=0; i<stack->b_num; i++) {
2439 if (global_state == MISDN_INITIALIZED) {
2440 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2445 if (stack->nst.l1_l2(&stack->nst, msg))
2460 int handle_l2(msg_t *msg)
2462 iframe_t *frm = (iframe_t*) msg->data;
2464 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2472 case DL_ESTABLISH | REQUEST:
2473 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2475 case DL_RELEASE | REQUEST:
2476 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2479 case DL_ESTABLISH | INDICATION:
2480 case DL_ESTABLISH | CONFIRM:
2482 cb_log (3, stack->port, "L2: L2Link Up! \n");
2489 case DL_RELEASE | INDICATION:
2490 case DL_RELEASE | CONFIRM:
2492 cb_log (3, stack->port, "L2: L2Link Down! \n");
2503 int handle_mgmt(msg_t *msg)
2505 iframe_t *frm = (iframe_t*) msg->data;
2507 if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2508 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2513 struct misdn_stack * stack=find_stack_by_addr(frm->addr);
2516 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
2517 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
2527 case MGR_SHORTSTATUS | INDICATION:
2528 case MGR_SHORTSTATUS | CONFIRM:
2529 cb_log(2, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
2531 switch (frm->dinfo) {
2532 case SSTATUS_L1_ACTIVATED:
2533 cb_log(1, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
2537 case SSTATUS_L1_DEACTIVATED:
2538 cb_log(1, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2540 /*reopen L1 if down*/
2541 if (stack->l1link==2)
2548 case SSTATUS_L2_ESTABLISHED:
2549 cb_log(1, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
2551 if ( !stack->ptp && !stack->nt )
2555 case SSTATUS_L2_RELEASED:
2556 cb_log(1, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
2564 case MGR_SETSTACK | INDICATION:
2565 cb_log(2, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2568 case MGR_DELLAYER | CONFIRM:
2569 cb_log(2, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
2576 if ( (frm->prim & 0x0f0000) == 0x0f0000) {
2577 cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
2586 msg_t *fetch_msg(int midev)
2588 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2593 cb_log(-1, 0, "fetch_msg: alloc msg failed !!");
2598 FD_SET(midev,&rdfs);
2600 mISDN_select(FD_SETSIZE, &rdfs, NULL, NULL, NULL);
2601 //select(FD_SETSIZE, &rdfs, NULL, NULL, NULL);
2603 if (FD_ISSET(midev, &rdfs)) {
2606 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, 5000);
2610 free_msg(msg); /* danger, cauz usualy freeing in main_loop */
2611 printf ("Got empty Msg?\n");
2616 if (errno == EAGAIN) {
2617 /*we wait for mISDN here*/
2618 cb_log(-1,0,"mISDN_read wants us to wait\n");
2623 cb_log(-1,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno);
2628 printf ("Select timeout\n");
2635 static void misdn_lib_isdn_event_catcher(void *arg)
2637 struct misdn_lib *mgr = arg;
2638 int zero_frm=0 , fff_frm=0 ;
2639 int midev= mgr->midev;
2643 msg_t *msg = fetch_msg(midev);
2649 frm = (iframe_t*) msg->data;
2651 /** When we make a call from NT2Ast we get this frames **/
2652 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
2658 cb_log(-1, port, "*** Alert: %d zero_frms caught\n", zero_frm);
2663 /** I get this sometimes after setup_bc **/
2664 if (frm->len == 0 && frm->dinfo == 0 && frm->prim == 0xffffffff ) {
2670 cb_log(-1, port, "*** Alert: %d fff_frms caught\n", fff_frm);
2675 manager_isdn_handler(frm, msg);
2681 /** App Interface **/
2685 iframe_t *frm=(iframe_t*)buff;
2686 int midev=mISDN_open();
2689 memset(buff,0,1025);
2691 if (midev<=0) return midev;
2693 /* create entity for layer 3 TE-mode */
2694 mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
2695 ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
2697 if (ret < mISDN_HEADER_LEN) {
2699 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
2703 entity = frm->dinfo & 0xffff ;
2712 void te_lib_destroy(int midev)
2715 mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
2717 cb_log(4, 0, "Entetity deleted\n");
2719 cb_log(4, 0, "midev closed\n");
2724 void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
2726 holded_bc->holded=0;
2729 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
2731 struct misdn_stack *stack;
2734 for (stack=glob_mgr->stack_list;
2736 stack=stack->next) {
2737 for (i=0; i<stack->b_num; i++)
2738 if (stack->bc[i].pid == pid) return &stack->bc[i];
2744 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
2746 struct misdn_stack *stack=get_stack_by_bc(bc);
2747 return find_bc_holded(stack);
2752 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel)
2754 struct misdn_stack *stack;
2757 if (channel < 0 || channel > MAX_BCHANS)
2760 for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
2762 if (stack->port == port) {
2764 if (channel <= stack->b_num) {
2765 for (i = 0; i < stack->b_num; i++) {
2766 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
2773 for (i = 0; i < stack->b_num; i++) {
2774 if (!stack->bc[i].in_use) {
2775 stack->bc[i].channel = channel;
2776 stack->bc[i].channel_preselected = channel?1:0;
2777 stack->bc[i].in_use = 1;
2778 return &stack->bc[i];
2788 char *fac2str (enum facility_type type) {
2790 enum facility_type p;
2793 { FACILITY_NONE, "FAC_NONE" },
2794 { FACILITY_CALLDEFLECT, "FAC_CALLDEFLECT"},
2795 { FACILITY_CENTREX, "FAC_CENTREX"}
2800 for (i=0; i < sizeof(arr)/sizeof( struct arr_el) ; i ++)
2801 if ( arr[i].p==type) return arr[i].s;
2803 return "FAC_UNKNOWN";
2806 void misdn_lib_log_ies(struct misdn_bchannel *bc)
2810 struct misdn_stack *stack=get_stack_by_bc(bc);
2814 cb_log(2, stack->port, " --> mode:%s cause:%d ocause:%d rad:%s cad:%s\n", stack->nt?"NT":"TE", bc->cause, bc->out_cause, bc->rad, bc->cad);
2816 cb_log(3, stack->port, " --> facility:%s out_facility:%s\n",fac2str(bc->fac_type),fac2str(bc->out_fac_type));
2818 cb_log(2, stack->port,
2819 " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c cpnnumplan:%c\n",
2821 bc->onumplan>=0?'0'+bc->onumplan:' ',
2822 bc->dnumplan>=0?'0'+bc->dnumplan:' ',
2823 bc->rnumplan>=0?'0'+bc->rnumplan:' ',
2824 bc->cpnnumplan>=0?'0'+bc->cpnnumplan:' '
2826 cb_log(3, stack->port, " --> screen:%d --> pres:%d\n",
2827 bc->screen, bc->pres);
2829 cb_log(2, stack->port, " --> channel:%d caps:%s pi:%x keypad:%s\n", bc->channel, bearer2str(bc->capability),bc->progress_indicator, bc->keypad);
2831 cb_log(3, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
2833 cb_log(3, stack->port, " --> pid:%d addr:%x l3id:%x\n", bc->pid, bc->addr, bc->l3_id);
2834 cb_log(3, stack->port, " --> b_stid:%x layer_id:%x\n", bc->b_stid, bc->layer_id);
2836 cb_log(4, stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
2839 int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
2846 struct misdn_stack *stack=get_stack_by_bc(bc);
2849 cb_log(-1,bc->port,"SENDEVENT: no Stack for event:%s oad:%s dad:%s \n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad);
2853 cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->uperid:%x\n",stack->nt, stack->upper_id);
2855 if ( stack->nt && !stack->l1link) {
2858 cb_log(1, stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
2859 misdn_lib_get_l1_up(stack);
2863 cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s \n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad);
2864 cb_log(1, stack->port, " --> bc_state:%s\n",bc_state2str(bc->bc_state));
2865 misdn_lib_log_ies(bc);
2869 if (create_process(glob_mgr->midev, bc)<0) {
2870 cb_log(-1, stack->port, " No free channel at the moment @ send_event\n");
2878 case EVENT_PROGRESS:
2879 case EVENT_ALERTING:
2880 case EVENT_PROCEEDING:
2881 case EVENT_SETUP_ACKNOWLEDGE:
2882 case EVENT_RETRIEVE_ACKNOWLEDGE:
2884 if (bc->channel <=0 ) { /* else we have the channel already */
2885 bc->channel = find_free_chan_in_stack(stack, 0);
2887 cb_log(-1, stack->port, " No free channel at the moment\n");
2893 /* Its that i generate channels */
2896 if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
2897 int ret=setup_bc(bc);
2898 if (ret == -EINVAL){
2899 cb_log(-1,bc->port,"send_event: setup_bc failed\n");
2904 if ( (event == EVENT_CONNECT ) && misdn_cap_is_speech(bc->capability) ) {
2905 if ( *bc->crypt_key ) {
2906 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);
2908 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
2911 if (!bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
2913 if (bc->ec_enable) manager_ec_enable(bc);
2915 if (bc->txgain != 0) {
2916 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
2917 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
2920 if ( bc->rxgain != 0 ) {
2921 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
2922 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
2927 if (event == EVENT_RETRIEVE_ACKNOWLEDGE) {
2928 manager_bchannel_activate(bc);
2932 case EVENT_HOLD_ACKNOWLEDGE:
2934 struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel));
2935 memcpy(holded_bc,bc,sizeof(struct misdn_bchannel));
2936 holded_bc->holded=1;
2937 stack_holder_add(stack,holded_bc);
2940 if (bc->bc_state == BCHAN_BRIDGED) {
2941 misdn_split_conf(bc,bc->conf_id);
2942 misdn_split_conf(bc->holded_bc,bc->holded_bc->conf_id);
2943 /*bc_state_change(bc,BCHAN_SETUPED);
2944 manager_bchannel_activate(bc);*/
2947 empty_chan_in_stack(stack,bc->channel);
2952 /** we set it up later at RETRIEVE_ACK again.**/
2953 /*holded_bc->upset=0;
2954 holded_bc->active=0;*/
2955 bc_state_change(holded_bc,BCHAN_CLEANED);
2957 cb_event( EVENT_NEW_BC, holded_bc, glob_mgr->user_data);
2964 case EVENT_RELEASE_COMPLETE:
2965 /*we do the cleanup in EVENT_CLEANUP*/
2968 case EVENT_CONNECT_ACKNOWLEDGE:
2970 if (misdn_cap_is_speech(bc->capability)) {
2971 if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
2972 if (bc->ec_enable) manager_ec_enable(bc);
2973 if ( bc->txgain != 0 ) {
2974 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
2975 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
2977 if ( bc->rxgain != 0 ) {
2978 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
2979 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
2988 /* Later we should think about sending bchannel data directly to misdn. */
2989 msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
2990 msg_queue_tail(&stack->downqueue, msg);
2991 sem_post(&glob_mgr->new_msg);
3000 int handle_err(msg_t *msg)
3002 iframe_t *frm = (iframe_t*) msg->data;
3008 cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x\n",frm->prim,frm->dinfo);
3011 cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x (already more than 100 of them)\n",frm->prim,frm->dinfo);
3020 switch (frm->prim) {
3021 case DL_DATA|INDICATION:
3023 int port=(frm->addr&MASTER_ID_MASK) >> 8;
3024 int channel=(frm->addr&CHILD_ID_MASK) >> 16;
3026 /*we flush the read buffer here*/
3028 cb_log(9,0,"BCHAN DATA without BC: addr:%x port:%d channel:%d\n",frm->addr, port,channel);
3034 struct misdn_bchannel *bc=find_bc_by_channel( port , channel);
3037 struct misdn_stack *stack=find_stack_by_port( port );
3040 cb_log(-1,0," --> stack not found\n");
3045 cb_log(-1,0," --> bc not found by channel\n");
3047 misdn_lib_get_l2_down(stack);
3050 misdn_lib_get_l1_down(stack);
3056 cb_log(3,port," --> BC in state:%s\n", bc_state2str(bc->bc_state));
3064 int queue_l2l3(msg_t *msg) {
3065 iframe_t *frm= (iframe_t*)msg->data;
3066 struct misdn_stack *stack;
3069 stack=find_stack_by_addr( frm->addr );
3076 msg_queue_tail(&stack->upqueue, msg);
3077 sem_post(&glob_mgr->new_msg);
3081 int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
3084 if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
3085 cb_log(0,0,"SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
3088 if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
3089 if (handle_bchan(msg))
3093 if (handle_timers(msg))
3096 if (handle_mgmt(msg))
3102 /* Its important to handle l1 AFTER l2 */
3106 /* The L2/L3 will be queued */
3107 if (queue_l2l3(msg))
3110 if (handle_err(msg))
3113 cb_log(-1, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);
3123 int misdn_lib_get_port_info(int port)
3125 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3127 struct misdn_stack *stack=find_stack_by_port(port);
3129 cb_log(-1, port, "misgn_lib_get_port: alloc_msg failed!\n");
3132 frm=(iframe_t*)msg->data;
3134 cb_log(-1, port, "There is no Stack for this port.\n");
3137 /* activate bchannel */
3138 frm->prim = CC_STATUS_ENQUIRY | REQUEST;
3140 frm->addr = stack->upper_id| FLG_MSG_DOWN;
3145 msg_queue_tail(&glob_mgr->activatequeue, msg);
3146 sem_post(&glob_mgr->new_msg);
3152 int misdn_lib_port_restart(int port)
3154 struct misdn_stack *stack=find_stack_by_port(port);
3156 cb_log(0, port, "Restarting this port.\n");
3158 cb_log(0, port, "Stack:%p\n",stack);
3162 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3166 cb_log(-1, port, "port_restart: alloc_msg failed\n");
3170 frm=(iframe_t*)msg->data;
3171 /* we must activate if we are deactivated */
3172 /* activate bchannel */
3173 frm->prim = DL_RELEASE | REQUEST;
3174 frm->addr = stack->upper_id | FLG_MSG_DOWN;
3178 msg_queue_tail(&glob_mgr->activatequeue, msg);
3179 sem_post(&glob_mgr->new_msg);
3183 stack_te_destroy(stack);
3186 struct misdn_stack *tmpstack;
3187 struct misdn_stack *newstack=stack_init(stack->midev ,port, stack->ptp);
3190 if (stack == glob_mgr->stack_list) {
3191 struct misdn_stack *n=glob_mgr->stack_list->next;
3192 glob_mgr->stack_list = newstack ;
3193 glob_mgr->stack_list->next = n;
3195 for (tmpstack=glob_mgr->stack_list;
3197 tmpstack=tmpstack->next)
3198 if (tmpstack->next == stack) break;
3200 if (!tmpstack->next) {
3201 cb_log(-1, port, "Stack to restart not found\n");
3204 struct misdn_stack *n=tmpstack->next->next;
3205 tmpstack->next=newstack;
3212 for(i=0;i<newstack->b_num; i++) {
3214 if ((r=init_bc(newstack, &newstack->bc[i], newstack->midev,port,i, "", 1))<0) {
3215 cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
3230 sem_t handler_started;
3232 void manager_event_handler(void *arg)
3234 sem_post(&handler_started);
3236 struct misdn_stack *stack;
3239 /** wait for events **/
3240 sem_wait(&glob_mgr->new_msg);
3242 for (msg=msg_dequeue(&glob_mgr->activatequeue);
3244 msg=msg_dequeue(&glob_mgr->activatequeue)
3248 iframe_t *frm = (iframe_t*) msg->data ;
3250 switch ( frm->prim) {
3251 case MGR_SETSTACK | REQUEST :
3254 mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
3259 for (stack=glob_mgr->stack_list;
3261 stack=stack->next ) {
3263 while ( (msg=msg_dequeue(&stack->upqueue)) ) {
3265 /** Handle L2/3 Signalling after bchans **/
3266 if (!handle_frm_nt(msg)) {
3268 if (!handle_frm(msg)) {
3270 cb_log(-1,stack->port,"Wow we've got a strange issue while dequeueing a Frame\n");
3275 /* Here we should check if we really want to
3276 send all the messages we've queued, lets
3277 assume we've queued a Disconnect, but
3278 received it already from the other side!*/
3280 while ( (msg=msg_dequeue(&stack->downqueue)) ) {
3282 if (stack->nst.manager_l3(&stack->nst, msg))
3283 cb_log(-1, stack->port, "Error@ Sending Message in NT-Stack.\n");
3286 iframe_t *frm = (iframe_t *)msg->data;
3287 struct misdn_bchannel *bc = find_bc_by_l3id(stack, frm->dinfo);
3288 cb_log(0,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo);
3289 if (bc) send_msg(glob_mgr->midev, bc, msg);
3297 int misdn_lib_maxports_get() { /** BE AWARE WE HAVE NO CB_LOG HERE! **/
3299 int i = mISDN_open();
3305 max = mISDN_get_stack_count(i);
3312 int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_data)
3314 struct misdn_lib *mgr=calloc(1, sizeof(struct misdn_lib));
3320 cb_log = iface->cb_log;
3321 cb_event = iface->cb_event;
3322 cb_jb_empty = iface->cb_jb_empty;
3330 debug_init( flags , NULL, NULL, NULL);
3332 debug_init(0 , NULL, NULL, NULL);
3334 if (!portlist || (*portlist == 0) ) return 1;
3339 strncpy(plist,portlist, 1024);
3343 memcpy(tone_425_flip,tone_425,TONE_425_SIZE);
3344 flip_buf_bits(tone_425_flip,TONE_425_SIZE);
3346 memcpy(tone_silence_flip,tone_SILENCE,TONE_SILENCE_SIZE);
3347 flip_buf_bits(tone_silence_flip,TONE_SILENCE_SIZE);
3349 midev=te_lib_init();
3352 port_count=mISDN_get_stack_count(midev);
3354 msg_queue_init(&mgr->activatequeue);
3356 if (sem_init(&mgr->new_msg, 1, 0)<0)
3357 sem_init(&mgr->new_msg, 0, 0);
3359 for (tok=strtok_r(plist," ,",&tokb );
3361 tok=strtok_r(NULL," ,",&tokb)) {
3362 int port = atoi(tok);
3363 struct misdn_stack *stack;
3367 if (strstr(tok, "ptp"))
3370 if (port > port_count) {
3371 cb_log(-1, port, "Couldn't Initialize this port since we have only %d ports\n", port_count);
3374 stack=stack_init(midev, port, ptp);
3377 perror("init_stack");
3381 if (stack && first) {
3382 mgr->stack_list=stack;
3386 for(i=0;i<stack->b_num; i++) {
3388 if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "", 1))<0) {
3389 cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
3399 struct misdn_stack * help;
3400 for ( help=mgr->stack_list; help; help=help->next )
3401 if (help->next == NULL) break;
3408 for(i=0;i<stack->b_num; i++) {
3410 if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "",1 ))<0) {
3411 cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
3420 if (sem_init(&handler_started, 1, 0)<0)
3421 sem_init(&handler_started, 0, 0);
3423 cb_log(4, 0, "Starting Event Handler\n");
3424 pthread_create( &mgr->event_handler_thread, NULL,(void*)manager_event_handler, mgr);
3426 sem_wait(&handler_started) ;
3427 cb_log(4, 0, "Starting Event Catcher\n");
3428 pthread_create( &mgr->event_thread, NULL, (void*)misdn_lib_isdn_event_catcher, mgr);
3430 cb_log(4, 0, "Event Catcher started\n");
3432 global_state= MISDN_INITIALIZED;
3434 return (mgr == NULL);
3437 void misdn_lib_destroy()
3439 struct misdn_stack *help;
3442 for ( help=glob_mgr->stack_list; help; help=help->next ) {
3443 for(i=0;i<help->b_num; i++) {
3445 mISDN_write_frame(help->midev, buf, help->bc[i].addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
3446 help->bc[i].addr = 0;
3448 cb_log (1, help->port, "Destroying this port.\n");
3449 stack_te_destroy(help);
3452 if (global_state == MISDN_INITIALIZED) {
3453 cb_log(4, 0, "Killing Handler Thread\n");
3454 if ( pthread_cancel(glob_mgr->event_handler_thread) == 0 ) {
3455 cb_log(4, 0, "Joining Handler Thread\n");
3456 pthread_join(glob_mgr->event_handler_thread, NULL);
3459 cb_log(4, 0, "Killing Main Thread\n");
3460 if ( pthread_cancel(glob_mgr->event_thread) == 0 ) {
3461 cb_log(4, 0, "Joining Main Thread\n");
3462 pthread_join(glob_mgr->event_thread, NULL);
3466 cb_log(1, 0, "Closing mISDN device\n");
3467 te_lib_destroy(glob_mgr->midev);
3470 char *manager_isdn_get_info(enum event_e event)
3472 return isdn_get_info(msgs_g , event, 0);
3475 void manager_bchannel_activate(struct misdn_bchannel *bc)
3477 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3480 struct misdn_stack *stack=get_stack_by_bc(bc);
3483 cb_log(-1, bc->port, "bchannel_activate: Stack not found !");
3488 cb_log(-1, stack->port, "bchannel_activate: alloc_msg failed !");
3494 switch (bc->bc_state) {
3500 cb_log(1, stack->port, "bchannel_activate: BC Not properly upsetted (state:%s) addr:%x\n", bc_state2str(bc->bc_state), bc->addr);
3508 frm=(iframe_t*)msg->data;
3509 /* we must activate if we are deactivated */
3510 clear_ibuffer(bc->astbuf);
3512 cb_log(5, stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
3514 /* activate bchannel */
3515 frm->prim = DL_ESTABLISH | REQUEST;
3516 frm->addr = bc->addr | FLG_MSG_DOWN ;
3520 msg_queue_tail(&glob_mgr->activatequeue, msg);
3521 sem_post(&glob_mgr->new_msg);
3523 bc_state_change(bc, BCHAN_ACTIVE);
3530 void manager_bchannel_deactivate(struct misdn_bchannel * bc)
3533 struct misdn_stack *stack=get_stack_by_bc(bc);
3536 switch (bc->bc_state) {
3537 case BCHAN_ACTIVATED:
3540 misdn_split_conf(bc,bc->conf_id);
3543 cb_log( 4, bc->port,"bchan_deactivate: called but not activated\n");
3548 cb_log(5, stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
3550 bc->generate_tone=0;
3553 dact.prim = DL_RELEASE | REQUEST;
3554 dact.addr = bc->addr | FLG_MSG_DOWN;
3558 mISDN_write(stack->midev, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
3559 clear_ibuffer(bc->astbuf);
3561 bc_state_change(bc,BCHAN_RELEASE);
3567 int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
3569 struct misdn_stack *stack=get_stack_by_bc(bc);
3571 switch (bc->bc_state) {
3572 case BCHAN_ACTIVATED:
3576 cb_log(3, bc->port, "BC not yet activated (state:%s)\n",bc_state2str(bc->bc_state));
3580 char buf[4096 + mISDN_HEADER_LEN];
3581 iframe_t *frm= (iframe_t*)buf;
3584 frm->prim = DL_DATA|REQUEST;
3586 frm->addr = bc->addr | FLG_MSG_DOWN ;
3589 memcpy(&buf[mISDN_HEADER_LEN], data,len);
3591 if ( ! misdn_cap_is_speech(bc->capability))
3592 cb_log(6, stack->port, "Writing %d data bytes\n",len);
3594 cb_log(9, stack->port, "Writing %d bytes 2 mISDN\n",len);
3595 r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
3597 if (bc->hdlc && bc->ack_hdlc) {
3598 cb_log(4,stack->port,"Awaiting Acknowledge [%d]\n",len);
3599 sem_wait((sem_t*)bc->ack_hdlc);
3600 cb_log(4,stack->port,"Acknowledged\n");
3609 * send control information to the channel (dsp-module)
3611 void manager_ph_control(struct misdn_bchannel *bc, long c1, long c2)