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>
17 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id);
18 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id);
20 int queue_cleanup_bc(struct misdn_bchannel *bc) ;
22 int misdn_lib_get_l2_up(struct misdn_stack *stack);
24 struct misdn_stack* get_misdn_stack( void );
27 static int bec_initialized=0;
31 int misdn_lib_port_block(int port)
33 struct misdn_stack *stack=get_misdn_stack();
34 for ( ; stack; stack=stack->next) {
35 if (stack->port == port) {
44 int misdn_lib_port_unblock(int port)
46 struct misdn_stack *stack=get_misdn_stack();
47 for ( ; stack; stack=stack->next) {
48 if (stack->port == port) {
57 int misdn_lib_is_ptp(int port)
59 struct misdn_stack *stack=get_misdn_stack();
60 for ( ; stack; stack=stack->next) {
61 if (stack->port == port) return stack->ptp;
66 int misdn_lib_get_maxchans(int port)
68 struct misdn_stack *stack=get_misdn_stack();
69 for ( ; stack; stack=stack->next) {
70 if (stack->port == port) {
81 struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc)
83 struct misdn_stack *stack=get_misdn_stack();
87 for ( ; stack; stack=stack->next) {
89 for (i=0; i <stack->b_num; i++) {
90 if ( bc->port == stack->port) return stack;
98 void get_show_stack_details(int port, char *buf)
100 struct misdn_stack *stack=get_misdn_stack();
102 for ( ; stack; stack=stack->next) {
103 if (stack->port == port) break;
107 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);
116 static int nt_err_cnt =0 ;
123 static enum global_states global_state=MISDN_INITIALIZING;
126 #include <mISDNuser/net_l2.h>
127 #include <mISDNuser/tone.h>
129 #include <semaphore.h>
133 #include "isdn_lib.h"
140 pthread_t event_thread;
141 pthread_t event_handler_thread;
146 msg_queue_t activatequeue;
150 struct misdn_stack *stack_list;
154 #define ECHOCAN_ON 123
155 #define ECHOCAN_OFF 124
158 #define MISDN_DEBUG 0
160 void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
162 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
164 int setup_bc(struct misdn_bchannel *bc);
166 int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
168 int misdn_lib_port_restart(int port);
170 extern struct isdn_msg msgs_g[];
172 #define ISDN_PID_L3_B_USER 0x430000ff
173 #define ISDN_PID_L4_B_USER 0x440000ff
175 /* #define MISDN_IBUF_SIZE 1024 */
176 #define MISDN_IBUF_SIZE 512
178 /* Fine Tuning of Inband Signalling time */
179 #define TONE_ALERT_CNT 41 /* 1 Sec */
180 #define TONE_ALERT_SILENCE_CNT 200 /* 4 Sec */
182 #define TONE_BUSY_CNT 20 /* ? */
183 #define TONE_BUSY_SILENCE_CNT 48 /* ? */
187 static struct misdn_lib *glob_mgr;
189 unsigned char tone_425_flip[TONE_425_SIZE];
190 unsigned char tone_silence_flip[TONE_SILENCE_SIZE];
192 static void misdn_lib_isdn_event_catcher(void *arg);
193 static int handle_event_nt(void *dat, void *arg);
196 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
197 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
198 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
200 /* from isdn_lib.h */
201 int init_bc(struct misdn_stack * stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime);
202 struct misdn_stack* stack_init(int midev, int port, int ptp);
203 void stack_destroy(struct misdn_stack* stack);
205 int te_lib_init( void ) ; /* returns midev */
206 void te_lib_destroy(int midev) ;
207 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
208 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc);
209 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
210 void manager_clean_bc(struct misdn_bchannel *bc );
211 void manager_bchannel_setup (struct misdn_bchannel *bc);
212 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
214 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *frm, int nt);
215 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *frm, int nt);
216 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *frm, struct misdn_bchannel *bc, int nt);
217 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt);
218 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt);
219 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
221 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
222 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
226 static char *bearer2str(int cap) {
227 static char *bearers[]={
236 case INFO_CAPABILITY_SPEECH:
239 case INFO_CAPABILITY_AUDIO_3_1K:
242 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
245 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
255 static char flip_table[256];
257 void init_flip_bits(void)
261 for (i = 0 ; i < 256 ; i++) {
262 unsigned char sample = 0 ;
263 for (k = 0; k<8; k++) {
264 if ( i & 1 << k ) sample |= 0x80 >> k;
266 flip_table[i] = sample;
270 unsigned char * flip_buf_bits ( unsigned char * buf , int len)
275 for (i = 0 ; i < len; i++) {
276 buf[i] = flip_table[buf[i]];
285 msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
292 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
297 printf("cannot allocate memory, trying again...\n");
301 printf("cannot allocate memory, system overloaded.\n");
307 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
315 size = sizeof(Q931_info_t)+2;
319 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
324 dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
327 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
328 frm = (iframe_t *)dmsg->data;
331 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
337 if (!i) printf("cannot allocate memory, trying again...\n");
341 printf("cannot allocate memory, system overloaded.\n");
346 int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
349 frm = (iframe_t *)dmsg->data;
350 struct misdn_stack *stack=get_stack_by_bc(bc);
353 cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
357 frm->addr = (stack->upper_id | FLG_MSG_DOWN);
360 frm->dinfo = bc->l3_id;
362 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
364 mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
375 int misdn_cap_is_speech(int cap)
376 /** Poor mans version **/
378 if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
379 (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
383 int misdn_inband_avail(struct misdn_bchannel *bc)
386 /*if ! early_bconnect we have never inband available*/
387 if ( ! bc->early_bconnect ) return 0;
389 switch (bc->progress_indicator) {
390 case INFO_PI_INBAND_AVAILABLE:
391 case INFO_PI_CALL_NOT_E2E_ISDN:
392 case INFO_PI_CALLED_NOT_ISDN:
401 void dump_chan_list(struct misdn_stack *stack)
405 for (i=0; i <stack->b_num; i++) {
406 cb_log(6, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
413 static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel)
417 cb_log(1,stack->port,"find_free_chan: req_chan:%d\n",channel);
419 if (channel < 0 || channel > MAX_BCHANS) {
420 cb_log(4, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
426 for (i = 0; i < stack->b_num; i++) {
427 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
428 if (!stack->channels[i]) {
429 cb_log (1, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
430 stack->channels[i] = 1;
432 cb_event(EVENT_NEW_CHANNEL, bc, NULL);
438 cb_log (4, stack->port, " !! NO FREE CHAN IN STACK\n");
439 dump_chan_list(stack);
444 int empty_chan_in_stack(struct misdn_stack *stack, int channel)
447 cb_log(0,stack?stack->port:0, "empty_chan_inst_stack: cannot empty channel %d\n",channel);
451 cb_log (4, stack?stack->port:0, "empty_chan_in_stack: %d\n",channel);
452 stack->channels[channel-1] = 0;
453 dump_chan_list(stack);
457 char *bc_state2str(enum bchannel_state state) {
460 struct bchan_state_s {
462 enum bchannel_state s;
464 {"BCHAN_CLEANED", BCHAN_CLEANED },
465 {"BCHAN_EMPTY", BCHAN_EMPTY},
466 {"BCHAN_SETUP", BCHAN_SETUP},
467 {"BCHAN_SETUPED", BCHAN_SETUPED},
468 {"BCHAN_ACTIVE", BCHAN_ACTIVE},
469 {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
470 {"BCHAN_BRIDGE", BCHAN_BRIDGE},
471 {"BCHAN_BRIDGED", BCHAN_BRIDGED},
472 {"BCHAN_RELEASE", BCHAN_RELEASE},
473 {"BCHAN_RELEASED", BCHAN_RELEASED},
474 {"BCHAN_CLEAN", BCHAN_CLEAN},
475 {"BCHAN_CLEAN_REQUEST", BCHAN_CLEAN_REQUEST},
476 {"BCHAN_ERROR", BCHAN_ERROR}
479 for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
480 if ( states[i].s == state)
486 void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
488 cb_log(5,bc->port,"BC_STATE_CHANGE: from:%s to:%s\n",
489 bc_state2str(bc->bc_state),
490 bc_state2str(state) );
493 case BCHAN_ACTIVATED:
494 if (bc->next_bc_state == BCHAN_BRIDGED) {
495 misdn_join_conf(bc, bc->conf_id);
496 bc->next_bc_state = BCHAN_EMPTY;
505 void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
507 cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
508 bc_state2str(bc->next_bc_state),
509 bc_state2str(state) );
511 bc->next_bc_state=state;
515 void empty_bc(struct misdn_bchannel *bc)
522 bc->sending_complete = 0;
524 bc->restart_channel=0;
528 bc->need_more_infos = 0;
540 bc->curptx=0; bc->curprx=0;
542 bc->crypt_key[0] = 0;
547 bc->dnumplan=NUMPLAN_UNKNOWN;
548 bc->onumplan=NUMPLAN_UNKNOWN;
549 bc->rnumplan=NUMPLAN_UNKNOWN;
550 bc->cpnnumplan=NUMPLAN_UNKNOWN;
555 bc->early_bconnect = 1;
558 bc->ec_deftaps = 128;
559 bc->ec_whenbridged = 0;
567 bc->pres=0 ; /* screened */
569 bc->evq=EVENT_NOTHING;
571 bc->progress_coding=0;
572 bc->progress_location=0;
573 bc->progress_indicator=0;
575 /** Set Default Bearer Caps **/
576 bc->capability=INFO_CAPABILITY_SPEECH;
577 bc->law=INFO_CODEC_ALAW;
588 bc->infos_pending[0] = 0;
595 bc->fac_type=FACILITY_NONE;
597 bc->te_choose_channel = 0;
603 int clean_up_bc(struct misdn_bchannel *bc)
606 unsigned char buff[32];
607 struct misdn_stack * stack;
609 cb_log(3, bc?bc->port:0, "$$$ CLEANUP CALLED pid:%d\n", bc?bc->pid:-1);
612 stack=get_stack_by_bc(bc);
614 if (!stack) return -1;
616 switch (bc->bc_state ) {
618 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
625 cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
627 manager_bchannel_deactivate(bc);
631 beroec_destroy(bc->ec);
635 if ( misdn_cap_is_speech(bc->capability) && bc->ec_enable) {
636 manager_ec_disable(bc);
639 mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
641 /*mISDN_clear_stack(stack->midev, bc->b_stid);*/
646 bc_state_change(bc, BCHAN_CLEANED);
653 void clear_l3(struct misdn_stack *stack)
657 for (i=0; i<stack->b_num; i++) {
658 if (global_state == MISDN_INITIALIZED) {
659 cb_event(EVENT_CLEANUP, &stack->bc[i], NULL);
660 empty_chan_in_stack(stack,i+1);
661 empty_bc(&stack->bc[i]);
662 clean_up_bc(&stack->bc[i]);
668 int set_chan_in_stack(struct misdn_stack *stack, int channel)
671 cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
673 stack->channels[channel-1] = 1;
675 cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
681 int chan_in_stack_free(struct misdn_stack *stack, int channel)
683 if (stack->channels[channel-1])
691 static int newteid=0;
693 #define MAXPROCS 0x100
695 int misdn_lib_get_l1_down(struct misdn_stack *stack)
699 act.prim = PH_DEACTIVATE | REQUEST;
700 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
706 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
712 int misdn_lib_get_l2_down(struct misdn_stack *stack)
715 if (stack->ptp && (stack->nt) ) {
718 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
720 if (stack->nst.manager_l3(&stack->nst, dmsg))
726 act.prim = DL_RELEASE| REQUEST;
727 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
731 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
738 int misdn_lib_get_l1_up(struct misdn_stack *stack)
742 act.prim = PH_ACTIVATE | REQUEST;
743 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
749 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
753 int misdn_lib_get_l2_up(struct misdn_stack *stack)
756 if (stack->ptp && (stack->nt) ) {
759 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
761 if (stack->nst.manager_l3(&stack->nst, dmsg))
767 act.prim = DL_ESTABLISH | REQUEST;
768 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
772 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
778 int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack)
782 act.prim = DL_ESTABLISH | REQUEST;
783 act.addr = (stack->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
787 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
791 int misdn_lib_get_l2_status(struct misdn_stack *stack)
795 act.prim = DL_ESTABLISH | REQUEST;
797 act.addr = (stack->upper_id | FLG_MSG_DOWN) ;
801 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
804 int misdn_lib_get_short_status(struct misdn_stack *stack)
809 act.prim = MGR_SHORTSTATUS | REQUEST;
811 act.addr = (stack->upper_id | MSG_BROADCAST) ;
813 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
816 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
821 static int create_process (int midev, struct misdn_bchannel *bc) {
825 struct misdn_stack *stack=get_stack_by_bc(bc);
829 free_chan = find_free_chan_in_stack(stack, bc, bc->channel_preselected?bc->channel:0);
830 if (!free_chan) return -1;
831 /*bc->channel=free_chan;*/
833 cb_log(4,stack->port, " --> found channel: %d\n",free_chan);
835 for (i=0; i <= MAXPROCS; i++)
836 if (stack->procids[i]==0) break;
839 cb_log(0, stack->port, "Couldnt Create New ProcId.\n");
846 ncr.prim = CC_NEW_CR | REQUEST;
848 ncr.addr = (stack->upper_id | FLG_MSG_DOWN) ;
854 cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
857 if (stack->ptp || bc->te_choose_channel) {
858 /* we know exactly which channels are in use */
859 free_chan = find_free_chan_in_stack(stack, bc, bc->channel_preselected?bc->channel:0);
860 if (!free_chan) return -1;
861 /*bc->channel=free_chan;*/
862 cb_log(2,stack->port, " --> found channel: %d\n",free_chan);
864 /* other phones could have made a call also on this port (ptmp) */
869 /* if we are in te-mode, we need to create a process first */
870 if (newteid++ > 0xffff)
873 l3_id = (entity<<16) | newteid;
874 /* preparing message */
875 ncr.prim = CC_NEW_CR | REQUEST;
877 ncr.addr = (stack->upper_id | FLG_MSG_DOWN) ;
884 cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
886 mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
893 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
899 int setup_bc(struct misdn_bchannel *bc)
901 unsigned char buff[1025];
907 struct misdn_stack *stack=get_stack_by_bc(bc);
910 cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
914 int midev=stack->midev;
915 int channel=bc->channel-1-(bc->channel>16);
916 int b_stid=stack->b_stids[channel>=0?channel:0];
919 switch (bc->bc_state) {
923 cb_log(4, stack->port, "$$$ bc already upsetted stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
927 cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
930 cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
932 bc_state_change(bc,BCHAN_ERROR);
941 memset(&li, 0, sizeof(li));
946 li.st = bc->b_stid; /* given idx */
953 if ( bc->hdlc || bc->nodsp) {
954 cb_log(4, stack->port,"setup_bc: without dsp\n");
956 int l = sizeof(li.name);
957 strncpy(li.name, "B L3", l);
960 li.pid.layermask = ISDN_LAYER((3));
961 li.pid.protocol[3] = ISDN_PID_L3_B_USER;
965 cb_log(4, stack->port,"setup_bc: with dsp\n");
967 int l = sizeof(li.name);
968 strncpy(li.name, "B L4", l);
971 li.pid.layermask = ISDN_LAYER((4));
972 li.pid.protocol[4] = ISDN_PID_L4_B_USER
978 ret = mISDN_new_layer(midev, &li);
980 cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
982 bc_state_change(bc,BCHAN_ERROR);
986 bc->layer_id = li.id;
989 memset(&pid, 0, sizeof(pid));
993 cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
996 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
997 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
998 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
999 pid.protocol[3] = ISDN_PID_L3_B_USER;
1000 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
1002 } else if ( bc->hdlc ) {
1003 cb_log(2, stack->port," --> HDLC Mode\n");
1005 bc->ack_hdlc=(sem_t*)malloc(sizeof(sem_t));
1006 if ( sem_init((sem_t*)bc->ack_hdlc, 1, 0)<0 )
1007 sem_init((sem_t*)bc->ack_hdlc, 0, 0);
1010 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
1011 pid.protocol[2] = ISDN_PID_L2_B_TRANS ;
1012 pid.protocol[3] = ISDN_PID_L3_B_USER;
1013 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
1015 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
1016 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1017 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1018 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1019 pid.protocol[4] = ISDN_PID_L4_B_USER;
1020 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1024 ret = mISDN_set_stack(midev, bc->b_stid, &pid);
1027 cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
1029 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1031 bc_state_change(bc,BCHAN_ERROR);
1036 ret = mISDN_get_setstack_ind(midev, bc->layer_id);
1039 cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
1040 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1042 bc_state_change(bc,BCHAN_ERROR);
1046 ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
1048 bc->addr = ret>0? ret : 0;
1051 cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
1052 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1054 bc_state_change(bc,BCHAN_ERROR);
1057 manager_bchannel_activate(bc);
1059 bc_state_change(bc,BCHAN_ACTIVATED);
1067 int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime)
1069 unsigned char buff[1025];
1070 iframe_t *frm = (iframe_t *)buff;
1075 cb_log(8, port, "Init.BC %d.\n",bidx);
1077 memset(bc, 0,sizeof(struct misdn_bchannel));
1080 int l = sizeof(bc->msn);
1081 strncpy(bc->msn,msn, l);
1087 bc_state_change(bc, BCHAN_CLEANED);
1089 bc->port=stack->port;
1090 bc->nt=stack->nt?1:0;
1093 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
1095 if (!ibuf) return -1;
1097 clear_ibuffer( ibuf);
1099 ibuf->rsem=malloc(sizeof(sem_t));
1103 if (sem_init(ibuf->rsem,1,0)<0)
1104 sem_init(ibuf->rsem,0,0);
1112 stack_info_t *stinf;
1113 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
1115 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1119 stinf = (stack_info_t *)&frm->data.p;
1121 cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
1129 struct misdn_stack* stack_init( int midev, int port, int ptp )
1132 unsigned char buff[1025];
1133 iframe_t *frm = (iframe_t *)buff;
1134 stack_info_t *stinf;
1138 struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
1139 if (!stack ) return NULL;
1142 cb_log(8, port, "Init. Stack.\n");
1144 memset(stack,0,sizeof(struct misdn_stack));
1146 for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
1152 stack->holding=NULL;
1155 msg_queue_init(&stack->downqueue);
1156 msg_queue_init(&stack->upqueue);
1158 /* query port's requirements */
1159 ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1161 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1165 stinf = (stack_info_t *)&frm->data.p;
1167 stack->d_stid = stinf->id;
1168 stack->b_num = stinf->childcnt;
1170 for (i=0; i<stinf->childcnt; i++)
1171 stack->b_stids[i] = stinf->child[i];
1173 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1174 case ISDN_PID_L0_TE_S0:
1177 case ISDN_PID_L0_NT_S0:
1178 cb_log(8, port, "NT Stack\n");
1183 case ISDN_PID_L0_TE_U:
1185 case ISDN_PID_L0_NT_U:
1187 case ISDN_PID_L0_TE_UP2:
1189 case ISDN_PID_L0_NT_UP2:
1191 case ISDN_PID_L0_TE_E1:
1192 cb_log(8, port, "TE S2M Stack\n");
1196 case ISDN_PID_L0_NT_E1:
1197 cb_log(8, port, "TE S2M Stack\n");
1203 cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1208 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) {
1219 cb_log(8, port, "Init. Stack.\n");
1221 memset(&li, 0, sizeof(li));
1223 int l = sizeof(li.name);
1224 strncpy(li.name,nt?"net l2":"user l4", l);
1229 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1230 li.pid.layermask = ISDN_LAYER((nt?2:4));
1231 li.st = stack->d_stid;
1234 ret = mISDN_new_layer(midev, &li);
1236 cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1241 stack->upper_id = li.id;
1242 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
1245 cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
1249 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3);
1250 if (stack->lower_id < 0) {
1251 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1255 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1256 if (stack->upper_id < 0) {
1257 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
1261 cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id);
1264 /* create nst (nt-mode only) */
1267 memset(&stack->nst, 0, sizeof(net_stack_t));
1268 memset(&stack->mgr, 0, sizeof(manager_t));
1270 stack->mgr.nst = &stack->nst;
1271 stack->nst.manager = &stack->mgr;
1273 stack->nst.l3_manager = handle_event_nt;
1274 stack->nst.device = midev;
1275 stack->nst.cardnr = port;
1276 stack->nst.d_stid = stack->d_stid;
1278 stack->nst.feature = FEATURE_NET_HOLD;
1280 stack->nst.feature |= FEATURE_NET_PTP;
1282 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1284 stack->nst.l1_id = stack->lower_id;
1285 stack->nst.l2_id = stack->upper_id;
1287 msg_queue_init(&stack->nst.down_queue);
1289 Isdnl2Init(&stack->nst);
1290 Isdnl3Init(&stack->nst);
1295 /*assume L1 is up, we'll get DEACTIVATES soon, for non
1300 misdn_lib_get_short_status(stack);
1301 misdn_lib_get_l1_up(stack);
1302 misdn_lib_get_l2_up(stack);
1306 cb_log(8,0,"stack_init: port:%d lowerId:%x upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
1312 void stack_destroy(struct misdn_stack* stack)
1318 cleanup_Isdnl2(&stack->nst);
1319 cleanup_Isdnl3(&stack->nst);
1322 if (stack->lower_id)
1323 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1325 if (stack->upper_id)
1326 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1330 struct misdn_stack * find_stack_by_addr(int addr)
1332 struct misdn_stack *stack;
1334 for (stack=glob_mgr->stack_list;
1336 stack=stack->next) {
1337 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
1345 struct misdn_stack * find_stack_by_port(int port)
1347 struct misdn_stack *stack;
1349 for (stack=glob_mgr->stack_list;
1352 if (stack->port == port) return stack;
1357 struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
1359 struct misdn_stack *stack;
1361 for (stack=glob_mgr->stack_list;
1364 if ( &stack->mgr == mgr_nt) return stack;
1369 struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1372 for (i=0; i<stack->b_num; i++) {
1373 if ( (stack->bc[i].l3_id & mask) == (l3id & mask)) return &stack->bc[i] ;
1375 return stack_holder_find(stack,l3id);
1379 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1382 for (i=0; i<stack->b_num; i++) {
1383 if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
1385 return stack_holder_find(stack,l3id);
1388 struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
1391 for (i=0; i<stack->b_num; i++) {
1392 if (stack->bc[i].holded ) return &stack->bc[i] ;
1398 struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1400 struct misdn_stack* stack;
1404 for (stack=glob_mgr->stack_list;
1406 stack=stack->next) {
1408 for (i=0; i< stack->b_num; i++) {
1410 if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) || stack->bc[i].layer_id== addr ) {
1411 return &stack->bc[i];
1422 struct misdn_bchannel *find_bc_by_channel(int port, int channel)
1424 struct misdn_stack* stack=find_stack_by_port(port);
1427 if (!stack) return NULL;
1429 for (i=0; i< stack->b_num; i++) {
1430 if ( stack->bc[i].channel== channel ) {
1431 return &stack->bc[i];
1442 int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1444 struct misdn_stack *stack=get_stack_by_bc(bc);
1450 case EVENT_CONNECT_ACKNOWLEDGE:
1452 if ( !misdn_cap_is_speech(bc->capability)) {
1453 int ret=setup_bc(bc);
1454 if (ret == -EINVAL){
1455 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1462 if ( *bc->crypt_key ) {
1463 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);
1464 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1466 case EVENT_ALERTING:
1467 case EVENT_PROGRESS:
1468 case EVENT_PROCEEDING:
1469 case EVENT_SETUP_ACKNOWLEDGE:
1476 if (bc->channel == 0xff) {
1477 bc->channel=find_free_chan_in_stack(stack, bc, 0);
1479 cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
1484 if (bc->channel >0 && bc->channel<255) {
1485 set_chan_in_stack(stack ,bc->channel);
1489 int ret=setup_bc(bc);
1490 if (ret == -EINVAL){
1491 cb_log(0,bc->port,"handle_event: setup_bc failed\n");
1492 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1498 case EVENT_RELEASE_COMPLETE:
1501 empty_chan_in_stack(stack,bc->channel);
1502 int tmpcause=bc->cause;
1510 } else { /** NT MODE **/
1516 int handle_new_process(struct misdn_stack *stack, iframe_t *frm)
1519 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0);
1523 cb_log(0, stack->port, " --> !! lib: No free channel!\n");
1527 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1528 bc->l3_id=frm->dinfo;
1532 int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1534 if (!stack) return -1;
1536 switch (frm->prim) {
1537 case CC_NEW_CR|INDICATION:
1538 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1539 if (handle_new_process(stack, frm) <0)
1542 case CC_NEW_CR|CONFIRM:
1544 case CC_NEW_CR|REQUEST:
1546 case CC_RELEASE_CR|REQUEST:
1548 case CC_RELEASE_CR|CONFIRM:
1550 case CC_RELEASE_CR|INDICATION:
1551 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
1553 struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
1554 struct misdn_bchannel dummybc;
1557 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1558 memset (&dummybc,0,sizeof(dummybc));
1559 dummybc.port=stack->port;
1560 dummybc.l3_id=frm->dinfo;
1565 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
1567 empty_chan_in_stack(stack,bc->channel);
1572 dump_chan_list(stack);
1574 bc->need_disconnect=0;
1576 bc->need_release_complete=0;
1578 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1579 if (bc->stack_holder) {
1580 cb_log(4,stack->port, "REMOVEING Holder\n");
1581 stack_holder_remove( stack, bc);
1587 cb_log(4, stack->port, "BC with dinfo: %x not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
1599 /*Emptys bc if it's reserved (no SETUP out yet)*/
1600 void misdn_lib_release(struct misdn_bchannel *bc)
1602 struct misdn_stack *stack=get_stack_by_bc(bc);
1605 cb_log(1,0,"misdn_release: No Stack found\n");
1609 if (bc->channel>0) {
1610 empty_chan_in_stack(stack,bc->channel);
1619 int misdn_lib_get_port_up (int port)
1621 struct misdn_stack *stack;
1623 for (stack=glob_mgr->stack_list;
1625 stack=stack->next) {
1627 if (stack->port == port) {
1630 misdn_lib_get_l1_up(stack);
1632 misdn_lib_get_l2_up(stack);
1641 int misdn_lib_get_port_down (int port)
1642 { /* Pull Down L1 */
1643 struct misdn_stack *stack;
1644 for (stack=glob_mgr->stack_list;
1646 stack=stack->next) {
1647 if (stack->port == port) {
1649 misdn_lib_get_l2_down(stack);
1650 misdn_lib_get_l1_down(stack);
1657 int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data)
1660 case FACILITY_CALLDEFLECT:
1661 strcpy(bc->out_fac.calldeflect_nr,(char*)data);
1664 cb_log(1,bc?bc->port:0,"We don't handle this facility yet: %d\n",fac);
1668 bc->out_fac_type=fac;
1670 misdn_lib_send_event(bc,EVENT_FACILITY);
1675 int misdn_lib_port_up(int port, int check)
1677 struct misdn_stack *stack;
1680 for (stack=glob_mgr->stack_list;
1682 stack=stack->next) {
1684 if ( !stack->ptp && !check) return 1;
1686 if (stack->port == port) {
1688 if (stack->blocked) {
1689 cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
1695 if (stack->l1link && stack->l2link) {
1698 cb_log(0,port, "Port Down L2:%d L1:%d\n",
1699 stack->l2link, stack->l1link);
1706 cb_log(0,port, "Port down PMP\n");
1718 handle_event_nt(void *dat, void *arg)
1720 manager_t *mgr = (manager_t *)dat;
1721 msg_t *msg = (msg_t *)arg;
1722 mISDNuser_head_t *hh;
1725 struct misdn_stack *stack=find_stack_by_mgr(mgr);
1731 hh=(mISDNuser_head_t*)msg->data;
1734 cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1737 case CC_RETRIEVE|INDICATION:
1739 iframe_t frm; /* fake te frm to add callref to global callreflist */
1740 frm.dinfo = hh->dinfo;
1742 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1744 frm.prim = CC_NEW_CR|INDICATION;
1746 if (handle_cr( stack, &frm)< 0) {
1748 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1749 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1750 stack->nst.manager_l3(&stack->nst, dmsg);
1755 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1756 struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
1759 cb_event(EVENT_NEW_BC, hold_bc, bc);
1760 cb_log(4, stack->port, "REMOVEING Holder\n");
1761 stack_holder_remove(stack, hold_bc);
1769 case CC_SETUP|CONFIRM:
1771 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1772 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1773 cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n",l3id );
1775 if (!bc) { cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
1776 cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1778 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1783 case CC_SETUP|INDICATION:
1785 iframe_t frm; /* fake te frm to add callref to global callreflist */
1786 frm.dinfo = hh->dinfo;
1787 frm.addr=stack->upper_id;
1788 frm.prim = CC_NEW_CR|INDICATION;
1790 if (handle_cr(stack, &frm)< 0) {
1792 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1793 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1794 stack->nst.manager_l3(&stack->nst, dmsg);
1801 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
1804 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1806 if ( !misdn_cap_is_speech(bc->capability)) {
1807 int ret=setup_bc(bc);
1808 if (ret == -EINVAL){
1809 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1818 case CC_ALERTING|INDICATION:
1819 case CC_PROCEEDING|INDICATION:
1820 case CC_SETUP_ACKNOWLEDGE|INDICATION:
1821 if(!stack->ptp) break;
1822 case CC_CONNECT|INDICATION:
1825 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1829 cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
1831 cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
1832 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1833 stack->nst.manager_l3(&stack->nst, dmsg);
1838 int ret=setup_bc(bc);
1839 if (ret == -EINVAL){
1840 cb_log(0,bc->port,"handle_event_nt: setup_bc failed\n");
1841 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1846 case CC_DISCONNECT|INDICATION:
1848 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1850 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1852 int myprocid=bc->l3_id&0x0000ffff;
1853 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1854 cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
1861 case CC_FACILITY|INDICATION:
1863 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1865 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1867 int myprocid=bc->l3_id&0x0000ffff;
1868 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1869 cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1875 case CC_RELEASE_COMPLETE|INDICATION:
1878 case CC_SUSPEND|INDICATION:
1881 cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
1882 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1883 stack->nst.manager_l3(&stack->nst, dmsg);
1888 case CC_RESUME|INDICATION:
1891 case CC_RELEASE|CONFIRM:
1894 case CC_RELEASE|INDICATION:
1897 case CC_RELEASE_CR|INDICATION:
1899 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1900 struct misdn_bchannel dummybc;
1901 iframe_t frm; /* fake te frm to remove callref from global callreflist */
1902 frm.dinfo = hh->dinfo;
1904 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1906 frm.prim = CC_RELEASE_CR|INDICATION;
1907 cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
1908 /** removing procid **/
1910 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1911 memset (&dummybc,0,sizeof(dummybc));
1912 dummybc.port=stack->port;
1913 dummybc.l3_id=hh->dinfo;
1918 if ( (bc->l3_id & 0xff00) == 0xff00) {
1919 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
1920 stack->procids[bc->l3_id&0xff] = 0 ;
1923 else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
1925 if (handle_cr(stack, &frm)<0) {
1933 case CC_NEW_CR|INDICATION:
1934 /* Got New CR for bchan, for now I handle this one in */
1935 /* connect_ack, Need to be changed */
1937 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1938 int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1939 if (!bc) { cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
1940 if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
1941 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
1942 stack->procids[bc->l3_id&0xff] = 0 ;
1944 cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID is %x\n",l3id );
1947 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1953 case DL_ESTABLISH | INDICATION:
1954 case DL_ESTABLISH | CONFIRM:
1956 cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
1958 if (stack->ptp && stack->l2link) {
1959 cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
1960 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
1972 case DL_RELEASE | INDICATION:
1973 case DL_RELEASE | CONFIRM:
1976 cb_log(3 , stack->port, "%% GOT L2 DeActivate Info.\n");
1978 if (stack->l2upcnt>3) {
1979 cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attemps!!!\n");
1982 if (stack->nt) misdn_lib_reinit_nt_stack(stack->port);
1984 if (stack->l1link) {
1985 misdn_lib_get_l2_up(stack);
1991 cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
2002 /* Parse Events and fire_up to App. */
2003 struct misdn_bchannel *bc;
2004 struct misdn_bchannel dummybc;
2006 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
2008 bc=find_bc_by_l3id(stack, hh->dinfo);
2012 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
2013 memset (&dummybc,0,sizeof(dummybc));
2014 dummybc.port=stack->port;
2015 dummybc.l3_id=hh->dinfo;
2019 isdn_msg_parse_event(msgs_g,msg,bc, 1);
2023 if (bc->channel>0 && bc->channel<255) {
2026 set_chan_in_stack(stack, bc->channel);
2028 cb_log(0,stack->port," --> PTMP but channel requested\n");
2032 bc->channel = find_free_chan_in_stack(stack, bc, 0);
2034 cb_log(0, stack->port, " No free channel at the moment\n");
2038 cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
2039 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
2040 stack->nst.manager_l3(&stack->nst, dmsg);
2052 case EVENT_RELEASE_COMPLETE:
2054 empty_chan_in_stack(stack, bc->channel);
2055 int tmpcause=bc->cause;
2065 if(!isdn_get_info(msgs_g,event,1)) {
2066 cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
2071 cb_log(1, stack->port, "Siemens Busy reject..\n");
2078 cb_event(event, bc, glob_mgr->user_data);
2082 cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo);
2093 int handle_timers(msg_t* msg)
2095 iframe_t *frm= (iframe_t*)msg->data;
2096 struct misdn_stack *stack;
2099 switch (frm->prim) {
2100 case MGR_INITTIMER | CONFIRM:
2101 case MGR_ADDTIMER | CONFIRM:
2102 case MGR_DELTIMER | CONFIRM:
2103 case MGR_REMOVETIMER | CONFIRM:
2110 if (frm->prim==(MGR_TIMER | INDICATION) ) {
2111 for (stack = glob_mgr->stack_list;
2113 stack = stack->next) {
2116 if (!stack->nt) continue;
2118 it = stack->nst.tlist;
2120 for(it=stack->nst.tlist;
2123 if (it->id == (int)frm->addr)
2128 ret = mISDN_write_frame(stack->midev, msg->data, frm->addr,
2129 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
2130 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
2131 ret = it->function(it->data);
2137 cb_log(0, 0, "Timer Msg without Timer ??\n");
2147 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
2149 bc->generate_tone=1;
2152 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
2154 bc->generate_tone=0;
2158 static int do_tone(struct misdn_bchannel *bc, int len)
2162 if (bc->generate_tone) {
2163 cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
2165 if ( !bc->nojitter ) {
2166 misdn_tx_jitter(bc,len);
2177 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
2179 char buf[4096 + mISDN_HEADER_LEN];
2180 char *data=&buf[mISDN_HEADER_LEN];
2181 iframe_t *txfrm= (iframe_t*)buf;
2184 jlen=cb_jb_empty(bc,data,len);
2187 flip_buf_bits( data, jlen);
2190 cb_log(7,bc->port,"Jitterbuffer Underrun.\n");
2194 flip_buf_bits(data,len);
2195 beroec_cancel_alaw_chunk(bc->ec, data, bc->bframe, len);
2196 flip_buf_bits(data,len);
2201 txfrm->prim = DL_DATA|REQUEST;
2205 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2208 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2210 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2212 #define MISDN_GEN_SILENCE
2213 #ifdef MISDN_GEN_SILENCE
2214 int cnt=len/TONE_SILENCE_SIZE;
2215 int rest=len%TONE_SILENCE_SIZE;
2218 for (i=0; i<cnt; i++) {
2219 memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE );
2220 data +=TONE_SILENCE_SIZE;
2224 memcpy(data, tone_silence_flip, rest);
2227 txfrm->prim = DL_DATA|REQUEST;
2231 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2234 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2236 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2242 int handle_bchan(msg_t *msg)
2244 iframe_t *frm= (iframe_t*)msg->data;
2247 struct misdn_bchannel *bc=find_bc_by_addr(frm->addr);
2250 cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
2254 struct misdn_stack *stack=get_stack_by_bc(bc);
2257 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);
2261 switch (frm->prim) {
2263 case MGR_SETSTACK| CONFIRM:
2264 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid);
2267 case MGR_SETSTACK| INDICATION:
2268 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid);
2272 bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer);
2275 if (errno == EAGAIN) {
2280 cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno));
2282 /* we kill the channel later, when we received some
2284 bc->addr= frm->addr;
2285 } else if ( bc->addr < 0) {
2286 cb_log(0, stack->port,"$$$ bc->addr <0 Error:%s\n",strerror(errno));
2290 cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
2295 switch(bc->bc_state) {
2297 bc_state_change(bc,BCHAN_SETUPED);
2300 case BCHAN_CLEAN_REQUEST:
2302 cb_log(0, stack->port," --> STATE WASN'T SETUP (but %s) in SETSTACK|IND pid:%d\n",bc_state2str(bc->bc_state), bc->pid);
2308 case MGR_DELLAYER| INDICATION:
2309 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid);
2312 case MGR_DELLAYER| CONFIRM:
2313 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid);
2321 case PH_ACTIVATE | INDICATION:
2322 case DL_ESTABLISH | INDICATION:
2323 cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid);
2328 case PH_ACTIVATE | CONFIRM:
2329 case DL_ESTABLISH | CONFIRM:
2331 cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid);
2336 case DL_ESTABLISH | REQUEST:
2339 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2344 case DL_RELEASE|REQUEST:
2347 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2352 case PH_DEACTIVATE | INDICATION:
2353 case DL_RELEASE | INDICATION:
2354 cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid);
2359 case PH_DEACTIVATE | CONFIRM:
2360 case DL_RELEASE | CONFIRM:
2361 cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid);
2366 case PH_CONTROL|INDICATION:
2368 unsigned int cont = *((unsigned int *)&frm->data.p);
2370 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);
2372 if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
2373 int dtmf = cont & DTMF_TONE_MASK;
2374 cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
2376 cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
2381 if (cont == BF_REJECT) {
2382 cb_log(4, stack->port, " --> BF REJECT\n");
2386 if (cont == BF_ACCEPT) {
2387 cb_log(4, stack->port, " --> BF ACCEPT\n");
2394 case PH_DATA|REQUEST:
2395 case DL_DATA|REQUEST:
2396 cb_log(0, stack->port, "DL_DATA REQUEST \n");
2403 case PH_DATA|INDICATION:
2404 case DL_DATA|INDICATION:
2406 bc->bframe = (void*)&frm->data.i;
2407 bc->bframe_len = frm->len;
2409 /** Anyway flip the bufbits **/
2410 if ( misdn_cap_is_speech(bc->capability) )
2411 flip_buf_bits(bc->bframe, bc->bframe_len);
2414 if (!bc->bframe_len) {
2415 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2420 if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
2421 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2427 cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
2431 if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
2434 #ifdef MISDN_B_DEBUG
2435 cb_log(0,bc->port,"do_tone START\n");
2437 t=do_tone(bc,frm->len);
2439 #ifdef MISDN_B_DEBUG
2440 cb_log(0,bc->port,"do_tone STOP (%d)\n",t);
2444 if ( misdn_cap_is_speech(bc->capability)) {
2445 if ( !bc->nojitter ) {
2446 #ifdef MISDN_B_DEBUG
2447 cb_log(0,bc->port,"tx_jitter START\n");
2449 misdn_tx_jitter(bc,frm->len);
2450 #ifdef MISDN_B_DEBUG
2451 cb_log(0,bc->port,"tx_jitter STOP\n");
2456 #ifdef MISDN_B_DEBUG
2457 cb_log(0,bc->port,"EVENT_B_DATA START\n");
2460 int i=cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
2461 #ifdef MISDN_B_DEBUG
2462 cb_log(0,bc->port,"EVENT_B_DATA STOP\n");
2466 cb_log(10,stack->port,"cb_event returned <0\n");
2467 /*clean_up_bc(bc);*/
2476 case PH_CONTROL | CONFIRM:
2477 cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
2481 case PH_DATA | CONFIRM:
2482 case DL_DATA|CONFIRM:
2485 cb_log(0, stack->port, "Data confirmed\n");
2491 case DL_DATA|RESPONSE:
2493 cb_log(0, stack->port, "Data response\n");
2504 int handle_frm_nt(msg_t *msg)
2506 iframe_t *frm= (iframe_t*)msg->data;
2507 struct misdn_stack *stack;
2510 stack=find_stack_by_addr( frm->addr );
2514 if (!stack || !stack->nt) {
2519 if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
2521 if (nt_err_cnt > 0 ) {
2522 if (nt_err_cnt < 100) {
2524 cb_log(0, stack->port, "NT Stack sends us error: %d \n", err);
2525 } else if (nt_err_cnt < 105){
2526 cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
2539 int handle_frm(msg_t *msg)
2541 iframe_t *frm = (iframe_t*) msg->data;
2543 struct misdn_stack *stack=find_stack_by_addr(frm->addr);
2546 cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim);
2549 if (!stack || stack->nt) {
2554 struct misdn_bchannel *bc;
2555 int ret=handle_cr(stack, frm);
2558 cb_log(3,stack?stack->port:0,"handle_frm: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
2566 bc=find_bc_by_l3id(stack, frm->dinfo);
2569 enum event_e event = isdn_msg_get_event(msgs_g, msg, 0);
2570 enum event_response_e response=RESPONSE_OK;
2572 isdn_msg_parse_event(msgs_g,msg,bc, 0);
2574 /** Preprocess some Events **/
2575 handle_event(bc, event, frm);
2576 /* shoot up event to App: */
2577 cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2579 if(!isdn_get_info(msgs_g,event,0))
2580 cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2582 response=cb_event(event, bc, glob_mgr->user_data);
2584 if (event == EVENT_SETUP) {
2586 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2588 cb_log(0, stack->port, "TOTALY IGNORING SETUP \n");
2591 case RESPONSE_IGNORE_SETUP:
2592 /* I think we should send CC_RELEASE_CR, but am not sure*/
2595 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
2597 empty_chan_in_stack(stack, bc->channel);
2599 bc_state_change(bc,BCHAN_CLEANED);
2601 cb_log(0, stack->port, "GOT IGNORE SETUP\n");
2606 cb_log(4, stack->port, "GOT SETUP OK\n");
2615 cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
2623 cb_log(0, stack->port, "NO BC FOR STACK\n");
2627 cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
2632 int handle_l1(msg_t *msg)
2634 iframe_t *frm = (iframe_t*) msg->data;
2635 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2638 if (!stack) return 0 ;
2640 switch (frm->prim) {
2641 case PH_ACTIVATE | CONFIRM:
2642 case PH_ACTIVATE | INDICATION:
2643 cb_log (3, stack->port, "L1: PH L1Link Up!\n");
2648 if (stack->nst.l1_l2(&stack->nst, msg))
2652 misdn_lib_get_l2_up(stack);
2657 for (i=0;i<stack->b_num; i++) {
2658 if (stack->bc[i].evq != EVENT_NOTHING) {
2659 cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
2660 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
2661 stack->bc[i].evq=EVENT_NOTHING;
2667 case PH_ACTIVATE | REQUEST:
2669 cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2672 case PH_DEACTIVATE | REQUEST:
2674 cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2677 case PH_DEACTIVATE | CONFIRM:
2678 case PH_DEACTIVATE | INDICATION:
2679 cb_log (3, stack->port, "L1: PH L1Link Down! \n");
2681 for (i=0; i<stack->b_num; i++) {
2682 if (global_state == MISDN_INITIALIZED) {
2683 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2688 if (stack->nst.l1_l2(&stack->nst, msg))
2702 int handle_l2(msg_t *msg)
2704 iframe_t *frm = (iframe_t*) msg->data;
2706 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2714 case DL_ESTABLISH | REQUEST:
2715 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2717 case DL_RELEASE | REQUEST:
2718 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2721 case DL_ESTABLISH | INDICATION:
2722 case DL_ESTABLISH | CONFIRM:
2724 cb_log (3, stack->port, "L2: L2Link Up! \n");
2725 if (stack->ptp && stack->l2link) {
2726 cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n");
2727 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2735 case DL_RELEASE | INDICATION:
2736 case DL_RELEASE | CONFIRM:
2738 cb_log (3, stack->port, "L2: L2Link Down! \n");
2749 int handle_mgmt(msg_t *msg)
2751 iframe_t *frm = (iframe_t*) msg->data;
2753 if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2754 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2759 struct misdn_stack * stack=find_stack_by_addr(frm->addr);
2762 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
2763 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
2773 case MGR_SHORTSTATUS | INDICATION:
2774 case MGR_SHORTSTATUS | CONFIRM:
2775 cb_log(5, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
2777 switch (frm->dinfo) {
2778 case SSTATUS_L1_ACTIVATED:
2779 cb_log(3, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
2783 case SSTATUS_L1_DEACTIVATED:
2784 cb_log(3, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2790 case SSTATUS_L2_ESTABLISHED:
2791 cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
2795 case SSTATUS_L2_RELEASED:
2796 cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
2804 case MGR_SETSTACK | INDICATION:
2805 cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2808 case MGR_DELLAYER | CONFIRM:
2809 cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
2816 if ( (frm->prim & 0x0f0000) == 0x0f0000) {
2817 cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
2826 msg_t *fetch_msg(int midev)
2828 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2833 cb_log(0, 0, "fetch_msg: alloc msg failed !!");
2839 FD_SET(midev,&rdfs);
2841 mISDN_select(FD_SETSIZE, &rdfs, NULL, NULL, NULL);
2842 //select(FD_SETSIZE, &rdfs, NULL, NULL, NULL);
2844 if (FD_ISSET(midev, &rdfs)) {
2848 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
2852 free_msg(msg); /* danger, cauz usualy freeing in main_loop */
2853 cb_log(6,0,"Got empty Msg..\n");
2858 if (errno == EAGAIN) {
2859 /*we wait for mISDN here*/
2860 cb_log(4,0,"mISDN_read wants us to wait\n");
2865 cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno);
2872 printf ("Select timeout\n");
2879 void misdn_lib_isdn_l1watcher(int port)
2881 struct misdn_stack *stack;
2883 for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
2887 cb_log(4, port, "Checking L1 State\n");
2888 if (!stack->l1link) {
2889 cb_log(4, port, "L1 State Down, trying to get it up again\n");
2890 misdn_lib_get_short_status(stack);
2891 misdn_lib_get_l1_up(stack);
2892 misdn_lib_get_l2_up(stack);
2897 static void misdn_lib_isdn_event_catcher(void *arg)
2899 struct misdn_lib *mgr = arg;
2900 int zero_frm=0 , fff_frm=0 ;
2901 int midev= mgr->midev;
2905 msg_t *msg = fetch_msg(midev);
2911 frm = (iframe_t*) msg->data;
2913 /** When we make a call from NT2Ast we get this frames **/
2914 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
2920 cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
2925 /** I get this sometimes after setup_bc **/
2926 if (frm->len == 0 && frm->dinfo == 0 && frm->prim == 0xffffffff ) {
2932 cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
2937 manager_isdn_handler(frm, msg);
2943 /** App Interface **/
2947 iframe_t *frm=(iframe_t*)buff;
2948 int midev=mISDN_open();
2951 memset(buff,0,1025);
2953 if (midev<=0) return midev;
2955 /* create entity for layer 3 TE-mode */
2956 mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
2957 ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
2959 if (ret < mISDN_HEADER_LEN) {
2961 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
2965 entity = frm->dinfo & 0xffff ;
2974 void te_lib_destroy(int midev)
2977 mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
2979 cb_log(4, 0, "Entetity deleted\n");
2981 cb_log(4, 0, "midev closed\n");
2986 void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
2988 holded_bc->holded=0;
2991 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
2993 struct misdn_stack *stack;
2996 for (stack=glob_mgr->stack_list;
2998 stack=stack->next) {
2999 for (i=0; i<stack->b_num; i++)
3000 if (stack->bc[i].pid == pid) return &stack->bc[i];
3006 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
3008 struct misdn_stack *stack=get_stack_by_bc(bc);
3009 return find_bc_holded(stack);
3014 void prepare_bc(struct misdn_bchannel*bc, int channel)
3016 bc->channel = channel;
3017 bc->channel_preselected = channel?1:0;
3019 bc->need_disconnect=1;
3021 bc->need_release_complete=1;
3024 if (++mypid>5000) mypid=1;
3038 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel)
3040 struct misdn_stack *stack;
3043 if (channel < 0 || channel > MAX_BCHANS) {
3044 cb_log(0,port,"Requested channel out of bounds (%d)\n",channel);
3048 for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
3050 if (stack->port == port) {
3051 if (stack->blocked) {
3052 cb_log(0,port,"Port is blocked\n");
3057 if (channel <= stack->b_num) {
3058 for (i = 0; i < stack->b_num; i++) {
3059 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
3060 cb_log(0,port,"Requested channel:%d on port:%d is already in use\n",channel, port);
3065 cb_log(0,port,"Requested channel:%d is out of bounds on port:%d\n",channel, port);
3069 for (i = 0; i < stack->b_num; i++) {
3070 if (!stack->bc[i].in_use) {
3071 prepare_bc(&stack->bc[i], channel);
3072 return &stack->bc[i];
3076 cb_log(1,port,"There is no free channel on port (%d)\n",port);
3081 cb_log(0,port,"Port is not configured (%d)\n",port);
3086 char *fac2str (enum facility_type type) {
3088 enum facility_type p;
3091 { FACILITY_NONE, "FAC_NONE" },
3092 { FACILITY_CALLDEFLECT, "FAC_CALLDEFLECT"},
3093 { FACILITY_CENTREX, "FAC_CENTREX"}
3098 for (i=0; i < sizeof(arr)/sizeof( struct arr_el) ; i ++)
3099 if ( arr[i].p==type) return arr[i].s;
3101 return "FAC_UNKNOWN";
3104 void misdn_lib_log_ies(struct misdn_bchannel *bc)
3108 struct misdn_stack *stack=get_stack_by_bc(bc);
3112 cb_log(2, stack->port, " --> channel:%d mode:%s cause:%d ocause:%d rad:%s cad:%s\n", bc->channel, stack->nt?"NT":"TE", bc->cause, bc->out_cause, bc->rad, bc->cad);
3114 cb_log(2, stack->port,
3115 " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c cpnnumplan:%c\n",
3117 bc->onumplan>=0?'0'+bc->onumplan:' ',
3118 bc->dnumplan>=0?'0'+bc->dnumplan:' ',
3119 bc->rnumplan>=0?'0'+bc->rnumplan:' ',
3120 bc->cpnnumplan>=0?'0'+bc->cpnnumplan:' '
3123 cb_log(3, stack->port, " --> caps:%s pi:%x keypad:%s sending_complete:%d\n", bearer2str(bc->capability),bc->progress_indicator, bc->keypad, bc->sending_complete);
3124 cb_log(4, stack->port, " --> screen:%d --> pres:%d\n",
3125 bc->screen, bc->pres);
3127 cb_log(4, stack->port, " --> addr:%x l3id:%x b_stid:%x layer_id:%x\n", bc->pid, bc->addr, bc->l3_id, bc->b_stid, bc->layer_id);
3129 cb_log(4, stack->port, " --> facility:%s out_facility:%s\n",fac2str(bc->fac_type),fac2str(bc->out_fac_type));
3131 cb_log(5, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
3133 cb_log(5, stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
3136 int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
3144 struct misdn_stack *stack=get_stack_by_bc(bc);
3147 cb_log(0,bc->port,"SENDEVENT: no Stack for event:%s oad:%s dad:%s \n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad);
3151 cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->uperid:%x\n",stack->nt, stack->upper_id);
3153 if ( stack->nt && !stack->l1link) {
3156 cb_log(1, stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
3157 misdn_lib_get_l1_up(stack);
3161 cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s pid:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, bc->pid);
3162 cb_log(1, stack->port, " --> bc_state:%s\n",bc_state2str(bc->bc_state));
3163 misdn_lib_log_ies(bc);
3167 if (create_process(glob_mgr->midev, bc)<0) {
3168 cb_log(0, stack->port, " No free channel at the moment @ send_event\n");
3176 if (ret == -EINVAL) {
3177 cb_log(0,bc->port,"send_event: setup_bc failed\n");
3183 case EVENT_PROGRESS:
3184 case EVENT_ALERTING:
3185 case EVENT_PROCEEDING:
3186 case EVENT_SETUP_ACKNOWLEDGE:
3187 if (!bc->nt && !stack->ptp) break;
3190 case EVENT_RETRIEVE_ACKNOWLEDGE:
3192 if (bc->channel <=0 ) { /* else we have the channel already */
3193 bc->channel = find_free_chan_in_stack(stack, bc, 0);
3195 cb_log(0, stack->port, " No free channel at the moment\n");
3201 /* Its that i generate channels */
3205 if (ret == -EINVAL) {
3206 cb_log(0,bc->port,"send_event: setup_bc failed\n");
3209 if ( (event == EVENT_CONNECT ) && misdn_cap_is_speech(bc->capability) ) {
3210 if ( *bc->crypt_key ) {
3211 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);
3213 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
3216 if (!bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
3218 if (bc->ec_enable) manager_ec_enable(bc);
3221 if (!bec_initialized) bc->bnec_tail=0;
3223 if (bc->bnec_tail) {
3224 bc->ec=beroec_new(bc->bnec_tail, BEROEC_SUBBAND, bc->bnec_ah,
3225 bc->bnec_td, bc->bnec_zero, bc->bnec_adapt, bc->bnec_nlp);
3231 if (bc->txgain != 0) {
3232 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
3233 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
3236 if ( bc->rxgain != 0 ) {
3237 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
3238 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
3243 if (event == EVENT_RETRIEVE_ACKNOWLEDGE) {
3244 cb_log(0,bc->port,"DO WE NEED TO DO SOMETHING HERE WITH THE BC ?\n");
3248 case EVENT_HOLD_ACKNOWLEDGE:
3250 struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel));
3251 memcpy(holded_bc,bc,sizeof(struct misdn_bchannel));
3252 holded_bc->holded=1;
3253 stack_holder_add(stack,holded_bc);
3256 if (bc->bc_state == BCHAN_BRIDGED) {
3257 misdn_split_conf(bc,bc->conf_id);
3258 misdn_split_conf(bc->holded_bc,bc->holded_bc->conf_id);
3262 empty_chan_in_stack(stack,bc->channel);
3267 /** we set it up later at RETRIEVE_ACK again.**/
3268 /*holded_bc->upset=0;
3269 holded_bc->active=0;*/
3270 bc_state_change(holded_bc,BCHAN_CLEANED);
3272 cb_event( EVENT_NEW_BC, bc, holded_bc);
3276 /* finishing the channel eh ? */
3277 case EVENT_DISCONNECT:
3278 if (!bc->need_disconnect) {
3279 cb_log(0,bc->port," --> we have already send Disconnect\n");
3283 bc->need_disconnect=0;
3286 if (!bc->need_release) {
3287 cb_log(0,bc->port," --> we have already send Release\n");
3290 bc->need_disconnect=0;
3293 case EVENT_RELEASE_COMPLETE:
3294 if (!bc->need_release_complete) {
3295 cb_log(0,bc->port," --> we have already send Release_complete\n");
3298 bc->need_disconnect=0;
3300 bc->need_release_complete=0;
3303 case EVENT_CONNECT_ACKNOWLEDGE:
3305 if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
3306 int ret=setup_bc(bc);
3307 if (ret == -EINVAL){
3308 cb_log(0,bc->port,"send_event: setup_bc failed\n");
3314 if (misdn_cap_is_speech(bc->capability)) {
3315 if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
3316 if (bc->ec_enable) manager_ec_enable(bc);
3318 if (!bec_initialized) bc->bnec_tail=0;
3320 if (bc->bnec_tail) {
3321 bc->ec=beroec_new(bc->bnec_tail, BEROEC_SUBBAND, bc->bnec_ah,
3322 bc->bnec_td, bc->bnec_zero, bc->bnec_adapt, bc->bnec_nlp);
3327 if ( bc->txgain != 0 ) {
3328 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
3329 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
3331 if ( bc->rxgain != 0 ) {
3332 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
3333 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
3342 /* Later we should think about sending bchannel data directly to misdn. */
3343 msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
3344 msg_queue_tail(&stack->downqueue, msg);
3345 sem_post(&glob_mgr->new_msg);
3354 int handle_err(msg_t *msg)
3356 iframe_t *frm = (iframe_t*) msg->data;
3362 cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x\n",frm->prim,frm->dinfo);
3365 cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x (already more than 100 of them)\n",frm->prim,frm->dinfo);
3374 switch (frm->prim) {
3375 case MGR_SETSTACK|INDICATION:
3376 return handle_bchan(msg);
3379 case MGR_SETSTACK|CONFIRM:
3380 case MGR_CLEARSTACK|CONFIRM:
3385 case DL_DATA|CONFIRM:
3386 cb_log(4,0,"DL_DATA|CONFIRM\n");
3390 case PH_CONTROL|CONFIRM:
3391 cb_log(4,0,"PH_CONTROL|CONFIRM\n");
3395 case DL_DATA|INDICATION:
3397 int port=(frm->addr&MASTER_ID_MASK) >> 8;
3398 int channel=(frm->addr&CHILD_ID_MASK) >> 16;
3400 /*we flush the read buffer here*/
3402 cb_log(9,0,"BCHAN DATA without BC: addr:%x port:%d channel:%d\n",frm->addr, port,channel);
3408 struct misdn_bchannel *bc=find_bc_by_channel( port , channel);
3411 struct misdn_stack *stack=find_stack_by_port( port );
3414 cb_log(0,0," --> stack not found\n");
3419 cb_log(0,0," --> bc not found by channel\n");
3421 misdn_lib_get_l2_down(stack);
3424 misdn_lib_get_l1_down(stack);
3430 cb_log(3,port," --> BC in state:%s\n", bc_state2str(bc->bc_state));
3438 int queue_l2l3(msg_t *msg) {
3439 iframe_t *frm= (iframe_t*)msg->data;
3440 struct misdn_stack *stack;
3441 stack=find_stack_by_addr( frm->addr );
3448 msg_queue_tail(&stack->upqueue, msg);
3449 sem_post(&glob_mgr->new_msg);
3453 int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
3456 if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
3457 cb_log(0,0,"SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
3460 if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
3461 #ifdef MISDN_HANDLER_DEBUG
3462 cb_log(0,0,"handle_bchan START\n");
3464 if (handle_bchan(msg)) {
3465 #ifdef MISDN_HANDLER_DEBUG
3466 cb_log(0,0,"handle_bchan STOP\n");
3470 #ifdef MISDN_HANDLER_DEBUG
3471 cb_log(0,0,"handle_bchan NOTSTOP\n");
3475 if (handle_timers(msg))
3478 if (handle_mgmt(msg))
3484 /* Its important to handle l1 AFTER l2 */
3488 #ifdef MISDN_HANDLER_DEBUG
3489 cb_log(0,0,"handle_frm_nt START\n");
3491 if (handle_frm_nt(msg)) {
3492 #ifdef MISDN_HANDLER_DEBUG
3493 cb_log(0,0,"handle_frm_nt STOP\n");
3497 #ifdef MISDN_HANDLER_DEBUG
3498 cb_log(0,0,"handle_frm_nt NOTSTOP\n");
3500 cb_log(0,0,"handle_frm START\n");
3503 if (handle_frm(msg)) {
3504 #ifdef MISDN_HANDLER_DEBUG
3505 cb_log(0,0,"handle_frm STOP\n");
3510 #ifdef MISDN_HANDLER_DEBUG
3511 cb_log(0,0,"handle_frm NOTSTOP\n");
3513 cb_log(0,0,"handle_err START\n");
3516 if (handle_err(msg)) {
3517 #ifdef MISDN_HANDLER_DEBUG
3518 cb_log(0,0,"handle_err STOP\n");
3522 #ifdef MISDN_HANDLER_DEBUG
3523 cb_log(0,0,"handle_err NOTSTOP\n");
3527 cb_log(0, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);
3537 int misdn_lib_get_port_info(int port)
3539 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3541 struct misdn_stack *stack=find_stack_by_port(port);
3543 cb_log(0, port, "misgn_lib_get_port: alloc_msg failed!\n");
3546 frm=(iframe_t*)msg->data;
3548 cb_log(0, port, "There is no Stack for this port.\n");
3551 /* activate bchannel */
3552 frm->prim = CC_STATUS_ENQUIRY | REQUEST;
3554 frm->addr = stack->upper_id| FLG_MSG_DOWN;
3559 msg_queue_tail(&glob_mgr->activatequeue, msg);
3560 sem_post(&glob_mgr->new_msg);
3567 int queue_cleanup_bc(struct misdn_bchannel *bc)
3569 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3572 cb_log(0, bc->port, "misgn_lib_get_port: alloc_msg failed!\n");
3575 frm=(iframe_t*)msg->data;
3577 /* activate bchannel */
3578 frm->prim = MGR_CLEARSTACK| REQUEST;
3580 frm->addr = bc->l3_id;
3582 frm->dinfo = bc->port;
3585 msg_queue_tail(&glob_mgr->activatequeue, msg);
3586 sem_post(&glob_mgr->new_msg);
3592 int misdn_lib_port_restart(int port)
3594 struct misdn_stack *stack=find_stack_by_port(port);
3596 cb_log(0, port, "Restarting this port.\n");
3598 cb_log(0, port, "Stack:%p\n",stack);