finish chan_misdn commit
authorKevin P. Fleming <kpfleming@digium.com>
Mon, 31 Oct 2005 23:30:09 +0000 (23:30 +0000)
committerKevin P. Fleming <kpfleming@digium.com>
Mon, 31 Oct 2005 23:30:09 +0000 (23:30 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6915 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/misdn/Makefile [new file with mode: 0755]
channels/misdn/chan_misdn_config.h [new file with mode: 0755]
channels/misdn/ie.c [new file with mode: 0755]
channels/misdn/isdn_lib.c [new file with mode: 0755]
channels/misdn/isdn_lib.h [new file with mode: 0755]
channels/misdn/isdn_msg_parser.c [new file with mode: 0755]
channels/misdn/portinfo.c [new file with mode: 0755]

diff --git a/channels/misdn/Makefile b/channels/misdn/Makefile
new file mode 100755 (executable)
index 0000000..0e0917a
--- /dev/null
@@ -0,0 +1,45 @@
+#
+# Makefile
+#
+# Make file for chan_misdn support
+#
+
+# Verify those options with main Makefile
+ifndef LINUX
+LINUX=/lib/modules/$(shell uname -r)/build
+endif
+
+CFLAGS         += -pipe -c
+SOURCES                = isdn_lib.c isdn_msg_parser.c 
+OBJDIR         = .
+OBJS           = isdn_lib.o isdn_msg_parser.o
+
+ifndef MISDNUSER
+MISDNUSER=/usr/src/install-misdn/mISDNuser
+endif
+
+MISDNCFLAGS    += -I$(MISDNUSER)/include -I$(MISDNUSER)/i4lnet -I$(MISDNUSER)/lib
+MISDNCFLAGS    += -DMISDNUSER_JOLLY -I$(LINUX)/include
+
+
+all: chan_misdn_lib.a Makefile.ast
+
+
+%.o: %.c
+       $(CC) $(MISDNCFLAGS) $(CFLAGS) -o $@ $<
+       
+
+chan_misdn_lib.a:      $(OBJS)
+       ar crv $@ $(OBJS)
+
+Makefile.ast:  FORCE
+       @echo CFLAGS+=$(MISDNCFLAGS) -Imisdn/ -DCHAN_MISDN_VERSION=\\\"0.1.1\\\" >$@.tmp
+       @echo MISDNUSER = $(MISDNUSER) >>$@.tmp
+       @if [ -r $@ ] && cmp -s $@ $@.tmp; then rm -f $@.tmp; else mv -f $@.tmp $@; fi
+
+
+FORCE:
+
+
+clean: 
+       rm *.a *.o Makefile.ast
diff --git a/channels/misdn/chan_misdn_config.h b/channels/misdn/chan_misdn_config.h
new file mode 100755 (executable)
index 0000000..328d888
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Chan_Misdn -- Channel Driver for Asterisk
+ *
+ * Interface to mISDN
+ *
+ * Copyright (C) 2004, Christian Richter
+ *
+ * Christian Richter <crich@beronet.com>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+
+
+#ifndef CHAN_MISDN_CONFIG_H
+#define CHAN_MISDN_CONFIG_H
+
+#define BUFFERSIZE 512
+
+enum misdn_cfg_elements {
+
+       /* port config items */
+       MISDN_CFG_FIRST = 0,
+       MISDN_CFG_PTP,                 /* int (bool) */
+       MISDN_CFG_GROUPNAME,           /* char[] */
+       MISDN_CFG_RXGAIN,              /* int */
+       MISDN_CFG_TXGAIN,              /* int */
+       MISDN_CFG_TE_CHOOSE_CHANNEL,   /* int (bool) */
+       MISDN_CFG_CONTEXT,             /* char[] */
+       MISDN_CFG_LANGUAGE,            /* char[] */
+       MISDN_CFG_CALLERID,            /* char[] */
+       MISDN_CFG_METHOD,              /* char[] */
+       MISDN_CFG_DIALPLAN,            /* int */
+       MISDN_CFG_NATPREFIX,           /* char[] */
+       MISDN_CFG_INTERNATPREFIX,      /* char[] */
+       MISDN_CFG_PRES,                /* int (bool) */
+       MISDN_CFG_ALWAYS_IMMEDIATE,    /* int (bool) */
+       MISDN_CFG_IMMEDIATE,           /* int (bool) */
+       MISDN_CFG_HOLD_ALLOWED,        /* int (bool) */
+       MISDN_CFG_EARLY_BCONNECT,      /* int (bool) */
+       MISDN_CFG_USE_CALLINGPRES,     /* int (bool) */
+       MISDN_CFG_ECHOCANCEL,          /* int */
+       MISDN_CFG_ECHOCANCELWHENBRIDGED,  /* int (bool) */
+       MISDN_CFG_ECHOTRAINING,        /* int (bool) */
+       MISDN_CFG_CALLGROUP,           /* ast_group_t */
+       MISDN_CFG_PICKUPGROUP,         /* ast_group_t */
+       MISDN_CFG_MSNS,                /* char[] */
+       MISDN_CFG_LAST,
+       
+       /* general config items */
+       MISDN_GEN_FIRST,
+       MISDN_GEN_DEBUG,               /* int */
+       MISDN_GEN_TRACEFILE,           /* char[] */
+       MISDN_GEN_TRACE_CALLS,         /* int (bool) */
+       MISDN_GEN_TRACE_DIR,           /* char[] */
+       MISDN_GEN_BRIDGING,            /* int (bool) */
+       MISDN_GEN_STOP_TONE,           /* int (bool) */
+       MISDN_GEN_APPEND_DIGITS2EXTEN, /* int (bool) */
+       MISDN_GEN_L1_INFO_OK,          /* int (bool) */
+       MISDN_GEN_CLEAR_L3,            /* int (bool) */
+       MISDN_GEN_DYNAMIC_CRYPT,       /* int (bool) */
+       MISDN_GEN_CRYPT_PREFIX,        /* char[] */
+       MISDN_GEN_CRYPT_KEYS,          /* char[] */
+       MISDN_GEN_LAST
+};
+
+enum misdn_cfg_method {
+       METHOD_STANDARD = 0,
+       METHOD_ROUND_ROBIN
+};
+
+/* you must call misdn_cfg_init before any other function of this header file */
+void misdn_cfg_init(int max_ports); 
+void misdn_cfg_reload(void);
+void misdn_cfg_destroy(void);
+
+/* if you requst a general config element, the port value is ignored. if the requested 
+ * value is not available, or the buffer is too small, the buffer will be nulled (in 
+ * case of a char* only its first byte will be nulled). */
+void misdn_cfg_get(int port, enum misdn_cfg_elements elem, void* buf, int bufsize);
+
+/* fills the buffer with a ',' separated list of all active ports */
+void misdn_cfg_get_ports_string(char *ports);
+
+/* fills the buffer with a nice printable string representation of the config element */
+void misdn_cfg_get_config_string(int port, enum misdn_cfg_elements elem, char* buf, int bufsize);
+
+/* returns the next available port number. returns -1 if the last one was reached. */
+int misdn_cfg_get_next_port(int port);
+int misdn_cfg_get_next_port_spin(int port);
+
+int misdn_cfg_is_msn_valid(int port, char* msn);
+int misdn_cfg_is_port_valid(int port);
+int misdn_cfg_is_group_method(char *group, enum misdn_cfg_method meth);
+
+#endif
diff --git a/channels/misdn/ie.c b/channels/misdn/ie.c
new file mode 100755 (executable)
index 0000000..b82d31b
--- /dev/null
@@ -0,0 +1,1608 @@
+
+/*
+ * Chan_Misdn -- Channel Driver for Asterisk
+ *
+ * Interface to mISDN
+ *
+ * Copyright (C) 2005, Christian Richter
+ *
+ * Christian Richter <crich@beronet.com>
+ *
+ * heaviliy patched from jollys ie.cpp, jolly gave me ALL
+ * rights for this code, i can even have my own copyright on it.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+/*
+  the pointer of enc_ie_* always points to the IE itself
+  if qi is not NULL (TE-mode), offset is set
+*/
+
+#include "isdn_lib.h"
+
+#define CENTREX_FAC     0x88
+#define CENTREX_ID      0xa1
+
+#define MISDN_IE_DEBG 0
+
+/* support stuff */
+static void strnncpy(unsigned char *dest, unsigned char *src, int len, int dst_len)
+{
+       if (len > dst_len-1)
+               len = dst_len-1;
+       strncpy((char *)dest, (char *)src, len);
+       dest[len] = '\0';
+}
+
+
+/* IE_COMPLETE */
+void enc_ie_complete(unsigned char **ntmode, msg_t *msg, int complete, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+
+       if (complete<0 || complete>1)
+       {
+               printf("%s: ERROR: complete(%d) is out of range.\n", __FUNCTION__, complete);
+               return;
+       }
+
+       if (complete)
+               if (MISDN_IE_DEBG) printf("    complete=%d\n", complete);
+
+       if (complete)
+       {
+               p = msg_put(msg, 1);
+               if (nt)
+               {
+                       *ntmode = p;
+               } else
+                       qi->sending_complete = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               p[0] = IE_COMPLETE;
+       }
+}
+
+void dec_ie_complete(unsigned char *p, Q931_info_t *qi, int *complete, int nt, struct misdn_bchannel *bc)
+{
+       *complete = 0;
+       if (!nt)
+       {
+               if (qi->sending_complete)
+                       *complete = 1;
+       } else
+               if (p)
+                       *complete = 1;
+
+       if (*complete)
+               if (MISDN_IE_DEBG) printf("    complete=%d\n", *complete);
+}
+
+
+/* IE_BEARER */
+void enc_ie_bearer(unsigned char **ntmode, msg_t *msg, int coding, int capability, int mode, int rate, int multi, int user, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (coding<0 || coding>3)
+       {
+               printf("%s: ERROR: coding(%d) is out of range.\n", __FUNCTION__, coding);
+               return;
+       }
+       if (capability<0 || capability>31)
+       {
+               printf("%s: ERROR: capability(%d) is out of range.\n", __FUNCTION__, capability);
+               return;
+       }
+       if (mode<0 || mode>3)
+       {
+               printf("%s: ERROR: mode(%d) is out of range.\n", __FUNCTION__, mode);
+               return;
+       }
+       if (rate<0 || rate>31)
+       {
+               printf("%s: ERROR: rate(%d) is out of range.\n", __FUNCTION__, rate);
+               return;
+       }
+       if (multi>127)
+       {
+               printf("%s: ERROR: multi(%d) is out of range.\n", __FUNCTION__, multi);
+               return;
+       }
+       if (user>31)
+       {
+               printf("%s: ERROR: user L1(%d) is out of range.\n", __FUNCTION__, rate);
+               return;
+       }
+       if (rate!=24 && multi>=0)
+       {
+               printf("%s: WARNING: multi(%d) is only possible if rate(%d) would be 24.\n", __FUNCTION__, multi, rate);
+               multi = -1;
+       }
+
+       if (MISDN_IE_DEBG) printf("    coding=%d capability=%d mode=%d rate=%d multi=%d user=%d\n", coding, capability, mode, rate, multi, user);
+
+       l = 2 + (multi>=0) + (user>=0);
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->bearer_capability = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_BEARER;
+       p[1] = l;
+       p[2] = 0x80 + (coding<<5) + capability;
+       p[3] = 0x80 + (mode<<5) + rate;
+       if (multi >= 0)
+               p[4] = 0x80 + multi;
+       if (user >= 0)
+               p[4+(multi>=0)] = 0xa0 + user;
+}
+
+void dec_ie_bearer(unsigned char *p, Q931_info_t *qi, int *coding, int *capability, int *mode, int *rate, int *multi, int *user, 
+                  int *async, int *urate, int *stopbits, int *dbits, int *parity, int nt, struct misdn_bchannel *bc)
+{
+       int octet;
+       *coding = -1;
+       *capability = -1;
+       *mode = -1;
+       *rate = -1;
+       *multi = -1;
+       *user = -1;
+       *async = -1;
+       *urate = -1;
+       *stopbits = -1;
+       *dbits = -1;
+       *parity = -1;
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->llc)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->llc + 1;
+               else if (qi->bearer_capability)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->bearer_capability + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 2)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *coding = (p[1]&0x60) >> 5;
+       *capability = p[1] & 0x1f;
+       octet = 2;
+       if (!(p[1] & 0x80))
+               octet++;
+
+       if (p[0] < octet)
+               goto done;
+
+       *mode = (p[octet]&0x60) >> 5;
+       *rate = p[octet] & 0x1f;
+
+       octet++;
+
+       if (p[0] < octet)
+               goto done;
+
+       if (*rate == 0x18) {
+               /* Rate multiplier only present if 64Kb/s base rate */
+               *multi = p[octet++] & 0x7f;
+       }
+
+       if (p[0] < octet)
+               goto done;
+
+       /* Start L1 info */
+       if ((p[octet] & 0x60) == 0x20) {
+               *user = p[octet] & 0x1f;
+
+               if (p[0] <= octet)
+                       goto done;
+               
+               if (p[octet++] & 0x80)
+                       goto l2;
+
+               *async = !!(p[octet] & 0x40);
+               /* 0x20 is inband negotiation */
+               *urate = p[octet] & 0x1f;
+
+               if (p[0] <= octet)
+                       goto done;
+               
+               if (p[octet++] & 0x80)
+                       goto l2;
+
+               /* Ignore next byte for now: Intermediate rate, NIC, flow control */
+
+               if (p[0] <= octet)
+                       goto done;
+               
+               if (p[octet++] & 0x80)
+                       goto l2;
+
+               /* And the next one. Header, multiframe, mode, assignor/ee, negotiation */
+
+               if (p[0] <= octet)
+                       goto done;
+               
+               if (!p[octet++] & 0x80)
+                       goto l2;
+
+               /* Wheee. V.110 speed information */
+
+               *stopbits = (p[octet] & 0x60) >> 5;
+               *dbits = (p[octet] & 0x18) >> 3; 
+               *parity = p[octet] & 7;
+
+               octet++;
+       }
+ l2: /* Nobody seems to want the rest so we don't bother (yet) */
+ done:         
+       if (MISDN_IE_DEBG) printf("    coding=%d capability=%d mode=%d rate=%d multi=%d user=%d async=%d urate=%d stopbits=%d dbits=%d parity=%d\n", *coding, *capability, *mode, *rate, *multi, *user, *async, *urate, *stopbits, *dbits, *parity);
+}
+
+
+/* IE_CALL_ID */
+void enc_ie_call_id(unsigned char **ntmode, msg_t *msg, unsigned char *callid, int callid_len, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       char debug[25];
+       int i;
+
+       if (!callid || callid_len<=0)
+       {
+               return;
+       }
+       if (callid_len>8)
+       {
+               printf("%s: ERROR: callid_len(%d) is out of range.\n", __FUNCTION__, callid_len);
+               return;
+       }
+
+       i = 0;
+       while(i < callid_len)
+       {
+               if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", callid[i]);
+               i++;
+       }
+               
+       if (MISDN_IE_DEBG) printf("    callid%s\n", debug);
+
+       l = callid_len;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->call_id = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_CALL_ID;
+       p[1] = l;
+       memcpy(p+2, callid, callid_len);
+}
+
+void dec_ie_call_id(unsigned char *p, Q931_info_t *qi, unsigned char *callid, int *callid_len, int nt, struct misdn_bchannel *bc)
+{
+       char debug[25];
+       int i;
+
+       *callid_len = -1;
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->call_id)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->call_id + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] > 8)
+       {
+               printf("%s: ERROR: IE too long (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *callid_len = p[0];
+       memcpy(callid, p+1, *callid_len);
+
+       i = 0;
+       while(i < *callid_len)
+       {
+               if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", callid[i]);
+               i++;
+       }
+               
+       if (MISDN_IE_DEBG) printf("    callid%s\n", debug);
+}
+
+
+/* IE_CALLED_PN */
+void enc_ie_called_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, unsigned char *number, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (type<0 || type>7)
+       {
+               printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type);
+               return;
+       }
+       if (plan<0 || plan>15)
+       {
+               printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan);
+               return;
+       }
+       if (!number[0])
+       {
+               printf("%s: ERROR: number is not given.\n", __FUNCTION__);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d number='%s'\n", type, plan, number);
+
+       l = 1+strlen((char *)number);
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->called_nr = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_CALLED_PN;
+       p[1] = l;
+       p[2] = 0x80 + (type<<4) + plan;
+       strncpy((char *)p+3, (char *)number, strlen((char *)number));
+}
+
+void dec_ie_called_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc)
+{
+       *type = -1;
+       *plan = -1;
+       *number = '\0';
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->called_nr)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->called_nr + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 2)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *type = (p[1]&0x70) >> 4;
+       *plan = p[1] & 0xf;
+       strnncpy(number, p+2, p[0]-1, number_len);
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d number='%s'\n", *type, *plan, number);
+}
+
+
+/* IE_CALLING_PN */
+void enc_ie_calling_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, unsigned char *number, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (type<0 || type>7)
+       {
+               printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type);
+               return;
+       }
+       if (plan<0 || plan>15)
+       {
+               printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan);
+               return;
+       }
+       if (present>3)
+       {
+               printf("%s: ERROR: present(%d) is out of range.\n", __FUNCTION__, present);
+               return;
+       }
+       if (present >= 0) if (screen<0 || screen>3)
+       {
+               printf("%s: ERROR: screen(%d) is out of range.\n", __FUNCTION__, screen);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d present=%d screen=%d number='%s'\n", type, plan, present, screen, number);
+
+       l = 1;
+       if (number) if (number[0])
+               l += strlen((char *)number);
+       if (present >= 0)
+               l += 1;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->calling_nr = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_CALLING_PN;
+       p[1] = l;
+       if (present >= 0)
+       {
+               p[2] = 0x00 + (type<<4) + plan;
+               p[3] = 0x80 + (present<<5) + screen;
+               if (number) if (number[0])
+                       strncpy((char *)p+4, (char *)number, strlen((char *)number));
+       } else
+       {
+               p[2] = 0x80 + (type<<4) + plan;
+               if (number) if (number[0])
+                       strncpy((char *)p+3, (char *)number, strlen((char *)number));
+       }
+}
+
+void dec_ie_calling_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc)
+{
+       *type = -1;
+       *plan = -1;
+       *present = -1;
+       *screen = -1;
+       *number = '\0';
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->calling_nr)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->calling_nr + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *type = (p[1]&0x70) >> 4;
+       *plan = p[1] & 0xf;
+       if (!(p[1] & 0x80))
+       {
+               if (p[0] < 2)
+               {
+                       printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+                       return;
+               }
+               *present = (p[2]&0x60) >> 5;
+               *screen = p[2] & 0x3;
+               strnncpy(number, p+3, p[0]-2, number_len);
+       } else
+       {
+               strnncpy(number, p+2, p[0]-1, number_len);
+               /* SPECIAL workarround for IBT software bug */ 
+               /* if (number[0]==0x80) */
+               /*  strcpy((char *)number, (char *)number+1); */
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d present=%d screen=%d number='%s'\n", *type, *plan, *present, *screen, number);
+}
+
+
+/* IE_CONNECTED_PN */
+void enc_ie_connected_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, unsigned char *number, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (type<0 || type>7)
+       {
+               printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type);
+               return;
+       }
+       if (plan<0 || plan>15)
+       {
+               printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan);
+               return;
+       }
+       if (present>3)
+       {
+               printf("%s: ERROR: present(%d) is out of range.\n", __FUNCTION__, present);
+               return;
+       }
+       if (present >= 0) if (screen<0 || screen>3)
+       {
+               printf("%s: ERROR: screen(%d) is out of range.\n", __FUNCTION__, screen);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d present=%d screen=%d number='%s'\n", type, plan, present, screen, number);
+
+       l = 1;
+       if (number) if (number[0])
+               l += strlen((char *)number);
+       if (present >= 0)
+               l += 1;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->connected_nr = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_CONNECT_PN;
+       p[1] = l;
+       if (present >= 0)
+       {
+               p[2] = 0x00 + (type<<4) + plan;
+               p[3] = 0x80 + (present<<5) + screen;
+               if (number) if (number[0])
+                       strncpy((char *)p+4, (char *)number, strlen((char *)number));
+       } else
+       {
+               p[2] = 0x80 + (type<<4) + plan;
+               if (number) if (number[0])
+                       strncpy((char *)p+3, (char *)number, strlen((char *)number));
+       }
+}
+
+void dec_ie_connected_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc)
+{
+       *type = -1;
+       *plan = -1;
+       *present = -1;
+       *screen = -1;
+       *number = '\0';
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->connected_nr)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->connected_nr + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *type = (p[1]&0x70) >> 4;
+       *plan = p[1] & 0xf;
+       if (!(p[1] & 0x80))
+       {
+               if (p[0] < 2)
+               {
+                       printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+                       return;
+               }
+               *present = (p[2]&0x60) >> 5;
+               *screen = p[2] & 0x3;
+               strnncpy(number, p+3, p[0]-2, number_len);
+       } else
+       {
+               strnncpy(number, p+2, p[0]-1, number_len);
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d present=%d screen=%d number='%s'\n", *type, *plan, *present, *screen, number);
+}
+
+
+/* IE_CAUSE */
+void enc_ie_cause(unsigned char **ntmode, msg_t *msg, int location, int cause, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (location<0 || location>7)
+       {
+               printf("%s: ERROR: location(%d) is out of range.\n", __FUNCTION__, location);
+               return;
+       }
+       if (cause<0 || cause>127)
+       {
+               printf("%s: ERROR: cause(%d) is out of range.\n", __FUNCTION__, cause);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    location=%d cause=%d\n", location, cause);
+
+       l = 2;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->cause = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_CAUSE;
+       p[1] = l;
+       p[2] = 0x80 + location;
+       p[3] = 0x80 + cause;
+}
+void enc_ie_cause_standalone(unsigned char **ntmode, msg_t *msg, int location, int cause, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p = msg_put(msg, 4);
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       if (ntmode)
+               *ntmode = p+1;
+       else
+               qi->cause = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_CAUSE;
+       p[1] = 2;
+       p[2] = 0x80 + location;
+       p[3] = 0x80 + cause;
+}
+
+
+void dec_ie_cause(unsigned char *p, Q931_info_t *qi, int *location, int *cause, int nt, struct misdn_bchannel *bc)
+{
+       *location = -1;
+       *cause = -1;
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->cause)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->cause + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 2)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *location = p[1] & 0x0f;
+       *cause = p[2] & 0x7f;
+
+       if (MISDN_IE_DEBG) printf("    location=%d cause=%d\n", *location, *cause);
+}
+
+
+/* IE_CHANNEL_ID */
+void enc_ie_channel_id(unsigned char **ntmode, msg_t *msg, int exclusive, int channel, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+       int pri = bc->stack->pri;
+
+       if (exclusive<0 || exclusive>1)
+       {
+               printf("%s: ERROR: exclusive(%d) is out of range.\n", __FUNCTION__, exclusive);
+               return;
+       }
+       if ((channel<0 || channel>0xff)
+           || (!pri && (channel>2 && channel<0xff))
+           || (pri && (channel>31 && channel<0xff))
+           || (pri && channel==16))
+       {
+               printf("%s: ERROR: channel(%d) is out of range.\n", __FUNCTION__, channel);
+               return;
+       }
+
+       /* if (MISDN_IE_DEBG) printf("    exclusive=%d channel=%d\n", exclusive, channel); */
+       
+
+       if (!pri)
+       {
+               /* BRI */
+               l = 1;
+               p = msg_put(msg, l+2);
+               if (nt)
+                       *ntmode = p+1;
+               else
+                       qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               p[0] = IE_CHANNEL_ID;
+               p[1] = l;
+               if (channel == 0xff)
+                       channel = 3;
+               p[2] = 0x80 + (exclusive<<3) + channel;
+               /* printf("    exclusive=%d channel=%d\n", exclusive, channel); */
+       } else
+       {
+               /* PRI */
+               if (channel == 0) /* no channel */
+                       return; /* IE not present */
+/*             if (MISDN_IE_DEBG) printf("channel = %d\n", channel); */
+               if (channel == 0xff) /* any channel */
+               {
+                       l = 1;
+                       p = msg_put(msg, l+2);
+                       if (nt)
+                               *ntmode = p+1;
+                       else
+                               qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t);
+                       p[0] = IE_CHANNEL_ID;
+                       p[1] = l;
+                       p[2] = 0x80 + 0x20 + 0x03;
+/*                     if (MISDN_IE_DEBG) printf("%02x\n", p[2]); */
+                       return; /* end */
+               }
+               l = 3;
+               p = msg_put(msg, l+2);
+               if (nt)
+                       *ntmode = p+1;
+               else
+                       qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               p[0] = IE_CHANNEL_ID;
+               p[1] = l;
+               p[2] = 0x80 + 0x20 + (exclusive<<3) + 0x01;
+               p[3] = 0x80 + 3; /* CCITT, Number, B-type */
+               p[4] = 0x80 + channel;
+/*             if (MISDN_IE_DEBG) printf("%02x %02x %02x\n", p[2], p[3], p[4]); */
+       }
+}
+
+void dec_ie_channel_id(unsigned char *p, Q931_info_t *qi, int *exclusive, int *channel, int nt, struct misdn_bchannel *bc)
+{
+       int pri = bc->stack->pri;
+
+       *exclusive = -1;
+       *channel = -1;
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->channel_id)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->channel_id + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       if (p[1] & 0x40)
+       {
+               printf("%s: ERROR: refering to channels of other interfaces is not supported.\n", __FUNCTION__);
+               return;
+       }
+       if (p[1] & 0x04)
+       {
+               printf("%s: ERROR: using d-channel is not supported.\n", __FUNCTION__);
+               return;
+       }
+
+       *exclusive = (p[1]&0x08) >> 3;
+       if (!pri)
+       {
+               /* BRI */
+               if (p[1] & 0x20)
+               {
+                       printf("%s: ERROR: extended channel ID with non PRI interface.\n", __FUNCTION__);
+                       return;
+               }
+               *channel = p[1] & 0x03;
+               if (*channel == 3)
+                       *channel = 0xff;
+       } else
+       {
+               /* PRI */
+               if (p[0] < 1)
+               {
+                       printf("%s: ERROR: IE too short for PRI (%d).\n", __FUNCTION__, p[0]);
+                       return;
+               }
+               if (!(p[1] & 0x20))
+               {
+                       printf("%s: ERROR: basic channel ID with PRI interface.\n", __FUNCTION__);
+                       return;
+               }
+               if ((p[1]&0x03) == 0x00)
+               {
+                       /* no channel */
+                       *channel = 0;
+                       return;
+               }
+               if ((p[1]&0x03) == 0x03)
+               {
+                       /* any channel */
+                       *channel = 0xff;
+                       return;
+               }
+               if (p[0] < 3)
+               {
+                       printf("%s: ERROR: IE too short for PRI with channel(%d).\n", __FUNCTION__, p[0]);
+                       return;
+               }
+               if (p[2] & 0x10)
+               {
+                       printf("%s: ERROR: channel map not supported.\n", __FUNCTION__);
+                       return;
+               }
+               *channel = p[3] & 0x7f;
+               if ( (*channel<1) | (*channel==16) | (*channel>31))
+               {
+                       printf("%s: ERROR: PRI interface channel out of range (%d).\n", __FUNCTION__, *channel);
+                       return;
+               }
+/*             if (MISDN_IE_DEBG) printf("%02x %02x %02x\n", p[1], p[2], p[3]); */
+       }
+
+       if (MISDN_IE_DEBG) printf("    exclusive=%d channel=%d\n", *exclusive, *channel);
+}
+
+
+/* IE_DATE */
+void enc_ie_date(unsigned char **ntmode, msg_t *msg, time_t ti, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       struct tm *tm;
+
+       tm = localtime(&ti);
+       if (!tm)
+       {
+               printf("%s: ERROR: gettimeofday() returned NULL.\n", __FUNCTION__);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    year=%d month=%d day=%d hour=%d minute=%d\n", tm->tm_year%100, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
+
+       l = 5;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->date = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_DATE;
+       p[1] = l;
+       p[2] = tm->tm_year % 100;
+       p[3] = tm->tm_mon + 1;
+       p[4] = tm->tm_mday;
+       p[5] = tm->tm_hour;
+       p[6] = tm->tm_min;
+}
+
+
+/* IE_DISPLAY */
+void enc_ie_display(unsigned char **ntmode, msg_t *msg, unsigned char *display, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (!display[0])
+       {
+               printf("%s: ERROR: display text not given.\n", __FUNCTION__);
+               return;
+       }
+
+       if (strlen((char *)display) > 80)
+       {
+               printf("%s: WARNING: display text too long (max 80 chars), cutting.\n", __FUNCTION__);
+               display[80] = '\0';
+       }
+
+       /* if (MISDN_IE_DEBG) printf("    display='%s' (len=%d)\n", display, strlen((char *)display)); */
+
+       l = strlen((char *)display);
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->display = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_DISPLAY;
+       p[1] = l;
+       strncpy((char *)p+2, (char *)display, strlen((char *)display));
+}
+
+void dec_ie_display(unsigned char *p, Q931_info_t *qi, unsigned char *display, int display_len, int nt, struct misdn_bchannel *bc)
+{
+       *display = '\0';
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->display)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->display + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       strnncpy(display, p+1, p[0], display_len);
+
+       if (MISDN_IE_DEBG) printf("    display='%s'\n", display);
+}
+
+
+/* IE_KEYPAD */
+void enc_ie_keypad(unsigned char **ntmode, msg_t *msg, unsigned char *keypad, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (!keypad[0])
+       {
+               printf("%s: ERROR: keypad info not given.\n", __FUNCTION__);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    keypad='%s'\n", keypad);
+
+       l = strlen((char *)keypad);
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->keypad = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_KEYPAD;
+       p[1] = l;
+       strncpy((char *)p+2, (char *)keypad, strlen((char *)keypad));
+}
+
+void dec_ie_keypad(unsigned char *p, Q931_info_t *qi, unsigned char *keypad, int keypad_len, int nt, struct misdn_bchannel *bc)
+{
+       *keypad = '\0';
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->keypad)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->keypad + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       strnncpy(keypad, p+1, p[0], keypad_len);
+
+       if (MISDN_IE_DEBG) printf("    keypad='%s'\n", keypad);
+}
+
+
+/* IE_NOTIFY */
+void enc_ie_notify(unsigned char **ntmode, msg_t *msg, int notify, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (notify<0 || notify>0x7f)
+       {
+               printf("%s: ERROR: notify(%d) is out of range.\n", __FUNCTION__, notify);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    notify=%d\n", notify);
+
+       l = 1;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->notify = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_NOTIFY;
+       p[1] = l;
+       p[2] = 0x80 + notify;
+}
+
+void dec_ie_notify(unsigned char *p, Q931_info_t *qi, int *notify, int nt, struct misdn_bchannel *bc)
+{
+       *notify = -1;
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->notify)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->notify + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *notify = p[1] & 0x7f;
+
+       if (MISDN_IE_DEBG) printf("    notify=%d\n", *notify);
+}
+
+
+/* IE_PROGRESS */
+void enc_ie_progress(unsigned char **ntmode, msg_t *msg, int coding, int location, int progress, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (coding<0 || coding>0x03)
+       {
+               printf("%s: ERROR: coding(%d) is out of range.\n", __FUNCTION__, coding);
+               return;
+       }
+       if (location<0 || location>0x0f)
+       {
+               printf("%s: ERROR: location(%d) is out of range.\n", __FUNCTION__, location);
+               return;
+       }
+       if (progress<0 || progress>0x7f)
+       {
+               printf("%s: ERROR: progress(%d) is out of range.\n", __FUNCTION__, progress);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    coding=%d location=%d progress=%d\n", coding, location, progress);
+
+       l = 2;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->progress = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_PROGRESS;
+       p[1] = l;
+       p[2] = 0x80 + (coding<<5) + location;
+       p[3] = 0x80 + progress;
+}
+
+void dec_ie_progress(unsigned char *p, Q931_info_t *qi, int *coding, int *location, int *progress, int nt, struct misdn_bchannel *bc)
+{
+       *coding = -1;
+       *location = -1;
+       //*progress = -1;
+       *progress = 0;
+       
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->progress)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->progress + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *coding = (p[1]&0x60) >> 5;
+       *location = p[1] & 0x0f;
+       *progress = p[2] & 0x7f;
+
+       if (MISDN_IE_DEBG) printf("    coding=%d location=%d progress=%d\n", *coding, *location, *progress);
+}
+
+
+/* IE_REDIR_NR (redirecting = during MT_SETUP) */
+void enc_ie_redir_nr(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, int reason, unsigned char *number, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       if (type<0 || type>7)
+       {
+               printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type);
+               return;
+       }
+       if (plan<0 || plan>15)
+       {
+               printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan);
+               return;
+       }
+       if (present > 3)
+       {
+               printf("%s: ERROR: present(%d) is out of range.\n", __FUNCTION__, present);
+               return;
+       }
+       if (present >= 0) if (screen<0 || screen>3)
+       {
+               printf("%s: ERROR: screen(%d) is out of range.\n", __FUNCTION__, screen);
+               return;
+       }
+       if (reason > 0x0f)
+       {
+               printf("%s: ERROR: reason(%d) is out of range.\n", __FUNCTION__, reason);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d present=%d screen=%d readon=%d number='%s'\n", type, plan, present, screen, reason, number);
+
+       l = 1;
+       if (number)
+               l += strlen((char *)number);
+       if (present >= 0)
+       {
+               l += 1;
+               if (reason >= 0)
+                       l += 1;
+       }
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->redirect_nr = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_REDIR_NR;
+       p[1] = l;
+       if (present >= 0)
+       {
+               if (reason >= 0)
+               {
+                       p[2] = 0x00 + (type<<4) + plan;
+                       p[3] = 0x00 + (present<<5) + screen;
+                       p[4] = 0x80 + reason;
+                       if (number)
+                               strncpy((char *)p+5, (char *)number, strlen((char *)number));
+               } else
+               {
+                       p[2] = 0x00 + (type<<4) + plan;
+                       p[3] = 0x80 + (present<<5) + screen;
+                       if (number)
+                               strncpy((char *)p+4, (char *)number, strlen((char *)number));
+               }
+       } else
+       {
+               p[2] = 0x80 + (type<<4) + plan;
+               if (number) if (number[0])
+                       strncpy((char *)p+3, (char *)number, strlen((char *)number));
+       }
+}
+
+void dec_ie_redir_nr(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, int *reason, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc)
+{
+       *type = -1;
+       *plan = -1;
+       *present = -1;
+       *screen = -1;
+       *reason = -1;
+       *number = '\0';
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->redirect_nr)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->redirect_nr + 1;
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *type = (p[1]&0x70) >> 4;
+       *plan = p[1] & 0xf;
+       if (!(p[1] & 0x80))
+       {
+               *present = (p[2]&0x60) >> 5;
+               *screen = p[2] & 0x3;
+               if (!(p[2] & 0x80))
+               {
+                       *reason = p[3] & 0x0f;
+                       strnncpy(number, p+4, p[0]-3, number_len);
+               } else
+               {
+                       strnncpy(number, p+3, p[0]-2, number_len);
+               }
+       } else
+       {
+               strnncpy(number, p+2, p[0]-1, number_len);
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d present=%d screen=%d reason=%d number='%s'\n", *type, *plan, *present, *screen, *reason, number);
+}
+
+
+/* IE_REDIR_DN (redirection = during MT_NOTIFY) */
+void enc_ie_redir_dn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, unsigned char *number, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+/*     Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); */
+       int l;
+
+       if (type<0 || type>7)
+       {
+               printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type);
+               return;
+       }
+       if (plan<0 || plan>15)
+       {
+               printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan);
+               return;
+       }
+       if (present > 3)
+       {
+               printf("%s: ERROR: present(%d) is out of range.\n", __FUNCTION__, present);
+               return;
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d present=%d number='%s'\n", type, plan, present, number);
+
+       l = 1;
+       if (number)
+               l += strlen((char *)number);
+       if (present >= 0)
+               l += 1;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+/* #warning REINSERT redir_dn, when included in te-mode */
+               /*qi->redir_dn = p - (unsigned char *)qi - sizeof(Q931_info_t)*/;
+       p[0] = IE_REDIR_DN;
+       p[1] = l;
+       if (present >= 0)
+       {
+               p[2] = 0x00 + (type<<4) + plan;
+               p[3] = 0x80 + (present<<5);
+               if (number)
+                       strncpy((char *)p+4, (char *)number, strlen((char *)number));
+       } else
+       {
+               p[2] = 0x80 + (type<<4) + plan;
+               if (number)
+                       strncpy((char *)p+3, (char *)number, strlen((char *)number));
+       }
+}
+
+void dec_ie_redir_dn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc)
+{
+       *type = -1;
+       *plan = -1;
+       *present = -1;
+       *number = '\0';
+
+       if (!nt)
+       {
+               p = NULL;
+/* #warning REINSERT redir_dn, when included in te-mode */
+/*             if (qi->redir_dn) */
+/*                     p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->redir_dn + 1; */
+       }
+       if (!p)
+               return;
+       if (p[0] < 1)
+       {
+               printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]);
+               return;
+       }
+
+       *type = (p[1]&0x70) >> 4;
+       *plan = p[1] & 0xf;
+       if (!(p[1] & 0x80))
+       {
+               *present = (p[2]&0x60) >> 5;
+               strnncpy(number, p+3, p[0]-2, number_len);
+       } else
+       {
+               strnncpy(number, p+2, p[0]-1, number_len);
+       }
+
+       if (MISDN_IE_DEBG) printf("    type=%d plan=%d present=%d number='%s'\n", *type, *plan, *present, number);
+}
+
+
+/* IE_FACILITY */
+void enc_ie_facility(unsigned char **ntmode, msg_t *msg, unsigned char *facility, int facility_len, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       char debug[768];
+       int i;
+
+       if (!facility || facility_len<=0)
+       {
+               return;
+       }
+
+       i = 0;
+       while(i < facility_len)
+       {
+               if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", facility[i]);
+               i++;
+       }
+               
+       if (MISDN_IE_DEBG) printf("    facility%s\n", debug);
+
+       l = facility_len;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->facility = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_FACILITY;
+       p[1] = l;
+       memcpy(p+2, facility, facility_len);
+}
+
+void dec_ie_facility(unsigned char *p, Q931_info_t *qi, unsigned char *facility, int *facility_len, int nt, struct misdn_bchannel *bc)
+{
+       int i;
+
+       *facility_len = 0;
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->facility)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->facility + 1;
+       }
+       if (!p)
+               return;
+
+       *facility_len = p[0];
+       memcpy(facility, p+1, *facility_len);
+       
+       i = 0;
+       while(i < *facility_len)
+       {
+               cb_log(3, bc->stack->port, " %02x", facility[i]);
+               i++;
+       }
+       cb_log(3, bc->stack->port, "    facility\n");
+}
+
+
+/* facility for siemens CENTEX (known parts implemented only) */
+void enc_facility_centrex(unsigned char **ntmode, msg_t *msg, unsigned char *cnip, int setup, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char centrex[256];
+       int i = 0;
+
+       if (!cnip)
+               return;
+
+       /* centrex facility */
+       centrex[i++] = CENTREX_FAC;
+       centrex[i++] = CENTREX_ID;
+
+       /* cnip */
+       if (strlen((char *)cnip) > 15)
+       {
+/*             if (options.deb & DEBUG_PORT) */
+               if (MISDN_IE_DEBG) printf("%s: CNIP/CONP text too long (max 13 chars), cutting.\n", __FUNCTION__);
+               cnip[15] = '\0';
+       }
+       /*  dunno what the 8 bytes mean */
+       if (setup)
+       {
+               centrex[i++] = 0x17;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x44;
+               centrex[i++] = 0x18;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x01;
+               centrex[i++] = 0x09;
+       } else
+       {
+               centrex[i++] = 0x18;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x81;
+               centrex[i++] = 0x09;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x01;
+               centrex[i++] = 0x0a;
+       }
+
+       centrex[i++] = 0x80;
+       centrex[i++] = strlen((char *)cnip);
+       strcpy((char *)(&centrex[i]), (char *)cnip);
+       i += strlen((char *)cnip);
+       if (MISDN_IE_DEBG) printf("    cnip='%s'\n", cnip);
+
+       /* encode facility */
+       enc_ie_facility(ntmode, msg, centrex, i, nt , bc);
+}
+
+void dec_facility_centrex(unsigned char *p, Q931_info_t *qi, unsigned char *cnip, int cnip_len, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char centrex[256];
+       char debug[768];
+       int facility_len = 0;
+       int i = 0, j;
+       *cnip = '\0';
+
+       dec_ie_facility(p, qi, centrex, &facility_len,  nt, bc);
+       if (facility_len >= 2)
+       {
+               if (centrex[i++] != CENTREX_FAC)
+                       return;
+               if (centrex[i++] != CENTREX_ID)
+                       return;
+       }
+
+       /* loop sub IEs of facility */
+       while(facility_len > i+1)
+       {
+               if (centrex[i+1]+i+1 > facility_len)
+               {
+                       printf("%s: ERROR: short read of centrex facility.\n", __FUNCTION__);
+                       return;
+               }
+               switch(centrex[i])
+               {
+               case 0x80:
+                       strnncpy(cnip, &centrex[i+2], centrex[i+1], cnip_len);
+                       if (MISDN_IE_DEBG) printf("    CENTREX cnip='%s'\n", cnip);
+                       break;
+
+               default:
+                       j = 0;
+                       while(j < centrex[i+1])
+                       {
+                               if (MISDN_IE_DEBG) printf(debug+(j*3), " %02x", centrex[i+1+j]);
+                               i++;
+                       }
+                       if (MISDN_IE_DEBG) printf("    CENTREX unknown=0x%2x len=%d%s\n", centrex[i], centrex[i+1], debug);
+               }
+               i += 1+centrex[i+1];
+       }
+}
+
+
+
+
+/* facility for siemens CENTEX (known parts implemented only) */
+void enc_facility_calldeflect(unsigned char **ntmode, msg_t *msg, unsigned char *nr, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char fac[256];
+       
+       if (!nr)
+               return;
+       
+       /* calldeflect facility */
+       
+       /* cnip */
+       if (strlen((char *)nr) > 15)
+       {
+/*             if (options.deb & DEBUG_PORT) */
+               if (MISDN_IE_DEBG) printf("%s: NR text too long (max 13 chars), cutting.\n", __FUNCTION__);
+               nr[15] = '\0';
+       }
+       
+       fac[0]=0; // len 
+       fac[1]=0; //len 
+       fac[2]=0x01; // Use D-Chan
+       fac[3]=0; // Keypad len
+       fac[4]=31;      // user user data? len = 31 = 29 + 2
+       fac[5]=0x1c;    // magic?
+       fac[6]=0x1d;    // strlen destination + 18 = 29
+       fac[7]=0x91;    // ..
+       fac[8]=0xA1;
+       fac[9]=0x1A;    // strlen destination + 15 = 26
+       fac[10]=0x02;
+       fac[11]=0x01;
+       fac[12]=0x70;
+       fac[13]=0x02;
+       fac[14]=0x01;
+       fac[15]=0x0d;
+       fac[16]=0x30;
+       fac[17]=0x12;   // strlen destination + 7 = 18
+       fac[18]=0x30;   // ...hm 0x30
+       fac[19]=0x0d;   // strlen destination + 2       
+       fac[20]=0x80;   // CLIP
+       fac[21]=0x0b;   //  strlen destination 
+       fac[22]=0x01;   //  destination start
+       fac[23]=0x01;   //  
+       fac[24]=0x01;   //  
+       fac[25]=0x01;   //  
+       fac[26]=0x01;   //  
+       fac[27]=0x01;   //  
+       fac[28]=0x01;   //  
+       fac[29]=0x01;   //  
+       fac[30]=0x01;   //  
+       fac[31]=0x01;   //  
+       fac[32]=0x01;   //  
+       fac[33]=0x01;   // 0x1 = sending complete
+       fac[34]=0x01;
+       fac[35]=0x01;
+                                  
+       memcpy((unsigned char *)fac+22,nr,strlen(nr));
+       fac[22+strlen( nr)]=0x01;       // fill with 0x01 if number is only 6 numbers (local call)
+       fac[23+strlen(nr)]=0x01;
+       fac[24+strlen(nr)]=0x01;
+       fac[25+strlen(nr)]=0x01;
+       fac[26+strlen(nr)]=0x01;
+       
+       fac[6]=18+strlen(nr);
+       fac[9]=15+strlen(nr);
+       fac[17]=7+strlen(nr);
+       fac[19]=2+strlen(nr);
+       fac[21]=strlen(nr);
+       
+       enc_ie_facility(ntmode, msg, &fac[4], 36-4, nt , bc);
+}
+
+
+/* IE_USERUSER */
+void enc_ie_useruser(unsigned char **ntmode, msg_t *msg, int protocol, unsigned char *user, int user_len, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+       char debug[768];
+       int i;
+
+       if (protocol<0 || protocol>127)
+       {
+               printf("%s: ERROR: protocol(%d) is out of range.\n", __FUNCTION__, protocol);
+               return;
+       }
+       if (!user || user_len<=0)
+       {
+               return;
+       }
+
+       i = 0;
+       while(i < user_len)
+       {
+               if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", user[i]);
+               i++;
+       }
+               
+       if (MISDN_IE_DEBG) printf("    protocol=%d user-user%s\n", protocol, debug);
+
+       l = user_len;
+       p = msg_put(msg, l+3);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->useruser = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_USER_USER;
+       p[1] = l;
+       p[2] = 0x80 + protocol;
+       memcpy(p+3, user, user_len);
+}
+
+void dec_ie_useruser(unsigned char *p, Q931_info_t *qi, int *protocol, unsigned char *user, int *user_len, int nt, struct misdn_bchannel *bc)
+{
+       char debug[768];
+       int i;
+
+       *user_len = 0;
+       *protocol = -1;
+
+       if (!nt)
+       {
+               p = NULL;
+               if (qi->useruser)
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->useruser + 1;
+       }
+       if (!p)
+               return;
+
+       *user_len = p[0]-1;
+       if (p[0] < 1)
+               return;
+       *protocol = p[1];
+       memcpy(user, p+2, (*user_len<=128)?*(user_len):128); /* clip to 128 maximum */
+
+       i = 0;
+       while(i < *user_len)
+       {
+               if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", user[i]);
+               i++;
+       }
+       debug[i*3] = '\0';
+               
+       if (MISDN_IE_DEBG) printf("    protocol=%d user-user%s\n", *protocol, debug);
+}
+
+
diff --git a/channels/misdn/isdn_lib.c b/channels/misdn/isdn_lib.c
new file mode 100755 (executable)
index 0000000..3ef71e2
--- /dev/null
@@ -0,0 +1,3103 @@
+/*
+ * Chan_Misdn -- Channel Driver for Asterisk
+ *
+ * Interface to mISDN
+ *
+ * Copyright (C) 2004, Christian Richter
+ *
+ * Christian Richter <crich@beronet.com>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+static int nt_err_cnt =0 ;
+
+enum global_states {
+       MISDN_INITIALIZING,
+       MISDN_INITIALIZED
+} ;
+
+static enum global_states  global_state=MISDN_INITIALIZING;
+
+
+#include <../i4lnet/net_l2.h>
+#include <tone.h>
+#include <unistd.h>
+#include <semaphore.h>
+#include <pthread.h>
+#include <signal.h>
+
+#include "isdn_lib.h"
+
+
+struct misdn_lib {
+       int midev;
+       int midev_nt;
+
+       pthread_t event_thread;
+       pthread_t event_handler_thread;
+  
+       void *user_data;
+
+       msg_queue_t upqueue;
+       msg_queue_t activatequeue; 
+  
+       sem_t new_msg;
+  
+       struct misdn_stack *stack_list;
+} ;
+
+#ifndef ECHOCAN_ON
+#define ECHOCAN_ON 123
+#define ECHOCAN_OFF 124
+#endif
+
+#define MISDN_DEBUG 0
+
+struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
+
+int setup_bc(struct misdn_bchannel *bc);
+
+int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
+
+int misdn_lib_port_restart(int port);
+
+extern struct isdn_msg msgs_g[]; 
+
+#define ISDN_PID_L3_B_USER 0x430000ff
+#define ISDN_PID_L4_B_USER 0x440000ff
+
+/* #define MISDN_IBUF_SIZE 1024 */
+#define MISDN_IBUF_SIZE 512
+
+/*  Fine Tuning of Inband  Signalling time */
+#define TONE_ALERT_CNT 41 /*  1 Sec  */
+#define TONE_ALERT_SILENCE_CNT 200 /*  4 Sec */
+
+#define TONE_BUSY_CNT 20 /*  ? */
+#define TONE_BUSY_SILENCE_CNT 48 /*  ? */
+
+static int  entity;
+
+static struct misdn_lib *glob_mgr;
+
+unsigned char tone_425_flip[TONE_425_SIZE];
+unsigned char tone_silence_flip[TONE_SILENCE_SIZE];
+
+static void misdn_lib_isdn_event_catcher(void *arg);
+static int handle_event_nt(void *dat, void *arg);
+
+
+void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
+void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
+struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
+
+/* from isdn_lib.h */
+int init_bc(struct misdn_stack * stack,  struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime);
+struct misdn_stack* stack_te_init(int midev,  int port, int ptp);
+void stack_te_destroy(struct misdn_stack* stack);
+       /* user iface */
+int te_lib_init( void ) ; /* returns midev */
+void te_lib_destroy(int midev) ;
+struct misdn_bchannel *manager_find_bc_by_pid(int pid);
+struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc);
+unsigned char * manager_flip_buf_bits ( unsigned char * buf , int len);
+void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
+void manager_clean_bc(struct misdn_bchannel *bc );
+void manager_bchannel_setup (struct misdn_bchannel *bc);
+void manager_bchannel_cleanup (struct misdn_bchannel *bc);
+
+int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *frm, int nt);
+enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *frm, int nt);
+int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *frm, struct misdn_bchannel *bc, int nt);
+char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt);
+msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt);
+void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
+       /* end */
+int bchdev_echocancel_activate(struct misdn_bchannel* dev);
+void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
+/* end */
+
+
+static char *bearer2str(int cap) {
+       static char *bearers[]={
+               "Speech",
+               "Audio 3.1k",
+               "Unres Digital",
+               "Res Digital",
+               "Unknown Bearer"
+       };
+       
+       switch (cap) {
+       case INFO_CAPABILITY_SPEECH:
+               return bearers[0];
+               break;
+       case INFO_CAPABILITY_AUDIO_3_1K:
+               return bearers[1];
+               break;
+       case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
+               return bearers[2];
+               break;
+       case INFO_CAPABILITY_DIGITAL_RESTRICTED:
+               return bearers[3];
+               break;
+       default:
+               return bearers[4];
+               break;
+       }
+}
+
+
+static char flip_table[256];
+
+void init_flip_bits(void)
+{
+       int i,k;
+       
+       for (i = 0 ; i < 256 ; i++) {
+               unsigned char sample = 0 ;
+               for (k = 0; k<8; k++) {
+                       if ( i & 1 << k ) sample |= 0x80 >>  k;
+               }
+               flip_table[i] = sample;
+       }
+}
+
+static unsigned char * flip_buf_bits ( unsigned char * buf , int len)
+{
+       int i;
+       char * start = buf;
+       
+       for (i = 0 ; i < len; i++) {
+               buf[i] = flip_table[buf[i]];
+       }
+       
+       return start;
+}
+
+
+
+
+msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
+{
+       int i = 0;
+       msg_t *dmsg;
+  
+       while(i < 10)
+       {
+               dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
+               if (dmsg)
+                       return(dmsg);
+      
+               if (!i)
+                       printf("cannot allocate memory, trying again...\n");
+               i++;
+               usleep(300000);
+       }
+       printf("cannot allocate memory, system overloaded.\n");
+       exit(-1);
+}
+
+
+
+msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
+{
+       int i = 0;
+       msg_t *dmsg;
+       Q931_info_t *qi;
+       iframe_t *frm;
+  
+       if (!ntmode)
+               size = sizeof(Q931_info_t)+2;
+  
+       while(i < 10) {
+               if (ntmode) {
+                       dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
+                       if (dmsg) {
+                               return(dmsg);
+                       }
+               } else {
+                       dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
+                       if (dmsg)
+                       {
+                               memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
+                               frm = (iframe_t *)dmsg->data;
+                               frm->prim = prim;
+                               frm->dinfo = dinfo;
+                               qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
+                               qi->type = mt;
+                               return(dmsg);
+                       }
+               }
+    
+               if (!i) printf("cannot allocate memory, trying again...\n");
+               i++;
+               usleep(300000);
+       }
+       printf("cannot allocate memory, system overloaded.\n");
+       exit(-1);
+}
+
+
+int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
+{
+       iframe_t *frm;
+       frm = (iframe_t *)dmsg->data;
+
+       frm->addr = (bc->stack->upper_id &  IF_ADDRMASK) | IF_DOWN ;
+       frm->dinfo = bc->l3_id;
+  
+       frm->len = (dmsg->len) - mISDN_HEADER_LEN;
+  
+       mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
+  
+       free_msg(dmsg);
+
+       return 0;
+}
+
+
+static int mypid=0;
+
+int misdn_cap_is_speech(int cap)
+/** Poor mans version **/
+{
+       if (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) return 1;
+       return 0;
+}
+
+int misdn_inband_avail(struct misdn_bchannel *bc)
+{
+       switch (bc->progress_indicator) {
+       case INFO_PI_INBAND_AVAILABLE:
+       case INFO_PI_CALL_NOT_E2E_ISDN:
+               return 1;
+       default:
+               return 0;
+       }
+       return 0;
+}
+
+
+void dump_chan_list(struct misdn_stack *stack)
+{
+       int i;
+
+       for (i=0; i <stack->b_num; i++) {
+               cb_log(3, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
+       }
+}
+
+
+
+
+static int find_free_chan_in_stack(struct misdn_stack *stack, int channel)
+{
+       int i;
+
+       if (channel < 0 || channel > MAX_BCHANS) {
+               cb_log(4, stack->port, " !! out of bound call to find_free_chan_in_stack! (port:%d ch:%d)\n", stack->port, channel);
+               return 0;
+       }
+       
+       channel--;
+  
+       for (i = 0; i < stack->b_num; i++) {
+               if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
+                       if (!stack->channels[i]) {
+                               cb_log (4, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
+                               stack->channels[i] = 1;
+                               return i+1;
+                       }
+               }
+       }
+
+       cb_log (4, stack->port, " !! NO FREE CHAN IN STACK\n");
+       dump_chan_list(stack);
+  
+       return 0;
+}
+
+int empty_chan_in_stack(struct misdn_stack *stack, int channel)
+{
+       cb_log (4, stack?stack->port:0, " --> empty chan %d\n",channel); 
+       stack->channels[channel-1] = 0;
+       dump_chan_list(stack);
+       return 0;
+}
+
+
+void empty_bc(struct misdn_bchannel *bc)
+{
+       bc->channel = 0;
+       bc->in_use = 0;
+
+       bc->nohdlc=0;
+       
+       bc->send_dtmf=0;
+       bc->nodsp=0;
+       bc->nojitter=0;
+       
+       bc->time_usec=0;
+       
+       bc->rxgain=0;
+       bc->txgain=0;
+
+       bc->crypt=0;
+       bc->curptx=0; bc->curprx=0;
+       
+       bc->crypt_key[0] = 0;
+  
+       bc->tone=TONE_NONE;
+       bc->tone_cnt2 = bc->tone_cnt=0;
+  
+       bc->dnumplan=NUMPLAN_UNKNOWN;
+       bc->onumplan=NUMPLAN_UNKNOWN;
+       bc->rnumplan=NUMPLAN_UNKNOWN;
+       
+       bc->active = 0;
+  
+       bc->ec_enable = 0;
+       bc->ec_deftaps = 128;
+       bc->ec_whenbridged = 0;
+       bc->ec_training = 400;
+       
+       
+       bc->orig=0;
+  
+       bc->cause=16;
+       bc->out_cause=16;
+       bc->pres=0 ; /* screened */
+       
+       bc->evq=EVENT_NOTHING;
+
+       bc->progress_coding=0;
+       bc->progress_location=0;
+       bc->progress_indicator=0;
+       
+/** Set Default Bearer Caps **/
+       bc->capability=INFO_CAPABILITY_SPEECH;
+       bc->law=INFO_CODEC_ALAW;
+       bc->mode=0;
+       bc->rate=0;
+       bc->user1=0;
+       bc->async=0;
+       bc->urate=0;
+       
+       
+       
+       bc->info_dad[0] = 0;
+       bc->display[0] = 0;
+       bc->infos_pending[0] = 0;
+       bc->oad[0] = 0;
+       bc->dad[0] = 0;
+       bc->orig_dad[0] = 0;
+       
+       bc->facility=FACILITY_NONE;
+       bc->facility_calldeflect_nr[0]=0;
+}
+
+
+int clean_up_bc(struct misdn_bchannel *bc)
+{
+       int ret=0;
+       unsigned char buff[32];
+       if (!bc || !bc->stack) return -1;
+  
+       if (!bc->upset) {
+               cb_log(5, bc->stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
+               return -1;
+       }
+  
+       cb_log(5, bc->stack->port, "$$$ Cleaning up bc with stid :%x\n", bc->b_stid);
+  
+       
+       if ( misdn_cap_is_speech(bc->capability) && bc->ec_enable) {
+               manager_ec_disable(bc);
+       }
+       
+       mISDN_write_frame(bc->stack->midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+       
+       
+       bc->b_stid = 0;
+       
+       bc->upset=0;
+       
+       return ret;
+}
+
+
+
+void clear_l3(struct misdn_stack *stack)
+{
+       int i;
+       for (i=0; i<stack->b_num; i++) {
+               if (global_state == MISDN_INITIALIZED)  {
+                       cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
+                       empty_chan_in_stack(stack,i+1);
+                       empty_bc(&stack->bc[i]);
+                       clean_up_bc(&stack->bc[i]);
+                       
+               }
+               
+       } 
+}
+
+int set_chan_in_stack(struct misdn_stack *stack, int channel)
+{
+       stack->channels[channel-1] = 1;
+  
+       return 0;
+}
+
+int chan_in_stack_free(struct misdn_stack *stack, int channel)
+{
+       if (stack->channels[channel-1])
+               return 0;
+  
+       return 1;
+}
+
+
+
+static int newteid=0;
+
+#ifdef MISDNUSER_JOLLY
+#define MAXPROCS 0x100
+#else
+#define MAXPROCS 0x10
+#endif
+
+static int create_process (int midev, struct misdn_bchannel *bc) {
+       iframe_t ncr;
+       int l3_id;
+       int i;
+       struct misdn_stack *stack=bc->stack;
+       int free_chan;
+  
+       if (stack->mode == NT_MODE) {
+               free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0);
+               if (!free_chan) return -1;
+               bc->channel=free_chan;
+    
+               for (i=0; i <= MAXPROCS; i++)
+                       if (stack->procids[i]==0) break;
+    
+               if (i== MAXPROCS) {
+                       cb_log(0, stack->port, "Couldnt Create New ProcId Port:%d\n",stack->port);
+                       return -1;
+               }
+               stack->procids[i]=1;
+
+#ifdef MISDNUSER_JOLLY
+               l3_id = 0xff00 | i;
+#else
+               l3_id = 0xfff0 | i;
+#endif
+    
+               ncr.prim = CC_NEW_CR | REQUEST; 
+               ncr.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+               ncr.dinfo = l3_id;
+               ncr.len = 0;
+
+               bc->l3_id = l3_id;
+               if (mypid>5000) mypid=0;
+               bc->pid=mypid++;
+      
+               cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
+    
+       } else { 
+               if (stack->ptp || stack->te_choose_channel) {
+                       /* we know exactly which channels are in use */
+                       free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0);
+                       if (!free_chan) return -1;
+                       bc->channel=free_chan;
+               } else {
+                       /* other phones could have made a call also on this port (ptmp) */
+                       bc->channel=0xff;
+               }
+    
+    
+               /* if we are in te-mode, we need to create a process first */
+               if (newteid++ > 0xffff)
+                       newteid = 0x0001;
+    
+               l3_id = (entity<<16) | newteid;
+               /* preparing message */
+               ncr.prim = CC_NEW_CR | REQUEST; 
+               ncr.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+               ncr.dinfo =l3_id;
+               ncr.len = 0;
+               /* send message */
+
+               bc->l3_id = l3_id;
+               if (mypid>5000) mypid=0;
+               bc->pid=mypid++;
+    
+               cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
+    
+               mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
+       }
+  
+       return l3_id;
+}
+
+
+
+int setup_bc(struct misdn_bchannel *bc)
+{
+       unsigned char buff[1025];
+  
+       mISDN_pid_t pid;
+       int ret;
+       int midev=bc->stack->midev;
+       int channel=bc->channel-1-(bc->channel>16);
+       int b_stid=bc->stack->b_stids[channel>=0?channel:0];
+       
+       if (bc->upset) {
+               cb_log(5, bc->stack->port, "$$$ bc already upsetted stid :%x\n", b_stid);
+               return -1;
+       }
+       
+       
+       if (bc->nodsp) {
+               clean_up_bc(bc);
+       }
+       
+       cb_log(5, bc->stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
+       
+       if (b_stid <= 0) {
+               cb_log(0, bc->stack->port," -- Stid <=0 at the moment on port:%d channel:%d\n",bc->stack->port,channel);
+               return 1;
+       }
+
+  
+       bc->b_stid = b_stid;
+       
+       {
+               layer_info_t li;
+               memset(&li, 0, sizeof(li));
+    
+               li.object_id = -1;
+               li.extentions = 0;
+               
+               li.st = bc->b_stid; /*  given idx */
+
+
+               if ( misdn_cap_is_speech(bc->capability) && !bc->nodsp && bc->async != 1) {
+                       cb_log(4, bc->stack->port,"setup_bc: with dsp\n");
+                       { 
+                               int l = sizeof(li.name);
+                               strncpy(li.name, "B L4", l);
+                               li.name[l-1] = 0;
+                       }
+                       li.pid.layermask = ISDN_LAYER((4));
+                       li.pid.protocol[4] = ISDN_PID_L4_B_USER;
+                       
+               } else {
+                       cb_log(4, bc->stack->port,"setup_bc: without dsp\n");
+                       { 
+                               int l = sizeof(li.name);
+                               strncpy(li.name, "B L3", l);
+                               li.name[l-1] = 0;
+                       }
+                       li.pid.layermask = ISDN_LAYER((3));
+                       li.pid.protocol[3] = ISDN_PID_L3_B_USER;
+               }
+               
+               ret = mISDN_new_layer(midev, &li);
+               if (ret <= 0) {
+                       cb_log(0, bc->stack->port,"New Layer Err: %d %s port:%d\n",ret,strerror(errno), bc->stack->port);
+                       return(-EINVAL);
+               }
+      
+               bc->layer_id = ret;
+       }
+       
+       memset(&pid, 0, sizeof(pid));
+       
+       bc->addr = ( bc->layer_id & IF_ADDRMASK) | IF_DOWN;
+       cb_log(4, bc->stack->port," --> Got Adr %x\n", bc->addr);
+       cb_log(4, bc->stack->port," --> Channel is %d\n", bc->channel);
+       
+       
+       if (bc->async == 1 || bc->nodsp) {
+               cb_log(4, bc->stack->port," --> TRANSPARENT Mode (no DSP)\n");
+               pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
+               pid.protocol[2] = ISDN_PID_L2_B_TRANS;
+               pid.protocol[3] = ISDN_PID_L3_B_USER;
+               pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
+       } else if ( misdn_cap_is_speech(bc->capability)) {
+               cb_log(4, bc->stack->port," --> TRANSPARENT Mode\n");
+               pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
+               pid.protocol[2] = ISDN_PID_L2_B_TRANS;
+               pid.protocol[3] = ISDN_PID_L3_B_DSP;
+               pid.protocol[4] = ISDN_PID_L4_B_USER;
+               pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
+               
+       } else {
+               cb_log(4, bc->stack->port," --> HDLC Mode\n");
+               pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
+               pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
+               pid.protocol[3] = ISDN_PID_L3_B_USER;
+               pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
+       }
+       
+       ret = mISDN_set_stack(midev, bc->b_stid, &pid);
+       
+       
+       if (ret){
+               cb_log(5, bc->stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
+    
+               mISDN_write_frame(midev, buff, bc->addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+               return(-EINVAL);
+       }
+  
+       bc->upset=1;
+  
+       return 0;
+}
+
+
+
+/** IFACE **/
+int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, int port, int bidx,  char *msn, int firsttime)
+{
+       unsigned char buff[1025];
+       iframe_t *frm = (iframe_t *)buff;
+       int ret;
+  
+       if (!bc) return -1;
+  
+       cb_log(4, port, "Init.BC %d on port:%d\n",bidx, port);
+       
+       memset(bc, 0,sizeof(struct misdn_bchannel));
+       
+       if (msn) {
+               int l = sizeof(bc->msn);
+               strncpy(bc->msn,msn, l);
+               bc->msn[l-1] = 0;
+       }
+
+
+       empty_bc(bc);
+       bc->upset=0;
+  
+       bc->astbuf= init_ibuffer(MISDN_IBUF_SIZE);
+       clear_ibuffer(  bc->astbuf);
+       bc->astbuf->rsem=malloc(sizeof(sem_t));
+  
+  
+       if (sem_init(bc->astbuf->rsem,1,0)<0)
+               sem_init(bc->astbuf->rsem,0,0);
+  
+  
+       bc->misdnbuf= init_ibuffer(MISDN_IBUF_SIZE);
+       clear_ibuffer(  bc->misdnbuf);
+       bc->misdnbuf->rsem=malloc(sizeof(sem_t));
+  
+       if (sem_init(bc->misdnbuf->rsem,1,0)< 0)
+               sem_init(bc->misdnbuf->rsem,0,0);
+  
+       bc->stack=stack;
+  
+       {
+               stack_info_t *stinf;
+               ret = mISDN_get_stack_info(midev, bc->stack->port, buff, sizeof(buff));
+               if (ret < 0) {
+                       cb_log(0, port, "%s: Cannot get stack info for port:%d (ret=%d)\n", __FUNCTION__, port, ret);
+                       return -1;
+               }
+    
+               stinf = (stack_info_t *)&frm->data.p;
+    
+               cb_log(4, port, " --> Child %x\n",stinf->child[bidx]);
+       }
+  
+       return 0;
+}
+
+
+
+struct misdn_stack * stack_nt_init(struct misdn_stack *stack, int midev, int port)
+{
+       int ret;
+       layer_info_t li;
+       interface_info_t ii;
+  
+  
+       cb_log(4, port, "Init. Stack on port:%d\n",port);
+       stack->mode = NT_MODE;
+  
+       stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, 1); 
+       if (stack->lower_id <= 0) {
+               cb_log(0, port, "%s: Cannot get layer(%d) id of port:%d\n", __FUNCTION__, 1, port);
+               return(NULL);
+       }
+  
+  
+       memset(&li, 0, sizeof(li));
+       {
+               int l = sizeof(li.name);
+               strncpy(li.name,"net l2", l);
+               li.name[l-1] = 0;
+       }
+       li.object_id = -1;
+       li.extentions = 0;
+       li.pid.protocol[2] = ISDN_PID_L2_LAPD_NET;
+       li.pid.layermask = ISDN_LAYER((2));
+       li.st = stack->d_stid;
+  
+  
+       stack->upper_id = mISDN_new_layer(midev, &li);
+       if (stack->upper_id <= 0) {
+               cb_log(0, port, "%s: Cannot add layer %d of port:%d\n", __FUNCTION__, 2, port);
+               return(NULL);
+       }
+
+       cb_log(4, port, "NT Stacks upper_id %x\n",stack->upper_id);
+   
+       memset(&ii, 0, sizeof(ii));
+       ii.extentions = EXT_IF_EXCLUSIV;
+       ii.owner = stack->upper_id;
+       ii.peer = stack->lower_id;
+       ii.stat = IF_DOWN;
+       ret = mISDN_connect(midev, &ii);
+       if (ret) {
+               cb_log(0, port, "%s: Cannot connect layer %d of port:%d exclusively.\n", __FUNCTION__, 2, port);
+               return(NULL);
+       }
+
+       /* create nst (nt-mode only) */
+       {
+               memset(&stack->nst, 0, sizeof(net_stack_t));
+               memset(&stack->mgr, 0, sizeof(manager_t));
+    
+               stack->mgr.nst = &stack->nst;
+               stack->nst.manager = &stack->mgr;
+    
+               stack->nst.l3_manager = handle_event_nt;
+               stack->nst.device = midev;
+               stack->nst.cardnr = port;
+               stack->nst.d_stid = stack->d_stid;
+    
+#ifdef MISDNUSER_JOLLY
+               stack->nst.feature = FEATURE_NET_HOLD;
+               if (stack->ptp)
+                       stack->nst.feature |= FEATURE_NET_PTP;
+               if (stack->pri)
+                       stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
+#endif
+    
+               stack->nst.l1_id = stack->lower_id;
+               stack->nst.l2_id = stack->upper_id;
+    
+               msg_queue_init(&stack->nst.down_queue);
+    
+               Isdnl2Init(&stack->nst);
+               Isdnl3Init(&stack->nst);
+       }
+       /* if ntmode, establish L1 to send the tei removal during start */
+       {
+               iframe_t act;
+               act.prim = PH_ACTIVATE | REQUEST; 
+               act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+               act.dinfo = 0;
+               act.len = 0;
+               mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+       }
+
+       stack->l2link=0;
+       if (stack->ptp) {
+               msg_t *dmsg;
+               /* L2 */
+               dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
+
+               if (stack->nst.manager_l3(&stack->nst, dmsg))
+                       free_msg(dmsg);
+
+               stack->l2link=1;
+       }
+  
+       return stack;
+}
+
+
+struct misdn_stack* stack_te_init( int midev, int port, int ptp )
+{
+       int ret;
+       unsigned char buff[1025];
+       iframe_t *frm = (iframe_t *)buff;
+       stack_info_t *stinf;
+       int i; 
+       layer_info_t li;
+       interface_info_t ii;
+       struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
+       if (!stack ) return NULL;
+
+
+       //cb_log(2, "Init. Stack on port:%d\n",port);
+       cb_log(4, port, "Init. Stack on port:%d\n",port);
+  
+       memset(stack,0,sizeof(struct misdn_stack));
+  
+       for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
+  
+       stack->port=port;
+       stack->midev=midev;
+       stack->ptp=ptp;
+  
+       stack->holding=NULL;
+       stack->pri=0;
+  
+       msg_queue_init(&stack->downqueue);
+  
+       /* query port's requirements */
+       ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
+       if (ret < 0) {
+               cb_log(0, port, "%s: Cannot get stack info for port:%d (ret=%d)\n", __FUNCTION__, port, ret);
+               return(NULL);
+       }
+  
+       stinf = (stack_info_t *)&frm->data.p;
+
+       stack->d_stid = stinf->id;
+       stack->b_num = stinf->childcnt;
+
+       for (i=0; i<stinf->childcnt; i++)
+               stack->b_stids[i] = stinf->child[i];
+  
+       switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
+       case ISDN_PID_L0_TE_S0:
+               //cb_log(2, "TE Stack\n");
+               stack->mode = TE_MODE;
+               break;
+       case ISDN_PID_L0_NT_S0:
+               cb_log(4, port, "NT Stack\n");
+
+               return stack_nt_init(stack,midev,port); 
+               break;
+
+       case ISDN_PID_L0_TE_U:
+               break;
+       case ISDN_PID_L0_NT_U:
+               break;
+       case ISDN_PID_L0_TE_UP2:
+               break;
+       case ISDN_PID_L0_NT_UP2:
+               break;
+       case ISDN_PID_L0_TE_E1:
+               cb_log(4, port, "TE S2M Stack\n");
+               stack->mode = TE_MODE;
+               stack->pri=1;
+               break;
+       case ISDN_PID_L0_NT_E1:
+               cb_log(4, port, "TE S2M Stack\n");
+               stack->mode = NT_MODE;
+               stack->pri=1;
+    
+               return stack_nt_init(stack,midev,port); 
+               break;
+       default:
+               cb_log(0, port, "unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]);
+
+       }
+  
+       if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { /* || (nt&&ptp) || pri */
+               stack->ptp = 1;
+       } else {
+               stack->ptp = 0;
+       }
+
+       stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, 3); 
+       if (stack->lower_id <= 0) {
+               cb_log(0, stack->port, "No lower Id port:%d\n", stack->port);
+               return(NULL);
+       }
+  
+       memset(&li, 0, sizeof(li));
+       {
+               int l = sizeof(li.name);
+               strncpy(li.name, "user L4", l);
+               li.name[l-1] = 0;
+       }
+       li.object_id = -1;
+       li.extentions = 0;
+  
+       li.pid.protocol[4] = ISDN_PID_L4_CAPI20;
+  
+       li.pid.layermask = ISDN_LAYER((4));
+       li.st = stack->d_stid;
+       stack->upper_id = mISDN_new_layer(midev, &li);
+  
+       if (stack->upper_id <= 0)       {
+               cb_log(0, stack->port, "No Upper ID port:%d\n",stack->port);
+               return(NULL);
+       } 
+  
+       memset(&ii, 0, sizeof(ii));
+       ii.extentions = EXT_IF_EXCLUSIV | EXT_IF_CREATE;
+       ii.owner = stack->upper_id;
+       ii.peer = stack->lower_id;
+       ii.stat = IF_DOWN;
+       ret = mISDN_connect(midev, &ii);
+       if (ret) {
+               cb_log(0, stack->port, "No Connect port:%d\n", stack->port);
+               return NULL;
+       }
+  
+
+       /* if ptp, establish link */
+       if (stack->ptp) {
+               iframe_t act;
+    
+#ifdef DL_STATUS
+               act.prim = DL_STATUS | REQUEST; 
+#else
+               act.prim = DL_ESTABLISH | REQUEST;
+#endif
+               act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN;
+               act.dinfo = 0;
+               act.len = 0;
+               mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+       }
+
+#ifdef DL_STATUS
+       { /* Pull Up L1 if we have JOLLY */ 
+               iframe_t act;
+               act.prim = PH_ACTIVATE | REQUEST; 
+               act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+               act.dinfo = 0;
+               act.len = 0;
+               mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+       }
+       
+       
+       if (!stack->ptp) {
+               iframe_t act;
+
+
+               act.prim = DL_STATUS | REQUEST; 
+    
+               act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN;
+               act.dinfo = 0;
+               act.len = 0;
+               mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+       }
+#endif
+       
+  
+       /*  initially, we assume that the link is NOT up */
+       stack->l2link = 0;
+       stack->l1link = 0;
+  
+       stack->next=NULL;
+  
+       return stack;
+  
+}
+
+void stack_te_destroy(struct misdn_stack* stack)
+{
+       char buf[1024];
+       if (!stack) return;
+  
+       if (stack->lower_id) 
+               mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+
+       if (stack->upper_id) 
+               mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+}
+
+
+struct misdn_stack * find_stack_by_addr(int  addr)
+{
+       struct misdn_stack *stack;
+  
+       for (stack=glob_mgr->stack_list;
+            stack;
+            stack=stack->next) {
+               if ( stack->upper_id == addr) return stack;
+       }
+  
+       return NULL;
+}
+
+
+struct misdn_stack * find_stack_by_port(int port)
+{
+       struct misdn_stack *stack;
+  
+       for (stack=glob_mgr->stack_list;
+            stack;
+            stack=stack->next) 
+               if (stack->port == port) return stack;
+  
+       return NULL;
+}
+
+struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
+{
+       struct misdn_stack *stack;
+  
+       for (stack=glob_mgr->stack_list;
+            stack;
+            stack=stack->next) 
+               if ( &stack->mgr == mgr_nt) return stack;
+  
+       return NULL;
+}
+
+struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
+{
+       int i;
+       for (i=0; i<stack->b_num; i++) {
+               if ( (stack->bc[i].l3_id & mask)  ==  (l3id & mask)) return &stack->bc[i] ;
+       }
+       return stack_holder_find(stack,l3id);
+}
+
+
+struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
+{
+       int i;
+       for (i=0; i<stack->b_num; i++) {
+               if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
+       }
+       return stack_holder_find(stack,l3id);
+}
+
+struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
+{
+       int i;
+       for (i=0; i<stack->b_num; i++) {
+               if (stack->bc[i].holded ) return &stack->bc[i] ;
+       }
+       return NULL;
+}
+
+
+struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
+{
+       int port = addr & IF_CONTRMASK;
+       struct misdn_stack* stack;
+       int i;
+
+
+       for (stack=glob_mgr->stack_list;
+            stack;
+            stack=stack->next) {
+    
+               if (stack->port == port) {
+                       for (i=0; i< stack->b_num; i++) {
+                               if (stack->bc[i].addr==addr) {
+                                       return &stack->bc[i];
+                               }
+                       }
+               }
+       }
+  
+       return NULL;
+}
+
+
+
+
+int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
+{
+
+       if (bc->stack->mode == TE_MODE) {
+               switch (event) {
+               case EVENT_CONNECT:
+                       if ( *bc->crypt_key ) {
+                               cb_log(4, bc->stack->port, "ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s\n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+                               
+                               manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
+                       }
+               case EVENT_SETUP:
+                       if (bc->channel>0 && bc->channel<255)
+                               set_chan_in_stack(bc->stack, bc->channel);
+                       break;
+               case EVENT_ALERTING:
+               case EVENT_PROGRESS:
+               case EVENT_PROCEEDING:
+               case EVENT_SETUP_ACKNOWLEDGE:
+                       
+               {
+                       struct misdn_stack *stack=find_stack_by_port(frm->addr&IF_CONTRMASK);
+                       if (!stack) return -1;
+                       
+                       if (bc->channel == 0xff) {
+                               bc->channel=find_free_chan_in_stack(bc->stack, 0);
+                               if (!bc->channel) {
+                                       cb_log(0, bc->stack->port, "Any Channel Requested, but we have no more!!\n");
+                                       break;
+                               }
+                       } 
+                       
+                       if (stack->mode == TE_MODE) {
+                               setup_bc(bc);
+                       }
+               }
+               
+               default:
+                       break;
+               }
+       } else {    /** NT MODE **/
+               
+       }
+       return 0;
+}
+
+int handle_new_process(struct misdn_stack *stack, iframe_t *frm)
+{
+  
+       struct misdn_bchannel* bc=misdn_lib_get_free_bc(frm->addr&IF_CONTRMASK, 0);
+       
+       if (!bc) {
+               cb_log(0, 0, " --> !! lib: No free channel!\n");
+               return -1;
+       }
+  
+       cb_log(4, bc->stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
+       bc->l3_id=frm->dinfo;
+       
+       if (mypid>5000) mypid=0;
+       bc->pid=mypid++;
+       return 0;
+}
+
+int handle_cr ( iframe_t *frm)
+{
+       struct misdn_stack *stack=find_stack_by_port(frm->addr&IF_CONTRMASK);
+
+       if (!stack) return -1;
+  
+       switch (frm->prim) {
+       case CC_NEW_CR|INDICATION:
+               cb_log(4, stack->port, " --> lib: NEW_CR Ind with l3id:%x port:%d\n",frm->dinfo, stack->port);
+               handle_new_process(stack, frm); 
+               return 1;
+       case CC_NEW_CR|CONFIRM:
+               return 1;
+       case CC_NEW_CR|REQUEST:
+               return 1;
+       case CC_RELEASE_CR|REQUEST:
+               return 1;
+       case CC_RELEASE_CR|CONFIRM:
+               break;
+       case CC_RELEASE_CR|INDICATION:
+               cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
+                       struct misdn_bchannel dummybc;
+      
+                       if (!bc) {
+                               cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", frm->dinfo, stack->port);
+                               memset (&dummybc,0,sizeof(dummybc));
+                               dummybc.stack=stack;
+                               dummybc.l3_id=frm->dinfo;
+                               bc=&dummybc; 
+                       }
+      
+                       if (bc) {
+                               cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
+                               empty_chan_in_stack(bc->stack,bc->channel);
+                               empty_bc(bc);
+                               clean_up_bc(bc);
+                               dump_chan_list(bc->stack);
+                               bc->pid = 0;
+                               cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
+                               
+                               if (bc->stack_holder) {
+                                       cb_log(4,stack->port, "REMOVEING Holder\n");
+                                       stack_holder_remove( bc->stack, bc);
+                                       free(bc);
+                               }
+                       }
+                       else {
+                               if (stack->mode == NT_MODE) 
+                                       cb_log(4, stack->port, "BC with dinfo: %x  not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
+                       }
+      
+                       return 1;
+               }
+               break;
+       }
+  
+       return 0;
+}
+
+
+/*Emptys bc if it's reserved (no SETUP out yet)*/
+void misdn_lib_release(struct misdn_bchannel *bc)
+{
+       if (bc->channel>=0) {
+               empty_chan_in_stack(bc->stack,bc->channel);
+               empty_bc(bc);
+       }
+       clean_up_bc(bc);
+}
+
+int misdn_lib_get_port_up (int port) 
+{ /* Pull Up L1 if we have JOLLY */ 
+       struct misdn_stack *stack;
+       
+       for (stack=glob_mgr->stack_list;
+            stack;
+            stack=stack->next) {
+               
+               if (stack->port == port) {
+                       iframe_t act;
+                       act.prim = DL_ESTABLISH | REQUEST; 
+                       act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+                       act.dinfo = 0;
+                       act.len = 0;
+                       mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+                       return 0;
+               }
+       }
+       return 0;
+}
+
+
+int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data)
+{
+       bc->facility=fac;
+       strcpy(bc->facility_calldeflect_nr,(char*)data);
+       
+       misdn_lib_send_event(bc,EVENT_FACILITY);
+       return 0;
+}
+
+
+int misdn_lib_port_up(int port)
+{
+       struct misdn_stack *stack;
+       
+       for (stack=glob_mgr->stack_list;
+            stack;
+            stack=stack->next) {
+               
+               if (stack->port == port) {
+                       if (stack->mode == NT_MODE) {
+                               if (stack->l1link)
+                                       return 1;
+                               else
+                                       return 0;
+                       } else {
+                               if (stack->l2link)
+                                       return 1;
+                               else
+                                       return 0;
+                       }
+
+               }
+       }
+  
+       return -1;
+}
+
+
+int
+handle_event_nt(void *dat, void *arg)
+{
+       manager_t *mgr = (manager_t *)dat;
+       msg_t *msg = (msg_t *)arg;
+#ifdef MISDNUSER_JOLLY
+       mISDNuser_head_t *hh;
+#else
+       mISDN_head_t *hh;
+#endif
+       struct misdn_stack *stack=find_stack_by_mgr(mgr);
+       int port;
+
+       if (!msg || !mgr)
+               return(-EINVAL);
+
+#ifdef MISDNUSER_JOLLY
+       hh=(mISDNuser_head_t*)msg->data;
+#else
+       hh=(mISDN_head_t*)msg->data;
+#endif
+  
+       port=hh->dinfo & IF_CONTRMASK;
+  
+       cb_log(4, stack->port, " --> lib: prim %x dinfo %x port: %d\n",hh->prim, hh->dinfo ,stack->port);
+  
+       {
+               switch(hh->prim){
+                       
+               case CC_RETRIEVE|INDICATION:
+               {
+                       iframe_t frm; /* fake te frm to add callref to global callreflist */
+                       frm.dinfo = hh->dinfo;
+                       frm.addr=stack->upper_id;
+                       frm.prim = CC_NEW_CR|INDICATION;
+                       
+                       if (handle_cr(&frm)< 0) {
+                               msg_t *dmsg;
+                               cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
+                               dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
+                               stack->nst.manager_l3(&stack->nst, dmsg);
+                               free_msg(msg);
+                               return 0;
+                       }
+                       
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+                       cb_event(EVENT_NEW_BC, bc, glob_mgr->user_data);
+                       struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
+                       if (hold_bc) {
+                               cb_log(4, stack->port, "REMOVEING Holder\n");
+                               stack_holder_remove(stack, hold_bc);
+                               free(hold_bc);
+                       }
+                       
+               }
+                       
+                       break;
+                       
+                       
+               case CC_SETUP|CONFIRM:
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+                       int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
+                       
+                       cb_log(4, bc?bc->stack->port:0, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID  is %x\n",l3id );
+       
+                       if (!bc) { cb_log(4, 0, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
+       
+                       bc->l3_id=l3id;
+                       cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
+               }
+               free_msg(msg);
+               return 0;
+      
+               case CC_SETUP|INDICATION:
+               {
+                       iframe_t frm; /* fake te frm to add callref to global callreflist */
+                       frm.dinfo = hh->dinfo;
+                       frm.addr=stack->upper_id;
+                       frm.prim = CC_NEW_CR|INDICATION;
+                       
+                       if (handle_cr(&frm)< 0) {
+                               msg_t *dmsg;
+                               cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
+                               dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
+                               stack->nst.manager_l3(&stack->nst, dmsg);
+                               free_msg(msg);
+                               return 0;
+                       }
+               }
+               break;
+
+    
+               case CC_ALERTING|INDICATION:
+               case CC_PROCEEDING|INDICATION:
+               case CC_CONNECT|INDICATION:
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+       
+                       setup_bc(bc);
+               }
+               break;
+               case CC_DISCONNECT|INDICATION:
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+                       if (!bc) {
+                               bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
+                               if (bc) { //repair reject bug
+                                       int myprocid=bc->l3_id&0x0000ffff;
+                                       hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
+                                       cb_log(4,bc->stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
+                               }
+                       }
+               }
+               break;
+
+               case CC_RELEASE_COMPLETE|INDICATION:
+                       break;
+
+               case CC_SUSPEND|INDICATION:
+               {
+                       msg_t *dmsg;
+                       cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
+                       dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
+                       stack->nst.manager_l3(&stack->nst, dmsg);
+                       free_msg(msg);
+                       return 0;
+               }
+               break;
+               case CC_RESUME|INDICATION:
+                       break;
+
+               case CC_RELEASE|CONFIRM:
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+                       cb_log(4, stack->port, " --> RELEASE CONFIRM, sending RELEASE_COMPLETE\n");
+                       if (bc) misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
+               }
+               hh->prim=CC_RELEASE|INDICATION;
+               break;  
+               case CC_RELEASE|INDICATION:
+                       break;
+
+               case CC_RELEASE_CR|INDICATION:
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+                       struct misdn_bchannel dummybc;
+                       iframe_t frm; /* fake te frm to remove callref from global callreflist */
+                       frm.dinfo = hh->dinfo;
+                       frm.addr=stack->upper_id;
+                       frm.prim = CC_RELEASE_CR|INDICATION;
+                       cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
+                       /** removing procid **/
+
+                       if (!bc) {
+                               cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", hh->dinfo, stack->port);
+                               memset (&dummybc,0,sizeof(dummybc));
+                               dummybc.stack=stack;
+                               dummybc.l3_id=hh->dinfo;
+                               bc=&dummybc; 
+                       }
+       
+                       if (bc) {
+#ifdef MISDNUSER_JOLLY
+                               if ( (bc->l3_id & 0xff00) == 0xff00) {
+                                       cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", bc->l3_id&0xff, stack->port);
+                                       stack->procids[bc->l3_id&0xff] = 0 ;
+                               }
+#else
+                               if ( (bc->l3_id & 0xfff0) == 0xfff0) {
+                                       cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", bc->l3_id&0xf, stack->port);
+                                       stack->procids[bc->l3_id&0xf] = 0 ;
+           
+                               }
+         
+#endif
+
+                       }
+                       else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is bad Port:%d\n", stack->port );
+       
+                       handle_cr(&frm);
+                       free_msg(msg);
+                       return 0 ;
+               }
+      
+               break;
+      
+               case CC_NEW_CR|INDICATION:
+                       /*  Got New CR for bchan, for now I handle this one in */
+                       /*  connect_ack, Need to be changed */
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+                       int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
+                       if (!bc) { cb_log(0, 0, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
+#ifdef MISDNUSER_JOLLY
+                       if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
+                               cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", 0xff&bc->l3_id, stack->port);
+                               stack->procids[bc->l3_id&0xff] = 0 ;
+                       }
+#else
+                       if (((l3id&0xfff0)!=0xfff0) && ((bc->l3_id&0xfff0)==0xfff0)) {
+                               cb_log(4, stack->port, "Removing Process Id:%x on port:%d\n", 0xf&bc->l3_id, stack->port);
+                               stack->procids[bc->l3_id&0xf] = 0 ;
+                       }
+
+#endif
+                       cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID  is %x\n",l3id );
+       
+                       bc->l3_id =l3id;
+                       cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
+       
+       
+                       free_msg(msg);
+                       return 0;
+               }
+      
+               case DL_ESTABLISH | INDICATION:
+               case DL_ESTABLISH | CONFIRM:
+               {
+                       cb_log(4, stack->port, "%% GOT L2 Activate Info port:%d\n",stack->port);
+                       stack->l2link = 1;
+       
+                       free_msg(msg);
+                       return 0;
+               }
+               break;
+
+               case DL_RELEASE | INDICATION:
+               case DL_RELEASE | CONFIRM:
+               {
+                       cb_log(4, stack->port, "%% GOT L2 DeActivate Info port:%d\n",stack->port);
+                       stack->l2link = 0;
+                       
+                       /** Clean the L3 here **/
+                       if (cb_clearl3_true())
+                               clear_l3(stack);
+                       
+                       free_msg(msg);
+                       return 0;
+               }
+               break;
+               }
+       }
+       
+  
+
+       {
+               /*  Parse Events and fire_up to App. */
+               struct misdn_bchannel *bc;
+               struct misdn_bchannel dummybc;
+               
+               enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
+    
+               bc=find_bc_by_l3id(stack, hh->dinfo);
+    
+               if (!bc) {
+      
+                       cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", hh->dinfo, stack->port);
+                       memset (&dummybc,0,sizeof(dummybc));
+                       dummybc.stack=stack;
+                       dummybc.l3_id=hh->dinfo;
+                       bc=&dummybc; 
+               }
+               if (bc ) {
+                       isdn_msg_parse_event(msgs_g,msg,bc, 1);
+                       
+                       if(!isdn_get_info(msgs_g,event,1)) {
+                               cb_log(4, bc->stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
+                       } else {
+                               cb_event(event, bc, glob_mgr->user_data);
+                       }
+
+      
+               } else {
+                       cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo);
+               }
+
+               free_msg(msg);
+       }
+
+
+       return 0;
+}
+
+
+int handle_timers(msg_t* msg)
+{
+       iframe_t *frm= (iframe_t*)msg->data;
+       struct misdn_stack *stack; 
+  
+       /* Timer Stuff */
+       switch (frm->prim) {
+       case MGR_INITTIMER | CONFIRM:
+       case MGR_ADDTIMER | CONFIRM:
+       case MGR_DELTIMER | CONFIRM:
+       case MGR_REMOVETIMER | CONFIRM:
+               free_msg(msg);
+               return(1);
+       }
+  
+  
+  
+       if (frm->prim==(MGR_TIMER | INDICATION) ) {
+               for (stack = glob_mgr->stack_list;
+                    stack;
+                    stack = stack->next) {
+                       itimer_t *it;
+      
+                       if (stack->mode != NT_MODE) continue;
+      
+                       it = stack->nst.tlist;
+                       /* find timer */
+                       for(it=stack->nst.tlist;
+                           it;
+                           it=it->next) {
+                               if (it->id == (int)frm->addr)
+                                       break;
+                       }
+                       if (it) {
+                               int ret;
+                               ret = mISDN_write_frame(stack->midev, msg->data, frm->addr,
+                                                       MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
+                               test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
+                               ret = it->function(it->data);
+                               free_msg(msg);
+                               return 1;
+                       }
+               }
+    
+               cb_log(0, 0, "Timer Msg without Timer ??\n");
+               free_msg(msg);
+               return 1;
+       }
+  
+       return 0;
+}
+
+
+
+
+static int do_tone(struct misdn_bchannel *bc, int len)
+{
+       char buf[4096 + mISDN_HEADER_LEN];
+       iframe_t *frm= (iframe_t*)buf;
+       int  r;
+  
+       if (bc->tone == TONE_NONE) return 0;
+
+       frm->prim = DL_DATA|REQUEST;
+       frm->dinfo = 0;
+       frm->addr = bc->addr | IF_DOWN;
+  
+       bc->tone_cnt+=len;
+
+       if (bc->tone_cnt < TONE_425_SIZE) return 1;
+
+       switch(bc->tone) {
+       case TONE_DIAL:
+       {
+               frm->len = TONE_425_SIZE;
+               memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
+      
+               r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+               if (r<frm->len) {
+                       perror("Error written less than told bytes :(\n");
+               }
+       }
+       break;
+
+       case TONE_ALERTING:
+               bc->tone_cnt2++;
+    
+               if (bc->tone_cnt2 <= TONE_ALERT_CNT) {
+                       frm->len = TONE_425_SIZE;
+                       memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
+                       r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+                       if (r<frm->len) {
+                               perror("Error written less than told bytes :(\n");
+                       }
+               } else if (bc->tone_cnt2 <= (TONE_ALERT_SILENCE_CNT)) {
+                       frm->len = TONE_SILENCE_SIZE;
+                       memcpy(&buf[mISDN_HEADER_LEN], tone_silence_flip ,TONE_SILENCE_SIZE);
+                       r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+               } else {
+                       bc->tone_cnt2=-1;
+               }
+               break;
+       case TONE_BUSY:
+               bc->tone_cnt2++;
+    
+               if (bc->tone_cnt2 <= TONE_BUSY_CNT) {
+                       frm->len = TONE_425_SIZE;
+                       memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
+                       r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+                       if (r<frm->len) {
+                               perror("Error written less than told bytes :(\n");
+                       }
+               } else if (bc->tone_cnt2 <= (TONE_BUSY_SILENCE_CNT)) {
+                       frm->len = TONE_SILENCE_SIZE;
+                       memcpy(&buf[mISDN_HEADER_LEN], tone_silence_flip ,TONE_SILENCE_SIZE);
+                       r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+               } else {
+                       bc->tone_cnt2=-1;
+               }
+               break;
+       case TONE_FILE:
+               break;
+       case TONE_NONE:
+               return 0;
+       }
+  
+       bc->tone_cnt -= TONE_425_SIZE ;
+       return 1;
+}
+
+
+
+int handle_bchan(msg_t *msg)
+{
+       iframe_t *frm= (iframe_t*)msg->data;
+       struct misdn_bchannel *bc;
+  
+       bc=find_bc_by_addr(frm->addr);
+  
+       if (!bc) return 0 ;
+  
+       switch (frm->prim) {
+       case PH_ACTIVATE | INDICATION:
+       case DL_ESTABLISH | INDICATION:
+               cb_log(4, bc->stack->port, "BCHAN: ACT Ind\n");
+               free_msg(msg);
+               return 1;    
+
+       case PH_ACTIVATE | CONFIRM:
+       case DL_ESTABLISH | CONFIRM:
+               cb_log(4, bc->stack->port, "BCHAN: bchan ACT Confirm\n");
+               free_msg(msg);
+               return 1;    
+
+       case PH_DEACTIVATE | INDICATION:
+       case DL_RELEASE | INDICATION:
+               cb_log (4, bc->stack->port, "BCHAN: DeACT Ind\n");
+               free_msg(msg);
+               return 1;
+    
+       case PH_DEACTIVATE | CONFIRM:
+       case DL_RELEASE | CONFIRM:
+               cb_log(4, bc->stack->port, "BCHAN: DeACT Conf\n");
+               free_msg(msg);
+               return 1;
+    
+       case PH_CONTROL|INDICATION:
+       {
+               unsigned long cont = *((unsigned long *)&frm->data.p);
+               
+               cb_log(4, bc->stack->port, "PH_CONTROL: port:%d channel:%d oad%d:%s dad%d:%s \n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+
+               if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
+                       int dtmf = cont & DTMF_TONE_MASK;
+                       cb_log(4, bc->stack->port, " --> DTMF TONE: %c\n",dtmf);
+                       bc->dtmf=dtmf;
+                       cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
+       
+                       free_msg(msg);
+                       return 1;
+               }
+               if (cont == BF_REJECT) {
+                       cb_log(4, bc->stack->port, " --> BF REJECT\n");
+                       free_msg(msg);
+                       return 1;
+               }
+               if (cont == BF_ACCEPT) {
+                       cb_log(4, bc->stack->port, " --> BF ACCEPT\n");
+                       free_msg(msg);
+                       return 1;
+               }
+       }
+       break;
+    
+       case PH_DATA|INDICATION:
+       case DL_DATA|INDICATION:
+       {
+               bc->bframe = (void*)&frm->data.i;
+               bc->bframe_len = frm->len;
+
+               /** Anyway flip the bufbits **/
+               flip_buf_bits(bc->bframe, bc->bframe_len);
+               
+               
+#if MISDN_DEBUG
+               cb_log(0, bc->stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
+#endif
+               
+               if (bc->active && frm->len > 0) {
+                       if (  !do_tone(bc, frm->len)   ) {
+                               
+                               if ( misdn_cap_is_speech(bc->capability)) {
+                                       if ( !bc->nojitter ) {
+                                               char buf[4096 + mISDN_HEADER_LEN];
+                                               iframe_t *txfrm= (iframe_t*)buf;
+                                               int len, r;
+                                               
+                                               len = ibuf_usedcount(bc->misdnbuf);
+                                               
+                                               if (len < frm->len) {
+                                                       /** send nothing 
+                                                        * till we are synced
+                                                        **/
+                                               } else {
+                                                       
+                                                       txfrm->prim = DL_DATA|REQUEST;
+                                                       txfrm->dinfo = 0;
+                                                       txfrm->addr = bc->addr; /*  | IF_DOWN; */
+                                                       txfrm->len = frm->len;
+                                                       ibuf_memcpy_r(&buf[mISDN_HEADER_LEN], bc->misdnbuf,frm->len);
+                                                       cb_log(9, bc->stack->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
+                                                       
+                                                       r=mISDN_write(bc->stack->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
+                                                       
+                                               }
+                                               
+                                       }
+                               }
+                               
+                               cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
+                       }
+               }
+               free_msg(msg);
+               return 1;
+       }
+
+
+       case PH_DATA | CONFIRM:
+       case DL_DATA|CONFIRM:
+#if MISDN_DEBUG
+               cb_log(0, bc->stack->port, "Data confirmed\n");
+#endif
+               free_msg(msg);
+               return 1;
+               break;
+       case DL_DATA|RESPONSE:
+#if MISDN_DEBUG
+               cb_log(0, bc->stack->port, "Data response\n");
+#endif
+               break;
+    
+       case DL_DATA | REQUEST:
+               break;
+       }
+  
+       return 0;
+}
+
+
+
+int handle_frm_nt(msg_t *msg)
+{
+       iframe_t *frm= (iframe_t*)msg->data;
+       struct misdn_stack *stack;
+       int err=0;
+  
+       stack=find_stack_by_addr((frm->addr & IF_ADDRMASK ) );
+  
+       if (!stack || stack->mode != NT_MODE) {
+               return 0;
+       }
+  
+
+       if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
+    
+               if (nt_err_cnt > 0 ) {
+                       if (nt_err_cnt < 100) {
+                               nt_err_cnt++; 
+                               cb_log(0, stack->port, "NT Stack sends us error: %d port:%d\n", err,stack->port);
+                       } else if (nt_err_cnt < 105){
+                               cb_log(0, stack->port, "NT Stack sends us error: %d port:%d over 100 times, so I'll stop this message\n", err,stack->port);
+                               nt_err_cnt = - 1; 
+                       }
+               }
+               free_msg(msg);
+               return 1;
+    
+       }
+  
+       return 1;
+}
+
+
+int handle_frm(msg_t *msg)
+{
+       iframe_t *frm = (iframe_t*) msg->data;
+       struct misdn_stack *stack=find_stack_by_addr(frm->addr & IF_ADDRMASK);
+  
+       if (!stack || stack->mode != TE_MODE) {
+               return 0;
+       }
+
+       {
+               struct misdn_bchannel *bc;
+
+               if(handle_cr(frm)) {
+                       free_msg(msg);
+                       return 1;
+               }
+    
+               bc=find_bc_by_l3id(stack, frm->dinfo);
+    
+               if (bc ) {
+                       enum event_e event = isdn_msg_get_event(msgs_g, msg, 0);
+                       enum event_response_e response=RESPONSE_OK;
+      
+                       isdn_msg_parse_event(msgs_g,msg,bc, 0);
+
+                       /** Preprocess some Events **/
+                       handle_event(bc, event, frm);
+                       /*  shoot up event to App: */
+                       cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
+      
+                       if(!isdn_get_info(msgs_g,event,0)) 
+                               cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
+                       else 
+                               response=cb_event(event, bc, glob_mgr->user_data);
+#if 1
+                       if (event == EVENT_SETUP) {
+                               switch (response) {
+                               case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
+                                       cb_log(0, stack->port, "TOTALY IGNORING SETUP: port:%d\n", frm->addr&IF_CONTRMASK);
+                                       break;
+                               case RESPONSE_IGNORE_SETUP:
+                                       /* I think we should send CC_RELEASE_CR, but am not sure*/
+                                       empty_chan_in_stack(bc->stack, bc->channel);
+                                       empty_bc(bc);
+         
+                                       cb_log(0, stack->port, "GOT IGNORE SETUP: port:%d\n", frm->addr&IF_CONTRMASK);
+                                       break;
+                               case RESPONSE_OK:
+                                       cb_log(4, stack->port, "GOT SETUP OK: port:%d\n", frm->addr&IF_CONTRMASK);
+                                       break;
+                               default:
+                                       break;
+                               }
+                       }
+      
+                       cb_log(5, stack->port, "Freeing Msg on prim:%x port:%d\n",frm->prim, frm->addr&IF_CONTRMASK);
+                       free_msg(msg);
+                       return 1;
+#endif
+      
+               } else {
+                       cb_log(0, stack->port, "NO BC FOR STACK: port:%d\n", frm->addr&IF_CONTRMASK);
+               }
+
+       }
+       cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x port:%d\n",frm->prim, frm->addr&IF_CONTRMASK);
+  
+       return 0;
+}
+
+
+int handle_l1(msg_t *msg)
+{
+       iframe_t *frm = (iframe_t*) msg->data;
+       struct misdn_stack *stack = find_stack_by_port(frm->addr & IF_CONTRMASK);
+       int i ;
+       
+       if (!stack) return 0 ;
+  
+       switch (frm->prim) {
+       case PH_ACTIVATE | CONFIRM:
+       case PH_ACTIVATE | INDICATION:
+               cb_log (1, stack->port, "L1: PH L1Link Up! port:%d\n",stack->port);
+               stack->l1link=1;
+               free_msg(msg);
+               
+               for (i=0;i<stack->b_num; i++) {
+                       if (stack->bc[i].evq != EVENT_NOTHING) {
+                               cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
+                               misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
+                               stack->bc[i].evq=EVENT_NOTHING;
+                       }
+                       
+               }
+               
+               return 1;
+       case PH_DEACTIVATE | CONFIRM:
+       case PH_DEACTIVATE | INDICATION:
+               cb_log (1, stack->port, "L1: PH L1Link Down! port:%d\n",stack->port);
+               stack->l1link=0;
+               for (i=0; i<stack->b_num; i++) {
+                       if (global_state == MISDN_INITIALIZED)  {
+                               cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
+                       }
+                       
+               } 
+               free_msg(msg);
+               return 1;
+       }
+  
+       return 0;
+}
+
+int handle_l2(msg_t *msg)
+{
+       iframe_t *frm = (iframe_t*) msg->data;
+       struct misdn_stack *stack = find_stack_by_addr(frm->addr & IF_ADDRMASK);
+
+       if (!stack) return 0 ;
+  
+       switch(frm->prim) {
+
+#ifdef DL_STATUS
+       case DL_STATUS | INDICATION:
+       case DL_STATUS | CONFIRM:
+               cb_log (3, stack->port, "L1: DL_STATUS! port:%d\n", stack->port);
+               
+               switch (frm->dinfo) {
+               case SDL_ESTAB:
+                       cb_log (4, stack->port, " --> SDL_ESTAB port:%d\n", stack->port);
+                       stack->l1link=1;
+                       goto dl_estab;
+               case SDL_REL:
+                       cb_log (4, stack->port, " --> SDL_REL port:%d\n", stack->port);
+                       stack->l1link=0;
+                       misdn_lib_get_port_up(stack->port);
+                       goto dl_rel;
+               }
+               break;
+#endif
+    
+    
+       case DL_ESTABLISH | INDICATION:
+       case DL_ESTABLISH | CONFIRM:
+#ifdef DL_STATUS
+       dl_estab:
+#endif
+       {
+               cb_log (3, stack->port, "L2: L2Link Up! port:%d\n", stack->port);
+               stack->l2link=1;
+               free_msg(msg);
+               return 1;
+       }
+       break;
+    
+       case DL_RELEASE | INDICATION:
+       case DL_RELEASE | CONFIRM:
+#ifdef DL_STATUS
+       dl_rel:
+#endif
+       {
+               cb_log (3, stack->port, "L2: L2Link Down! port:%d\n", stack->port);
+               stack->l2link=0;
+
+               if (cb_clearl3_true())
+                       clear_l3(stack);
+               
+               free_msg(msg);
+               return 1;
+       }
+       break;
+       }
+       return 0;
+}
+
+
+int handle_mgmt(msg_t *msg)
+{
+       iframe_t *frm = (iframe_t*) msg->data;
+  
+       if ( (frm->prim & 0x0f0000) ==  0x0f0000) {
+               cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
+               free_msg(msg);
+               return 1;
+       }
+    
+       return 0;
+}
+
+
+msg_t *fetch_msg(int midev) 
+{
+       msg_t *msg=alloc_msg(MAX_MSG_SIZE);
+       int r;
+       fd_set rdfs;
+
+       if (!msg) {
+               cb_log(0, 0, "fetch_msg: alloc msg failed !!");
+               return NULL;
+       }
+       
+       FD_ZERO(&rdfs);
+       FD_SET(midev,&rdfs);
+  
+       mISDN_select(FD_SETSIZE, &rdfs, NULL, NULL, NULL);
+  
+       if (FD_ISSET(midev, &rdfs)) {
+    
+               r=mISDN_read(midev,msg->data,MAX_MSG_SIZE,0);
+               msg->len=r;
+    
+               if (r==0) {
+                       free_msg(msg); /* danger, cauz usualy freeing in main_loop */
+                       printf ("Got empty Msg?\n");
+                       return NULL;
+               }
+
+               return msg;
+       } else {
+               printf ("Select timeout\n");
+       }
+  
+       return NULL;
+}
+
+
+static void misdn_lib_isdn_event_catcher(void *arg)
+{
+       struct misdn_lib *mgr = arg;
+       int zero_frm=0 , fff_frm=0 ;
+       int midev= mgr->midev;
+       int port;
+       
+       //cb_log(5, 0, "In event_catcher thread\n");
+       
+       while (1) {
+               msg_t *msg = fetch_msg(midev); 
+               iframe_t *frm;
+               
+               
+               if (!msg) continue;
+               
+               frm = (iframe_t*) msg->data;
+               port = frm->addr&IF_CONTRMASK;
+               
+               /** When we make a call from NT2Ast we get this frames **/
+               if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
+                       zero_frm++; 
+                       free_msg(msg);
+                       continue;
+               } else {
+                       if (zero_frm) {
+                               cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
+                               zero_frm = 0 ;
+                       }
+               }
+               
+               /** I get this sometimes after setup_bc **/
+               if (frm->len == 0 &&  frm->dinfo == 0 && frm->prim == 0xffffffff ) {
+                       fff_frm++; 
+                       free_msg(msg);
+                       continue;
+               } else {
+                       if (fff_frm) {
+                               cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
+                               fff_frm = 0 ;
+                       }
+               }
+               
+               manager_isdn_handler(frm, msg);
+       }
+
+}
+
+
+/** App Interface **/
+
+int te_lib_init() {
+       char buff[1025];
+       iframe_t *frm=(iframe_t*)buff;
+       int midev=mISDN_open();
+       int ret;
+
+       memset(buff,0,1025);
+  
+       if  (midev<=0) return midev;
+  
+/* create entity for layer 3 TE-mode */
+       mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+       ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
+  
+       if (ret < mISDN_HEADER_LEN) {
+       noentity:
+               fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
+               exit(-1);
+       }
+  
+       entity = frm->dinfo & 0xffff ;
+  
+       if (!entity)
+               goto noentity;
+
+       return midev;
+  
+}
+
+void te_lib_destroy(int midev)
+{
+       char buf[1024];
+       mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
+
+       cb_log(4, 0, "Entetity deleted\n");
+       mISDN_close(midev);
+       cb_log(4, 0, "midev closed\n");
+}
+
+
+
+void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
+{
+       holded_bc->holded=0;
+}
+
+struct misdn_bchannel *manager_find_bc_by_pid(int pid)
+{
+       struct misdn_stack *stack;
+       int i;
+  
+       for (stack=glob_mgr->stack_list;
+            stack;
+            stack=stack->next) {
+               for (i=0; i<stack->b_num; i++)
+                       if (stack->bc[i].pid == pid) return &stack->bc[i];
+       }
+  
+       return NULL;
+}
+
+struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
+{
+       return find_bc_holded(bc->stack);
+}
+
+
+
+struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel)
+{
+       struct misdn_stack *stack;
+       int i;
+       
+       if (channel < 0 || channel > MAX_BCHANS)
+               return NULL;
+
+       for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
+    
+               if (stack->port == port) {
+                       if (channel > 0) {
+                               if (channel <= stack->b_num) {
+                                       for (i = 0; i < stack->b_num; i++) {
+                                               if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
+                                                       return NULL;
+                                               }
+                                       }
+                               } else
+                                       return NULL;
+                       }
+                       for (i = 0; i < stack->b_num; i++) {
+                               if (!stack->bc[i].in_use) {
+                                       stack->bc[i].channel = channel;
+                                       stack->bc[i].channel_preselected = channel?1:0;
+                                       stack->bc[i].in_use = 1;
+                                       return &stack->bc[i];
+                               }
+                       }
+                       return NULL;
+               }
+       }
+       return NULL;
+}
+
+void misdn_lib_log_ies(struct misdn_bchannel *bc)
+{
+       if (!bc) return;
+       
+       cb_log(2, bc->stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", bc->stack->mode==NT_MODE?"NT":"TE", bc->cause, bc->out_cause, bc->rad);
+       
+       cb_log(2, bc->stack->port,
+              " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c\n",
+              bc->info_dad,
+              bc->onumplan>=0?'0'+bc->onumplan:' ',
+              bc->dnumplan>=0?'0'+bc->dnumplan:' ',
+              bc->rnumplan>=0?'0'+bc->rnumplan:' '
+               );
+       
+       cb_log(2, bc->stack->port, " --> channel:%d caps:%s pi:%x keypad:%s\n", bc->channel, bearer2str(bc->capability),bc->progress_indicator, bc->keypad);
+
+       cb_log(3, bc->stack->port, " --> pid:%d addr:%x l3id:%x\n", bc->pid, bc->addr, bc->l3_id);
+
+       cb_log(4, bc->stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
+}
+
+int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
+{
+       msg_t *msg; 
+       int err = -1 ;
+  
+       if (!bc) goto ERR; 
+
+       if ( bc->stack->mode == NT_MODE && !bc->stack->l1link) {
+               /** Queue Event **/
+               bc->evq=event;
+               cb_log(1, bc->stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
+               { /* Pull Up L1 */ 
+                       iframe_t act;
+                       act.prim = PH_ACTIVATE | REQUEST; 
+                       act.addr = (bc->stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+                       act.dinfo = 0;
+                       act.len = 0;
+                       mISDN_write(glob_mgr->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+               }
+               
+               return 0;
+       }
+       
+       cb_log(1, bc->stack->port, "I SEND:%s oad:%s dad:%s port:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, bc->stack->port);
+       misdn_lib_log_ies(bc);
+       
+       switch (event) {
+       case EVENT_SETUP:
+               if (create_process(glob_mgr->midev, bc)<0) {
+                       cb_log(0,  bc->stack->port, " No free channel at the moment @ send_event\n");
+                       err=-ENOCHAN;
+                       goto ERR;
+               }
+               break;
+
+       case EVENT_CONNECT:
+       case EVENT_PROGRESS:
+       case EVENT_ALERTING:
+       case EVENT_PROCEEDING:
+       case EVENT_SETUP_ACKNOWLEDGE:
+       case EVENT_RETRIEVE_ACKNOWLEDGE:
+               
+               if (bc->stack->mode == NT_MODE) {
+                       if (bc->channel <=0 ) { /*  else we have the channel already */
+                               bc->channel = find_free_chan_in_stack(bc->stack, 0);
+                               if (!bc->channel) {
+                                       cb_log(0, bc->stack->port, " No free channel at the moment\n");
+                                       err=-ENOCHAN;
+                                       goto ERR;
+                               }
+                       }
+                       /* Its that i generate channels */
+               }
+               
+               setup_bc(bc);
+               
+               
+               if ( event == EVENT_CONNECT ) {
+                       if ( *bc->crypt_key ) {
+                               cb_log(4, bc->stack->port,  " --> ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s \n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+                               
+                               manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
+                       }
+                       
+                       if ( misdn_cap_is_speech(bc->capability)) {
+                               if (!bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
+                               
+                               if (bc->ec_enable) manager_ec_enable(bc);
+                               
+                               if (bc->txgain != 0) {
+                                       cb_log(4, bc->stack->port,  "--> Changing txgain to %d\n", bc->txgain);
+                                       manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
+                               }
+                               
+                               if ( bc->rxgain != 0 ) {
+                                       cb_log(4, bc->stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
+                                       manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
+                               }
+                       }
+               }
+               
+               if (event == EVENT_RETRIEVE_ACKNOWLEDGE) {
+                       manager_bchannel_activate(bc);
+               }
+               
+               break;
+    
+       case EVENT_HOLD_ACKNOWLEDGE:
+       {
+               struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel));
+               
+               memcpy(holded_bc,bc,sizeof(struct misdn_bchannel));
+               
+               holded_bc->holded=1;
+               stack_holder_add(bc->stack,holded_bc);
+               
+               if (bc->stack->mode == NT_MODE) {
+                       empty_chan_in_stack(bc->stack,bc->channel);
+                       empty_bc(bc);
+                       clean_up_bc(bc);
+               }
+               
+               /** we set it up later at RETRIEVE_ACK again.**/
+               holded_bc->upset=0;
+               holded_bc->active=0;
+               
+               cb_event( EVENT_NEW_BC, holded_bc, glob_mgr->user_data);
+       }
+       break;
+       
+       case EVENT_RELEASE:
+               break;
+               
+       case EVENT_RELEASE_COMPLETE:
+               empty_chan_in_stack(bc->stack,bc->channel);
+               empty_bc(bc);
+               clean_up_bc(bc);
+               break;
+    
+       case EVENT_CONNECT_ACKNOWLEDGE:
+               
+               if (misdn_cap_is_speech(bc->capability)) {
+                       if (  !bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
+                       if (bc->ec_enable) manager_ec_enable(bc);
+                       if ( bc->txgain != 0 ) {
+                               cb_log(4, bc->stack->port, "--> Changing txgain to %d\n", bc->txgain);
+                               manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
+                       }
+                       if ( bc->rxgain != 0 ) {
+                               cb_log(4, bc->stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
+                               manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
+                       }
+               }
+               break;
+    
+       default:
+               break;
+       }
+  
+       /* Later we should think about sending bchannel data directly to misdn. */
+       msg = isdn_msg_build_event(msgs_g, bc, event, bc->stack->mode==NT_MODE?1:0);
+       msg_queue_tail(&bc->stack->downqueue, msg);
+       sem_post(&glob_mgr->new_msg);
+  
+       return 0;
+  
+ ERR:
+       return -1; 
+}
+
+
+
+int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
+{  
+
+       if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
+               printf("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
+       }
+
+       if (handle_timers(msg)) 
+               return 0 ;
+    
+       if (handle_mgmt(msg)) 
+               return 0 ;
+
+       if (handle_l2(msg)) 
+               return 0 ;
+
+       /* Its important to handle l1 AFTER l2  */
+       if (handle_l1(msg)) 
+               return 0 ;
+
+       if (handle_bchan(msg)) 
+               return 0 ;
+
+       /** Handle L2/3 Signalling after bchans **/ 
+       if (handle_frm_nt(msg)) 
+               return 0 ;
+    
+       if (handle_frm(msg)) 
+               return 0 ;
+
+       cb_log(0, frm->addr&IF_CONTRMASK, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on port: %d\n",frm->prim, frm->len, frm->addr, frm->dinfo, frm->addr&IF_CONTRMASK);
+   
+       free_msg(msg);
+
+       return 0;
+}
+
+
+
+
+int misdn_lib_get_port_info(int port)
+{
+       msg_t *msg=alloc_msg(MAX_MSG_SIZE);
+       iframe_t *frm;
+       struct misdn_stack *stack=find_stack_by_port(port);
+       if (!msg) {
+               cb_log(0, port, "misgn_lib_get_port: alloc_msg failed!\n");
+               return -1;
+       }
+       frm=(iframe_t*)msg->data;
+       if (!stack ) {
+               cb_log(0, port, "There is no Stack on Port:%d\n",port);
+               return -1;
+       }
+       /* activate bchannel */
+       frm->prim = CC_STATUS_ENQUIRY | REQUEST;
+       frm->addr = stack->upper_id;
+       frm->dinfo = 0;
+       frm->len = 0;
+  
+       msg_queue_tail(&glob_mgr->activatequeue, msg);
+       sem_post(&glob_mgr->new_msg);
+
+  
+       return 0; 
+}
+
+int misdn_lib_port_restart(int port)
+{
+       struct misdn_stack *stack=find_stack_by_port(port);
+       cb_log(0, port, "Restarting Port:%d\n",port);
+       if (stack) {
+               cb_log(0, port, "Stack:%p\n",stack);
+
+               
+               clear_l3(stack);
+               
+               {
+                       msg_t *msg=alloc_msg(MAX_MSG_SIZE);
+                       iframe_t *frm;
+
+                       if (!msg) {
+                               cb_log(0, port, "port_restart: alloc_msg fialed\n");
+                               return -1;
+                       }
+                       
+                       frm=(iframe_t*)msg->data;
+                       /* we must activate if we are deactivated */
+                       /* activate bchannel */
+       
+                       frm->prim = DL_RELEASE | REQUEST;
+       
+                       frm->addr = stack->upper_id ;
+                       frm->dinfo = 0;
+                       frm->len = 0;
+                       msg_queue_tail(&glob_mgr->activatequeue, msg);
+                       sem_post(&glob_mgr->new_msg);
+               }
+               return 0;
+    
+               stack_te_destroy(stack);
+      
+               {
+                       struct misdn_stack *tmpstack;
+                       struct misdn_stack *newstack=stack_te_init(stack->midev ,port, stack->ptp);
+      
+      
+                       if (stack == glob_mgr->stack_list) {
+                               struct misdn_stack *n=glob_mgr->stack_list->next;
+                               glob_mgr->stack_list = newstack ;
+                               glob_mgr->stack_list->next = n;
+                       } else {
+                               for (tmpstack=glob_mgr->stack_list;
+                                    tmpstack->next;
+                                    tmpstack=tmpstack->next) 
+                                       if (tmpstack->next == stack) break;
+
+                               if (!tmpstack->next) {
+                                       cb_log(0, port, "Stack to restart not found\n");
+                                       return 0;
+                               }  else {
+                                       struct misdn_stack *n=tmpstack->next->next;
+                                       tmpstack->next=newstack;
+                                       newstack->next=n;
+                               }
+                       }
+      
+                       {
+                               int i;
+                               for(i=0;i<newstack->b_num; i++) {
+                                       int r;
+                                       if ((r=init_bc(newstack, &newstack->bc[i], newstack->midev,port,i, "", 1))<0) {
+                                               cb_log(0, port, "Got Err @ init_bc :%d\n",r);
+                                               return 0;
+                                       }
+                               }
+                       }
+      
+                       free(stack);
+               }
+       }
+
+       return 0;
+}
+
+
+
+sem_t handler_started; 
+
+void manager_event_handler(void *arg)
+{
+       sem_post(&handler_started); 
+       while (1) {
+               struct misdn_stack *stack;
+               msg_t *msg;
+    
+               /** wait for events **/
+               sem_wait(&glob_mgr->new_msg);
+    
+               for (msg=msg_dequeue(&glob_mgr->activatequeue);
+                    msg;
+                    msg=msg_dequeue(&glob_mgr->activatequeue)
+                       )
+               {
+       
+                       iframe_t *frm =  (iframe_t*) msg->data ;
+
+                       switch ( frm->prim) {
+                       case MGR_SETSTACK | REQUEST :
+                               break;
+                       default:
+                               mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
+                               free_msg(msg);
+                       }
+               }
+
+               for (stack=glob_mgr->stack_list;
+                    stack;
+                    stack=stack->next ) { 
+                       while ( (msg=msg_dequeue(&stack->downqueue)) ) {
+       
+                               if (stack->mode == NT_MODE ){
+         
+                                       if (stack->nst.manager_l3(&stack->nst, msg))
+                                               cb_log(0, stack->port, "Error@ Sending Message in NT-Stack.\n");
+         
+                               } else {
+                                       if (msg) {
+                                               iframe_t *frm = (iframe_t *)msg->data;
+                                               struct misdn_bchannel *bc = find_bc_by_l3id(stack, frm->dinfo);
+           
+                                               if (bc) send_msg(glob_mgr->midev, bc, msg);
+                                       }
+                               }
+                       }
+               }
+       }
+}
+
+
+int misdn_lib_maxports_get() { /** BE AWARE WE HAVE NO CB_LOG HERE! **/
+       
+       int i = mISDN_open();
+       int max=0;
+       
+       if (i<0)
+               return -1;
+
+       max = mISDN_get_stack_count(i);
+       
+       mISDN_close(i);
+       
+       return max;
+}
+
+int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_data)
+{
+       struct misdn_lib *mgr=calloc(1, sizeof(struct misdn_lib));
+       char *tok, *tokb;
+       char plist[1024];
+       int midev;
+       int port_count=0;
+       cb_log = iface->cb_log;
+       cb_event = iface->cb_event;
+       cb_clearl3_true = iface->cb_clearl3_true;
+       
+       glob_mgr = mgr;
+  
+       msg_init();
+       debug_init(0 , NULL, NULL, NULL);
+       
+       if (!portlist || (*portlist == 0) ) return 1;
+       
+       init_flip_bits();
+       
+       {
+               strncpy(plist,portlist, 1024);
+               plist[1023] = 0;
+       }
+  
+       memcpy(tone_425_flip,tone_425,TONE_425_SIZE);
+       flip_buf_bits(tone_425_flip,TONE_425_SIZE);
+
+       memcpy(tone_silence_flip,tone_SILENCE,TONE_SILENCE_SIZE);
+       flip_buf_bits(tone_silence_flip,TONE_SILENCE_SIZE);
+  
+       midev=te_lib_init();
+       mgr->midev=midev;
+
+       port_count=mISDN_get_stack_count(midev);
+  
+       msg_queue_init(&mgr->activatequeue);
+  
+       if (sem_init(&mgr->new_msg, 1, 0)<0)
+               sem_init(&mgr->new_msg, 0, 0);
+  
+       for (tok=strtok_r(plist," ,",&tokb );
+            tok; 
+            tok=strtok_r(NULL," ,",&tokb)) {
+               int port = atoi(tok);
+               struct misdn_stack *stack;
+               static int first=1;
+               int ptp=0;
+    
+               if (strstr(tok, "ptp"))
+                       ptp=1;
+
+               if (port > port_count) {
+                       cb_log(0, port, "Couldn't Initialize Port:%d since we have only %d ports\n",port, port_count);
+                       exit(1);
+               }
+               stack=stack_te_init(midev, port, ptp);
+    
+    
+    
+               if (!stack) {
+                       perror("init_stack");
+                       exit(1);
+               }
+    
+               if (stack && first) {
+                       mgr->stack_list=stack;
+                       first=0;
+                       {
+                               int i;
+                               for(i=0;i<stack->b_num; i++) {
+                                       int r;
+                                       if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "", 1))<0) {
+                                               cb_log(0, port, "Got Err @ init_bc :%d\n",r);
+                                               exit(1);
+                                       }
+                               }
+                       }
+      
+                       continue;
+               }
+    
+               if (stack) {
+                       struct misdn_stack * help;
+                       for ( help=mgr->stack_list; help; help=help->next ) 
+                               if (help->next == NULL) break;
+      
+      
+                       help->next=stack;
+
+                       {
+                               int i;
+                               for(i=0;i<stack->b_num; i++) {
+                                       int r;
+                                       if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "",1 ))<0) {
+                                               cb_log(0, port, "Got Err @ init_bc :%d\n",r);
+                                               exit(1);
+                                       } 
+                               }
+                       }
+               }
+    
+       }
+  
+       if (sem_init(&handler_started, 1, 0)<0)
+               sem_init(&handler_started, 0, 0);
+  
+       cb_log(4, 0, "Starting Event Handler\n");
+       pthread_create( &mgr->event_handler_thread, NULL,(void*)manager_event_handler, mgr);
+  
+       sem_wait(&handler_started) ;
+       cb_log(4, 0, "Starting Event Catcher\n");
+       pthread_create( &mgr->event_thread, NULL, (void*)misdn_lib_isdn_event_catcher, mgr);
+  
+       cb_log(4, 0, "Event Catcher started\n");
+  
+       global_state= MISDN_INITIALIZED; 
+  
+       return (mgr == NULL);
+}
+
+
+
+void misdn_lib_destroy()
+{
+       struct misdn_stack *help;
+       int i;
+  
+       for ( help=glob_mgr->stack_list; help; help=help->next ) {
+               for(i=0;i<help->b_num; i++) {
+                       char buf[1024];
+      
+                       mISDN_write_frame(help->midev, buf, help->bc[i].addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+                       help->bc[i].addr = 0;
+               }
+
+
+               cb_log (1, help->port, "Destroying port:%d\n", help->port);
+               stack_te_destroy(help);
+       }
+       
+       if (global_state == MISDN_INITIALIZED) {
+               cb_log(4, 0, "Killing Handler Thread\n");
+               if ( pthread_cancel(glob_mgr->event_handler_thread) == 0 ) {
+                       cb_log(4, 0, "Joining Handler Thread\n");
+                       pthread_join(glob_mgr->event_handler_thread, NULL);
+               }
+         
+         
+               cb_log(4, 0, "Killing Main Thread\n");
+               if ( pthread_cancel(glob_mgr->event_thread) == 0 ) {
+                       cb_log(4, 0, "Joining Main Thread\n");
+                       pthread_join(glob_mgr->event_thread, NULL);
+               }
+       }
+  
+       cb_log(1, 0, "Closing mISDN device\n");
+       te_lib_destroy(glob_mgr->midev);
+}
+
+
+char *manager_isdn_get_info(enum event_e event)
+{
+       return isdn_get_info(msgs_g , event, 0);
+}
+
+
+
+
+void manager_bchannel_activate(struct misdn_bchannel *bc)
+{
+       msg_t *msg=alloc_msg(MAX_MSG_SIZE);
+       iframe_t *frm;
+
+       if (!msg) {
+               cb_log(0, bc->stack->port, "bchannel_activate: alloc_msg failed !");
+               return ;
+       }
+       
+       frm=(iframe_t*)msg->data;
+       /* we must activate if we are deactivated */
+       clear_ibuffer(bc->misdnbuf);
+       clear_ibuffer(bc->astbuf);
+       
+       
+       if (bc->active) return;
+  
+       cb_log(5, bc->stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
+
+       /* activate bchannel */
+       frm->prim = DL_ESTABLISH | REQUEST;
+       frm->addr = bc->addr ;
+       frm->dinfo = 0;
+       frm->len = 0;
+       
+       msg_queue_tail(&glob_mgr->activatequeue, msg);
+       sem_post(&glob_mgr->new_msg);
+
+       bc->active=1;
+  
+       return ;
+  
+}
+
+
+void manager_bchannel_deactivate(struct misdn_bchannel * bc)
+{
+       iframe_t dact;
+
+  
+       if (!bc->active) return;
+  
+       cb_log(5, bc->stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
+  
+       bc->tone=TONE_NONE;
+
+       dact.prim = DL_RELEASE | REQUEST;
+       dact.addr = bc->addr;
+       dact.dinfo = 0;
+       dact.len = 0;
+  
+       mISDN_write(bc->stack->midev, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
+       clear_ibuffer(bc->misdnbuf);
+       clear_ibuffer(bc->astbuf);
+       bc->active=0;
+  
+       return;
+}
+
+
+int manager_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
+{
+       
+       if (!bc->active) return -1;   
+       
+       flip_buf_bits(data,len);
+       
+       if ( !bc->nojitter && misdn_cap_is_speech(bc->capability) ) {
+               if (len > ibuf_freecount(bc->misdnbuf)) {
+                       len=ibuf_freecount(bc->misdnbuf);
+               }
+               ibuf_memcpy_w(bc->misdnbuf, (unsigned char*)data, len);
+       } else {
+               char buf[4096 + mISDN_HEADER_LEN];
+               iframe_t *frm= (iframe_t*)buf;
+               int  r;
+               
+               frm->prim = DL_DATA|REQUEST;
+               frm->dinfo = 0;
+               frm->addr = bc->addr | IF_DOWN;
+               frm->len = len;
+               memcpy(&buf[mISDN_HEADER_LEN], data,len);
+
+               
+               
+               if ( misdn_cap_is_speech(bc->capability))
+                       cb_log(4, bc->stack->port, "Writing %d bytes\n",len);
+               
+               cb_log(9, bc->stack->port, "Wrinting %d bytes 2 mISDN\n",len);
+               r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
+       }
+
+       return 0;
+}
+
+
+
+
+void manager_send_tone (struct misdn_bchannel *bc, enum tone_e tone)
+{
+       if (tone != TONE_NONE) manager_bchannel_activate(bc);
+       bc->tone=tone;
+       bc->tone_cnt2=-1;
+       bc->tone_cnt=0;
+}
+
+
+
+/*
+ * send control information to the channel (dsp-module)
+ */
+void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2)
+{
+       unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
+       iframe_t *ctrl = (iframe_t *)buffer; /* preload data */
+       unsigned long *d = (unsigned long *)&ctrl->data.p;
+  
+       ctrl->prim = PH_CONTROL | REQUEST;
+       ctrl->addr = bc->addr;
+       ctrl->dinfo = 0;
+       ctrl->len = sizeof(unsigned long)*2;
+       *d++ = c1;
+       *d++ = c2;
+       mISDN_write(bc->stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
+}
+
+/*
+ * send control information to the channel (dsp-module)
+ */
+void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len)
+{
+       unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
+       iframe_t *ctrl = (iframe_t *)buffer;
+       unsigned long *d = (unsigned long *)&ctrl->data.p;
+       
+       ctrl->prim = PH_CONTROL | REQUEST;
+       ctrl->addr = bc->addr;
+       ctrl->dinfo = 0;
+       ctrl->len = sizeof(unsigned long) + c2_len;
+       *d++ = c1;
+       memcpy(d, c2, c2_len);
+       mISDN_write(bc->stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
+}
+
+
+
+
+void manager_clean_bc(struct misdn_bchannel *bc )
+{
+       if (bc->state == STATE_CONNECTED)
+               misdn_lib_send_event(bc,EVENT_DISCONNECT);
+
+       empty_chan_in_stack(bc->stack, bc->channel);
+       empty_bc(bc);
+  
+       misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
+}
+
+
+void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder)
+{
+       struct misdn_bchannel *help;
+
+       cb_log(4,holder->stack->port, "*HOLDER: add %x\n",holder->l3_id);
+       
+       holder->stack_holder=1;
+       
+       if (!stack ) return ;
+       
+       holder->next=NULL;
+       
+       if (!stack->holding) {
+               stack->holding = holder;
+               return;
+       }
+  
+       for (help=stack->holding;
+            help;
+            help=help->next) {
+               if (!help->next) {
+                       help->next=holder;
+               }
+       }
+  
+}
+
+void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder)
+{
+       struct misdn_bchannel *h1;
+
+       if (!holder->stack_holder) return;
+       
+       cb_log(4,holder->stack->port, "*HOLDER: remove %x\n",holder->l3_id);
+       if (!stack || ! stack->holding) return;
+  
+       if (holder == stack->holding) {
+               stack->holding = stack->holding->next;
+               return;
+       }
+       
+       for (h1=stack->holding;
+            h1;
+            h1=h1->next) {
+               if (h1->next == holder) {
+                       h1->next=h1->next->next;
+                       return ;
+               }
+       }
+}
+
+
+struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id)
+{
+       struct misdn_bchannel *help;
+
+       cb_log(4,stack?stack->port:0, "*HOLDER: find %x\n",l3id);
+       
+       if (!stack) return NULL;
+       
+       for (help=stack->holding;
+            help;
+            help=help->next) {
+               if (help->l3_id == l3id) {
+                       cb_log(4,stack->port, "*HOLDER: found bc\n");
+                       return help;
+               }
+       }
+
+       cb_log(4,stack->port, "*HOLDER: find nothing\n");
+       return NULL;
+}
+
+
+
+void manager_ec_enable(struct misdn_bchannel *bc)
+{
+       int ec_arr[2];
+       
+       cb_log(1, bc?bc->stack->port:0,"Sending Control ECHOCAN_ON enblock\n");
+       
+       switch (bc->ec_deftaps) {
+       case 4:
+       case 8:
+       case 16:
+       case 32:
+       case 64:
+       case 128:
+       case 256:
+       case 512:
+       case 1024:
+               cb_log(4, bc->stack->port, "Taps is %d\n",bc->ec_deftaps);
+               break;
+       default:
+               cb_log(0, bc->stack->port, "Taps should be power of 2\n");
+               bc->ec_deftaps=128;
+       }
+
+       ec_arr[0]=bc->ec_deftaps;
+       ec_arr[1]=bc->ec_training;
+       
+       manager_ph_control_block(bc,  ECHOCAN_ON,  ec_arr, sizeof(ec_arr));
+}
+
+
+void manager_ec_disable(struct misdn_bchannel *bc)
+{
+       cb_log(1, bc?bc->stack->port:0, "Sending Control ECHOCAN_OFF\n");
+       manager_ph_control(bc,  ECHOCAN_OFF, 0);
+}
+
+struct misdn_stack* get_misdn_stack() {
+       return glob_mgr->stack_list;
+}
diff --git a/channels/misdn/isdn_lib.h b/channels/misdn/isdn_lib.h
new file mode 100755 (executable)
index 0000000..a6f3a25
--- /dev/null
@@ -0,0 +1,439 @@
+/*
+ * Chan_Misdn -- Channel Driver for Asterisk
+ *
+ * Interface to mISDN
+ *
+ * Copyright (C) 2004, Christian Richter
+ *
+ * Christian Richter <crich@beronet.com>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+#ifndef TE_LIB
+#define TE_LIB
+
+#include <mISDNlib.h>
+#include <isdn_net.h>
+#include <l3dss1.h>
+#include <net_l3.h>
+
+#include <pthread.h>
+
+#ifndef mISDNUSER_HEAD_SIZE
+
+#ifdef MISDNUSER_JOLLY
+#define mISDNUSER_HEAD_SIZE (sizeof(mISDNuser_head_t))
+#else
+#define mISDNUSER_HEAD_SIZE (sizeof(mISDN_head_t))
+#endif
+#endif
+
+#define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt
+#define MISDN_ASTERISK_PVT(ast) 1
+#define MISDN_ASTERISK_TYPE(ast) ast->tech->type
+
+
+/* #include "ies.h" */
+
+#define MAX_BCHANS 30
+
+
+/** For initialization usage **/
+/* typedef int ie_nothing_t ;*/
+/** end of init usage **/
+
+
+enum bc_state_e {
+       STATE_NOTHING=0,
+       STATE_NULL,
+       STATE_CALL_INIT,
+       STATE_CONNECTED,
+       STATE_HOLD_ACKNOWLEDGE
+};
+
+
+enum tone_e {
+       TONE_NONE=0,
+       TONE_DIAL,
+       TONE_ALERTING,
+       TONE_BUSY,
+       TONE_FILE
+};
+
+enum misdn_err_e {
+       ENOCHAN=1
+};
+
+
+
+enum mISDN_NUMBER_PLAN {
+       NUMPLAN_UNINITIALIZED=-1,
+       NUMPLAN_INTERNATIONAL=0x1,
+       NUMPLAN_NATIONAL=0x2,
+       NUMPLAN_SUBSCRIBER=0x4,
+       NUMPLAN_UNKNOWN=0x0
+}; 
+
+
+enum event_response_e {
+       RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE,
+       RESPONSE_IGNORE_SETUP,
+       RESPONSE_ERR,
+       RESPONSE_OK
+};
+
+
+
+enum event_e {
+       EVENT_NOTHING,
+       EVENT_BCHAN_DATA,
+       EVENT_CLEANUP,
+       EVENT_PROCEEDING,
+       EVENT_PROGRESS,
+       EVENT_SETUP,
+       EVENT_ALERTING,
+       EVENT_CONNECT,
+       EVENT_SETUP_ACKNOWLEDGE,
+       EVENT_CONNECT_ACKNOWLEDGE ,
+       EVENT_USER_INFORMATION,
+       EVENT_SUSPEND_REJECT,
+       EVENT_RESUME_REJECT,
+       EVENT_HOLD,
+       EVENT_SUSPEND,
+       EVENT_RESUME,
+       EVENT_HOLD_ACKNOWLEDGE,
+       EVENT_SUSPEND_ACKNOWLEDGE,
+       EVENT_RESUME_ACKNOWLEDGE,
+       EVENT_HOLD_REJECT,
+       EVENT_RETRIEVE,
+       EVENT_RETRIEVE_ACKNOWLEDGE,
+       EVENT_RETRIEVE_REJECT,
+       EVENT_DISCONNECT,
+       EVENT_RESTART,
+       EVENT_RELEASE,
+       EVENT_RELEASE_COMPLETE,
+       EVENT_FACILITY,
+       EVENT_NOTIFY,
+       EVENT_STATUS_ENQUIRY,
+       EVENT_INFORMATION,
+       EVENT_STATUS,
+       EVENT_TIMEOUT,
+       EVENT_DTMF_TONE,
+       EVENT_NEW_L3ID,
+       EVENT_NEW_BC,
+       EVENT_UNKNOWN
+}; 
+
+
+enum ie_name_e {
+       IE_DUMMY,
+       IE_LAST
+};
+
+enum { /* bearer capability */
+       INFO_CAPABILITY_SPEECH=0,
+       INFO_CAPABILITY_AUDIO_3_1K=0x10 ,
+       INFO_CAPABILITY_AUDIO_7K=0x11 ,
+       INFO_CAPABILITY_VIDEO =0x18,
+       INFO_CAPABILITY_DIGITAL_UNRESTRICTED =0x8,
+       INFO_CAPABILITY_DIGITAL_RESTRICTED =0x09,
+       INFO_CAPABILITY_DIGITAL_UNRESTRICTED_TONES
+};
+
+enum { /* progress indicators */
+       INFO_PI_CALL_NOT_E2E_ISDN =0x01,
+       INFO_PI_CALLED_NOT_ISDN =0x02,
+       INFO_PI_CALLER_NOT_ISDN =0x03,
+       INFO_PI_CALLER_RETURNED_TO_ISDN =0x04,
+       INFO_PI_INBAND_AVAILABLE =0x08,
+       INFO_PI_DELAY_AT_INTERF =0x0a,
+       INFO_PI_INTERWORKING_WITH_PUBLIC =0x10,
+       INFO_PI_INTERWORKING_NO_RELEASE =0x11,
+       INFO_PI_INTERWORKING_NO_RELEASE_PRE_ANSWER =0x12,
+       INFO_PI_INTERWORKING_NO_RELEASE_POST_ANSWER =0x13
+};
+
+enum { /*CODECS*/
+       INFO_CODEC_ULAW=2,
+       INFO_CODEC_ALAW=3
+}; 
+
+
+enum layer_e {
+       L3,
+       L2,
+       L1,
+       UNKNOWN
+}; 
+
+enum facility_type {
+       FACILITY_NONE,
+       FACILITY_CALLDEFLECT
+};
+
+struct misdn_bchannel {
+       /** init stuff **/
+       int b_stid;
+       /* int b_addr; */
+       int layer_id;
+  
+       /** var stuff**/
+       int l3_id;
+       int pid;
+       int ces;
+  
+       int channel;
+       int channel_preselected;
+       
+       int in_use;
+       int addr;
+
+       unsigned char * bframe;
+       int bframe_len;
+       int time_usec;
+       
+       sem_t astsem;
+       sem_t misdnsem;
+       ibuffer_t *astbuf;
+       ibuffer_t *misdnbuf;
+  
+       /* dtmf digit */
+       int dtmf;
+       int send_dtmf;
+
+       /* wether we should use jollys dsp or not */
+       int nodsp;
+       
+       /* wether we should use our jitter buf system or not */
+       int nojitter;
+       
+       enum mISDN_NUMBER_PLAN dnumplan;
+       enum mISDN_NUMBER_PLAN rnumplan;
+       enum mISDN_NUMBER_PLAN onumplan;
+
+       int progress_coding;
+       int progress_location;
+       int progress_indicator;
+       
+       enum facility_type facility;
+       char facility_calldeflect_nr[15];
+       
+       enum event_e evq;
+       
+       /*** CRYPTING STUFF ***/
+       
+       int crypt;
+       int curprx;
+       int curptx; 
+       char crypt_key[255];
+  
+       int crypt_state;
+    
+       /*char ast_dtmf_buf[255];
+         char misdn_dtmf_buf[255]; */
+  
+       /*** CRYPTING STUFF END***/
+  
+       int active;
+       int upset;
+
+       enum tone_e tone;
+       int tone_cnt;
+       int tone_cnt2;
+  
+       enum bc_state_e state;
+
+       int holded;
+       int stack_holder;
+       
+       int pres;
+  
+       int nohdlc;
+       
+       int capability;
+       int law;
+       /** V110 Stuff **/
+       int rate;
+       int mode;
+
+       int user1;
+       int urate;
+       int async;
+       /* V110 */
+  
+       unsigned char display[84];
+       unsigned char msn[32];
+       unsigned char oad[32];
+       unsigned char rad[32];
+       unsigned char dad[32];
+       unsigned char orig_dad[32];
+       unsigned char keypad[32];
+  
+       unsigned char info_dad[64];
+       unsigned char infos_pending[64];
+       unsigned char info_keypad[32];
+       unsigned char clisub[24];
+       unsigned char cldsub[24];
+       unsigned char fac[132];
+       unsigned char uu[256];
+  
+       int cause;
+       int out_cause;
+  
+       /* struct misdn_bchannel hold_bc; */
+  
+       /** list stuf **/
+
+       int ec_enable;
+       int ec_deftaps;
+       int ec_whenbridged;
+       int ec_training;
+       
+       int orig;
+
+       int txgain;
+       int rxgain;
+  
+       struct misdn_bchannel *next;
+       struct misdn_stack *stack;
+};
+
+struct misdn_stack {
+       /** is first element because &nst equals &mISDNlist **/
+       net_stack_t nst;
+       manager_t mgr;
+  
+       int d_stid;
+  
+       int b_num;
+  
+       int b_stids[MAX_BCHANS + 1];
+  
+       int ptp;
+       int lower_id;
+       int upper_id;
+  
+       int l2link;
+  
+       time_t l2establish;
+  
+       int l1link;
+       int midev;
+  
+       enum mode_e {NT_MODE, TE_MODE} mode;
+       int pri;
+  
+
+       int procids[0x100+1];
+
+       msg_queue_t downqueue;
+       int busy;
+  
+       int port;
+       struct misdn_bchannel bc[MAX_BCHANS + 1];
+  
+       struct misdn_bchannel* bc_list; 
+  
+       int channels[MAX_BCHANS + 1];
+
+  
+  
+       int te_choose_channel;
+  
+
+       struct misdn_bchannel *holding; /* Queue which holds holded channels :) */
+  
+       struct misdn_stack *next;
+}; 
+
+struct misdn_stack* get_misdn_stack( void );
+
+enum event_response_e (*cb_event) (enum event_e event, struct misdn_bchannel *bc, void *user_data);
+void (*cb_log) (int level, int port, char *tmpl, ...);
+int (*cb_clearl3_true)(void);
+
+struct misdn_lib_iface {
+       
+       enum event_response_e (*cb_event)(enum event_e event, struct misdn_bchannel *bc, void *user_data);
+       void (*cb_log)(int level, int port, char *tmpl, ...);
+       int (*cb_clearl3_true)(void);
+};
+
+/***** USER IFACE **********/
+
+int misdn_lib_init(char *portlist, struct misdn_lib_iface* iface, void *user_data);
+int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event );
+void misdn_lib_destroy(void);
+
+void misdn_lib_log_ies(struct misdn_bchannel *bc);
+
+char *manager_isdn_get_info(enum event_e event);
+
+void misdn_lib_transfer(struct misdn_bchannel* holded_bc);
+
+struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel);
+
+void manager_bchannel_activate(struct misdn_bchannel *bc);
+void manager_bchannel_deactivate(struct misdn_bchannel * bc);
+int manager_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len);
+void manager_send_tone (struct misdn_bchannel *bc, enum tone_e tone);
+
+void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2);
+
+
+int misdn_lib_port_restart(int port);
+int misdn_lib_get_port_info(int port);
+
+int misdn_lib_port_up(int port);
+
+int misdn_lib_get_port_up (int port) ;
+     
+int misdn_lib_maxports_get(void) ;
+
+void misdn_lib_release(struct misdn_bchannel *bc);
+
+int misdn_cap_is_speech(int cap);
+int misdn_inband_avail(struct misdn_bchannel *bc);
+
+int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data);
+
+
+struct isdn_msg {
+       unsigned long misdn_msg;
+  
+       enum layer_e layer;
+       enum event_e event;
+  
+       void (*msg_parser)(struct isdn_msg *msgs, msg_t *msg, struct misdn_bchannel *bc, int nt);
+       msg_t *(*msg_builder)(struct isdn_msg *msgs, struct misdn_bchannel *bc, int nt);
+       void (*msg_printer)(struct isdn_msg *msgs);
+  
+       char *info;
+  
+} ; 
+
+
+
+
+
+
+
+
+void manager_ec_enable(struct misdn_bchannel *bc);
+void manager_ec_disable(struct misdn_bchannel *bc);
+
+
+/* for isdn_msg_parser.c */
+msg_t *create_l3msg(int prim, int mt, int dinfo , int size, int nt);
+
+
+
+#define PRI_TRANS_CAP_SPEECH                                    0x0
+#define PRI_TRANS_CAP_DIGITAL                                   0x08
+#define PRI_TRANS_CAP_RESTRICTED_DIGITAL                        0x09
+#define PRI_TRANS_CAP_3_1K_AUDIO                                0x10
+#define PRI_TRANS_CAP_7K_AUDIO                                  0x11
+
+#endif
diff --git a/channels/misdn/isdn_msg_parser.c b/channels/misdn/isdn_msg_parser.c
new file mode 100755 (executable)
index 0000000..0dd8d4c
--- /dev/null
@@ -0,0 +1,1352 @@
+/*
+ * Chan_Misdn -- Channel Driver for Asterisk
+ *
+ * Interface to mISDN
+ *
+ * Copyright (C) 2004, Christian Richter
+ *
+ * Christian Richter <crich@beronet.com>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+
+#include "isdn_lib.h"
+#include "ie.c"
+
+
+void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       CALL_PROCEEDING_t *proceeding=(CALL_PROCEEDING_t*)((unsigned long)msg->data+ HEADER_LEN);
+       {
+               int  exclusive, channel;
+               dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)proceeding, &exclusive, &channel, nt,bc);
+               
+               if (channel==0xff) /* any channel */
+                       channel=-1;
+    
+               /*  ALERT: is that everytime true ?  */
+               if (channel > 0 && bc->stack->mode == NT_MODE) 
+                       bc->channel = channel;
+       }
+       
+       dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)proceeding, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
+       
+#if DEBUG 
+       printf("Parsing PROCEEDING Msg\n"); 
+#endif
+
+}
+msg_t *build_proceeding (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       CALL_PROCEEDING_t *proceeding;
+       msg_t *msg =(msg_t*)create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING,  bc?bc->l3_id:-1, sizeof(CALL_PROCEEDING_t) ,nt); 
+  
+       proceeding=(CALL_PROCEEDING_t*)((msg->data+HEADER_LEN));
+
+       enc_ie_channel_id(&proceeding->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
+  
+       if (nt) 
+               enc_ie_progress(&proceeding->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
+  
+
+#if DEBUG 
+       printf("Building PROCEEDING Msg\n"); 
+#endif
+       return msg; 
+}
+void print_proceeding (struct isdn_msg msgs[]) 
+{
+}
+
+void parse_alerting (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; 
+       ALERTING_t *alerting=(ALERTING_t*)((unsigned long)(msg->data+HEADER_LEN));
+       //Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN);
+       
+       dec_ie_progress(alerting->PROGRESS, (Q931_info_t *)alerting, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
+       
+#if DEBUG 
+       printf("Parsing ALERTING Msg\n"); 
+#endif
+
+}
+msg_t *build_alerting (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       ALERTING_t *alerting;
+       msg_t *msg =(msg_t*)create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING,  bc?bc->l3_id:-1, sizeof(ALERTING_t) ,nt); 
+  
+       alerting=(ALERTING_t*)((msg->data+HEADER_LEN)); 
+  
+       enc_ie_channel_id(&alerting->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
+       
+       if (nt) 
+               enc_ie_progress(&alerting->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
+#if DEBUG 
+       printf("Building ALERTING Msg\n"); 
+#endif
+       return msg; 
+}
+void print_alerting (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_progress (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       PROGRESS_t *progress=(PROGRESS_t*)((unsigned long)(msg->data+HEADER_LEN)); 
+       //Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN);  
+       
+       dec_ie_progress(progress->PROGRESS, (Q931_info_t *)progress, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
+       
+#if DEBUG 
+       printf("Parsing PROGRESS Msg\n"); 
+#endif
+}
+
+msg_t *build_progress (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       PROGRESS_t *progress;
+       msg_t *msg =(msg_t*)create_l3msg(CC_PROGRESS | REQUEST, MT_PROGRESS,  bc?bc->l3_id:-1, sizeof(PROGRESS_t) ,nt); 
+       progress=(PROGRESS_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building PROGRESS Msg\n"); 
+#endif
+       return msg; 
+}
+void print_progress (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_setup (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{ 
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       SETUP_t *setup= (SETUP_t*)((unsigned long)msg->data+HEADER_LEN);
+       Q931_info_t *qi=(Q931_info_t*)((unsigned long)msg->data+HEADER_LEN);
+
+#if DEBUG 
+       printf("Parsing SETUP Msg\n"); 
+#endif
+       {
+               int type,plan,present, screen;
+               char id[32];
+               dec_ie_calling_pn(setup->CALLING_PN, qi, &type, &plan, &present, &screen, (unsigned char *)id, sizeof(id), nt,bc);
+
+               bc->onumplan=type; 
+               strcpy(bc->oad, id);
+               switch (present) {
+               case 0:
+//                     cb_log(3, bc->stack->port, " --> Pres:0\n");
+                       bc->pres=0; /* screened */
+                       break;
+               case 1:
+//                     cb_log(3, bc->stack->port, " --> Pres:1\n");
+                       bc->pres=1; /* not screened */
+                       break;
+               default:
+//                     cb_log(3, bc->stack->port, " --> Pres:%d\n",present);
+                       bc->pres=0;
+               }
+               switch (screen) {
+               case 0:
+//                     cb_log(4, bc->stack->port, " --> Screen:0\n");
+                       break;
+               default:
+//                     cb_log(4, bc->stack->port, " --> Screen:%d\n",screen);
+                       ;
+               } 
+       }
+       {
+               int  type, plan;
+               char number[32]; 
+               dec_ie_called_pn(setup->CALLED_PN, (Q931_info_t *)setup, &type, &plan, (unsigned char *)number, sizeof(number), nt,bc);
+               strcpy(bc->dad, number);
+               bc->dnumplan=type; 
+       }
+       {
+               char keypad[32];
+               dec_ie_keypad(setup->KEYPAD, (Q931_info_t *)setup, (unsigned char *)keypad, sizeof(keypad), nt,bc);
+               strcpy(bc->keypad, keypad);
+       }
+
+       {
+               int  sending_complete;
+               dec_ie_complete(setup->COMPLETE, (Q931_info_t *)setup, &sending_complete, nt,bc);
+       }
+  
+       {
+               int  type, plan, present, screen, reason;
+               char id[32]; 
+               dec_ie_redir_nr(setup->REDIR_NR, (Q931_info_t *)setup, &type, &plan, &present, &screen, &reason, (unsigned char *)id, sizeof(id), nt,bc);
+    
+               strcpy(bc->rad, id);
+               bc->rnumplan=type; 
+//             cb_log(3, bc->stack->port, " --> Redirecting number (REDIR_NR): '%s'\n", id);
+       }
+       {
+               int  coding, capability, mode, rate, multi, user, async, urate, stopbits, dbits, parity;
+               dec_ie_bearer(setup->BEARER, (Q931_info_t *)setup, &coding, &capability, &mode, &rate, &multi, &user, &async, &urate, &stopbits, &dbits, &parity, nt,bc);
+               switch (capability) {
+               case -1: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; 
+//                     cb_log(2, bc->stack->port, " --> cap -1 -> digital\n");
+                       break;
+               case 0: bc->capability=INFO_CAPABILITY_SPEECH;
+//                     cb_log(2, bc->stack->port, " --> cap speech\n");
+                       break;
+               case 8: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
+                       bc->user1 = user;
+                       bc->async = async;
+                       bc->urate = urate;
+                       
+                       bc->rate = rate;
+                       bc->mode = mode;
+                       
+//                     cb_log(2, bc->stack->port, " --> cap unres Digital (user l1 %d, async %d, user rate %d\n", user, async, urate);
+                       break;
+               case 9: bc->capability=INFO_CAPABILITY_DIGITAL_RESTRICTED;
+//                     cb_log(2, bc->stack->port, " --> cap res Digital\n");
+                       break;
+               default:
+//                     cb_log(2, bc->stack->port, " --> cap Else\n");
+                       break;
+               }
+
+               switch(user) {
+               case 2:
+                       bc->law=INFO_CODEC_ULAW;
+                       break;
+               case 3:
+                       bc->law=INFO_CODEC_ALAW;
+                       break;
+               default:
+                       bc->law=INFO_CODEC_ALAW;
+                       
+               }
+               
+               bc->capability=capability; 
+       }
+       {
+               int  exclusive, channel;
+               dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)setup, &exclusive, &channel, nt,bc);
+               if (channel==0xff) /* any channel */
+                       channel=-1;
+
+               if (channel > 0) 
+                       bc->channel = channel;
+       }
+       
+       dec_ie_progress(setup->PROGRESS, (Q931_info_t *)setup, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
+       
+}
+
+#define ANY_CHANNEL 0xff /* IE attribut for 'any channel' */
+msg_t *build_setup (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       SETUP_t *setup;
+       msg_t *msg =(msg_t*)create_l3msg(CC_SETUP | REQUEST, MT_SETUP,  bc?bc->l3_id:-1, sizeof(SETUP_t) ,nt); 
+  
+       setup=(SETUP_t*)((msg->data+HEADER_LEN)); 
+  
+//     cb_log(2, bc->stack->port, " --> oad %s dad %s channel %d\n",bc->oad, bc->dad,bc->channel);
+       if (bc->channel == 0 || bc->channel == ANY_CHANNEL || bc->channel==-1)
+               enc_ie_channel_id(&setup->CHANNEL_ID, msg, 0, bc->channel, nt,bc);
+       else
+               enc_ie_channel_id(&setup->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
+  
+       {
+               int type=bc->onumplan,plan=1,present=bc->pres,screen=0;
+               enc_ie_calling_pn(&setup->CALLING_PN, msg, type, plan, present,
+                                 screen, bc->oad, nt, bc);
+       }
+  
+       {
+               if (bc->dad[0])
+                       enc_ie_called_pn(&setup->CALLED_PN, msg, bc->dnumplan, 1, bc->dad, nt,bc);
+       }
+  
+       if (*bc->display) {
+               enc_ie_display(&setup->DISPLAY, msg, bc->display, nt,bc);
+       }
+  
+       {
+               int coding=0, capability, mode=0 /*  2 for packet ! */
+                       ,user, rate=0x10;
+               switch (bc->capability) {
+               case INFO_CAPABILITY_SPEECH: capability = 0;
+//                     cb_log(2, bc->stack->port, " --> Speech\n");
+                       break;
+               case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: capability = 8;
+//                     cb_log(2, bc->stack->port, " --> cap unres Digital\n");
+                       break;
+               case INFO_CAPABILITY_DIGITAL_RESTRICTED: capability = 9;
+//                     cb_log(2, bc->stack->port, " --> cap res Digital\n");
+                       break;
+               default:
+//                     cb_log(2, bc->stack->port, " --> cap Speech\n");
+                       capability=bc->capability; 
+               }
+               
+               switch (bc->law) {
+               case INFO_CODEC_ULAW: user=2;
+//                     cb_log(2, bc->stack->port, " --> Codec Ulaw\n");
+                       break;
+               case INFO_CODEC_ALAW: user=3;
+//                     cb_log(2, bc->stack->port, " --> Codec Alaw\n");
+                       break;
+               default:
+                       user=3;
+               }
+    
+               enc_ie_bearer(&setup->BEARER, msg, coding, capability, mode, rate, -1, user, nt,bc);
+       }
+  
+#if DEBUG 
+       printf("Building SETUP Msg\n"); 
+#endif
+       return msg; 
+}
+
+void print_setup (struct isdn_msg msgs[]) 
+{
+}
+
+void parse_connect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       CONNECT_t *connect=(CONNECT_t*)((unsigned long)(msg->data+HEADER_LEN));
+  
+       bc->ces = connect->ces;
+       bc->ces = connect->ces;
+
+       dec_ie_progress(connect->PROGRESS, (Q931_info_t *)connect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
+       
+#if DEBUG 
+       printf("Parsing CONNECT Msg\n"); 
+#endif
+}
+msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       CONNECT_t *connect;
+       msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_t) ,nt); 
+  
+       connect=(CONNECT_t*)((msg->data+HEADER_LEN)); 
+
+       if (nt) {
+               time_t now;
+               time(&now);
+               enc_ie_date(&connect->DATE, msg, now, nt,bc);
+       }
+  
+       {
+               int type=0, plan=1, present=2, screen=0;
+               enc_ie_connected_pn(&connect->CONNECT_PN, msg, type,plan, present, screen, (unsigned char*) bc->dad , nt , bc);
+       }
+
+#if DEBUG 
+       printf("Building CONNECT Msg\n"); 
+#endif
+       return msg; 
+}
+void print_connect (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       SETUP_ACKNOWLEDGE_t *setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((unsigned long)(msg->data+HEADER_LEN));
+
+       {
+               int  exclusive, channel;
+               dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)setup_acknowledge, &exclusive, &channel, nt,bc);
+
+               if (channel==0xff) /* any channel */
+                       channel=-1;
+
+               if (channel > 0) 
+                       bc->channel = channel;
+       }
+       
+       dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)setup_acknowledge, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
+#if DEBUG 
+       printf("Parsing SETUP_ACKNOWLEDGE Msg\n"); 
+#endif
+
+}
+msg_t *build_setup_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       SETUP_ACKNOWLEDGE_t *setup_acknowledge;
+       msg_t *msg =(msg_t*)create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SETUP_ACKNOWLEDGE_t) ,nt); 
+       setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
+  
+       enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
+  
+       if (nt) 
+               enc_ie_progress(&setup_acknowledge->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
+  
+#if DEBUG 
+       printf("Building SETUP_ACKNOWLEDGE Msg\n"); 
+#endif
+       return msg; 
+}
+
+void print_setup_acknowledge (struct isdn_msg msgs[]) 
+{
+}
+
+void parse_connect_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing CONNECT_ACKNOWLEDGE Msg\n"); 
+#endif
+
+}
+msg_t *build_connect_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       CONNECT_ACKNOWLEDGE_t *connect_acknowledge;
+       msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_ACKNOWLEDGE_t) ,nt); 
+       connect_acknowledge=(CONNECT_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
+  
+       enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
+  
+#if DEBUG 
+       printf("Building CONNECT_ACKNOWLEDGE Msg\n"); 
+#endif
+       return msg; 
+}
+void print_connect_acknowledge (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_user_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing USER_INFORMATION Msg\n"); 
+#endif
+
+}
+msg_t *build_user_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       USER_INFORMATION_t *user_information;
+       msg_t *msg =(msg_t*)create_l3msg(CC_USER_INFORMATION | REQUEST, MT_USER_INFORMATION,  bc?bc->l3_id:-1, sizeof(USER_INFORMATION_t) ,nt); 
+       user_information=(USER_INFORMATION_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building USER_INFORMATION Msg\n"); 
+#endif
+       return msg; 
+}
+void print_user_information (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing SUSPEND_REJECT Msg\n"); 
+#endif
+
+}
+msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       SUSPEND_REJECT_t *suspend_reject;
+       msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT,  bc?bc->l3_id:-1, sizeof(SUSPEND_REJECT_t) ,nt); 
+       suspend_reject=(SUSPEND_REJECT_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building SUSPEND_REJECT Msg\n"); 
+#endif
+       return msg; 
+}
+void print_suspend_reject (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing RESUME_REJECT Msg\n"); 
+#endif
+
+}
+msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RESUME_REJECT_t *resume_reject;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT,  bc?bc->l3_id:-1, sizeof(RESUME_REJECT_t) ,nt); 
+       resume_reject=(RESUME_REJECT_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building RESUME_REJECT Msg\n"); 
+#endif
+       return msg; 
+}
+void print_resume_reject (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing HOLD Msg\n"); 
+#endif
+
+}
+msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       HOLD_t *hold;
+       msg_t *msg =(msg_t*)create_l3msg(CC_HOLD | REQUEST, MT_HOLD,  bc?bc->l3_id:-1, sizeof(HOLD_t) ,nt); 
+       hold=(HOLD_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building HOLD Msg\n"); 
+#endif
+       return msg; 
+}
+void print_hold (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing SUSPEND Msg\n"); 
+#endif
+
+}
+msg_t *build_suspend (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       SUSPEND_t *suspend;
+       msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND | REQUEST, MT_SUSPEND,  bc?bc->l3_id:-1, sizeof(SUSPEND_t) ,nt); 
+       suspend=(SUSPEND_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building SUSPEND Msg\n"); 
+#endif
+       return msg; 
+}
+void print_suspend (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing RESUME Msg\n"); 
+#endif
+
+}
+msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RESUME_t *resume;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RESUME | REQUEST, MT_RESUME,  bc?bc->l3_id:-1, sizeof(RESUME_t) ,nt); 
+       resume=(RESUME_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building RESUME Msg\n"); 
+#endif
+       return msg; 
+}
+void print_resume (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing HOLD_ACKNOWLEDGE Msg\n"); 
+#endif
+
+}
+msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       HOLD_ACKNOWLEDGE_t *hold_acknowledge;
+       msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(HOLD_ACKNOWLEDGE_t) ,nt); 
+       hold_acknowledge=(HOLD_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building HOLD_ACKNOWLEDGE Msg\n"); 
+#endif
+       return msg; 
+}
+void print_hold_acknowledge (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing SUSPEND_ACKNOWLEDGE Msg\n"); 
+#endif
+
+}
+msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
+       msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SUSPEND_ACKNOWLEDGE_t) ,nt); 
+       suspend_acknowledge=(SUSPEND_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building SUSPEND_ACKNOWLEDGE Msg\n"); 
+#endif
+       return msg; 
+}
+void print_suspend_acknowledge (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing RESUME_ACKNOWLEDGE Msg\n"); 
+#endif
+
+}
+msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RESUME_ACKNOWLEDGE_t *resume_acknowledge;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RESUME_ACKNOWLEDGE_t) ,nt); 
+       resume_acknowledge=(RESUME_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building RESUME_ACKNOWLEDGE Msg\n"); 
+#endif
+       return msg; 
+}
+void print_resume_acknowledge (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing HOLD_REJECT Msg\n"); 
+#endif
+
+}
+msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       HOLD_REJECT_t *hold_reject;
+       msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT,  bc?bc->l3_id:-1, sizeof(HOLD_REJECT_t) ,nt); 
+       hold_reject=(HOLD_REJECT_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building HOLD_REJECT Msg\n"); 
+#endif
+       return msg; 
+}
+void print_hold_reject (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing RETRIEVE Msg\n"); 
+#endif
+
+}
+msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RETRIEVE_t *retrieve;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE | REQUEST, MT_RETRIEVE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_t) ,nt); 
+       retrieve=(RETRIEVE_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building RETRIEVE Msg\n"); 
+#endif
+       return msg; 
+}
+void print_retrieve (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing RETRIEVE_ACKNOWLEDGE Msg\n"); 
+#endif
+
+}
+msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_ACKNOWLEDGE_t) ,nt); 
+       retrieve_acknowledge=(RETRIEVE_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
+
+       enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
+#if DEBUG 
+       printf("Building RETRIEVE_ACKNOWLEDGE Msg\n"); 
+#endif
+       return msg; 
+}
+void print_retrieve_acknowledge (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing RETRIEVE_REJECT Msg\n"); 
+#endif
+
+}
+msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RETRIEVE_REJECT_t *retrieve_reject;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT,  bc?bc->l3_id:-1, sizeof(RETRIEVE_REJECT_t) ,nt); 
+       retrieve_reject=(RETRIEVE_REJECT_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building RETRIEVE_REJECT Msg\n"); 
+#endif
+       return msg; 
+}
+void print_retrieve_reject (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_disconnect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       DISCONNECT_t *disconnect=(DISCONNECT_t*)((unsigned long)(msg->data+HEADER_LEN));
+       int location;
+  
+       dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &bc->cause, nt,bc);
+
+       dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)disconnect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
+#if DEBUG 
+       printf("Parsing DISCONNECT Msg\n"); 
+#endif
+
+}
+msg_t *build_disconnect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       DISCONNECT_t *disconnect;
+       msg_t *msg =(msg_t*)create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT,  bc?bc->l3_id:-1, sizeof(DISCONNECT_t) ,nt); 
+       
+       disconnect=(DISCONNECT_t*)((msg->data+HEADER_LEN)); 
+       
+       enc_ie_cause(&disconnect->CAUSE, msg, (nt)?1:0, bc->out_cause,nt,bc);
+       if (nt) enc_ie_progress(&disconnect->PROGRESS, msg, 0, nt?1:5, 8 ,nt,bc);
+  
+#if DEBUG 
+       printf("Building DISCONNECT Msg\n"); 
+#endif
+       return msg; 
+}
+void print_disconnect (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RESTART_t *restart=(RESTART_t*)((unsigned long)(msg->data+HEADER_LEN));
+  
+#if DEBUG 
+       printf("Parsing RESTART Msg\n");
+#endif
+  
+       {
+               int  exclusive, channel;
+               dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &channel, nt,bc);
+               if (channel==0xff) /* any channel */
+                       channel=-1;
+               cb_log(0, bc->stack->port, "CC_RESTART Request on channel:%d on port:%d\n",bc->stack->port);
+       }
+  
+}
+msg_t *build_restart (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RESTART_t *restart;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RESTART | REQUEST, MT_RESTART,  bc?bc->l3_id:-1, sizeof(RESTART_t) ,nt); 
+       restart=(RESTART_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building RESTART Msg\n"); 
+#endif
+       return msg; 
+}
+void print_restart (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_release (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RELEASE_t *release=(RELEASE_t*)((unsigned long)(msg->data+HEADER_LEN));
+       int location;
+  
+       dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &bc->cause, nt,bc);
+#if DEBUG 
+       printf("Parsing RELEASE Msg\n"); 
+#endif
+
+}
+msg_t *build_release (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RELEASE_t *release;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE,  bc?bc->l3_id:-1, sizeof(RELEASE_t) ,nt); 
+       release=(RELEASE_t*)((msg->data+HEADER_LEN)); 
+  
+  
+       enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
+  
+#if DEBUG 
+       printf("Building RELEASE Msg\n"); 
+#endif
+       return msg; 
+}
+void print_release (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RELEASE_COMPLETE_t *release_complete=(RELEASE_COMPLETE_t*)((unsigned long)(msg->data+HEADER_LEN));
+       int location;
+       iframe_t *frm = (iframe_t*) msg->data;
+#ifdef MISDNUSER_JOLLY
+       mISDNuser_head_t *hh;
+       hh=(mISDNuser_head_t*)msg->data;
+#else
+       mISDN_head_t *hh;
+       hh=(mISDN_head_t*)msg->data;
+#endif
+  
+       if (nt) {
+               if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
+                       cb_log(0, bc->stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] port:%d\n",bc->stack->port);
+                       return;
+               }
+       } else {
+               if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
+                       cb_log(0, bc->stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] port:%d\n",bc->stack->port);
+                       return;
+               }
+       }
+       dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &bc->cause, nt,bc);
+
+  
+#if DEBUG 
+       printf("Parsing RELEASE_COMPLETE Msg\n"); 
+#endif
+
+}
+msg_t *build_release_complete (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       RELEASE_COMPLETE_t *release_complete;
+       msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE,  bc?bc->l3_id:-1, sizeof(RELEASE_COMPLETE_t) ,nt); 
+       release_complete=(RELEASE_COMPLETE_t*)((msg->data+HEADER_LEN)); 
+       
+       enc_ie_cause(&release_complete->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
+  
+#if DEBUG 
+       printf("Building RELEASE_COMPLETE Msg\n"); 
+#endif
+       return msg; 
+}
+void print_release_complete (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_facility (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       FACILITY_t *facility=(FACILITY_t*)((unsigned long)(msg->data+HEADER_LEN)); 
+       Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN);  
+
+
+#if DEBUG 
+       printf("Parsing FACILITY Msg\n"); 
+#endif
+
+       {
+               char fac[128];
+               int facility_len;
+               
+               dec_ie_facility(facility->FACILITY, qi, fac, &facility_len,  nt, bc);
+       }
+               
+}
+msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       FACILITY_t *facility;
+       msg_t *msg =(msg_t*)create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY,  bc?bc->l3_id:-1, sizeof(FACILITY_t) ,nt); 
+       facility=(FACILITY_t*)((msg->data+HEADER_LEN)); 
+
+       {
+               if (*bc->display) {
+                       printf("Sending %s as Display\n", bc->display);
+                       enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc);
+               }
+
+               
+               
+               switch ( bc->facility ) {
+               case FACILITY_CALLDEFLECT:
+                       enc_facility_calldeflect(&facility->FACILITY, msg, bc->facility_calldeflect_nr, nt, bc);
+                       
+                       break;
+               case FACILITY_NONE:
+                       break;
+               }
+               
+       }
+       
+#if DEBUG 
+       printf("Building FACILITY Msg\n"); 
+#endif
+       return msg; 
+}
+void print_facility (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing NOTIFY Msg\n"); 
+#endif
+
+}
+msg_t *build_notify (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       NOTIFY_t *notify;
+       msg_t *msg =(msg_t*)create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY,  bc?bc->l3_id:-1, sizeof(NOTIFY_t) ,nt); 
+       notify=(NOTIFY_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building NOTIFY Msg\n"); 
+#endif
+       return msg; 
+}
+void print_notify (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_status_enquiry (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing STATUS_ENQUIRY Msg\n"); 
+#endif
+
+}
+msg_t *build_status_enquiry (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       STATUS_ENQUIRY_t *status_enquiry;
+       msg_t *msg =(msg_t*)create_l3msg(CC_STATUS_ENQUIRY | REQUEST, MT_STATUS_ENQUIRY,  bc?bc->l3_id:-1, sizeof(STATUS_ENQUIRY_t) ,nt); 
+       status_enquiry=(STATUS_ENQUIRY_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building STATUS_ENQUIRY Msg\n"); 
+#endif
+       return msg; 
+}
+void print_status_enquiry (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       INFORMATION_t *information=(INFORMATION_t*)((unsigned long)(msg->data+HEADER_LEN));
+
+       {
+               int  type, plan;
+               char number[32];
+               char keypad[32];
+               dec_ie_called_pn(information->CALLED_PN, (Q931_info_t *)information, &type, &plan, (unsigned char *)number, sizeof(number), nt,bc);
+               dec_ie_keypad(information->KEYPAD, (Q931_info_t *)information, (unsigned char *)keypad, sizeof(keypad), nt,bc);
+               strcpy(bc->info_dad, number);
+               strcpy(bc->keypad,keypad);
+    
+       }
+#if DEBUG 
+       printf("Parsing INFORMATION Msg\n"); 
+#endif
+
+}
+msg_t *build_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       INFORMATION_t *information;
+       msg_t *msg =(msg_t*)create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION,  bc?bc->l3_id:-1, sizeof(INFORMATION_t) ,nt); 
+       information=(INFORMATION_t*)((msg->data+HEADER_LEN)); 
+  
+       {
+               enc_ie_called_pn(&information->CALLED_PN, msg, 0, 1, bc->info_dad, nt,bc);
+       }
+
+       {
+               if (*bc->display) {
+                       printf("Sending %s as Display\n", bc->display);
+                       enc_ie_display(&information->DISPLAY, msg, bc->display, nt,bc);
+               }
+       }
+  
+#if DEBUG 
+       printf("Building INFORMATION Msg\n"); 
+#endif
+       return msg; 
+}
+void print_information (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_status (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       STATUS_t *status=(STATUS_t*)((unsigned long)(msg->data+HEADER_LEN));
+       int location;
+  
+       dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &bc->cause, nt,bc);
+       ;
+
+#if DEBUG 
+       printf("Parsing STATUS Msg\n"); 
+#endif
+
+}
+msg_t *build_status (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       STATUS_t *status;
+       msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt); 
+       status=(STATUS_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building STATUS Msg\n"); 
+#endif
+       return msg; 
+}
+void print_status (struct isdn_msg msgs[]) 
+{
+}
+
+
+void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
+{
+#if DEBUG 
+       printf("Parsing STATUS Msg\n"); 
+#endif
+
+}
+msg_t *build_timeout (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
+{
+       int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
+       STATUS_t *status;
+       msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt); 
+       status=(STATUS_t*)((msg->data+HEADER_LEN)); 
+
+#if DEBUG 
+       printf("Building STATUS Msg\n"); 
+#endif
+       return msg; 
+}
+void print_timeout (struct isdn_msg msgs[]) 
+{
+}
+
+
+/************************************/
+
+
+
+
+/** Msg Array **/
+
+struct isdn_msg msgs_g[] = {
+       {CC_PROCEEDING,L3,EVENT_PROCEEDING,
+        parse_proceeding,build_proceeding,print_proceeding,
+        "PROCEEDING"},
+       {CC_ALERTING,L3,EVENT_ALERTING,
+        parse_alerting,build_alerting,print_alerting,
+        "ALERTING"},
+       {CC_PROGRESS,L3,EVENT_PROGRESS,
+        parse_progress,build_progress,print_progress,
+        "PROGRESS"},
+       {CC_SETUP,L3,EVENT_SETUP,
+        parse_setup,build_setup,print_setup,
+        "SETUP"},
+       {CC_CONNECT,L3,EVENT_CONNECT,
+        parse_connect,build_connect,print_connect,
+        "CONNECT"},
+       {CC_SETUP_ACKNOWLEDGE,L3,EVENT_SETUP_ACKNOWLEDGE,
+        parse_setup_acknowledge,build_setup_acknowledge,print_setup_acknowledge,
+        "SETUP_ACKNOWLEDGE"},
+       {CC_CONNECT_ACKNOWLEDGE ,L3,EVENT_CONNECT_ACKNOWLEDGE ,
+        parse_connect_acknowledge ,build_connect_acknowledge ,print_connect_acknowledge ,
+        "CONNECT_ACKNOWLEDGE "},
+       {CC_USER_INFORMATION,L3,EVENT_USER_INFORMATION,
+        parse_user_information,build_user_information,print_user_information,
+        "USER_INFORMATION"},
+       {CC_SUSPEND_REJECT,L3,EVENT_SUSPEND_REJECT,
+        parse_suspend_reject,build_suspend_reject,print_suspend_reject,
+        "SUSPEND_REJECT"},
+       {CC_RESUME_REJECT,L3,EVENT_RESUME_REJECT,
+        parse_resume_reject,build_resume_reject,print_resume_reject,
+        "RESUME_REJECT"},
+       {CC_HOLD,L3,EVENT_HOLD,
+        parse_hold,build_hold,print_hold,
+        "HOLD"},
+       {CC_SUSPEND,L3,EVENT_SUSPEND,
+        parse_suspend,build_suspend,print_suspend,
+        "SUSPEND"},
+       {CC_RESUME,L3,EVENT_RESUME,
+        parse_resume,build_resume,print_resume,
+        "RESUME"},
+       {CC_HOLD_ACKNOWLEDGE,L3,EVENT_HOLD_ACKNOWLEDGE,
+        parse_hold_acknowledge,build_hold_acknowledge,print_hold_acknowledge,
+        "HOLD_ACKNOWLEDGE"},
+       {CC_SUSPEND_ACKNOWLEDGE,L3,EVENT_SUSPEND_ACKNOWLEDGE,
+        parse_suspend_acknowledge,build_suspend_acknowledge,print_suspend_acknowledge,
+        "SUSPEND_ACKNOWLEDGE"},
+       {CC_RESUME_ACKNOWLEDGE,L3,EVENT_RESUME_ACKNOWLEDGE,
+        parse_resume_acknowledge,build_resume_acknowledge,print_resume_acknowledge,
+        "RESUME_ACKNOWLEDGE"},
+       {CC_HOLD_REJECT,L3,EVENT_HOLD_REJECT,
+        parse_hold_reject,build_hold_reject,print_hold_reject,
+        "HOLD_REJECT"},
+       {CC_RETRIEVE,L3,EVENT_RETRIEVE,
+        parse_retrieve,build_retrieve,print_retrieve,
+        "RETRIEVE"},
+       {CC_RETRIEVE_ACKNOWLEDGE,L3,EVENT_RETRIEVE_ACKNOWLEDGE,
+        parse_retrieve_acknowledge,build_retrieve_acknowledge,print_retrieve_acknowledge,
+        "RETRIEVE_ACKNOWLEDGE"},
+       {CC_RETRIEVE_REJECT,L3,EVENT_RETRIEVE_REJECT,
+        parse_retrieve_reject,build_retrieve_reject,print_retrieve_reject,
+        "RETRIEVE_REJECT"},
+       {CC_DISCONNECT,L3,EVENT_DISCONNECT,
+        parse_disconnect,build_disconnect,print_disconnect,
+        "DISCONNECT"},
+       {CC_RESTART,L3,EVENT_RESTART,
+        parse_restart,build_restart,print_restart,
+        "RESTART"},
+       {CC_RELEASE,L3,EVENT_RELEASE,
+        parse_release,build_release,print_release,
+        "RELEASE"},
+       {CC_RELEASE_COMPLETE,L3,EVENT_RELEASE_COMPLETE,
+        parse_release_complete,build_release_complete,print_release_complete,
+        "RELEASE_COMPLETE"},
+       {CC_FACILITY,L3,EVENT_FACILITY,
+        parse_facility,build_facility,print_facility,
+        "FACILITY"},
+       {CC_NOTIFY,L3,EVENT_NOTIFY,
+        parse_notify,build_notify,print_notify,
+        "NOTIFY"},
+       {CC_STATUS_ENQUIRY,L3,EVENT_STATUS_ENQUIRY,
+        parse_status_enquiry,build_status_enquiry,print_status_enquiry,
+        "STATUS_ENQUIRY"},
+       {CC_INFORMATION,L3,EVENT_INFORMATION,
+        parse_information,build_information,print_information,
+        "INFORMATION"},
+       {CC_STATUS,L3,EVENT_STATUS,
+        parse_status,build_status,print_status,
+        "STATUS"},
+       {CC_TIMEOUT,L3,EVENT_TIMEOUT,
+        parse_timeout,build_timeout,print_timeout,
+        "TIMEOUT"},
+       {0,0,0,NULL,NULL,NULL,NULL}
+};
+
+#define msgs_max (sizeof(msgs_g)/sizeof(struct isdn_msg))
+
+/** INTERFACE FCTS ***/
+int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *msg, int nt)
+{
+       int i;
+
+       if (nt){
+#ifdef MISDNUSER_JOLLY
+               mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data;
+#else
+               mISDN_head_t *hh = (mISDN_head_t*)msg->data;
+#endif
+    
+               for (i=0; i< msgs_max -1; i++) 
+                       if ( (hh->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
+    
+       } else {
+               iframe_t *frm = (iframe_t*)msg->data;
+    
+               for (i=0; i< msgs_max -1; i++) 
+                       if ( (frm->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
+       }
+
+       return -1;
+}
+
+int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt)
+{
+       int i;
+       for (i=0; i< msgs_max; i++) 
+               if ( event == msgs[i].event) return i;
+
+       cb_log(4,0, "get_index: EVENT NOT FOUND!!\n");
+       
+       return -1;
+}
+
+enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *msg, int nt)
+{
+       int i=isdn_msg_get_index(msgs, msg, nt);
+       if(i>=0) return msgs[i].event;
+       return EVENT_UNKNOWN;
+}
+
+char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt)
+{
+       int i=isdn_msg_get_index(msgs, msg, nt);
+       if(i>=0) return msgs[i].info;
+       return NULL;
+}
+
+
+char EVENT_CLEAN_INFO[] = "CLEAN_UP";
+char EVENT_DTMF_TONE_INFO[] = "DTMF_TONE";
+char EVENT_NEW_L3ID_INFO[] = "NEW_L3ID";
+char EVENT_NEW_BC_INFO[] = "NEW_BC";
+char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA";
+
+char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
+{
+       int i=isdn_msg_get_index_by_event(msgs, event, nt);
+       
+       if(i>=0) return msgs[i].info;
+       
+       if (event == EVENT_CLEANUP) return EVENT_CLEAN_INFO;
+       if (event == EVENT_DTMF_TONE) return EVENT_DTMF_TONE_INFO;
+       if (event == EVENT_NEW_L3ID) return EVENT_NEW_L3ID_INFO;
+       if (event == EVENT_NEW_BC) return EVENT_NEW_BC_INFO;
+       if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO;
+       
+       return NULL;
+}
+
+int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
+{
+       int i=isdn_msg_get_index(msgs, msg, nt);
+       if(i<0) return -1;
+
+       msgs[i].msg_parser(msgs, msg, bc, nt);
+       return 0;
+}
+
+msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt)
+{
+       int i=isdn_msg_get_index_by_event(msgs, event, nt);
+       if(i<0) return NULL;
+  
+       return  msgs[i].msg_builder(msgs, bc, nt);
+}
+
diff --git a/channels/misdn/portinfo.c b/channels/misdn/portinfo.c
new file mode 100755 (executable)
index 0000000..c7add95
--- /dev/null
@@ -0,0 +1,197 @@
+
+
+#include "isdn_lib.h"
+
+
+/*
+ * global function to show all available isdn ports
+ */
+void isdn_port_info(void)
+{
+       int err;
+       int i, ii, p;
+       int useable, nt, pri;
+       unsigned char buff[1025];
+       iframe_t *frm = (iframe_t *)buff;
+       stack_info_t *stinf;
+       int device;
+
+       /* open mISDN */
+       if ((device = mISDN_open()) < 0)
+       {
+               fprintf(stderr, "mISDN_open() failed: ret=%d errno=%d (%s) Check for mISDN modules and device.\n", device, errno, strerror(errno));
+               exit(-1);
+       }
+
+       /* get number of stacks */
+       i = 1;
+       ii = mISDN_get_stack_count(device);
+       printf("\n");
+       if (ii <= 0)
+       {
+               printf("Found no card. Please be sure to load card drivers.\n");
+       }
+
+       /* loop the number of cards and get their info */
+       while(i <= ii)
+       {
+               err = mISDN_get_stack_info(device, i, buff, sizeof(buff));
+               if (err <= 0)
+               {
+                       fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err);
+                       break;
+               }
+               stinf = (stack_info_t *)&frm->data.p;
+
+               nt = pri = 0;
+               useable = 1;
+
+               /* output the port info */
+               printf("Port %2d: ", i);
+               switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
+               {
+                       case ISDN_PID_L0_TE_S0:
+                       printf("TE-mode BRI S/T interface line (for phone lines)");
+#if 0
+                       if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK)
+                               printf(" HFC multiport card");
+#endif
+                       break;
+                       case ISDN_PID_L0_NT_S0:
+                       nt = 1;
+                       printf("NT-mode BRI S/T interface port (for phones)");
+#if 0
+                       if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK)
+                               printf(" HFC multiport card");
+#endif
+                       break;
+                       case ISDN_PID_L0_TE_U:
+                       printf("TE-mode BRI U   interface line");
+                       break;
+                       case ISDN_PID_L0_NT_U:
+                       nt = 1;
+                       printf("NT-mode BRI U   interface port");
+                       break;
+                       case ISDN_PID_L0_TE_UP2:
+                       printf("TE-mode BRI Up2 interface line");
+                       break;
+                       case ISDN_PID_L0_NT_UP2:
+                       nt = 1;
+                       printf("NT-mode BRI Up2 interface port");
+                       break;
+                       case ISDN_PID_L0_TE_E1:
+                       pri = 1;
+                       printf("TE-mode PRI E1  interface line (for phone lines)");
+#if 0
+                       if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK)
+                               printf(" HFC-E1 card");
+#endif
+                       break;
+                       case ISDN_PID_L0_NT_E1:
+                       nt = 1;
+                       pri = 1;
+                       printf("NT-mode PRI E1  interface port (for phones)");
+#if 0
+                       if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK)
+                               printf(" HFC-E1 card");
+#endif
+                       break;
+                       default:
+                       useable = 0;
+                       printf("unknown type 0x%08x",stinf->pid.protocol[0]);
+               }
+               printf("\n");
+
+               if (nt)
+               {
+                       if (stinf->pid.protocol[1] == 0)
+                       {
+                               useable = 0;
+                               printf(" -> Missing layer 1 NT-mode protocol.\n");
+                       }
+                       p = 2;
+                       while(p <= MAX_LAYER_NR) {
+                               if (stinf->pid.protocol[p])
+                               {
+                                       useable = 0;
+                                       printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for NT lib.\n", p, stinf->pid.protocol[p]);
+                               }
+                               p++;
+                       }
+                       if (useable)
+                       {
+                               if (pri)
+                                       printf(" -> Interface is Point-To-Point (PRI).\n");
+                               else
+                                       printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
+                       }
+               } else
+               {
+                       if (stinf->pid.protocol[1] == 0)
+                       {
+                               useable = 0;
+                               printf(" -> Missing layer 1 protocol.\n");
+                       }
+                       if (stinf->pid.protocol[2] == 0)
+                       {
+                               useable = 0;
+                               printf(" -> Missing layer 2 protocol.\n");
+                       }
+                       if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP)
+                       {
+                               printf(" -> Interface is Poin-To-Point.\n");
+                       }
+                       if (stinf->pid.protocol[3] == 0)
+                       {
+                               useable = 0;
+                               printf(" -> Missing layer 3 protocol.\n");
+                       } else
+                       {
+                               printf(" -> Protocol: ");
+                               switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
+                               {
+                                       case ISDN_PID_L3_DSS1USER:
+                                       printf("DSS1 (Euro ISDN)");
+                                       break;
+
+                                       default:
+                                       useable = 0;
+                                       printf("unknown protocol 0x%08x",stinf->pid.protocol[3]);
+                               }
+                               printf("\n");
+                       }
+                       p = 4;
+                       while(p <= MAX_LAYER_NR) {
+                               if (stinf->pid.protocol[p])
+                               {
+                                       useable = 0;
+                                       printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for TE lib.\n", p, stinf->pid.protocol[p]);
+                               }
+                               p++;
+                       }
+                       printf(" -> childcnt: %d\n",stinf->childcnt);
+               }
+
+               if (!useable)
+                       printf(" * Port NOT useable for PBX\n");
+
+               printf("--------\n");
+
+               i++;
+       }
+       printf("\n");
+
+       /* close mISDN */
+       if ((err = mISDN_close(device)))
+       {
+               fprintf(stderr, "mISDN_close() failed: err=%d '%s'\n", err, strerror(err));
+               exit(-1);
+       }
+}
+
+
+int main()
+{
+  isdn_port_info();
+  return 0;
+}