diff options
author | kpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-03-26 17:00:22 +0000 |
---|---|---|
committer | kpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-03-26 17:00:22 +0000 |
commit | 9774f6cc479ebeb3142c1dac84fa7cdc5399abf6 (patch) | |
tree | 931988c5027ba3c9d19d89628bd2ef07dec473be /1.2-netsec/channels/misdn | |
parent | 6dec48224155a3102f16d6be8f6752dde5d9f15a (diff) |
Creating tag for the release of asterisk-1.2.5-netsec
git-svn-id: http://svn.digium.com/svn/asterisk/tags/1.2.5-netsec@15029 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to '1.2-netsec/channels/misdn')
-rw-r--r-- | 1.2-netsec/channels/misdn/Makefile | 72 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/chan_misdn_config.h | 98 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/ie.c | 1621 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/isdn_lib.c | 3308 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/isdn_lib.h | 367 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/isdn_lib_intern.h | 99 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/isdn_msg_parser.c | 1356 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/mISDN.patch | 2500 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/mISDNuser.patch | 271 | ||||
-rw-r--r-- | 1.2-netsec/channels/misdn/portinfo.c | 197 |
10 files changed, 9889 insertions, 0 deletions
diff --git a/1.2-netsec/channels/misdn/Makefile b/1.2-netsec/channels/misdn/Makefile new file mode 100644 index 000000000..e0e18cf85 --- /dev/null +++ b/1.2-netsec/channels/misdn/Makefile @@ -0,0 +1,72 @@ +# +# Makefile +# +# Make file for chan_misdn support +# + +# Verify those options with main Makefile +CFLAGS += -pipe -c -DMISDNUSER_JOLLY +SOURCES = isdn_lib.c isdn_msg_parser.c +OBJDIR = . +OBJS = isdn_lib.o isdn_msg_parser.o + + +all: chan_misdn_lib.a + + +%.o: %.c + $(CC) $(CFLAGS) -o $@ $< + + +chan_misdn_lib.a: $(OBJS) + ar crv $@ $(OBJS) + +misdn: test_preempt + if [ ! -d lib ] ; then \ + mkdir lib; \ + cd lib ; \ + wget http://isdn.jolly.de/download/v3.1/mISDN_for_PBX4Linux-3.0.tar.gz ;\ + tar xzf mISDN_for_PBX4Linux-3.0.tar.gz; \ + wget http://isdn.jolly.de/download/v3.1/mISDNuser_for_PBX4Linux-3.0.tar.gz ;\ + tar xzf mISDNuser_for_PBX4Linux-3.0.tar.gz ;\ + cd mISDN; patch -p1 <../../mISDN.patch; \ + cd ../mISDNuser ; patch -p1 <../../mISDNuser.patch; \ + fi + cd lib/mISDN ; make install + cd lib/mISDNuser ; make install + +LINUX=/lib/modules/$(uname -r)/build +GCCVERSION=$(shell $(CC) --version | grep GCC | cut -d " " -f 3 | cut -d "." -f 1) + +test_preempt: + @if grep 'CONFIG_DEBUG_SPINLOCK=y' $(LINUX)/.config ; then \ + echo -e "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!\nDisable the DEBUG_SPINLOCK Setting in your Kernel Config.\n with this option set, mISDN will not work! \n\n" ;\ + read ; \ + exit 1 ; \ + fi + @if grep 'CONFIG_DEBUG_SPINLOCK_SLEEP=y' $(LINUX)/.config ; then \ + echo -e "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!\nDisable the DEBUG_SPINLOCK_SLEEP Setting in your Kernel Config.\n with this option set, mISDN will not work! \n\n" ;\ + read ; \ + exit 1 ; \ + fi + @if grep 'CONFIG_SMP=y' $(LINUX)/.config ; then \ + echo -e "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!\nDisable the SMP Setting in your Kernel Config.\n\n" ; \ + read ; \ + exit 1 ; \ + fi + @if test "$(GCCVERSION)" -gt 3 ; then \ + echo -e "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!\nYou're using GCC 4! Please downgrade to gcc-3.x and type:\nexport CC=gcc-3.x\nbefore issuing make again.\nyou won't have success with gcc-4!\n\n" ; \ + read ; \ + exit 1 ; \ + fi + + + +FORCE: + +clean: + rm -rf *.a *.o *.so + +misdn_clean: + rm -rf lib + diff --git a/1.2-netsec/channels/misdn/chan_misdn_config.h b/1.2-netsec/channels/misdn/chan_misdn_config.h new file mode 100644 index 000000000..250a74e54 --- /dev/null +++ b/1.2-netsec/channels/misdn/chan_misdn_config.h @@ -0,0 +1,98 @@ +/* + * 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_LOCALDIALPLAN, /* 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/1.2-netsec/channels/misdn/ie.c b/1.2-netsec/channels/misdn/ie.c new file mode 100644 index 000000000..4d1815f65 --- /dev/null +++ b/1.2-netsec/channels/misdn/ie.c @@ -0,0 +1,1621 @@ + +/* + * 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 <string.h> + + +#include "isdn_lib_intern.h" + +#include <mISDNuser/mISDNlib.h> +#include <mISDNuser/isdn_net.h> +#include <mISDNuser/l3dss1.h> +#include <mISDNuser/net_l3.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; + struct misdn_stack *stack=get_stack_by_bc(bc); + int pri = 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) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + int pri =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; + struct misdn_stack *stack=get_stack_by_bc(bc); + + *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, stack->port, " %02x", facility[i]); + i++; + } + cb_log(3, 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 *)(¢rex[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, ¢rex[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/1.2-netsec/channels/misdn/isdn_lib.c b/1.2-netsec/channels/misdn/isdn_lib.c new file mode 100644 index 000000000..aa1d51cea --- /dev/null +++ b/1.2-netsec/channels/misdn/isdn_lib.c @@ -0,0 +1,3308 @@ +/* + * 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_intern.h" + + +int misdn_ibuf_freecount(void *buf) +{ + return ibuf_freecount( (ibuffer_t*)buf); +} + +int misdn_ibuf_usedcount(void *buf) +{ + return ibuf_usedcount( (ibuffer_t*)buf); +} + +void misdn_ibuf_memcpy_r(char *to, void *buf, int len) +{ + ibuf_memcpy_r( to, (ibuffer_t*)buf, len); +} + +void misdn_ibuf_memcpy_w(void *buf, char *from, int len) +{ + ibuf_memcpy_w((ibuffer_t*)buf, from, len); +} + +struct misdn_stack* get_misdn_stack( void ); + + +int misdn_lib_is_ptp(int port) +{ + struct misdn_stack *stack=get_misdn_stack(); + for ( ; stack; stack=stack->next) { + if (stack->port == port) return stack->ptp; + } + return -1; +} + + +struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc) +{ + struct misdn_stack *stack=get_misdn_stack(); + + if (!bc) return NULL; + + for ( ; stack; stack=stack->next) { + int i; + for (i=0; i <stack->b_num; i++) { + if ( bc->port == stack->port) return stack; + } + } + + return NULL; +} + + +void get_show_stack_details(int port, char *buf) +{ + struct misdn_stack *stack=get_misdn_stack(); + + for ( ; stack; stack=stack->next) { + if (stack->port == port) break; + } + + if (stack) { + sprintf(buf, "* Stack Addr: Port %d Type %s Prot. %s L2Link %s L1Link:%s", stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN"); + } else { + buf[0]=0; + } + +} + + +static int nt_err_cnt =0 ; + +enum global_states { + MISDN_INITIALIZING, + MISDN_INITIALIZED +} ; + +static enum global_states global_state=MISDN_INITIALIZING; + + +#include <mISDNuser/net_l2.h> +#include <mISDNuser/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; + struct misdn_stack *stack=get_stack_by_bc(bc); + + frm->addr = (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) && + (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1; + return 0; +} + +int misdn_inband_avail(struct misdn_bchannel *bc) +{ + + /*if ! early_bconnect we have never inband available*/ + if ( ! bc->early_bconnect ) return 0; + + switch (bc->progress_indicator) { + case INFO_PI_INBAND_AVAILABLE: + case INFO_PI_CALL_NOT_E2E_ISDN: + case INFO_PI_CALLED_NOT_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->state=STATE_NOTHING; + + bc->channel = 0; + bc->in_use = 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->early_bconnect = 1; + + bc->ec_enable = 0; + bc->ec_deftaps = 128; + bc->ec_whenbridged = 0; + bc->ec_training = 1; + + + 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; + + bc->te_choose_channel = 0; +} + + +int clean_up_bc(struct misdn_bchannel *bc) +{ + int ret=0; + unsigned char buff[32]; + struct misdn_stack * stack; + + if (!bc ) return -1; + stack=get_stack_by_bc(bc); + if (!stack) return -1; + + if (!bc->upset) { + cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid); + return -1; + } + + cb_log(5, 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(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 + + +int misdn_lib_get_l1_up(struct misdn_stack *stack) +{ + /* 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; + + return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + +} + +int misdn_lib_get_l2_up(struct misdn_stack *stack) +{ + + if (stack->ptp && (stack->mode == NT_MODE) ) { + msg_t *dmsg; + /* L2 */ + dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0); + + if (stack->nst.manager_l3(&stack->nst, dmsg)) + free_msg(dmsg); + + } else { + iframe_t act; + + act.prim = DL_ESTABLISH | REQUEST; + + act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN; + act.dinfo = 0; + act.len = 0; + return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + } + + return 0; +} + + +int misdn_lib_get_l2_status(struct misdn_stack *stack) +{ + 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; + return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); +} + + +static int create_process (int midev, struct misdn_bchannel *bc) { + iframe_t ncr; + int l3_id; + int i; + struct misdn_stack *stack=get_stack_by_bc(bc); + 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 || bc->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; +} + + +void misdn_lib_setup_bc(struct misdn_bchannel *bc) +{ + setup_bc(bc); +} + + +int setup_bc(struct misdn_bchannel *bc) +{ + unsigned char buff[1025]; + + mISDN_pid_t pid; + int ret; + + struct misdn_stack *stack=get_stack_by_bc(bc); + + int midev=stack->midev; + int channel=bc->channel-1-(bc->channel>16); + int b_stid=stack->b_stids[channel>=0?channel:0]; + + + if (bc->nodsp ) + clean_up_bc(bc); + + if ( !misdn_cap_is_speech(bc->capability)) + clean_up_bc(bc); + + + if (bc->upset) { + cb_log(4, stack->port, "$$$ bc already upsetted stid :%x\n", b_stid); + return -1; + } + + cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid); + + if (b_stid <= 0) { + cb_log(0, stack->port," -- Stid <=0 at the moment on port:%d channel:%d\n",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, 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, 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, stack->port,"New Layer Err: %d %s port:%d\n",ret,strerror(errno), 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, stack->port," --> Got Adr %x\n", bc->addr); + cb_log(4, stack->port," --> Channel is %d\n", bc->channel); + + + if (bc->async == 1 || bc->nodsp) { + cb_log(4, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\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, 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, 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, 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->port=stack->port; + bc->nt=stack->mode==NT_MODE?1:0; + + { + ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE); + ibuffer_t* mbuf= init_ibuffer(MISDN_IBUF_SIZE); + + if (!ibuf) return -1; + if (!mbuf) return -1; + + clear_ibuffer( ibuf); + clear_ibuffer( mbuf); + + ibuf->rsem=malloc(sizeof(sem_t)); + mbuf->rsem=malloc(sizeof(sem_t)); + + bc->astbuf=ibuf; + bc->misdnbuf=mbuf; + + if (sem_init(ibuf->rsem,1,0)<0) + sem_init(ibuf->rsem,0,0); + if (sem_init(mbuf->rsem,1,0)< 0) + sem_init(mbuf->rsem,0,0); + + } + + + + + { + stack_info_t *stinf; + ret = mISDN_get_stack_info(midev, 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); + } + + misdn_lib_get_l1_up(stack); + + if (stack->ptp) { + misdn_lib_get_l2_up(stack); + stack->l2link=0; + } + + + 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; + } + + misdn_lib_get_l1_up(stack); + misdn_lib_get_l2_status(stack); + + /* 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) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + if (stack->mode == TE_MODE) { + switch (event) { + + case EVENT_CONNECT_ACKNOWLEDGE: + manager_bchannel_activate(bc); + break; + case EVENT_CONNECT: + + if ( *bc->crypt_key ) { + cb_log(4, stack->port, "ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s\n", 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(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(stack, 0); + if (!bc->channel) { + cb_log(0, 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, 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.port=stack->port; + 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(stack,bc->channel); + empty_bc(bc); + clean_up_bc(bc); + dump_chan_list(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( 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) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + + if (!stack) { + cb_log(1,0,"misdn_release: No Stack found\n"); + return; + } + + if (bc->channel>=0) { + empty_chan_in_stack(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) { + + if (!stack->l1link) + misdn_lib_get_l1_up(stack); + if (!stack->l2link) + misdn_lib_get_l2_up(stack); + + 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->l1link) + 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?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_CONNECT|INDICATION: + case CC_ALERTING|INDICATION: + case CC_PROCEEDING|INDICATION: + + { + struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo); + + if (!bc) { + msg_t *dmsg; + cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x port:%d\n",hh->dinfo, stack->port); + + cb_log(0, stack->port, "Releaseing call %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; + + } + + 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,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.port=stack->port; + 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.port=stack->port; + 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, 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; + struct misdn_stack *stack=get_stack_by_bc(bc); + + 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(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(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(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(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(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 ; + + struct misdn_stack *stack=get_stack_by_bc(bc); + + if (!stack) return 0; + + switch (frm->prim) { + case PH_ACTIVATE | INDICATION: + case DL_ESTABLISH | INDICATION: + cb_log(4, stack->port, "BCHAN: ACT Ind\n"); + free_msg(msg); + return 1; + + case PH_ACTIVATE | CONFIRM: + case DL_ESTABLISH | CONFIRM: + cb_log(4, stack->port, "BCHAN: bchan ACT Confirm\n"); + free_msg(msg); + + return 1; + + case PH_DEACTIVATE | INDICATION: + case DL_RELEASE | INDICATION: + cb_log (4, stack->port, "BCHAN: DeACT Ind\n"); + free_msg(msg); + return 1; + + case PH_DEACTIVATE | CONFIRM: + case DL_RELEASE | CONFIRM: + cb_log(4, 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, stack->port, "PH_CONTROL: port:%d channel:%d oad%d:%s dad%d:%s \n", 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, 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, stack->port, " --> BF REJECT\n"); + free_msg(msg); + return 1; + } + if (cont == BF_ACCEPT) { + cb_log(4, 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 **/ + if ( misdn_cap_is_speech(bc->capability) ) + flip_buf_bits(bc->bframe, bc->bframe_len); + + +#if MISDN_DEBUG + cb_log(0, 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, stack->port, "Transmitting %d samples 2 misdn\n", txfrm->len); + + r=mISDN_write(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, stack->port, "Data confirmed\n"); +#endif + free_msg(msg); + return 1; + break; + case DL_DATA|RESPONSE: +#if MISDN_DEBUG + cb_log(0, 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(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; + + if (stack->mode==NT_MODE) { + + if (stack->nst.l1_l2(&stack->nst, msg)) + free_msg(msg); + } else { + 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); + + for (i=0; i<stack->b_num; i++) { + if (global_state == MISDN_INITIALIZED) { + cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data); + } + + } + + if (stack->mode==NT_MODE) { + if (stack->nst.l1_l2(&stack->nst, msg)) + free_msg(msg); + + } else { + free_msg(msg); + } + + stack->l1link=0; + stack->l2link=0; + + 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, "L2: 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_l2_up(stack); + 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; + + 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) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + return find_bc_holded(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; + + struct misdn_stack *stack=get_stack_by_bc(bc); + + if (!stack) return; + + cb_log(2, stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", stack->mode==NT_MODE?"NT":"TE", bc->cause, bc->out_cause, bc->rad); + + cb_log(2, 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, stack->port, " --> channel:%d caps:%s pi:%x keypad:%s\n", bc->channel, bearer2str(bc->capability),bc->progress_indicator, bc->keypad); + + cb_log(3, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1); + + cb_log(3, stack->port, " --> pid:%d addr:%x l3id:%x\n", bc->pid, bc->addr, bc->l3_id); + + cb_log(4, 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; + + struct misdn_stack *stack=get_stack_by_bc(bc); + + if ( stack->mode == NT_MODE && !stack->l1link) { + /** Queue Event **/ + bc->evq=event; + cb_log(1, 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 = (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, stack->port, "I SEND:%s oad:%s dad:%s port:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, stack->port); + misdn_lib_log_ies(bc); + + switch (event) { + case EVENT_SETUP: + if (create_process(glob_mgr->midev, bc)<0) { + cb_log(0, 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 (stack->mode == NT_MODE) { + if (bc->channel <=0 ) { /* else we have the channel already */ + bc->channel = find_free_chan_in_stack(stack, 0); + if (!bc->channel) { + cb_log(0, 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, stack->port, " --> ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s \n", 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, 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, 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(stack,holded_bc); + + if (stack->mode == NT_MODE) { + empty_chan_in_stack(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(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, 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, 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, stack->mode==NT_MODE?1:0); + msg_queue_tail(&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; + + struct misdn_stack *stack=get_stack_by_bc(bc); + + if (!msg) { + cb_log(0, 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, 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; + + struct misdn_stack *stack=get_stack_by_bc(bc); + if (!bc->active) return; + + cb_log(5, 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(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) +{ + + struct misdn_stack *stack=get_stack_by_bc(bc); + if (!bc->active) return -1; + + if ( misdn_cap_is_speech(bc->capability) ) + 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, stack->port, "Writing %d bytes\n",len); + + cb_log(9, stack->port, "Wrinting %d bytes 2 mISDN\n",len); + r=mISDN_write(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; + struct misdn_stack *stack=get_stack_by_bc(bc); + + ctrl->prim = PH_CONTROL | REQUEST; + ctrl->addr = bc->addr; + ctrl->dinfo = 0; + ctrl->len = sizeof(unsigned long)*2; + *d++ = c1; + *d++ = c2; + mISDN_write(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; + struct misdn_stack *stack=get_stack_by_bc(bc); + + 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(stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC); +} + + + + +void manager_clean_bc(struct misdn_bchannel *bc ) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + + if (bc->state == STATE_CONNECTED) + misdn_lib_send_event(bc,EVENT_DISCONNECT); + + empty_chan_in_stack(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,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,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]; + + struct misdn_stack *stack=get_stack_by_bc(bc); + + cb_log(1, stack?stack->port:0,"Sending Control ECHOCAN_ON taps:%d training:%d\n",bc->ec_deftaps, bc->ec_training); + + 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, stack->port, "Taps is %d\n",bc->ec_deftaps); + break; + default: + cb_log(0, 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) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + + cb_log(1, stack?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; +} + + + +void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2) { + manager_ph_control(bc1, CMX_RECEIVE_OFF, 0); + manager_ph_control(bc2, CMX_RECEIVE_OFF, 0); + + manager_ph_control(bc1, CMX_CONF_JOIN, (bc1->pid<<1) +1); + manager_ph_control(bc2, CMX_CONF_JOIN, (bc1->pid<<1) +1); +} + +void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2) +{ + + manager_ph_control(bc1, CMX_RECEIVE_ON, 0) ; + manager_ph_control(bc2, CMX_RECEIVE_ON, 0); + + manager_ph_control(bc1, CMX_CONF_SPLIT, (bc1->pid<<1) +1); + manager_ph_control(bc2, CMX_CONF_SPLIT, (bc1->pid<<1) +1); + +} diff --git a/1.2-netsec/channels/misdn/isdn_lib.h b/1.2-netsec/channels/misdn/isdn_lib.h new file mode 100644 index 000000000..28a4fe7b6 --- /dev/null +++ b/1.2-netsec/channels/misdn/isdn_lib.h @@ -0,0 +1,367 @@ +/* + * 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 + +/** For initialization usage **/ +/* typedef int ie_nothing_t ;*/ +/** end of init usage **/ + + +#define MAX_BCHANS 30 + +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 { + + int nt; + int port; + /** 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; + + + void *astbuf; + void *misdnbuf; + + + int te_choose_channel; + int early_bconnect; + + /* 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 screen; + + 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; +}; + + +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); + + +void manager_ec_enable(struct misdn_bchannel *bc); +void manager_ec_disable(struct misdn_bchannel *bc); + +void get_show_stack_details(int port, char *buf); + + +/** Ibuf interface **/ +int misdn_ibuf_usedcount(void *buf); +int misdn_ibuf_freecount(void *buf); +void misdn_ibuf_memcpy_r(char *to, void *from, int len); +void misdn_ibuf_memcpy_w(void *buf, char *from, int len); + +/** Ibuf interface End **/ + +void misdn_lib_setup_bc(struct misdn_bchannel *bc); + +void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2); +void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2); + + +int misdn_lib_is_ptp(int port); + +#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/1.2-netsec/channels/misdn/isdn_lib_intern.h b/1.2-netsec/channels/misdn/isdn_lib_intern.h new file mode 100644 index 000000000..0305a2dad --- /dev/null +++ b/1.2-netsec/channels/misdn/isdn_lib_intern.h @@ -0,0 +1,99 @@ +#ifndef ISDN_LIB_INTERN +#define ISDN_LIB_INTER + + +#include <mISDNuser/mISDNlib.h> +#include <mISDNuser/isdn_net.h> +#include <mISDNuser/l3dss1.h> +#include <mISDNuser/net_l3.h> + +#include <pthread.h> + +#include "isdn_lib.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 + + +ibuffer_t *astbuf; +ibuffer_t *misdnbuf; + + +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; + +} ; + +/* for isdn_msg_parser.c */ +msg_t *create_l3msg(int prim, int mt, int dinfo , int size, int nt); + + + +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]; + + + struct misdn_bchannel *holding; /* Queue which holds holded channels :) */ + + struct misdn_stack *next; +}; + + +struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc); + +#endif diff --git a/1.2-netsec/channels/misdn/isdn_msg_parser.c b/1.2-netsec/channels/misdn/isdn_msg_parser.c new file mode 100644 index 000000000..9b59f8685 --- /dev/null +++ b/1.2-netsec/channels/misdn/isdn_msg_parser.c @@ -0,0 +1,1356 @@ +/* + * 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); + struct misdn_stack *stack=get_stack_by_bc(bc); + + { + 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 && 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=bc->screen; + 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->law) { + case INFO_CODEC_ULAW: user=2; + break; + case INFO_CODEC_ALAW: user=3; + break; + default: + user=3; + } + + switch (bc->capability) { + case INFO_CAPABILITY_SPEECH: capability = 0; + break; + case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: capability = 8; + user=-1; + break; + case INFO_CAPABILITY_DIGITAL_RESTRICTED: capability = 9; + user=-1; + break; + default: + capability=bc->capability; + } + + + + 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)); + + struct misdn_stack *stack=get_stack_by_bc(bc); + +#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, stack->port, "CC_RESTART Request on channel:%d on port:%d\n",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; + + struct misdn_stack *stack=get_stack_by_bc(bc); + +#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, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] port:%d\n",stack->port); + return; + } + } else { + if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) { + cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] port:%d\n",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/1.2-netsec/channels/misdn/mISDN.patch b/1.2-netsec/channels/misdn/mISDN.patch new file mode 100644 index 000000000..ccc8ae4b2 --- /dev/null +++ b/1.2-netsec/channels/misdn/mISDN.patch @@ -0,0 +1,2500 @@ +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/arith.h mISDN/drivers/isdn/hardware/mISDN/arith.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/arith.h 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/arith.h 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,347 @@ ++#ifndef _ZAPTEL_ARITH_H ++#define _ZAPTEL_ARITH_H ++/* ++ * Handy add/subtract functions to operate on chunks of shorts. ++ * Feel free to add customizations for additional architectures ++ * ++ */ ++ ++#ifdef CONFIG_ZAPTEL_MMX ++#ifdef ZT_CHUNKSIZE ++static inline void __ACSS(volatile short *dst, const short *src) ++{ ++ __asm__ __volatile__ ( ++ "movq 0(%0), %%mm0;\n" ++ "movq 0(%1), %%mm1;\n" ++ "movq 8(%0), %%mm2;\n" ++ "movq 8(%1), %%mm3;\n" ++ "paddsw %%mm1, %%mm0;\n" ++ "paddsw %%mm3, %%mm2;\n" ++ "movq %%mm0, 0(%0);\n" ++ "movq %%mm2, 8(%0);\n" ++ : "=r" (dst) ++ : "r" (src), "0" (dst) ++ : "memory" ++#if CLOBBERMMX ++ , "%mm0", "%mm1", "%mm2", "%mm3" ++#endif ++ ); ++ ++} ++static inline void __SCSS(volatile short *dst, const short *src) ++{ ++ __asm__ __volatile__ ( ++ "movq 0(%0), %%mm0;\n" ++ "movq 0(%1), %%mm1;\n" ++ "movq 8(%0), %%mm2;\n" ++ "movq 8(%1), %%mm3;\n" ++ "psubsw %%mm1, %%mm0;\n" ++ "psubsw %%mm3, %%mm2;\n" ++ "movq %%mm0, 0(%0);\n" ++ "movq %%mm2, 8(%0);\n" ++ : "=r" (dst) ++ : "r" (src), "0" (dst) ++ : "memory" ++#if CLOBBERMMX ++ , "%mm0", "%mm1", "%mm2", "%mm3" ++#endif ++ ); ++ ++} ++ ++#if (ZT_CHUNKSIZE == 8) ++#define ACSS(a,b) __ACSS(a,b) ++#define SCSS(a,b) __SCSS(a,b) ++#elif (ZT_CHUNKSIZE > 8) ++static inline void ACSS(volatile short *dst, const short *src) ++{ ++ int x; ++ for (x=0;x<ZT_CHUNKSIZE;x+=8) ++ __ACSS(dst + x, src + x); ++} ++static inline void SCSS(volatile short *dst, const short *src) ++{ ++ int x; ++ for (x=0;x<ZT_CHUNKSIZE;x+=8) ++ __SCSS(dst + x, src + x); ++} ++#else ++#error No MMX for ZT_CHUNKSIZE < 8 ++#endif ++#endif ++static inline int CONVOLVE(const int *coeffs, const short *hist, int len) ++{ ++ int sum; ++ /* Divide length by 16 */ ++ len >>= 4; ++ ++ /* Clear our accumulator, mm4 */ ++ ++ /* ++ ++ For every set of eight... ++ ++ Load 16 coefficients into four registers... ++ Shift each word right 16 to make them shorts... ++ Pack the resulting shorts into two registers... ++ With the coefficients now in mm0 and mm2, load the ++ history into mm1 and mm3... ++ Multiply/add mm1 into mm0, and mm3 into mm2... ++ Add mm2 into mm0 (without saturation, alas). Now we have two half-results. ++ Accumulate in mm4 (again, without saturation, alas) ++ */ ++ __asm__ ( ++ "pxor %%mm4, %%mm4;\n" ++ "mov %1, %%edi;\n" ++ "mov %2, %%esi;\n" ++ "mov %3, %%ecx;\n" ++ "1:" ++ "movq 0(%%edi), %%mm0;\n" ++ "movq 8(%%edi), %%mm1;\n" ++ "movq 16(%%edi), %%mm2;\n" ++ "movq 24(%%edi), %%mm3;\n" ++ /* can't use 4/5 since 4 is the accumulator for us */ ++ "movq 32(%%edi), %%mm6;\n" ++ "movq 40(%%edi), %%mm7;\n" ++ "psrad $16, %%mm0;\n" ++ "psrad $16, %%mm1;\n" ++ "psrad $16, %%mm2;\n" ++ "psrad $16, %%mm3;\n" ++ "psrad $16, %%mm6;\n" ++ "psrad $16, %%mm7;\n" ++ "packssdw %%mm1, %%mm0;\n" ++ "packssdw %%mm3, %%mm2;\n" ++ "packssdw %%mm7, %%mm6;\n" ++ "movq 0(%%esi), %%mm1;\n" ++ "movq 8(%%esi), %%mm3;\n" ++ "movq 16(%%esi), %%mm7;\n" ++ "pmaddwd %%mm1, %%mm0;\n" ++ "pmaddwd %%mm3, %%mm2;\n" ++ "pmaddwd %%mm7, %%mm6;\n" ++ "paddd %%mm6, %%mm4;\n" ++ "paddd %%mm2, %%mm4;\n" ++ "paddd %%mm0, %%mm4;\n" ++ /* Come back and do for the last few bytes */ ++ "movq 48(%%edi), %%mm6;\n" ++ "movq 56(%%edi), %%mm7;\n" ++ "psrad $16, %%mm6;\n" ++ "psrad $16, %%mm7;\n" ++ "packssdw %%mm7, %%mm6;\n" ++ "movq 24(%%esi), %%mm7;\n" ++ "pmaddwd %%mm7, %%mm6;\n" ++ "paddd %%mm6, %%mm4;\n" ++ "add $64, %%edi;\n" ++ "add $32, %%esi;\n" ++ "dec %%ecx;\n" ++ "jnz 1b;\n" ++ "movq %%mm4, %%mm0;\n" ++ "psrlq $32, %%mm0;\n" ++ "paddd %%mm0, %%mm4;\n" ++ "movd %%mm4, %0;\n" ++ : "=r" (sum) ++ : "r" (coeffs), "r" (hist), "r" (len) ++ : "%ecx", "%edi", "%esi" ++ ); ++ ++ return sum; ++} ++ ++static inline void UPDATE(volatile int *taps, const short *history, const int nsuppr, const int ntaps) ++{ ++ int i; ++ int correction; ++ for (i=0;i<ntaps;i++) { ++ correction = history[i] * nsuppr; ++ taps[i] += correction; ++ } ++} ++ ++static inline void UPDATE2(volatile int *taps, volatile short *taps_short, const short *history, const int nsuppr, const int ntaps) ++{ ++ int i; ++ int correction; ++#if 0 ++ ntaps >>= 4; ++ /* First, load up taps, */ ++ __asm__ ( ++ "pxor %%mm4, %%mm4;\n" ++ "mov %0, %%edi;\n" ++ "mov %1, %%esi;\n" ++ "mov %3, %%ecx;\n" ++ "1:" ++ "jnz 1b;\n" ++ "movq %%mm4, %%mm0;\n" ++ "psrlq $32, %%mm0;\n" ++ "paddd %%mm0, %%mm4;\n" ++ "movd %%mm4, %0;\n" ++ : "=r" (taps), "=r" (taps_short) ++ : "r" (history), "r" (nsuppr), "r" (ntaps), "0" (taps) ++ : "%ecx", "%edi", "%esi" ++ ); ++#endif ++#if 1 ++ for (i=0;i<ntaps;i++) { ++ correction = history[i] * nsuppr; ++ taps[i] += correction; ++ taps_short[i] = taps[i] >> 16; ++ } ++#endif ++} ++ ++static inline int CONVOLVE2(const short *coeffs, const short *hist, int len) ++{ ++ int sum; ++ /* Divide length by 16 */ ++ len >>= 4; ++ ++ /* Clear our accumulator, mm4 */ ++ ++ /* ++ ++ For every set of eight... ++ Load in eight coefficients and eight historic samples, multliply add and ++ accumulate the result ++ */ ++ __asm__ ( ++ "pxor %%mm4, %%mm4;\n" ++ "mov %1, %%edi;\n" ++ "mov %2, %%esi;\n" ++ "mov %3, %%ecx;\n" ++ "1:" ++ "movq 0(%%edi), %%mm0;\n" ++ "movq 8(%%edi), %%mm2;\n" ++ "movq 0(%%esi), %%mm1;\n" ++ "movq 8(%%esi), %%mm3;\n" ++ "pmaddwd %%mm1, %%mm0;\n" ++ "pmaddwd %%mm3, %%mm2;\n" ++ "paddd %%mm2, %%mm4;\n" ++ "paddd %%mm0, %%mm4;\n" ++ "movq 16(%%edi), %%mm0;\n" ++ "movq 24(%%edi), %%mm2;\n" ++ "movq 16(%%esi), %%mm1;\n" ++ "movq 24(%%esi), %%mm3;\n" ++ "pmaddwd %%mm1, %%mm0;\n" ++ "pmaddwd %%mm3, %%mm2;\n" ++ "paddd %%mm2, %%mm4;\n" ++ "paddd %%mm0, %%mm4;\n" ++ "add $32, %%edi;\n" ++ "add $32, %%esi;\n" ++ "dec %%ecx;\n" ++ "jnz 1b;\n" ++ "movq %%mm4, %%mm0;\n" ++ "psrlq $32, %%mm0;\n" ++ "paddd %%mm0, %%mm4;\n" ++ "movd %%mm4, %0;\n" ++ : "=r" (sum) ++ : "r" (coeffs), "r" (hist), "r" (len) ++ : "%ecx", "%edi", "%esi" ++ ); ++ ++ return sum; ++} ++static inline short MAX16(const short *y, int len, int *pos) ++{ ++ int k; ++ short max = 0; ++ int bestpos = 0; ++ for (k=0;k<len;k++) { ++ if (max < y[k]) { ++ bestpos = k; ++ max = y[k]; ++ } ++ } ++ *pos = (len - 1 - bestpos); ++ return max; ++} ++ ++ ++ ++#else ++ ++#ifdef ZT_CHUNKSIZE ++static inline void ACSS(short *dst, short *src) ++{ ++ int x,sum; ++ /* Add src to dst with saturation, storing in dst */ ++ for (x=0;x<ZT_CHUNKSIZE;x++) { ++ sum = dst[x]+src[x]; ++ if (sum > 32767) ++ sum = 32767; ++ else if (sum < -32768) ++ sum = -32768; ++ dst[x] = sum; ++ } ++} ++ ++static inline void SCSS(short *dst, short *src) ++{ ++ int x,sum; ++ /* Add src to dst with saturation, storing in dst */ ++ for (x=0;x<ZT_CHUNKSIZE;x++) { ++ sum = dst[x]-src[x]; ++ if (sum > 32767) ++ sum = 32767; ++ else if (sum < -32768) ++ sum = -32768; ++ dst[x] = sum; ++ } ++} ++ ++#endif /* ZT_CHUNKSIZE */ ++ ++static inline int CONVOLVE(const int *coeffs, const short *hist, int len) ++{ ++ int x; ++ int sum = 0; ++ for (x=0;x<len;x++) ++ sum += (coeffs[x] >> 16) * hist[x]; ++ return sum; ++} ++ ++static inline int CONVOLVE2(const short *coeffs, const short *hist, int len) ++{ ++ int x; ++ int sum = 0; ++ for (x=0;x<len;x++) ++ sum += coeffs[x] * hist[x]; ++ return sum; ++} ++ ++static inline void UPDATE(int *taps, const short *history, const int nsuppr, const int ntaps) ++{ ++ int i; ++ int correction; ++ for (i=0;i<ntaps;i++) { ++ correction = history[i] * nsuppr; ++ taps[i] += correction; ++ } ++} ++ ++static inline void UPDATE2(int *taps, short *taps_short, const short *history, const int nsuppr, const int ntaps) ++{ ++ int i; ++ int correction; ++ for (i=0;i<ntaps;i++) { ++ correction = history[i] * nsuppr; ++ taps[i] += correction; ++ taps_short[i] = taps[i] >> 16; ++ } ++} ++ ++static inline short MAX16(const short *y, int len, int *pos) ++{ ++ int k; ++ short max = 0; ++ int bestpos = 0; ++ for (k=0;k<len;k++) { ++ if (max < y[k]) { ++ bestpos = k; ++ max = y[k]; ++ } ++ } ++ *pos = (len - 1 - bestpos); ++ return max; ++} ++ ++#endif /* MMX */ ++#endif /* _ZAPTEL_ARITH_H */ +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/biquad.h mISDN/drivers/isdn/hardware/mISDN/biquad.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/biquad.h 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/biquad.h 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,73 @@ ++/* ++ * SpanDSP - a series of DSP components for telephony ++ * ++ * biquad.h - General telephony bi-quad section routines (currently this just ++ * handles canonic/type 2 form) ++ * ++ * Written by Steve Underwood <steveu@coppice.org> ++ * ++ * Copyright (C) 2001 Steve Underwood ++ * ++ * All rights reserved. ++ * ++ * This program is free software; you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation; either version 2 of the License, or ++ * (at your option) any later version. ++ * ++ * This program is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with this program; if not, write to the Free Software ++ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ++ * ++ */ ++ ++typedef struct ++{ ++ int32_t gain; ++ int32_t a1; ++ int32_t a2; ++ int32_t b1; ++ int32_t b2; ++ ++ int32_t z1; ++ int32_t z2; ++} biquad2_state_t; ++ ++static inline void biquad2_init (biquad2_state_t *bq, ++ int32_t gain, ++ int32_t a1, ++ int32_t a2, ++ int32_t b1, ++ int32_t b2) ++{ ++ bq->gain = gain; ++ bq->a1 = a1; ++ bq->a2 = a2; ++ bq->b1 = b1; ++ bq->b2 = b2; ++ ++ bq->z1 = 0; ++ bq->z2 = 0; ++} ++/*- End of function --------------------------------------------------------*/ ++ ++static inline int16_t biquad2 (biquad2_state_t *bq, int16_t sample) ++{ ++ int32_t y; ++ int32_t z0; ++ ++ z0 = sample*bq->gain + bq->z1*bq->a1 + bq->z2*bq->a2; ++ y = z0 + bq->z1*bq->b1 + bq->z2*bq->b2; ++ ++ bq->z2 = bq->z1; ++ bq->z1 = z0 >> 15; ++ y >>= 15; ++ return y; ++} ++/*- End of function --------------------------------------------------------*/ ++/*- End of file ------------------------------------------------------------*/ +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/dsp_cancel.c mISDN/drivers/isdn/hardware/mISDN/dsp_cancel.c +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/dsp_cancel.c 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/dsp_cancel.c 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,390 @@ ++/* $Id$ ++ * ++ * Simple but fast Echo cancellation for mISDN_dsp. ++ * ++ * Copyright Andreas Eversberg (jolly@jolly.de) ++ * ++ * This software may be used and distributed according to the terms ++ * of the GNU General Public License, incorporated herein by reference. ++ * ++ */ ++ ++#include "layer1.h" ++#include "helper.h" ++#include "debug.h" ++#include "dsp.h" ++ ++ ++/* ++ * how this works: ++ * ++ * ++ * ++ */ ++void bchdev_echocancel_chunk(dsp_t* dev, uint8_t *rxchunk, uint8_t *txchunk, uint16_t size); ++int bchdev_echocancel_activate(dsp_t* dev, int deftaps, int train); ++void bchdev_echocancel_deactivate(dsp_t* dev); ++ ++ ++ ++ ++ ++ ++static char flip_table[256]; ++ ++void dsp_cancel_init_flip_bits() ++{ ++ 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; ++} ++ ++ ++ ++void ++dsp_cancel_tx(dsp_t *dsp, u8 *data, int len) ++{ ++ if (!dsp ) return ; ++ if (!data) return; ++ ++ if (dsp->txbuflen + len < ECHOCAN_BUFLEN) { ++ memcpy(&dsp->txbuf[dsp->txbuflen],data,len); ++ dsp->txbuflen+=len; ++ } else { ++ printk("ECHOCAN: TXBUF Overflow len:%d newlen:%d\n",dsp->txbuflen,len); ++ dsp->txbuflen=0; ++ } ++ ++} ++ ++void ++dsp_cancel_rx(dsp_t *dsp, u8 *data, int len) ++{ ++ if (!dsp ) return ; ++ if (!data) return; ++ ++ if (len <= dsp->txbuflen) { ++ char tmp[ECHOCAN_BUFLEN]; ++ ++ int delta=dsp->txbuflen-len; ++ ++ memcpy(tmp,&dsp->txbuf[len],delta); ++ ++ flip_buf_bits(data,len); ++ flip_buf_bits(dsp->txbuf,len); ++ bchdev_echocancel_chunk(dsp, data, dsp->txbuf, len); ++ flip_buf_bits(data,len); ++ ++ memcpy(dsp->txbuf,tmp,delta); ++ dsp->txbuflen=delta; ++ //dsp->txbuflen=0; ++ ++ //bchdev_echocancel_chunk(dsp, dsp->txbuf, data, len); ++ } else { ++ printk("ECHOCAN: TXBUF Underrun len:%d newlen:%d\n",dsp->txbuflen,len); ++ } ++ ++} ++ ++int ++dsp_cancel_init(dsp_t *dsp, int deftaps, int training, int delay) ++{ ++ ++ if (!dsp) return -1; ++ ++ printk("DSP_CANCEL_INIT called\n"); ++ ++ if (delay < 0) ++ { ++ printk("Disabling EC\n"); ++ dsp->cancel_enable = 0; ++ ++ dsp->txbuflen=0; ++ ++ bchdev_echocancel_deactivate(dsp); ++ ++ return(0); ++ } ++ ++ dsp->txbuflen=0; ++ dsp->rxbuflen=0; ++ ++ ++ bchdev_echocancel_activate(dsp,deftaps, training); ++ ++ printk("Enabling EC\n"); ++ dsp->cancel_enable = 1; ++ return(0); ++} ++ ++ ++ ++ ++ ++/*****************************************************/ ++#define __ECHO_STATE_MUTE (1 << 8) ++#define ECHO_STATE_IDLE (0) ++#define ECHO_STATE_PRETRAINING (1 | (__ECHO_STATE_MUTE)) ++#define ECHO_STATE_STARTTRAINING (2 | (__ECHO_STATE_MUTE)) ++#define ECHO_STATE_AWAITINGECHO (3 | (__ECHO_STATE_MUTE)) ++#define ECHO_STATE_TRAINING (4 | (__ECHO_STATE_MUTE)) ++#define ECHO_STATE_ACTIVE (5) ++ ++#define AMI_MASK 0x55 ++ ++ ++static unsigned char linear2alaw (short linear) ++{ ++ int mask; ++ int seg; ++ int pcm_val; ++ static int seg_end[8] = ++ { ++ 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF ++ }; ++ ++ pcm_val = linear; ++ if (pcm_val >= 0) ++ { ++ /* Sign (7th) bit = 1 */ ++ mask = AMI_MASK | 0x80; ++ } ++ else ++ { ++ /* Sign bit = 0 */ ++ mask = AMI_MASK; ++ pcm_val = -pcm_val; ++ } ++ ++ /* Convert the scaled magnitude to segment number. */ ++ for (seg = 0; seg < 8; seg++) ++ { ++ if (pcm_val <= seg_end[seg]) ++ break; ++ } ++ /* Combine the sign, segment, and quantization bits. */ ++ return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask; ++} ++ ++/*- End of function --------------------------------------------------------*/ ++ ++static short int alaw2linear (uint8_t alaw) ++{ ++ int i; ++ int seg; ++ ++ alaw ^= AMI_MASK; ++ i = ((alaw & 0x0F) << 4); ++ seg = (((int) alaw & 0x70) >> 4); ++ if (seg) ++ i = (i + 0x100) << (seg - 1); ++ return (short int) ((alaw & 0x80) ? i : -i); ++} ++ ++ ++/** @return string of given echo cancellation state */ ++char* bchdev_echocancel_statestr(uint16_t state) ++{ ++ switch(state) { ++ case ECHO_STATE_IDLE: ++ return "idle"; ++ break; ++ case ECHO_STATE_PRETRAINING: ++ return "pre-training"; ++ break; ++ case ECHO_STATE_STARTTRAINING: ++ return "transmit impulse"; ++ break; ++ case ECHO_STATE_AWAITINGECHO: ++ return "awaiting echo"; ++ break; ++ case ECHO_STATE_TRAINING: ++ return "training start"; ++ break; ++ case ECHO_STATE_ACTIVE: ++ return "training finished"; ++ break; ++ default: ++ return "unknown"; ++ } ++} ++ ++/** Changes state of echo cancellation to given state */ ++void bchdev_echocancel_setstate(dsp_t* dev, uint16_t state) ++{ ++ char* statestr = bchdev_echocancel_statestr(state); ++ ++ printk("bchdev: echo cancel state %d (%s)\n", state & 0xff, statestr); ++ if (state == ECHO_STATE_ACTIVE) ++ printk("bchdev: %d taps trained\n", dev->echolastupdate); ++ dev->echostate = state; ++} ++ ++static int buf_size=0; ++static int ec_timer=2000; ++//static int ec_timer=1000; ++ ++ ++/** Activates echo cancellation for the given bch_dev, device must have been locked before! */ ++int bchdev_echocancel_activate(dsp_t* dev, int deftaps, int training) ++{ ++ int taps; ++ ++ if (! dev) return -EINVAL; ++ ++ if (dev->ec && dev->ecdis_rd && dev->ecdis_wr) { ++ // already active ++ return 0; ++ } ++ ++ if (deftaps>0) { ++ taps=deftaps; ++ } else { ++ taps=128; ++ } ++ ++ ++ switch (buf_size) { ++ case 0: taps += 0; break; ++ case 1: taps += 256-128; break; ++ case 2: taps += 512-128; break; ++ default: taps += 1024-128; ++ } ++ ++ if (!dev->ec) dev->ec = echo_can_create(taps, 0); ++ if (!dev->ec) { ++ return -ENOMEM; ++ } ++ ++ dev->echolastupdate = 0; ++ ++ if (!training) { ++ dev->echotimer=0; ++ bchdev_echocancel_setstate(dev, ECHO_STATE_IDLE); ++ } else { ++ if (training<10) ++ training= ec_timer; ++ ++ dev->echotimer = training; ++ bchdev_echocancel_setstate(dev, ECHO_STATE_PRETRAINING); ++ ++ } ++ ++ if (!dev->ecdis_rd) dev->ecdis_rd = kmalloc(sizeof(echo_can_disable_detector_state_t), GFP_KERNEL); ++ if (!dev->ecdis_rd) { ++ kfree(dev->ec); dev->ec = NULL; ++ return -ENOMEM; ++ } ++ echo_can_disable_detector_init(dev->ecdis_rd); ++ ++ if (!dev->ecdis_wr) dev->ecdis_wr = kmalloc(sizeof(echo_can_disable_detector_state_t), GFP_KERNEL); ++ if (!dev->ecdis_wr) { ++ kfree(dev->ec); dev->ec = NULL; ++ kfree(dev->ecdis_rd); dev->ecdis_rd = NULL; ++ return -ENOMEM; ++ } ++ echo_can_disable_detector_init(dev->ecdis_wr); ++ ++ return 0; ++} ++ ++/** Deactivates echo cancellation for the given bch_dev, device must have been locked before! */ ++void bchdev_echocancel_deactivate(dsp_t* dev) ++{ ++ if (! dev) return; ++ ++ //chan_misdn_log("bchdev: deactivating echo cancellation on port=%04x, chan=%02x\n", dev->stack->port, dev->channel); ++ ++ if (dev->ec) echo_can_free(dev->ec); ++ dev->ec = NULL; ++ ++ dev->echolastupdate = 0; ++ dev->echotimer = 0; ++ bchdev_echocancel_setstate(dev, ECHO_STATE_IDLE); ++ ++ if (dev->ecdis_rd) kfree(dev->ecdis_rd); ++ dev->ecdis_rd = NULL; ++ ++ if (dev->ecdis_wr) kfree(dev->ecdis_wr); ++ dev->ecdis_wr = NULL; ++} ++ ++/** Processes one TX- and one RX-packet with echocancellation */ ++void bchdev_echocancel_chunk(dsp_t* dev, uint8_t *rxchunk, uint8_t *txchunk, uint16_t size) ++{ ++ int16_t rxlin, txlin; ++ uint16_t pos; ++ ++ /* Perform echo cancellation on a chunk if requested */ ++ if (dev->ec) { ++ if (dev->echostate & __ECHO_STATE_MUTE) { ++ if (dev->echostate == ECHO_STATE_STARTTRAINING) { ++ // Transmit impulse now ++ txchunk[0] = linear2alaw(16384); ++ memset(txchunk+1, 0, size-1); ++ bchdev_echocancel_setstate(dev, ECHO_STATE_TRAINING); //AWAITINGECHO); ++ } else { ++ // train the echo cancellation ++ for (pos = 0; pos < size; pos++) { ++ rxlin = alaw2linear(rxchunk[pos]); ++ txlin = alaw2linear(txchunk[pos]); ++ if (dev->echostate == ECHO_STATE_PRETRAINING) { ++ if (dev->echotimer <= 0) { ++ dev->echotimer = 0; ++ bchdev_echocancel_setstate(dev, ECHO_STATE_STARTTRAINING); ++ } else { ++ dev->echotimer--; ++ } ++ } ++ if ((dev->echostate == ECHO_STATE_AWAITINGECHO) && (txlin > 8000)) { ++ dev->echolastupdate = 0; ++ bchdev_echocancel_setstate(dev, ECHO_STATE_TRAINING); ++ } ++ if (dev->echostate == ECHO_STATE_TRAINING) { ++ if (echo_can_traintap(dev->ec, dev->echolastupdate++, rxlin)) { ++ bchdev_echocancel_setstate(dev, ECHO_STATE_ACTIVE); ++ } ++ } ++ ++ rxchunk[pos] = linear2alaw(0); ++ txchunk[pos] = linear2alaw(0); ++ } ++ } ++ } else { ++ for (pos = 0; pos < size; pos++) { ++ rxlin = alaw2linear(rxchunk[pos]); ++ txlin = alaw2linear(txchunk[pos]); ++ ++ if (echo_can_disable_detector_update(dev->ecdis_rd, rxlin) || ++ echo_can_disable_detector_update(dev->ecdis_wr, txlin)) { ++ bchdev_echocancel_deactivate(dev); ++ printk("EC: Fax detected, EC disabled\n"); ++ return ; ++ } else { ++ rxlin = echo_can_update(dev->ec, txlin, rxlin); ++ rxchunk[pos] = linear2alaw(rxlin); ++ } ++ } ++ } ++ } ++} ++ ++/******************************************************/ +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/dsp_core.c mISDN/drivers/isdn/hardware/mISDN/dsp_core.c +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/dsp_core.c 2005-01-29 17:15:21.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/dsp_core.c 2005-12-02 09:57:08.000000000 +0100 +@@ -42,17 +42,11 @@ + * v | + * +-----+-------------+-----+ + * |(3)(4) | +- * | | +- * | | + * | CMX | + * | | +- * | | +- * | | +- * | | + * | +-------------+ + * | | ^ + * | | | +- * | | | + * |+---------+| +----+----+ + * ||(1) || |(5) | + * || || | | +@@ -62,7 +56,6 @@ + * |+----+----+| +----+----+ + * +-----+-----+ ^ + * | | +- * | | + * v | + * +----+----+ +----+----+ + * |(5) | |(2) | +@@ -74,8 +67,18 @@ + * | ^ + * | | + * v | ++ * +----+-------------+----+ ++ * |(7) | ++ * | | ++ * | Echo Cancellation | ++ * | | ++ * | | ++ * +----+-------------+----+ ++ * | ^ ++ * | | ++ * v | + * +----+----+ +----+----+ +- * |(7) | |(7) | ++ * |(8) | |(8) | + * | | | | + * | Encrypt | | Decrypt | + * | | | | +@@ -115,6 +118,13 @@ + * data to/form upper layer may be swithed on/off individually without loosing + * features of CMX, Tones and DTMF. + * ++ * Echo Cancellation: Sometimes we like to cancel echo from the interface. ++ * Note that a VoIP call may not have echo caused by the IP phone. The echo ++ * is generated by the telephone line connected to it. Because the delay ++ * is high, it becomes an echo. RESULT: Echo Cachelation is required if ++ * both echo AND delay is applied to an interface. ++ * Remember that software CMX always generates a more or less delay. ++ * + * If all used features can be realized in hardware, and if transmit and/or + * receive data ist disabled, the card may not send/receive any data at all. + * Not receiving is usefull if only announcements are played. Not sending is +@@ -215,6 +225,9 @@ + printk(KERN_ERR "%s: failed to create tx packet\n", __FUNCTION__); + return; + } ++ /* if echo cancellation is enabled */ ++ if (dsp->cancel_enable) ++ dsp_cancel_tx(dsp, nskb->data, nskb->len); + /* crypt if enabled */ + if (dsp->bf_enable) + dsp_bf_encrypt(dsp, nskb->data, nskb->len); +@@ -380,6 +393,34 @@ + if (dsp_debug & DEBUG_DSP_CMX) + dsp_cmx_debug(dsp); + break; ++ case ECHOCAN_ON: /* turn echo calcellation on */ ++ ++ if (len<4) { ++ ret = -EINVAL; ++ break; ++ } ++ int ec_arr[2]; ++ ++ memcpy(&ec_arr,data,sizeof(ec_arr)); ++ ++ ++ printk("data[0]: %d data[1]: %d, len :%d\n",ec_arr[0], ++ ec_arr[1] ,len); ++ ++ if (dsp_debug & DEBUG_DSP_CORE) ++ printk(KERN_DEBUG "%s: turn echo cancelation on (delay=%d attenuation-shift=%d\n", __FUNCTION__, ec_arr[0], ec_arr[1]); ++ ++ ret = dsp_cancel_init(dsp, ec_arr[0], ec_arr[1] ,1); ++ ++ dsp_cmx_hardware(dsp->conf, dsp); ++ break; ++ case ECHOCAN_OFF: /* turn echo calcellation off */ ++ if (dsp_debug & DEBUG_DSP_CORE) ++ printk(KERN_DEBUG "%s: turn echo cancelation off\n", __FUNCTION__); ++ ++ ret = dsp_cancel_init(dsp, 0,0,-1); ++ dsp_cmx_hardware(dsp->conf, dsp); ++ break; + case BF_ENABLE_KEY: /* turn blowfish on */ + if (len<4 || len>56) { + ret = -EINVAL; +@@ -522,6 +563,9 @@ + /* decrypt if enabled */ + if (dsp->bf_enable) + dsp_bf_decrypt(dsp, skb->data, skb->len); ++ /* if echo cancellation is enabled */ ++ if (dsp->cancel_enable) ++ dsp_cancel_rx(dsp, skb->data, skb->len); + /* check if dtmf soft decoding is turned on */ + if (dsp->dtmf.software) { + digits = dsp_dtmf_goertzel_decode(dsp, skb->data, skb->len, (dsp_options&DSP_OPT_ULAW)?1:0); +@@ -919,6 +963,9 @@ + dsp_audio_generate_ulaw_samples(); + dsp_audio_generate_volume_changes(); + ++ ++ dsp_cancel_init_flip_bits(); ++ + /* init global lock */ + lock_HW_init(&dsp_lock); + +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/dsp.h mISDN/drivers/isdn/hardware/mISDN/dsp.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/dsp.h 2005-01-29 17:15:31.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/dsp.h 2005-12-02 09:57:08.000000000 +0100 +@@ -40,6 +40,13 @@ + #include "memdbg.h" + #endif + ++#include "ecdis.h" ++#include "mec2.h" ++ ++//#include "mec.h" ++//#include "mec3.h" ++ ++ + extern int dsp_options; + extern int dsp_debug; + +@@ -109,6 +116,8 @@ + + #define DSP_DTMF_NPOINTS 102 + ++#define ECHOCAN_BUFLEN 4*128 ++ + typedef struct _dtmf_t { + int software; /* dtmf uses software decoding */ + int hardware; /* dtmf uses hardware decoding */ +@@ -120,6 +129,13 @@ + } dtmf_t; + + ++/************** ++ *Cancel Stuff* ++ ***************/ ++ ++void dsp_cancel_init_flip_bits(void); ++ ++ + /*************** + * tones stuff * + ***************/ +@@ -200,6 +216,25 @@ + u8 bf_crypt_inring[16]; + u8 bf_data_out[9]; + int bf_sync; ++ ++ /* echo cancellation stuff */ ++ int cancel_enable; ++ echo_can_state_t* ec; /**< == NULL: echo cancellation disabled; ++ != NULL: echo cancellation enabled */ ++ ++ echo_can_disable_detector_state_t* ecdis_rd; ++ echo_can_disable_detector_state_t* ecdis_wr; ++ ++ uint16_t echotimer; ++ uint16_t echostate; ++ uint16_t echolastupdate; ++ ++ char txbuf[ECHOCAN_BUFLEN]; ++ int txbuflen; ++ ++ char rxbuf[ECHOCAN_BUFLEN]; ++ int rxbuflen; ++ + } dsp_t; + + /* functions */ +@@ -228,4 +263,8 @@ + extern int dsp_bf_init(dsp_t *dsp, const u8 *key, unsigned int keylen); + extern void dsp_bf_cleanup(dsp_t *dsp); + ++extern void dsp_cancel_tx(dsp_t *dsp, u8 *data, int len); ++extern void dsp_cancel_rx(dsp_t *dsp, u8 *data, int len); ++extern int dsp_cancel_init(dsp_t *dsp, int taps, int training, int delay); ++ + +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/ec.c mISDN/drivers/isdn/hardware/mISDN/ec.c +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/ec.c 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/ec.c 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,105 @@ ++#include "mec2.h" ++#include "ec.h" ++ ++ ++ ++#define __ECHO_STATE_MUTE (1 << 8) ++#define ECHO_STATE_IDLE (0) ++#define ECHO_STATE_PRETRAINING (1 | (__ECHO_STATE_MUTE)) ++#define ECHO_STATE_STARTTRAINING (2 | (__ECHO_STATE_MUTE)) ++#define ECHO_STATE_AWAITINGECHO (3 | (__ECHO_STATE_MUTE)) ++#define ECHO_STATE_TRAINING (4 | (__ECHO_STATE_MUTE)) ++#define ECHO_STATE_ACTIVE (5) ++ ++#define AMI_MASK 0x55 ++ ++static unsigned char linear2alaw (short linear) ++{ ++ int mask; ++ int seg; ++ int pcm_val; ++ static int seg_end[8] = ++ { ++ 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF ++ }; ++ ++ pcm_val = linear; ++ if (pcm_val >= 0) ++ { ++ /* Sign (7th) bit = 1 */ ++ mask = AMI_MASK | 0x80; ++ } ++ else ++ { ++ /* Sign bit = 0 */ ++ mask = AMI_MASK; ++ pcm_val = -pcm_val; ++ } ++ ++ /* Convert the scaled magnitude to segment number. */ ++ for (seg = 0; seg < 8; seg++) ++ { ++ if (pcm_val <= seg_end[seg]) ++ break; ++ } ++ /* Combine the sign, segment, and quantization bits. */ ++ return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask; ++} ++/*- End of function --------------------------------------------------------*/ ++ ++static short int alaw2linear (uint8_t alaw) ++{ ++ int i; ++ int seg; ++ ++ alaw ^= AMI_MASK; ++ i = ((alaw & 0x0F) << 4); ++ seg = (((int) alaw & 0x70) >> 4); ++ if (seg) ++ i = (i + 0x100) << (seg - 1); ++ return (short int) ((alaw & 0x80) ? i : -i); ++} ++ ++ ++void ec_chunk(struct echo_can_s *echo_can, unsigned char *rxchunk, const unsigned char *txchunk, int chunk_size) ++{ ++ short rxlin, txlin; ++ int x; ++ //unsigned long flags; ++ /* Perform echo cancellation on a chunk if necessary */ ++ if (echo_can->ec) { ++ if (echo_can->echostate & __ECHO_STATE_MUTE) { ++ /* Special stuff for training the echo can */ ++ for (x=0;x< chunk_size;x++) { ++ rxlin = alaw2linear(rxchunk[x]); ++ txlin = alaw2linear(txchunk[x]); ++ if (echo_can->echostate == ECHO_STATE_PRETRAINING) { ++ if (--echo_can->echotimer <= 0) { ++ echo_can->echotimer = 0; ++ echo_can->echostate = ECHO_STATE_STARTTRAINING; ++ } ++ } ++ if ((echo_can->echostate == ECHO_STATE_AWAITINGECHO) && (txlin > 8000)) { ++ echo_can->echolastupdate = 0; ++ echo_can->echostate = ECHO_STATE_TRAINING; ++ } ++ if (echo_can->echostate == ECHO_STATE_TRAINING) { ++ if (echo_can_traintap(echo_can->ec, echo_can->echolastupdate++, rxlin)) { ++#if 0 ++ printf("Finished training (%d taps trained)!\n", echo_can->echolastupdate); ++#endif ++ echo_can->echostate = ECHO_STATE_ACTIVE; ++ } ++ } ++ rxlin = 0; ++ rxchunk[x] = linear2alaw((int)rxlin); ++ } ++ } else { ++ for (x=0;x<chunk_size;x++) { ++ rxlin = alaw2linear(rxchunk[x]); ++ rxlin = echo_can_update(echo_can->ec, alaw2linear(txchunk[x]), rxlin); ++ rxchunk[x] = linear2alaw((int)rxlin); ++ } ++ } ++ } ++} +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/ecdis.h mISDN/drivers/isdn/hardware/mISDN/ecdis.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/ecdis.h 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/ecdis.h 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,118 @@ ++/* ++ * SpanDSP - a series of DSP components for telephony ++ * ++ * ec_disable_detector.h - A detector which should eventually meet the ++ * G.164/G.165 requirements for detecting the ++ * 2100Hz echo cancellor disable tone. ++ * ++ * Written by Steve Underwood <steveu@coppice.org> ++ * ++ * Copyright (C) 2001 Steve Underwood ++ * ++ * All rights reserved. ++ * ++ * This program is free software; you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation; either version 2 of the License, or ++ * (at your option) any later version. ++ * ++ * This program is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with this program; if not, write to the Free Software ++ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ++ * ++ */ ++ ++#include "biquad.h" ++ ++typedef struct ++{ ++ biquad2_state_t notch; ++ int notch_level; ++ int channel_level; ++ int tone_present; ++ int tone_cycle_duration; ++ int good_cycles; ++ int hit; ++} echo_can_disable_detector_state_t; ++ ++ ++#define FALSE 0 ++#define TRUE (!FALSE) ++ ++static inline void echo_can_disable_detector_init (echo_can_disable_detector_state_t *det) ++{ ++ /* Elliptic notch */ ++ /* This is actually centred at 2095Hz, but gets the balance we want, due ++ to the asymmetric walls of the notch */ ++ biquad2_init (&det->notch, ++ (int32_t) (-0.7600000*32768.0), ++ (int32_t) (-0.1183852*32768.0), ++ (int32_t) (-0.5104039*32768.0), ++ (int32_t) ( 0.1567596*32768.0), ++ (int32_t) ( 1.0000000*32768.0)); ++ ++ det->channel_level = 0; ++ det->notch_level = 0; ++ det->tone_present = FALSE; ++ det->tone_cycle_duration = 0; ++ det->good_cycles = 0; ++ det->hit = 0; ++} ++/*- End of function --------------------------------------------------------*/ ++ ++static inline int echo_can_disable_detector_update (echo_can_disable_detector_state_t *det, ++ int16_t amp) ++{ ++ int16_t notched; ++ ++ notched = biquad2 (&det->notch, amp); ++ /* Estimate the overall energy in the channel, and the energy in ++ the notch (i.e. overall channel energy - tone energy => noise). ++ Use abs instead of multiply for speed (is it really faster?). ++ Damp the overall energy a little more for a stable result. ++ Damp the notch energy a little less, so we don't damp out the ++ blip every time the phase reverses */ ++ det->channel_level += ((abs(amp) - det->channel_level) >> 5); ++ det->notch_level += ((abs(notched) - det->notch_level) >> 4); ++ if (det->channel_level > 280) ++ { ++ /* There is adequate energy in the channel. Is it mostly at 2100Hz? */ ++ if (det->notch_level*6 < det->channel_level) ++ { ++ /* The notch says yes, so we have the tone. */ ++ if (!det->tone_present) ++ { ++ /* Do we get a kick every 450+-25ms? */ ++ if (det->tone_cycle_duration >= 425*8 ++ && ++ det->tone_cycle_duration <= 475*8) ++ { ++ det->good_cycles++; ++ if (det->good_cycles > 2) ++ det->hit = TRUE; ++ } ++ det->tone_cycle_duration = 0; ++ } ++ det->tone_present = TRUE; ++ } ++ else ++ { ++ det->tone_present = FALSE; ++ } ++ det->tone_cycle_duration++; ++ } ++ else ++ { ++ det->tone_present = FALSE; ++ det->tone_cycle_duration = 0; ++ det->good_cycles = 0; ++ } ++ return det->hit; ++} ++/*- End of function --------------------------------------------------------*/ ++/*- End of file ------------------------------------------------------------*/ +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/ec.h mISDN/drivers/isdn/hardware/mISDN/ec.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/ec.h 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/ec.h 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,12 @@ ++ ++ ++ ++struct echo_can_s { ++ int echostate; ++ int echotimer; ++ int echolastupdate; ++ echo_can_state_t *ec; ++}; ++ ++ ++ +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/hfc_multi.c mISDN/drivers/isdn/hardware/mISDN/hfc_multi.c +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/hfc_multi.c 2005-01-31 18:24:03.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/hfc_multi.c 2005-12-02 09:57:08.000000000 +0100 +@@ -136,7 +136,7 @@ + static int nt_t1_count[] = { 480, 240, 120, 60, 30, 15, 8, 4 }; + #define CLKDEL_TE 0x0f /* CLKDEL in TE mode */ + #define CLKDEL_NT 0x0c /* CLKDEL in NT mode (0x60 MUST not be included!) */ +-static u_char silence = 0xff; /* silence by LAW */ ++static u_char mysilence = 0xff; /* silence by LAW */ + + /* enable 32 bit fifo access (PC usage) */ + #define FIFO_32BIT_ACCESS +@@ -903,11 +903,11 @@ + bch->tx_idx = bch->tx_len = 0; + } + /* now we have no more data, so in case of transparent, +- * we set the last byte in fifo to 'silence' in case we will get ++ * we set the last byte in fifo to 'mysilence' in case we will get + * no more data at all. this prevents sending an undefined value. + */ + if (!hdlc) +- HFC_outb_(hc, A_FIFO_DATA0_NOINC, silence); ++ HFC_outb_(hc, A_FIFO_DATA0_NOINC, mysilence); + } + + +@@ -1551,7 +1551,7 @@ + HFC_outb(hc, A_IRQ_MSK, 0); + HFC_outb(hc, R_INC_RES_FIFO, V_RES_F); + HFC_wait(hc); +- HFC_outb_(hc, A_FIFO_DATA0_NOINC, silence); /* tx silence */ ++ HFC_outb_(hc, A_FIFO_DATA0_NOINC, mysilence); /* tx silence */ + /* enable RX fifo */ + HFC_outb(hc, R_FIFO, (ch<<1)|1); + HFC_wait(hc); +@@ -1692,7 +1692,7 @@ + + /* if off */ + if (len <= 0) { +- HFC_outb_(hc, A_FIFO_DATA0_NOINC, silence); ++ HFC_outb_(hc, A_FIFO_DATA0_NOINC, mysilence); + if (hc->chan[ch].slot_tx>=0) { + if (debug & DEBUG_HFCMULTI_MODE) + printk(KERN_DEBUG "%s: connecting PCM due to no more TONE: channel %d slot_tx %d\n", __FUNCTION__, ch, hc->chan[ch].slot_tx); +@@ -2183,7 +2183,7 @@ + ret = 0; + break; + +- /* set silence */ ++ /* set mysilence */ + case HW_SPL_LOOP_OFF: + if (debug & DEBUG_HFCMULTI_MSG) + printk(KERN_DEBUG "%s: HW_SPL_LOOP_OFF\n", __FUNCTION__); +@@ -2799,7 +2799,13 @@ + if (debug & DEBUG_HFCMULTI_INIT) + printk(KERN_DEBUG "setup_pci(): investigating card entry %d (looking for type %d)\n", i, hc->type); + inuse: ++ ++#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,9) ++ tmp_dev = pci_get_subsys(id_list[i].vendor_id, id_list[i].device_id, id_list[i].vendor_sub, id_list[i].device_sub, tmp_dev); ++#else + tmp_dev = pci_find_subsys(id_list[i].vendor_id, id_list[i].device_id, id_list[i].vendor_sub, id_list[i].device_sub, tmp_dev); ++#endif ++ + if (tmp_dev) { + /* skip if already in use */ + list_for_each_entry_safe(hc_tmp, next, &HFCM_obj.ilist, list) { +@@ -3318,9 +3324,9 @@ + hc->type = type[HFC_cnt] & 0xff; + if (type[HFC_cnt] & 0x100) { + test_and_set_bit(HFC_CHIP_ULAW, &hc->chip); +- silence = 0xff; /* ulaw silence */ ++ mysilence = 0xff; /* ulaw silence */ + } else +- silence = 0x2a; /* alaw silence */ ++ mysilence = 0x2a; /* alaw silence */ + if (type[HFC_cnt] & 0x200) + test_and_set_bit(HFC_CHIP_DTMF, &hc->chip); + // if ((type[HFC_cnt]&0x400) && hc->type==4) +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/l3_udss1.c mISDN/drivers/isdn/hardware/mISDN/l3_udss1.c +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/l3_udss1.c 2005-03-26 11:21:39.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/l3_udss1.c 2005-12-02 09:57:08.000000000 +0100 +@@ -1202,6 +1202,14 @@ + err = check_infoelements(pc, skb, ie_PROGRESS); + if (err) + l3dss1_std_ie_err(pc, err); ++ /* START: patch by steinwej - http://www.beronet.com/bugs/bug_view_page.php?bug_id=0000095 */ ++ /* clear T310 if running */ ++ L3DelTimer(&pc->timer); ++ if (pc->t303skb) { ++ dev_kfree_skb(pc->t303skb); ++ pc->t303skb = NULL; ++ } ++ /* END */ + if (ERR_IE_COMPREHENSION != err) { + if (mISDN_l3up(pc, CC_PROGRESS | INDICATION, skb)) + dev_kfree_skb(skb); +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/Makefile mISDN/drivers/isdn/hardware/mISDN/Makefile +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/Makefile 2005-06-05 14:44:10.000000000 +0200 ++++ mISDN/drivers/isdn/hardware/mISDN/Makefile 2005-12-05 19:03:11.000000000 +0100 +@@ -30,6 +30,7 @@ + + ifdef CONFIG_MISDN_SPEEDFAX + obj-$(CONFIG_MISDN_DRV) += sedlfax.o ++obj-$(CONFIG_MISDN_DRV) += faxl3.o + endif + + ifdef CONFIG_MISDN_W6692 +@@ -70,8 +71,6 @@ + asn1_basic_service.o asn1_address.o asn1_enc.o capi_enc.o \ + supp_serv.o + mISDN_dtmf-objs := dtmf.o +-mISDN_dsp-objs := dsp_core.o dsp_cmx.o dsp_tones.o dsp_dtmf.o dsp_audio.o dsp_blowfish.o ++mISDN_dsp-objs := dsp_core.o dsp_cmx.o dsp_tones.o dsp_dtmf.o dsp_audio.o dsp_blowfish.o dsp_cancel.o + mISDN_x25dte-objs := x25_dte.o x25_l3.o + I4LmISDN-objs := i4l_mISDN.o +- +-include Rules.mISDN +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/Makefile.v2.6 mISDN/drivers/isdn/hardware/mISDN/Makefile.v2.6 +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/Makefile.v2.6 2005-06-05 14:44:10.000000000 +0200 ++++ mISDN/drivers/isdn/hardware/mISDN/Makefile.v2.6 2005-12-02 09:57:08.000000000 +0100 +@@ -71,6 +71,6 @@ + asn1_basic_service.o asn1_address.o asn1_enc.o capi_enc.o \ + supp_serv.o + mISDN_dtmf-objs := dtmf.o +-mISDN_dsp-objs := dsp_core.o dsp_cmx.o dsp_tones.o dsp_dtmf.o dsp_audio.o dsp_blowfish.o ++mISDN_dsp-objs := dsp_core.o dsp_cmx.o dsp_tones.o dsp_dtmf.o dsp_audio.o dsp_blowfish.o dsp_cancel.o + mISDN_x25dte-objs := x25_dte.o x25_l3.o + I4LmISDN-objs := i4l_mISDN.o +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/mec2_const.h mISDN/drivers/isdn/hardware/mISDN/mec2_const.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/mec2_const.h 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/mec2_const.h 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,25 @@ ++/* ++ Important constants for tuning mec2 echo can ++ */ ++#ifndef _MEC2_CONST_H ++#define _MEC2_CONST_H ++ ++ ++/* Convergence speed -- higher means slower */ ++#define DEFAULT_BETA1_I 2048 ++#define DEFAULT_SIGMA_LY_I 7 ++#define DEFAULT_SIGMA_LU_I 7 ++#define DEFAULT_ALPHA_ST_I 5 ++#define DEFAULT_ALPHA_YT_I 5 ++#define DEFAULT_CUTOFF_I 128 ++#define DEFAULT_HANGT 600 ++#define DEFAULT_SUPPR_I 16 ++#define MIN_UPDATE_THRESH_I 4096 ++#define DEFAULT_M 16 ++#define SUPPR_FLOOR -64 ++#define SUPPR_CEIL -24 ++#define RES_SUPR_FACTOR -20 ++#define AGGRESSIVE_HCNTR 160 /* 20ms */ ++ ++#endif /* _MEC2_CONST_H */ ++ +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/mec2.h mISDN/drivers/isdn/hardware/mISDN/mec2.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/mec2.h 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/mec2.h 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,409 @@ ++/* ++ * Mark's Second Echo Canceller ++ * ++ * Copyright (C) 2002, Digium, Inc. ++ * ++ * This program is free software and may be used and ++ * distributed according to the terms of the GNU ++ * General Public License, incorporated herein by ++ * reference. ++ * ++ */ ++#ifndef _MARK2_ECHO_H ++#define _MARK2_ECHO_H ++ ++#ifdef __KERNEL__ ++#include <linux/kernel.h> ++#include <linux/slab.h> ++#define MALLOC(a) kmalloc((a), GFP_KERNEL) ++#define FREE(a) kfree(a) ++#else ++#include <stdlib.h> ++#include <unistd.h> ++#include <stdint.h> ++#include <string.h> ++#include <math.h> ++#define MALLOC(a) malloc(a) ++#define FREE(a) free(a) ++#endif ++ ++/* Get optimized routines for math */ ++#include "arith.h" ++ ++#ifndef NULL ++#define NULL 0 ++#endif ++#ifndef FALSE ++#define FALSE 0 ++#endif ++#ifndef TRUE ++#define TRUE (!FALSE) ++#endif ++ ++#include "mec2_const.h" ++ ++/* Circular buffer definition */ ++typedef struct { ++ int idx_d; ++ int size_d; ++ short *buf_d; /* Twice as large as we need */ ++} echo_can_cb_s; ++ ++// class definition ++// ++typedef struct { ++ /* Echo canceller definition */ ++ ++ /* absolute time */ ++ int i_d; ++ ++ /* pre-computed constants */ ++ ++ int N_d; ++ int beta2_i; ++ ++ // declare accumulators for power computations ++ // ++ int Ly_i; ++ int Lu_i; ++ ++ // declare an accumulator for the near-end signal detector ++ // ++ int s_tilde_i; ++ int HCNTR_d; ++ ++ // circular buffers and coefficients ++ // ++ int *a_i; ++ short *a_s; ++ echo_can_cb_s y_s; ++ echo_can_cb_s s_s; ++ echo_can_cb_s u_s; ++ echo_can_cb_s y_tilde_s; ++ int y_tilde_i; ++ ++ /* Max memory */ ++ short max_y_tilde; ++ int max_y_tilde_pos; ++ ++} echo_can_state_t; ++ ++static inline void init_cb_s(echo_can_cb_s *cb, int len, void *where) ++{ ++ cb->buf_d = (short *)where; ++ cb->idx_d = 0; ++ cb->size_d = len; ++} ++ ++static inline void add_cc_s(echo_can_cb_s *cb, short newval) ++{ ++ /* Can't use modulus because N+M isn't a power of two (generally) */ ++ cb->idx_d--; ++ if (cb->idx_d < (int)0) ++ {cb->idx_d += cb->size_d;} ++ /* Load two copies into memory */ ++ cb->buf_d[cb->idx_d] = newval; ++ cb->buf_d[cb->idx_d + cb->size_d] = newval; ++} ++ ++static inline short get_cc_s(echo_can_cb_s *cb, int pos) ++{ ++ /* Load two copies into memory */ ++ return cb->buf_d[cb->idx_d + pos]; ++} ++ ++static inline void init_cc(echo_can_state_t *ec, int N, int maxy, int maxu) { ++ ++ void *ptr = ec; ++ unsigned long tmp; ++ /* double-word align past end of state */ ++ ptr += sizeof(echo_can_state_t); ++ tmp = (unsigned long)ptr; ++ tmp += 3; ++ tmp &= ~3L; ++ ptr = (void *)tmp; ++ ++ // reset parameters ++ // ++ ec->N_d = N; ++ ec->beta2_i = DEFAULT_BETA1_I; ++ ++ // allocate coefficient memory ++ // ++ ec->a_i = ptr; ++ ptr += (sizeof(int) * ec->N_d); ++ ec->a_s = ptr; ++ ptr += (sizeof(short) * ec->N_d); ++ ++ /* Reset Y circular buffer (short version) */ ++ init_cb_s(&ec->y_s, maxy, ptr); ++ ptr += (sizeof(short) * (maxy) * 2); ++ ++ /* Reset Sig circular buffer (short version for FIR filter) */ ++ init_cb_s(&ec->s_s, (1 << DEFAULT_ALPHA_ST_I), ptr); ++ ptr += (sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) * 2); ++ ++ init_cb_s(&ec->u_s, maxu, ptr); ++ ptr += (sizeof(short) * maxu * 2); ++ ++ // allocate a buffer for the reference signal power computation ++ // ++ init_cb_s(&ec->y_tilde_s, ec->N_d, ptr); ++ ++ ++ // reset absolute time ++ // ++ ec->i_d = (int)0; ++ ++ // reset the power computations (for y and u) ++ // ++ ec->Ly_i = DEFAULT_CUTOFF_I; ++ ec->Lu_i = DEFAULT_CUTOFF_I; ++ ++ // reset the near-end speech detector ++ // ++ ec->s_tilde_i = 0; ++ ec->HCNTR_d = (int)0; ++ ++ // exit gracefully ++ // ++} ++ ++static inline void echo_can_free(echo_can_state_t *ec) ++{ ++ FREE(ec); ++} ++ ++static inline short echo_can_update(echo_can_state_t *ec, short iref, short isig) { ++ ++ /* declare local variables that are used more than once ++ */ ++ int k; ++ int rs; ++ short u; ++ int Py_i; ++ int two_beta_i; ++ ++ /*************************************************************************** ++ // ++ // flow A on pg. 428 ++ // ++ ***************************************************************************/ ++ ++ /* eq. (16): high-pass filter the input to generate the next value; ++ // push the current value into the circular buffer ++ // ++ // sdc_im1_d = sdc_d; ++ // sdc_d = sig; ++ // s_i_d = sdc_d; ++ // s_d = s_i_d; ++ // s_i_d = (float)(1.0 - gamma_d) * s_i_d ++ + (float)(0.5 * (1.0 - gamma_d)) * (sdc_d - sdc_im1_d); */ ++ ++ ++ /* Delete last sample from power estimate */ ++ ec->y_tilde_i -= abs(get_cc_s(&ec->y_s, (1 << DEFAULT_ALPHA_YT_I) - 1 )) >> DEFAULT_ALPHA_YT_I; ++ /* push the reference data onto the circular buffer */ ++ add_cc_s(&ec->y_s, iref); ++ ++ /* eq. (2): compute r in fixed-point */ ++ rs = CONVOLVE2(ec->a_s, ec->y_s.buf_d + ec->y_s.idx_d, ec->N_d); ++ rs >>= 15; ++ ++ /* eq. (3): compute the output value (see figure 3) and the error ++ // note: the error is the same as the output signal when near-end ++ // speech is not present ++ */ ++ u = isig - rs; ++ ++ add_cc_s(&ec->u_s, u); ++ ++ ++ ++ /* Delete oldest part of received s_tilde */ ++ ec->s_tilde_i -= abs(get_cc_s(&ec->s_s, (1 << DEFAULT_ALPHA_ST_I) - 1 )); ++ ++ /* push the signal on the circular buffer, too */ ++ add_cc_s(&ec->s_s, isig); ++ ec->s_tilde_i += abs(isig); ++ ec->y_tilde_i += abs(iref) >> DEFAULT_ALPHA_YT_I; ++ ++ /* Add to our list of recent y_tilde's */ ++ add_cc_s(&ec->y_tilde_s, ec->y_tilde_i); ++ ++ /**************************************************************************** ++ // ++ // flow B on pg. 428 ++ // ++ ****************************************************************************/ ++ ++ /* compute the new convergence factor ++ */ ++ Py_i = (ec->Ly_i >> DEFAULT_SIGMA_LY_I) * (ec->Ly_i >> DEFAULT_SIGMA_LY_I); ++ Py_i >>= 15; ++ if (ec->HCNTR_d > 0) { ++ Py_i = (1 << 15); ++ } ++ ++#if 0 ++ printf("Py: %e, Py_i: %e\n", Py, Py_i * AMPL_SCALE_1); ++#endif ++ ++ /* Vary rate of adaptation depending on position in the file ++ // Do not do this for the first (DEFAULT_UPDATE_TIME) secs after speech ++ // has begun of the file to allow the echo cancellor to estimate the ++ // channel accurately ++ */ ++#if 0 ++ if (ec->start_speech_d != 0 ){ ++ if ( ec->i_d > (DEFAULT_T0 + ec->start_speech_d)*(SAMPLE_FREQ) ){ ++ ec->beta2_d = max_cc_float(MIN_BETA, ++ DEFAULT_BETA1 * exp((-1/DEFAULT_TAU)*((ec->i_d/(float)SAMPLE_FREQ) - ++ DEFAULT_T0 - ++ ec->start_speech_d))); ++ } ++ } ++ else {ec->beta2_d = DEFAULT_BETA1;} ++#endif ++ ++ ec->beta2_i = DEFAULT_BETA1_I; /* Fixed point, inverted */ ++ ++ two_beta_i = (ec->beta2_i * Py_i) >> 15; /* Fixed point version, inverted */ ++ if (!two_beta_i) ++ two_beta_i++; ++ ++ /* Update Lu_i (Suppressed power estimate) */ ++ ec->Lu_i -= abs(get_cc_s(&ec->u_s, (1 << DEFAULT_SIGMA_LU_I) - 1 )) ; ++ ec->Lu_i += abs(u); ++ ++ /* eq. (10): update power estimate of the reference ++ */ ++ ec->Ly_i -= abs(get_cc_s(&ec->y_s, (1 << DEFAULT_SIGMA_LY_I) - 1)) ; ++ ec->Ly_i += abs(iref); ++ ++ if (ec->Ly_i < DEFAULT_CUTOFF_I) ++ ec->Ly_i = DEFAULT_CUTOFF_I; ++ ++#if 0 ++ printf("Float: %e, Int: %e\n", ec->Ly_d, (ec->Ly_i >> DEFAULT_SIGMA_LY_I) * AMPL_SCALE_1); ++#endif ++ ++ if (ec->y_tilde_i > ec->max_y_tilde) { ++ /* New highest y_tilde with full life */ ++ ec->max_y_tilde = ec->y_tilde_i; ++ ec->max_y_tilde_pos = ec->N_d - 1; ++ } else if (--ec->max_y_tilde_pos < 0) { ++ /* Time to find new max y tilde... */ ++ ec->max_y_tilde = MAX16(ec->y_tilde_s.buf_d + ec->y_tilde_s.idx_d, ec->N_d, &ec->max_y_tilde_pos); ++ } ++ ++ if ((ec->s_tilde_i >> (DEFAULT_ALPHA_ST_I - 1)) > ec->max_y_tilde) ++ { ++ ec->HCNTR_d = DEFAULT_HANGT; ++ } ++ else if (ec->HCNTR_d > (int)0) ++ { ++ ec->HCNTR_d--; ++ } ++ ++ /* update coefficients if no near-end speech and we have enough signal ++ * to bother trying to update. ++ */ ++ if (!ec->HCNTR_d && !(ec->i_d % DEFAULT_M) && ++ (ec->Lu_i > MIN_UPDATE_THRESH_I)) { ++ // loop over all filter coefficients ++ // ++ for (k=0; k<ec->N_d; k++) { ++ ++ // eq. (7): compute an expectation over M_d samples ++ // ++ int grad2; ++ grad2 = CONVOLVE2(ec->u_s.buf_d + ec->u_s.idx_d, ++ ec->y_s.buf_d + ec->y_s.idx_d + k, DEFAULT_M); ++ // eq. (7): update the coefficient ++ // ++ ec->a_i[k] += grad2 / two_beta_i; ++ ec->a_s[k] = ec->a_i[k] >> 16; ++ } ++ } ++ ++ /* paragraph below eq. (15): if no near-end speech, ++ // check for residual error suppression ++ */ ++#ifndef NO_ECHO_SUPPRESSOR ++#ifdef AGGRESSIVE_SUPPRESSOR ++ if ((ec->HCNTR_d < AGGRESSIVE_HCNTR) && (ec->Ly_i > (ec->Lu_i << 1))) { ++ u = u * (ec->Lu_i >> DEFAULT_SIGMA_LU_I) / ((ec->Ly_i >> (DEFAULT_SIGMA_LY_I)) + 1); ++ u = u * (ec->Lu_i >> DEFAULT_SIGMA_LU_I) / ((ec->Ly_i >> (DEFAULT_SIGMA_LY_I)) + 1); ++ } ++#else ++ if ((ec->HCNTR_d == 0) && ((ec->Ly_i/(ec->Lu_i + 1)) > DEFAULT_SUPPR_I)) { ++ u = u * (ec->Lu_i >> DEFAULT_SIGMA_LU_I) / ((ec->Ly_i >> (DEFAULT_SIGMA_LY_I + 2)) + 1); ++ } ++#endif ++#endif ++ ++#if 0 ++ if ((ec->HCNTR_d == 0) && ((ec->Lu_d/ec->Ly_d) < DEFAULT_SUPPR) && ++ (ec->Lu_d/ec->Ly_d > EC_MIN_DB_VALUE)) { ++ suppr_factor = (10/(float)(SUPPR_FLOOR-SUPPR_CEIL))*log(ec->Lu_d/ec->Ly_d) ++ - SUPPR_CEIL/(float)(SUPPR_FLOOR - SUPPR_CEIL); ++ ++ u_suppr = pow(10.0,(suppr_factor)*RES_SUPR_FACTOR/10.0)*u_suppr; ++ ++ } ++#endif ++ ec->i_d++; ++ return u; ++} ++ ++static inline echo_can_state_t *echo_can_create(int len, int adaption_mode) ++{ ++ echo_can_state_t *ec; ++ int maxy; ++ int maxu; ++ maxy = len + DEFAULT_M; ++ maxu = DEFAULT_M; ++ if (maxy < (1 << DEFAULT_ALPHA_YT_I)) ++ maxy = (1 << DEFAULT_ALPHA_YT_I); ++ if (maxy < (1 << DEFAULT_SIGMA_LY_I)) ++ maxy = (1 << DEFAULT_SIGMA_LY_I); ++ if (maxu < (1 << DEFAULT_SIGMA_LU_I)) ++ maxu = (1 << DEFAULT_SIGMA_LU_I); ++ ec = (echo_can_state_t *)MALLOC(sizeof(echo_can_state_t) + ++ 4 + /* align */ ++ sizeof(int) * len + /* a_i */ ++ sizeof(short) * len + /* a_s */ ++ 2 * sizeof(short) * (maxy) + /* y_s */ ++ 2 * sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) + /* s_s */ ++ 2 * sizeof(short) * (maxu) + /* u_s */ ++ 2 * sizeof(short) * len); /* y_tilde_s */ ++ if (ec) { ++ memset(ec, 0, sizeof(echo_can_state_t) + ++ 4 + /* align */ ++ sizeof(int) * len + /* a_i */ ++ sizeof(short) * len + /* a_s */ ++ 2 * sizeof(short) * (maxy) + /* y_s */ ++ 2 * sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) + /* s_s */ ++ 2 * sizeof(short) * (maxu) + /* u_s */ ++ 2 * sizeof(short) * len); /* y_tilde_s */ ++ init_cc(ec, len, maxy, maxu); ++ } ++ return ec; ++} ++ ++static inline int echo_can_traintap(echo_can_state_t *ec, int pos, short val) ++{ ++ /* Reset hang counter to avoid adjustments after ++ initial forced training */ ++ ec->HCNTR_d = ec->N_d << 1; ++ if (pos >= ec->N_d) ++ return 1; ++ ec->a_i[pos] = val << 17; ++ ec->a_s[pos] = val << 1; ++ if (++pos >= ec->N_d) ++ return 1; ++ return 0; ++} ++ ++#endif +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/mec3.h mISDN/drivers/isdn/hardware/mISDN/mec3.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/mec3.h 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/mec3.h 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,243 @@ ++/* ++ * Mark's Third Echo Canceller ++ * ++ * Copyright (C) 2003, Digium, Inc. ++ * ++ * This program is free software and may be used ++ * and distributed under the terms of the GNU General Public ++ * License, incorporated herein by reference. ++ * ++ * Dedicated to the crew of the Columbia, STS-107 for their ++ * bravery and courageous sacrifice for science. ++ * ++ */ ++ ++#ifndef _MARK3_ECHO_H ++#define _MARK3_ECHO_H ++ ++ ++ ++#ifdef __KERNEL__ ++#include <linux/kernel.h> ++#include <linux/slab.h> ++#define MALLOC(a) kmalloc((a), GFP_KERNEL) ++#define FREE(a) kfree(a) ++#else ++#include <stdlib.h> ++#include <unistd.h> ++#include <stdint.h> ++#include <string.h> ++#include <math.h> ++#define MALLOC(a) malloc(a) ++#define FREE(a) free(a) ++#endif ++ ++/* Features */ ++ ++/* ++ * DO_BACKUP -- Backup coefficients, and revert in the presense of double talk to try to prevent ++ * them from diverging during the ramp-up before the DTD kicks in ++ */ ++/* #define DO_BACKUP */ ++ ++#define STEP_SHIFT 2 /* Convergence rate higher = slower / better (as a shift) */ ++ ++#define SIGMA_REF_PWR 655 /* Keep denominator from being 0 */ ++ ++#define MIN_TX_ENERGY 256 /* Must have at least this much reference */ ++#define MIN_RX_ENERGY 32 /* Must have at least this much receive energy */ ++ ++#define MAX_ATTENUATION_SHIFT 6 /* Maximum amount of loss we care about */ ++#define MAX_BETA 1024 ++ ++#define SUPPR_SHIFT 4 /* Amount of loss at which we suppress audio */ ++ ++#define HANG_TIME 600 /* Hangover time */ ++ ++#define NTAPS 2048 /* Maximum number of echo can taps */ ++ ++#define BACKUP 256 /* Backup every this number of samples */ ++ ++#define POWER_OFFSET 5 /* Shift power by this amount to be sure we don't overflow the ++ reference power. Higher = less likely to overflow, lower = more accurage */ ++ ++#include "arith.h" ++ ++typedef struct { ++ short buf[NTAPS * 2]; ++ short max; ++ int maxexp; ++} cbuf_s; ++ ++typedef struct { ++ short a_s[NTAPS]; /* Coefficients in shorts */ ++ int a_i[NTAPS]; /* Coefficients in ints*/ ++#ifdef DO_BACKUP ++ int b_i[NTAPS]; /* Coefficients (backup1) */ ++ int c_i[NTAPS]; /* Coefficients (backup2) */ ++#endif ++ cbuf_s ref; /* Reference excitation */ ++ cbuf_s sig; /* Signal (echo + near end + noise) */ ++ cbuf_s e; /* Error */ ++ int refpwr; /* Reference power */ ++ int taps; /* Number of taps */ ++ int tappwr; /* Power of taps */ ++ int hcntr; /* Hangtime counter */ ++ int pos; /* Position in curcular buffers */ ++ int backup; /* Backup timer */ ++} echo_can_state_t; ++ ++static inline void echo_can_free(echo_can_state_t *ec) ++{ ++ FREE(ec); ++} ++ ++static inline void buf_add(cbuf_s *b, short sample, int pos, int taps) ++{ ++ /* Store and keep track of maxima */ ++ int x; ++ b->buf[pos] = sample; ++ b->buf[pos + taps] = sample; ++ if (sample > b->max) { ++ b->max = sample; ++ b->maxexp = taps; ++ } else { ++ b->maxexp--; ++ if (!b->maxexp) { ++ b->max = 0; ++ for (x=0;x<taps;x++) ++ if (b->max < abs(b->buf[pos + x])) { ++ b->max = abs(b->buf[pos + x]); ++ b->maxexp = x + 1; ++ } ++ } ++ } ++} ++ ++static inline short echo_can_update(echo_can_state_t *ec, short ref, short sig) ++{ ++ int x; ++ short u; ++ int refpwr; ++ int beta; /* Factor */ ++ int se; /* Simulated echo */ ++#ifdef DO_BACKUP ++ if (!ec->backup) { ++ /* Backup coefficients periodically */ ++ ec->backup = BACKUP; ++ memcpy(ec->c_i,ec->b_i,sizeof(ec->c_i)); ++ memcpy(ec->b_i,ec->a_i,sizeof(ec->b_i)); ++ } else ++ ec->backup--; ++#endif ++ /* Remove old samples from reference power calculation */ ++ ec->refpwr -= ((ec->ref.buf[ec->pos] * ec->ref.buf[ec->pos]) >> POWER_OFFSET); ++ ++ /* Store signal and reference */ ++ buf_add(&ec->ref, ref, ec->pos, ec->taps); ++ buf_add(&ec->sig, sig, ec->pos, ec->taps); ++ ++ /* Add new reference power */ ++ ec->refpwr += ((ec->ref.buf[ec->pos] * ec->ref.buf[ec->pos]) >> POWER_OFFSET); ++ ++ ++ /* Calculate simulated echo */ ++ se = CONVOLVE2(ec->a_s, ec->ref.buf + ec->pos, ec->taps); ++ se >>= 15; ++ ++ u = sig - se; ++ if (ec->hcntr) ++ ec->hcntr--; ++ ++ /* Store error */ ++ buf_add(&ec->e, sig, ec->pos, ec->taps); ++ if ((ec->ref.max > MIN_TX_ENERGY) && ++ (ec->sig.max > MIN_RX_ENERGY) && ++ (ec->e.max > (ec->ref.max >> MAX_ATTENUATION_SHIFT))) { ++ /* We have sufficient energy */ ++ if (ec->sig.max < (ec->ref.max >> 1)) { ++ /* No double talk */ ++ if (!ec->hcntr) { ++ refpwr = ec->refpwr >> (16 - POWER_OFFSET); ++ if (refpwr < SIGMA_REF_PWR) ++ refpwr = SIGMA_REF_PWR; ++ beta = (u << 16) / refpwr; ++ beta >>= STEP_SHIFT; ++ if (beta > MAX_BETA) ++ beta = 0; ++ if (beta < -MAX_BETA) ++ beta = 0; ++ /* Update coefficients */ ++ for (x=0;x<ec->taps;x++) { ++ ec->a_i[x] += beta * ec->ref.buf[ec->pos + x]; ++ ec->a_s[x] = ec->a_i[x] >> 16; ++ } ++ } ++ } else { ++#ifdef DO_BACKUP ++ if (!ec->hcntr) { ++ /* Our double talk detector is turning on for the first time. Revert ++ our coefficients, since we're probably well into the double talk by now */ ++ memcpy(ec->a_i, ec->c_i, sizeof(ec->a_i)); ++ for (x=0;x<ec->taps;x++) { ++ ec->a_s[x] = ec->a_i[x] >> 16; ++ } ++ } ++#endif ++ /* Reset hang-time counter, and prevent backups */ ++ ec->hcntr = HANG_TIME; ++#ifdef DO_BACKUP ++ ec->backup = BACKUP; ++#endif ++ } ++ } ++#ifndef NO_ECHO__SUPPRESSOR ++ if (ec->e.max < (ec->ref.max >> SUPPR_SHIFT)) { ++ /* Suppress residual echo */ ++ u *= u; ++ u >>= 16; ++ } ++#endif ++ ec->pos--; ++ if (ec->pos < 0) ++ ec->pos = ec->taps-1; ++ return u; ++} ++ ++static inline echo_can_state_t *echo_can_create(int taps, int adaption_mode) ++{ ++ echo_can_state_t *ec; ++ int x; ++ ++ //taps = NTAPS; ++ ec = MALLOC(sizeof(echo_can_state_t)); ++ if (ec) { ++ memset(ec, 0, sizeof(echo_can_state_t)); ++ ec->taps = taps; ++ ec->pos = ec->taps-1; ++ for (x=0;x<31;x++) { ++ if ((1 << x) >= ec->taps) { ++ ec->tappwr = x; ++ break; ++ } ++ } ++ } ++ return ec; ++} ++ ++static inline int echo_can_traintap(echo_can_state_t *ec, int pos, short val) ++{ ++ /* Reset hang counter to avoid adjustments after ++ initial forced training */ ++ ec->hcntr = ec->taps << 1; ++ if (pos >= ec->taps) ++ return 1; ++ ec->a_i[pos] = val << 17; ++ ec->a_s[pos] = val << 1; ++ if (++pos >= ec->taps) ++ return 1; ++ return 0; ++} ++ ++ ++#endif +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/mec.h mISDN/drivers/isdn/hardware/mISDN/mec.h +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/mec.h 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/drivers/isdn/hardware/mISDN/mec.h 2005-12-02 09:57:08.000000000 +0100 +@@ -0,0 +1,308 @@ ++/* ++ * Mark's Echo Canceller ++ * ++ * Mark Spencer <markster@linux-support.net> ++ * ++ * Simple, LMS Echo Canceller with double talk detection. ++ * Partly based on the TI App note: ++ * "Digital Voice Echo Canceller with a TMS 32020" ++ * ++ * Special additional thanks to: ++ * Jim Dixon (Lambda Telecommunications) ++ * Iman Ghobrial (Adtran, Inc.) ++ * ++ * Copyright (C) 2001, Linux Support Services, Inc. ++ * ++ * This program is free software and may be used and ++ * distributed according to the terms of the GNU ++ * General Public License, incorporated herein by ++ * reference. ++ * ++ */ ++ ++#ifndef _MEC_H ++#define _MEC_H ++ ++/* You have to express the size of the echo canceller in taps as ++ a power of 2 (6 = 64 taps, 7 = 128 taps, 8 = 256 taps) */ ++#define NUM_TAPS_POW2 6 /* Size of echo canceller in power of 2 (taps) */ ++#define NUM_TAPS (1 << NUM_TAPS_POW2) /* Actual number of taps */ ++#define TAP_MASK (NUM_TAPS-1) ++ ++ ++#define SIGMA_LU_POW NUM_TAPS_POW2 ++#define SIGMA_LY_POW NUM_TAPS_POW2 ++#define SIGMA_YT_POW (NUM_TAPS_POW2 - 1) ++#define SIGMA_ST_POW (NUM_TAPS_POW2 - 1) ++ ++#define BETA_POW 8 ++ ++#define CUTOFF_S 4 ++ ++/* The higher you make this, the better the quality, but the more CPU time required */ ++#define MIN_QUALITY 100 ++ ++/* This optimization saves a lot of processor but may degrade quality */ ++#define OPTIMIZEDIV ++ ++#if 0 ++/* This converges much more slowly but saves processor */ ++#define MIN_UPDATE 256 ++#define MIN_SKIP 8 ++#endif ++ ++#define HANG_T 600 /* 600 samples, or 75ms */ ++ ++typedef struct mark_ec { ++ /* Circular position */ ++ int cpos; ++ short y[NUM_TAPS]; /* Last N samples (relative to cpos) transmitted */ ++ short y_abs[NUM_TAPS]; /* Last N samples (relative to cpos) transmitted (abs value) */ ++ short s[NUM_TAPS]; /* Last N samples (relative to cpos) received */ ++ short s_abs[NUM_TAPS]; /* Last N samples (relative to cpos) received (abs value) */ ++ short u[NUM_TAPS]; /* Last N samples (relative to cpos) with echo removed */ ++ short u_abs[NUM_TAPS]; /* Last N samples (relative to cpos) with echo removed */ ++ ++ int Ly; /* tx power */ ++ int Lu; /* Power of echo-cancelled output */ ++ ++ int Ty[NUM_TAPS]; /* Short term power estimate of transmit */ ++ int Ts; /* Short term power estimate of received signal */ ++ ++ int a[NUM_TAPS]; /* Tap weight coefficients (not relative) */ ++ ++ short sdc[NUM_TAPS]; /* Near end signal before High Pass Filter */ ++ ++ int samples; /* Sample count */ ++ int pass; /* Number of passes we've made */ ++ ++ int hangt; ++ ++ int lastmax; /* Optimize maximum search */ ++ int maxTy; /* Maximum Ty */ ++} echo_can_state_t; ++ ++#define INLINE inline ++ ++#ifdef __KERNEL__ ++#include <linux/kernel.h> ++#include <linux/slab.h> ++#define MALLOC(a) kmalloc((a), GFP_KERNEL) ++#define FREE(a) kfree((a)) ++#else ++#include <stdlib.h> ++#include <unistd.h> ++#include <stdint.h> ++#include <string.h> ++#define MALLOC(a) malloc(a) ++#define FREE(a) free(a) ++#endif ++ ++static INLINE echo_can_state_t *echo_can_create(int len, int adaption_mode) ++{ ++ echo_can_state_t *ec; ++ /* Uhm, we're only one length, sorry. */ ++ ec = MALLOC(sizeof(echo_can_state_t)); ++ if (ec) ++ memset(ec, 0, sizeof(*ec)); ++ return ec; ++} ++ ++#define PASSPOS 32000 ++#undef PASSPOS ++ ++static INLINE void echo_can_free(echo_can_state_t *ec) ++{ ++ FREE(ec); ++} ++ ++static INLINE int16_t echo_can_update(echo_can_state_t *ec, int16_t tx, int16_t rx) ++{ ++ /* Process a sample, where tx is the near end and rx is the far end + echo */ ++ ++ int suppr; ++ int nsuppr; ++ short rxabs, txabs; ++ register int Lu; ++ register int x; ++ register int pos; ++ register int r_hat; /* Estimated echo */ ++ int oldrxabs; ++ int oldtxabs; ++ int oldsupprabs; ++ int supprabs; ++#ifdef MIN_UPDATE ++ int totalupd; ++#endif ++ ++ txabs = abs(tx); ++ rxabs = abs(rx); ++ ++ ec->pass++; ++ ++ r_hat = 0; ++ ++ /* Load next value */ ++ ec->y[ec->cpos] = tx; ++ ++ /* Load next abs value */ ++ oldtxabs = ec->y_abs[ec->cpos]; ++ ec->y_abs[ec->cpos] = txabs; ++ ++ /* Bring in receive value (near-end signal) */ ++ ec->sdc[ec->cpos] = rx; ++ ++ /* Bring in receive value absolute value */ ++ oldrxabs = ec->s_abs[ec->cpos]; ++ ec->s_abs[ec->cpos] = rxabs; ++ ++ Lu = ec->Lu | 1; ++ ++#if 0 ++ /* Apply first order high pass filter (3 dB @ 160 Hz) */ ++ tx = ec->s[ec->cpos] = (1.0-DEFGAMMA) * ec->s[(ec->cpos - 1) & TAP_MASK] + ++ 0.5 * (1.0-DEFGAMMA) * ( ec->sdc[(ec->cpos - 1) & TAP_MASK] - ec->sdc[(ec->cpos - 2) & TAP_MASK]); ++#endif ++ ++ /* Estimate echo */ ++ pos = ec->cpos; ++ for (x=0;x<NUM_TAPS;x++) { ++ r_hat += ec->a[x] * ec->y[pos]; ++ /* Go backwards in time and loop around circular buffer */ ++ pos = (pos - 1) & TAP_MASK; ++ } ++ ++ r_hat >>= 16; ++ ++ if (ec->hangt > 0) ++ ec->hangt--; ++ ++ /* printf("rx: %F, rhat: %F\n", rx, r_hat); */ ++ /* Calculate suppressed amount */ ++ suppr = rx - r_hat; ++ ++ if (ec->pass > NUM_TAPS) { ++ /* Have to have enough taps to start with */ ++ if (ec->maxTy > ec->Ts) { ++ /* There is no near-end speech detected */ ++ if (!ec->hangt) { ++ /* We're not in the hang-time from the end of near-end speech */ ++ if ((ec->Ly > 1024) && ((ec->Ly / Lu) < MIN_QUALITY)) { ++#ifdef OPTIMIZEDIV ++ /* We both have enough signal on the transmit */ ++ nsuppr = (suppr << 18) / ec->Ly; ++ ++ if (nsuppr > 32767) ++ nsuppr = 32767; ++ if (nsuppr < -32768) ++ nsuppr = -32768; ++ ++ nsuppr /= ec->Ly; ++#else ++ /* We both have enough signal on the transmit */ ++ nsuppr = (suppr << 16) / ec->Ly; ++ ++ if (nsuppr > 32767) ++ nsuppr = 32767; ++ if (nsuppr < -32768) ++ nsuppr = -32768; ++ ++#endif ++ ++ /* Update coefficients */ ++ pos = ec->cpos; ++#ifdef MIN_UPDATE ++ totalupd =0; ++#endif ++ for (x=0;x<NUM_TAPS;x++) { ++ register int adj; ++ adj = ec->y[pos] * nsuppr; ++#ifndef OPTIMIZEDIV ++ adj /= ec->Ly; ++ adj >>= BETA_POW; ++#else ++ adj >>= BETA_POW + 2; ++#endif ++#ifdef PASSPOS ++ if (ec->pass > PASSPOS) ++ printf("tx: %d, old %d: %d, adj %d, nsuppr: %d, power: %d\n", tx, x, ec->a[x], adj, nsuppr, ec->Ly); ++#endif ++ ec->a[x] += adj; ++#ifdef MIN_UPDATE ++ totalupd += abs(adj); ++#endif ++ /* Go backwards in time and loop around circular buffer */ ++ pos = (pos - 1) & TAP_MASK; ++ } ++#ifdef MIN_UPDATE ++ /* If we didn't update at least this much, delay for many more taps */ ++ if (totalupd < MIN_UPDATE) { ++ ec->hangt += MIN_SKIP; ++ } ++#endif ++ } ++ ++ } ++ } else ++ /* Near end speech detected */ ++ ec->hangt = HANG_T; ++ } ++ ++ /* Save supression and absolute values */ ++ supprabs = abs(suppr); ++ oldsupprabs = ec->u_abs[ec->cpos]; ++ ec->u[ec->cpos] = suppr; ++ ec->u_abs[ec->cpos] = supprabs; ++ ++ /* Update tx power */ ++ ec->Ly += (txabs >> SIGMA_LY_POW) - (oldtxabs >> SIGMA_LY_POW); ++ ++ /* Update rx power */ ++ ec->Lu += (supprabs >> SIGMA_LU_POW) - (oldsupprabs >> SIGMA_LU_POW); ++ ++ /* Short term power of tx */ ++ ec->Ty[ec->cpos] = ec->Ty[(ec->cpos - 1) & TAP_MASK] + ++ ((txabs >> SIGMA_YT_POW ) - (oldtxabs >> SIGMA_YT_POW)); ++ ++ /* Keep track of highest */ ++ if (ec->lastmax == ec->cpos) { ++ register int maxTy = 0; ++ /* Have to loop through and find the new highest since our old highest expired */ ++ /* Estimate echo */ ++ pos = ec->cpos; ++ for (x=0;x<NUM_TAPS;x++) { ++ if (ec->Ty[pos] > maxTy) ++ maxTy = ec->Ty[pos]; ++ /* Go backwards in time and loop around circular buffer */ ++ pos = (pos - 1) & TAP_MASK; ++ } ++ ec->maxTy = maxTy; ++ } else { ++ /* Just keep the highest */ ++ if (ec->Ty[ec->cpos] > ec->maxTy) { ++ ec->maxTy = ec->Ty[ec->cpos]; ++ ec->lastmax = ec->cpos; ++ } ++ } ++ ec->Ts += (rxabs >> SIGMA_ST_POW) - (oldrxabs >> SIGMA_ST_POW) ; ++ ++ /* Increment position memory */ ++ ec->cpos = (ec->cpos + 1 ) & TAP_MASK; ++ ++ return suppr; ++} ++ ++static inline int echo_can_traintap(echo_can_state_t *ec, int pos, short val) ++{ ++ /* Reset hang counter to avoid adjustments after ++ initial forced training */ ++ ec->hangt = NUM_TAPS << 1; ++ if (pos >= NUM_TAPS) ++ return 1; ++ ec->a[pos] = val << 17; ++ if (++pos >= NUM_TAPS) ++ return 1; ++ return 0; ++} ++ ++#endif +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/sedl_fax.c mISDN/drivers/isdn/hardware/mISDN/sedl_fax.c +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/sedl_fax.c 2004-08-27 21:27:40.000000000 +0200 ++++ mISDN/drivers/isdn/hardware/mISDN/sedl_fax.c 2005-12-02 09:57:08.000000000 +0100 +@@ -811,8 +811,8 @@ + return(err); + } + +- printk(KERN_INFO "mISDN: sedlpci found adapter %s at %s\n", +- (char *) ent->driver_data, pdev->slot_name); ++/* printk(KERN_INFO "mISDN: sedlpci found adapter %s at %s\n", ++ (char *) ent->driver_data, pdev->slot_name); */ + + card->cfg = pci_resource_start(pdev, 0); + card->irq = pdev->irq; +diff -u -r -P /tmp/mISDN/drivers/isdn/hardware/mISDN/w6692.c mISDN/drivers/isdn/hardware/mISDN/w6692.c +--- /tmp/mISDN/drivers/isdn/hardware/mISDN/w6692.c 2004-08-27 21:27:40.000000000 +0200 ++++ mISDN/drivers/isdn/hardware/mISDN/w6692.c 2005-12-02 09:57:08.000000000 +0100 +@@ -1502,8 +1502,8 @@ + return(err); + } + +- printk(KERN_INFO "mISDN_w6692: found adapter %s at %s\n", +- (char *) ent->driver_data, pdev->slot_name); ++/* printk(KERN_INFO "mISDN_w6692: found adapter %s at %s\n", ++ (char *) ent->driver_data, pdev->slot_name); */ + + card->addr = pci_resource_start(pdev, 1); + card->irq = pdev->irq; +diff -u -r -P /tmp/mISDN/include/linux/mISDNif.h mISDN/include/linux/mISDNif.h +--- /tmp/mISDN/include/linux/mISDNif.h 2005-02-05 11:18:17.000000000 +0100 ++++ mISDN/include/linux/mISDNif.h 2005-12-02 09:57:08.000000000 +0100 +@@ -173,6 +173,8 @@ + #define BF_DISABLE 0x2315 + #define BF_ACCEPT 0x2316 + #define BF_REJECT 0x2317 ++#define ECHOCAN_ON 0x2318 ++#define ECHOCAN_OFF 0x2319 + #define HW_POTS_ON 0x1001 + #define HW_POTS_OFF 0x1002 + #define HW_POTS_SETMICVOL 0x1100 +diff -u -r -P /tmp/mISDN/Makefile mISDN/Makefile +--- /tmp/mISDN/Makefile 1970-01-01 01:00:00.000000000 +0100 ++++ mISDN/Makefile 2005-12-05 19:08:57.000000000 +0100 +@@ -0,0 +1,54 @@ ++BASEDIR=$(shell pwd) ++ ++ ++INSTALL_PREFIX := / ++export INSTALL_PREFIX ++ ++#PATH to linux source/headers ++#LINUX=/usr/src/linux ++LINUX=/lib/modules/$(shell uname -r)/build ++ ++MISDNDIR=$(BASEDIR) ++MISDN_SRC=$(MISDNDIR)/drivers/isdn/hardware/mISDN ++ ++######################################## ++# USER CONFIGS END ++######################################## ++ ++CONFIGS+=CONFIG_MISDN_DRV=m CONFIG_MISDN_DSP=m ++CONFIGS+=CONFIG_MISDN_HFCMULTI=m ++CONFIGS+=CONFIG_MISDN_HFCPCI=m ++CONFIGS+=CONFIG_MISDN_HFCUSB=m ++#CONFIGS+=CONFIG_MISDN_AVM_FRITZ=m ++ ++ ++MINCLUDES+=-I$(MISDNDIR)/include ++ ++all: ++ @echo ++ @echo "Makeing mISDN" ++ @echo "=============" ++ @echo ++ cp $(MISDNDIR)/drivers/isdn/hardware/mISDN/Makefile.v2.6 $(MISDNDIR)/drivers/isdn/hardware/mISDN/Makefile ++ ++ cd $(LINUX) ; make SUBDIRS=$(MISDN_SRC) modules $(CONFIGS) LINUXINCLUDE="$(MINCLUDES) -I$(LINUX)/include" ++ ++ ++ ++install: all ++ cd $(LINUX) ; make SUBDIRS=$(MISDN_SRC) modules_install ++ cp $(MISDNDIR)/include/linux/*.h $(INSTALL_PREFIX)/usr/include/linux/ ++ depmod ++ ++.PHONY: install all clean ++ ++clean: ++ rm -rf drivers/isdn/hardware/mISDN/*.o ++ rm -rf drivers/isdn/hardware/mISDN/*.ko ++ rm -rf *~ ++ find . -iname ".*.cmd" -exec rm -rf {} \; ++ find . -iname ".*.d" -exec rm -rf {} \; ++ find . -iname "*.mod.c" -exec rm -rf {} \; ++ find . -iname "*.mod" -exec rm -rf {} \; ++ ++ diff --git a/1.2-netsec/channels/misdn/mISDNuser.patch b/1.2-netsec/channels/misdn/mISDNuser.patch new file mode 100644 index 000000000..a5a70acc8 --- /dev/null +++ b/1.2-netsec/channels/misdn/mISDNuser.patch @@ -0,0 +1,271 @@ +diff -u -r -P /tmp/mISDNuser/example/Makefile mISDNuser/example/Makefile +--- /tmp/mISDNuser/example/Makefile 2004-08-28 14:31:02.000000000 +0200 ++++ mISDNuser/example/Makefile 2005-12-05 18:57:10.000000000 +0100 +@@ -3,6 +3,11 @@ + + all: $(PROGS) + ++install: ++ for i in $(PROGS) ; do \ ++ install -m 755 $$i $(INSTALL_PREFIX)/usr/bin ;\ ++ done ++ + testcon: testcon.o $(mISDNLIB) + + testnet: testnet.o $(mISDNLIB) +@@ -26,7 +31,7 @@ + + + clean: +- rm -f *.o *~ DEADJOE ++ rm -f *.o *.so *~ DEADJOE + rm -f loadfirm logger testcon testcon_l2 testnet + + distclean: clean +diff -u -r -P /tmp/mISDNuser/i4lnet/Makefile mISDNuser/i4lnet/Makefile +--- /tmp/mISDNuser/i4lnet/Makefile 2004-08-28 14:27:53.000000000 +0200 ++++ mISDNuser/i4lnet/Makefile 2005-12-05 18:57:40.000000000 +0100 +@@ -1,9 +1,18 @@ + +-all: libisdnnet.a ++all: libisdnnet.a libisdnnet.so ++ ++install: ++ install -m 644 libisdnnet.so $(INSTALL_PREFIX)/usr/lib/ ++ cp *.h $(INSTALL_PREFIX)/usr/include/mISDNuser/ ++ + + ISDNNETOBJ = net_if.o isdn_debug.o isdn_msg.o fsm.o net_l2.o tei.o net_l3.o \ + manager.o tone.o bchannel.o g711.o + ++libisdnnet.so: $(ISDNNETOBJ) ++ rm -f $@ ++ $(CC) -shared -Xlinker -x -o $@ $(ISDNNETOBJ) ++ + libisdnnet.a: $(ISDNNETOBJ) + rm -f $@ + ar cr $@ $(ISDNNETOBJ) +@@ -33,7 +42,7 @@ + g711.o: g711.c $(INCLUDEDIR)/g711.h + + clean: +- rm -f *.o *~ DEADJOE ++ rm -f *.o *.so *~ DEADJOE + rm -f libisdnnet.a + + distclean: clean +diff -u -r -P /tmp/mISDNuser/i4lnet/net_if.c mISDNuser/i4lnet/net_if.c +--- /tmp/mISDNuser/i4lnet/net_if.c 2004-12-05 18:23:40.000000000 +0100 ++++ mISDNuser/i4lnet/net_if.c 2005-11-14 09:27:58.000000000 +0100 +@@ -1,7 +1,7 @@ + #include <stdlib.h> + #include <unistd.h> + #include <string.h> +-#include <asm/bitops.h> ++#include "net_l2.h" + #include "isdn_net.h" + #include "bchannel.h" + #include "helper.h" +diff -u -r -P /tmp/mISDNuser/i4lnet/net_l2.c mISDNuser/i4lnet/net_l2.c +--- /tmp/mISDNuser/i4lnet/net_l2.c 2004-12-05 23:22:15.000000000 +0100 ++++ mISDNuser/i4lnet/net_l2.c 2005-11-14 09:27:58.000000000 +0100 +@@ -32,18 +32,6 @@ + + #define L2_STATE_COUNT (ST_L2_8+1) + +-static char *strL2State[] = +-{ +- "ST_L2_1", +- "ST_L2_2", +- "ST_L2_3", +- "ST_L2_4", +- "ST_L2_5", +- "ST_L2_6", +- "ST_L2_7", +- "ST_L2_8", +-}; +- + enum { + EV_L2_UI, + EV_L2_SABME, +diff -u -r -P /tmp/mISDNuser/i4lnet/net_l2.h mISDNuser/i4lnet/net_l2.h +--- /tmp/mISDNuser/i4lnet/net_l2.h 2004-12-05 00:47:10.000000000 +0100 ++++ mISDNuser/i4lnet/net_l2.h 2005-11-14 09:27:58.000000000 +0100 +@@ -9,7 +9,6 @@ + #ifndef NET_L2_H + #define NET_L2_H + +-#include <asm/bitops.h> + #include "mISDNlib.h" + #include "isdn_net.h" + #include "fsm.h" +@@ -118,4 +117,31 @@ + #define FLG_LAPD_NET 18 + #define FLG_TEI_T201_1 19 + ++ ++/* Simple replacement for the NON-ATOMIC routines which asm/bitops.h ++ was providing. */ ++static inline int test_bit(int bit, unsigned long *word) ++{ ++ return !!((*word) & (1<<bit)); ++} ++static inline int test_and_clear_bit(int bit, unsigned long *word) ++{ ++ int ret = !!((*word) & (1<<bit)); ++ *word &= ~(1<<bit); ++ return ret; ++} ++static inline int test_and_set_bit(int bit, unsigned long *word) ++{ ++ int ret = !!((*word) & (1<<bit)); ++ *word |= 1<<bit; ++ return ret; ++} ++static inline void clear_bit(int bit, unsigned long *word) ++{ ++ *word &= ~(1<<bit); ++} ++static inline void set_bit(int bit, unsigned long *word) ++{ ++ *word |= 1<<bit; ++} + #endif +diff -u -r -P /tmp/mISDNuser/i4lnet/net_l3.c mISDNuser/i4lnet/net_l3.c +--- /tmp/mISDNuser/i4lnet/net_l3.c 2005-04-30 17:32:06.000000000 +0200 ++++ mISDNuser/i4lnet/net_l3.c 2005-11-14 09:27:58.000000000 +0100 +@@ -9,7 +9,6 @@ + */ + + #include <stdlib.h> +-#include <asm/bitops.h> + #include "mISDNlib.h" + #include "net_l2.h" + #include "net_l3.h" +diff -u -r -P /tmp/mISDNuser/lib/Makefile mISDNuser/lib/Makefile +--- /tmp/mISDNuser/lib/Makefile 2004-08-28 14:28:38.000000000 +0200 ++++ mISDNuser/lib/Makefile 2005-12-05 18:57:27.000000000 +0100 +@@ -1,7 +1,18 @@ + +-all: libmISDN.a ++all: libmISDN.a libmISDN.so + +-libmISDN.a: device.o layer.o stack.o status.o ++install: ++ install -m 644 libmISDN.so $(INSTALL_PREFIX)/usr/lib/ ++ ++LIBMISDN_OBJS=device.o layer.o stack.o status.o ++ ++libmISDN.so: $(LIBMISDN_OBJS) ++ rm -f $@ ++ $(CC) -shared -Xlinker -x -o $@ $(LIBMISDN_OBJS) ++ ++ ++ ++libmISDN.a: $(LIBMISDN_OBJS) + rm -f $@ + ar -r $@ $^ + ar -s $@ +@@ -12,9 +23,8 @@ + status.o : status.c ../include/mISDNlib.h + + clean: +- rm -f *.o *~ DEADJOE ++ rm -f *.o *.so *~ DEADJOE + rm -f libmISDN.a + + distclean: clean + rm -f *.a +- +diff -u -r -P /tmp/mISDNuser/Makefile mISDNuser/Makefile +--- /tmp/mISDNuser/Makefile 2004-08-28 14:30:55.000000000 +0200 ++++ mISDNuser/Makefile 2005-12-05 19:16:52.000000000 +0100 +@@ -1,10 +1,26 @@ ++# ++# Set this to your local copy of mISDN ++# ++MISDNDIR := /usr/src/mISDN ++ ++# ++# Change this to create an install prefix for the shared libs, programms and ++# includes ++# ++INSTALL_PREFIX := / ++export INSTALL_PREFIX ++ ++MISDNINCLUDEDIR := $(MISDNDIR)/include ++export MISDNINCLUDEDIR ++ + mISDN_DIR := $(PWD) + export mISDN_DIR + + INCLUDEDIR := $(mISDN_DIR)/include + export INCLUDEDIR + +-CFLAGS:= -g -Wall -O2 -I $(INCLUDEDIR) ++CFLAGS:= -g -Wall -O2 -I $(INCLUDEDIR) -I $(MISDNINCLUDEDIR) ++CFLAGS+= -D CLOSE_REPORT=1 + export CFLAGS + + mISDNLIB := $(PWD)/lib/libmISDN.a +@@ -20,9 +36,20 @@ + + LIBS := lib/libmISDN.a + +-all: ++all: test_misdn_includes + make TARGET=$@ subdirs + ++ ++install_path: ++ mkdir -p $(INSTALL_PREFIX)/usr/bin/ ++ mkdir -p $(INSTALL_PREFIX)/usr/lib/mISDNuser/ ++ mkdir -p $(INSTALL_PREFIX)/usr/include/mISDNuser/ ++ ++install: install_path all ++ make TARGET=install subdirs ++ cp include/*.h $(INSTALL_PREFIX)/usr/include/mISDNuser/ ++ ++ + subdirs: + set -e; for i in $(SUBDIRS) ; do $(MAKE) -C $$i $(TARGET); done + +@@ -61,3 +88,8 @@ + voiparchiv: ARCHIVNAME := $(ARCHIVDIR)/$(MAINDIR)_voip-$(VERSION).tar.bz2 + voiparchiv: archiv + ++ ++test_misdn_includes: ++ @if ! echo "#include <linux/mISDNif.h>" | gcc -I$(MISDNINCLUDEDIR) -C -E - >/tmp/muh ; then echo -e "\n\nYou either don't seem to have installed mISDN properly\nor you haven't set the MISDNDIR variable in this very Makefile.\n\nPlease either install mISDN or set the MISDNDIR properly\n"; exit 1; fi ++ ++ +diff -u -r -P /tmp/mISDNuser/tenovis/Makefile mISDNuser/tenovis/Makefile +--- /tmp/mISDNuser/tenovis/Makefile 2004-08-28 14:29:27.000000000 +0200 ++++ mISDNuser/tenovis/Makefile 2005-12-05 18:51:05.000000000 +0100 +@@ -10,6 +10,11 @@ + + all: sublib $(PROGS) + ++install: ++ for i in $(PROGS) ; do \ ++ install -m 755 $$i $(INSTALL_PREFIX)/usr/bin ;\ ++ done ++ + testlib: testlib.o $(TENOVISLIB) $(mISDNLIB) + + tstlib: tstlib.o $(TENOVISLIB) $(mISDNLIB) +diff -u -r -P /tmp/mISDNuser/voip/Makefile mISDNuser/voip/Makefile +--- /tmp/mISDNuser/voip/Makefile 2004-08-28 14:29:53.000000000 +0200 ++++ mISDNuser/voip/Makefile 2005-12-05 18:50:39.000000000 +0100 +@@ -24,6 +24,11 @@ + + all: $(PROGRAMMS) + ++install: ++ for i in $(PROGRAMMS) ; do \ ++ install -m 755 $$i $(INSTALL_PREFIX)/usr/bin ;\ ++ done ++ + INTERNET_PORT = 2074 + + CARGS = -DInternet_Port=$(INTERNET_PORT) diff --git a/1.2-netsec/channels/misdn/portinfo.c b/1.2-netsec/channels/misdn/portinfo.c new file mode 100644 index 000000000..c7add95fb --- /dev/null +++ b/1.2-netsec/channels/misdn/portinfo.c @@ -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; +} |