diff options
author | russell <russell@f38db490-d61c-443f-a65b-d21fe96a405b> | 2009-01-16 15:51:43 +0000 |
---|---|---|
committer | russell <russell@f38db490-d61c-443f-a65b-d21fe96a405b> | 2009-01-16 15:51:43 +0000 |
commit | 52acc4c45786fc04da9536c2801eb2b483cc2009 (patch) | |
tree | 3e0b68760b7b6d49c72de4fd5b747ed4d5726ad3 /channels/misdn | |
parent | 5d9d64e584ec5220835eb10a124b555d449fa3d3 (diff) | |
parent | caebf8461f9849f484eb5bbd649880e457c20e31 (diff) |
Creating tag for the release of asterisk-1.4.23-rc4
git-svn-id: http://svn.digium.com/svn/asterisk/tags/1.4.23-rc4@168755 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'channels/misdn')
-rw-r--r-- | channels/misdn/Makefile | 17 | ||||
-rw-r--r-- | channels/misdn/chan_misdn_config.h | 152 | ||||
-rw-r--r-- | channels/misdn/ie.c | 1422 | ||||
-rw-r--r-- | channels/misdn/isdn_lib.c | 4661 | ||||
-rw-r--r-- | channels/misdn/isdn_lib.h | 674 | ||||
-rw-r--r-- | channels/misdn/isdn_lib_intern.h | 142 | ||||
-rw-r--r-- | channels/misdn/isdn_msg_parser.c | 1348 | ||||
-rw-r--r-- | channels/misdn/portinfo.c | 198 |
8 files changed, 8614 insertions, 0 deletions
diff --git a/channels/misdn/Makefile b/channels/misdn/Makefile new file mode 100644 index 000000000..e277636e6 --- /dev/null +++ b/channels/misdn/Makefile @@ -0,0 +1,17 @@ +# +# Makefile for chan_misdn support +# +ifneq ($(wildcard /usr/include/linux/mISDNdsp.h),) +CFLAGS+=-DMISDN_1_2 +endif + +all: + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +portinfo: portinfo.o + $(CC) -o $@ $^ -lisdnnet -lmISDN -lpthread + +clean: + rm -rf *.a *.o *.so portinfo *.i diff --git a/channels/misdn/chan_misdn_config.h b/channels/misdn/chan_misdn_config.h new file mode 100644 index 000000000..f675704c0 --- /dev/null +++ b/channels/misdn/chan_misdn_config.h @@ -0,0 +1,152 @@ +/* + * 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_GROUPNAME, /* char[] */ + MISDN_CFG_ALLOWED_BEARERS, /* char[] */ + MISDN_CFG_FAR_ALERTING, /* int (bool) */ + MISDN_CFG_RXGAIN, /* int */ + MISDN_CFG_TXGAIN, /* int */ + MISDN_CFG_TE_CHOOSE_CHANNEL, /* int (bool) */ + MISDN_CFG_PMP_L1_CHECK, /* int (bool) */ + MISDN_CFG_REJECT_CAUSE, /* int */ + MISDN_CFG_ALARM_BLOCK, /* int (bool) */ + MISDN_CFG_HDLC, /* int (bool) */ + MISDN_CFG_CONTEXT, /* char[] */ + MISDN_CFG_LANGUAGE, /* char[] */ + MISDN_CFG_MUSICCLASS, /* char[] */ + MISDN_CFG_CALLERID, /* char[] */ + MISDN_CFG_METHOD, /* char[] */ + MISDN_CFG_DIALPLAN, /* int */ + MISDN_CFG_LOCALDIALPLAN, /* int */ + MISDN_CFG_CPNDIALPLAN, /* int */ + MISDN_CFG_NATPREFIX, /* char[] */ + MISDN_CFG_INTERNATPREFIX, /* char[] */ + MISDN_CFG_PRES, /* int */ + MISDN_CFG_SCREEN, /* int */ + MISDN_CFG_ALWAYS_IMMEDIATE, /* int (bool) */ + MISDN_CFG_NODIALTONE, /* int (bool) */ + MISDN_CFG_IMMEDIATE, /* int (bool) */ + MISDN_CFG_SENDDTMF, /* int (bool) */ + MISDN_CFG_ASTDTMF, /* int (bool) */ + MISDN_CFG_HOLD_ALLOWED, /* int (bool) */ + MISDN_CFG_EARLY_BCONNECT, /* int (bool) */ + MISDN_CFG_INCOMING_EARLY_AUDIO, /* int (bool) */ + MISDN_CFG_ECHOCANCEL, /* int */ +#ifdef MISDN_1_2 + MISDN_CFG_PIPELINE, /* char[] */ +#endif + +#ifdef WITH_BEROEC + MISDN_CFG_BNECHOCANCEL, + MISDN_CFG_BNEC_ANTIHOWL, + MISDN_CFG_BNEC_NLP, + MISDN_CFG_BNEC_ZEROCOEFF, + MISDN_CFG_BNEC_TD, + MISDN_CFG_BNEC_ADAPT, +#endif + MISDN_CFG_NEED_MORE_INFOS, /* bool */ + MISDN_CFG_NOAUTORESPOND_ON_SETUP, /* bool */ + MISDN_CFG_NTTIMEOUT, /* bool */ + MISDN_CFG_BRIDGING, /* bool */ + MISDN_CFG_JITTERBUFFER, /* int */ + MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, /* int */ + MISDN_CFG_CALLGROUP, /* ast_group_t */ + MISDN_CFG_PICKUPGROUP, /* ast_group_t */ + MISDN_CFG_MAX_IN, /* int */ + MISDN_CFG_MAX_OUT, /* int */ + MISDN_CFG_L1_TIMEOUT, /* int */ + MISDN_CFG_OVERLAP_DIAL, /* int (bool)*/ + MISDN_CFG_MSNS, /* char[] */ + MISDN_CFG_FAXDETECT, /* char[] */ + MISDN_CFG_FAXDETECT_CONTEXT, /* char[] */ + MISDN_CFG_FAXDETECT_TIMEOUT, /* int */ + MISDN_CFG_PTP, /* int (bool) */ + MISDN_CFG_LAST, + + /* general config items */ + MISDN_GEN_FIRST, +#ifndef MISDN_1_2 + MISDN_GEN_MISDN_INIT, /* char[] */ +#endif + MISDN_GEN_DEBUG, /* int */ + MISDN_GEN_TRACEFILE, /* char[] */ + MISDN_GEN_BRIDGING, /* int (bool) */ + MISDN_GEN_STOP_TONE, /* int (bool) */ + MISDN_GEN_APPEND_DIGITS2EXTEN, /* int (bool) */ + MISDN_GEN_DYNAMIC_CRYPT, /* int (bool) */ + MISDN_GEN_CRYPT_PREFIX, /* char[] */ + MISDN_GEN_CRYPT_KEYS, /* char[] */ + MISDN_GEN_NTKEEPCALLS, /* int (bool) */ + MISDN_GEN_NTDEBUGFLAGS, /* int */ + MISDN_GEN_NTDEBUGFILE, /* char[] */ + MISDN_GEN_LAST +}; + +enum misdn_cfg_method { + METHOD_STANDARD = 0, + METHOD_ROUND_ROBIN, + METHOD_STANDARD_DEC +}; + +/* you must call misdn_cfg_init before any other function of this header file */ +int misdn_cfg_init(int max_ports); +void misdn_cfg_reload(void); +void misdn_cfg_destroy(void); + +void misdn_cfg_update_ptp( 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); + +/* returns the enum element for the given name, returns MISDN_CFG_FIRST if none was found */ +enum misdn_cfg_elements misdn_cfg_get_elem (char *name); + +/* fills the buffer with the name of the given config element */ +void misdn_cfg_get_name (enum misdn_cfg_elements elem, void *buf, int bufsize); + +/* fills the buffer with the description of the given config element */ +void misdn_cfg_get_desc (enum misdn_cfg_elements elem, void *buf, int bufsize, void *buf_default, int bufsize_default); + +/* 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); + +#if 0 +char *misdn_cfg_get_next_group(char *group); +int misdn_cfg_get_next_port_in_group(int port, char *group); +#endif + +#endif diff --git a/channels/misdn/ie.c b/channels/misdn/ie.c new file mode 100644 index 000000000..2e7fae998 --- /dev/null +++ b/channels/misdn/ie.c @@ -0,0 +1,1422 @@ + +/* + * 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 <mISDNuser/mISDNlib.h> +#include <mISDNuser/isdn_net.h> +#include <mISDNuser/l3dss1.h> +#include <mISDNuser/net_l3.h> + + + +#define MISDN_IE_DEBG 0 + +/* support stuff */ +static void strnncpy(char *dest, 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 */ +static 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->QI_ELEMENT(sending_complete) = p - (unsigned char *)qi - sizeof(Q931_info_t); + + p[0] = IE_COMPLETE; + } +} + +static 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->QI_ELEMENT(sending_complete)) + *complete = 1; + } else + if (p) + *complete = 1; + + if (*complete) + if (MISDN_IE_DEBG) printf(" complete=%d\n", *complete); +} + + +/* IE_BEARER */ +static 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->QI_ELEMENT(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; +} + +static 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; +#ifdef LLC_SUPPORT + if (qi->QI_ELEMENT(llc)) { + + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(llc) + 1; + } +#endif + if (qi->QI_ELEMENT(bearer_capability)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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 */ +#if 0 +static void enc_ie_call_id(unsigned char **ntmode, msg_t *msg, 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->QI_ELEMENT(call_id) = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CALL_ID; + p[1] = l; + memcpy(p+2, callid, callid_len); +} +#endif + +#if 0 +static void dec_ie_call_id(unsigned char *p, Q931_info_t *qi, 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->QI_ELEMENT(call_id)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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); +} +#endif + +/* IE_CALLED_PN */ +static void enc_ie_called_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, 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->QI_ELEMENT(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)); +} + +static void dec_ie_called_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, char *number, int number_len, int nt, struct misdn_bchannel *bc) +{ + *type = -1; + *plan = -1; + *number = '\0'; + + if (!nt) + { + p = NULL; + if (qi->QI_ELEMENT(called_nr)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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, (char *)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 */ +static void enc_ie_calling_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, 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->QI_ELEMENT(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)); + } +} + +static void dec_ie_calling_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, 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->QI_ELEMENT(calling_nr)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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, (char *)p+3, p[0]-2, number_len); + } else + { + strnncpy(number, (char *)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 */ +static void enc_ie_connected_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, 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->QI_ELEMENT(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)); + } +} + +static void dec_ie_connected_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, 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->QI_ELEMENT(connected_nr)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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, (char *)p+3, p[0]-2, number_len); + } else + { + strnncpy(number, (char *)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 */ +static 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->QI_ELEMENT(cause) = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CAUSE; + p[1] = l; + p[2] = 0x80 + location; + p[3] = 0x80 + cause; +} + +#if 0 +static 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->QI_ELEMENT(cause) = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CAUSE; + p[1] = 2; + p[2] = 0x80 + location; + p[3] = 0x80 + cause; +} +#endif + +static 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->QI_ELEMENT(cause)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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 */ +static 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->QI_ELEMENT(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->QI_ELEMENT(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->QI_ELEMENT(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]); */ + } +} + +static 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->QI_ELEMENT(channel_id)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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 */ +static 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->QI_ELEMENT(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 */ +static void enc_ie_display(unsigned char **ntmode, msg_t *msg, 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->QI_ELEMENT(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)); +} + +#if 0 +static void dec_ie_display(unsigned char *p, Q931_info_t *qi, char *display, int display_len, int nt, struct misdn_bchannel *bc) +{ + *display = '\0'; + + if (!nt) + { + p = NULL; + if (qi->QI_ELEMENT(display)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(display) + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + strnncpy(display, (char *)p+1, p[0], display_len); + + if (MISDN_IE_DEBG) printf(" display='%s'\n", display); +} +#endif + +/* IE_KEYPAD */ +#if 1 +static void enc_ie_keypad(unsigned char **ntmode, msg_t *msg, 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(keypad); + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->QI_ELEMENT(keypad) = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_KEYPAD; + p[1] = l; + strncpy((char *)p+2, keypad, strlen(keypad)); +} +#endif + +static void dec_ie_keypad(unsigned char *p, Q931_info_t *qi, char *keypad, int keypad_len, int nt, struct misdn_bchannel *bc) +{ + *keypad = '\0'; + + if (!nt) + { + p = NULL; + if (qi->QI_ELEMENT(keypad)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(keypad) + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + strnncpy(keypad, (char *)p+1, p[0], keypad_len); + + if (MISDN_IE_DEBG) printf(" keypad='%s'\n", keypad); +} + + +/* IE_NOTIFY */ +#if 0 +static 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->QI_ELEMENT(notify) = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_NOTIFY; + p[1] = l; + p[2] = 0x80 + notify; +} +#endif + +#if 0 +static 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->QI_ELEMENT(notify)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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); +} +#endif + + +/* IE_PROGRESS */ +static 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->QI_ELEMENT(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; +} + +static 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->QI_ELEMENT(progress)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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) */ +static void enc_ie_redir_nr(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, int reason, 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->QI_ELEMENT(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)); + } +} + +static void dec_ie_redir_nr(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, int *reason, 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->QI_ELEMENT(redirect_nr)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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, (char *)p+4, p[0]-3, number_len); + } else + { + strnncpy(number, (char *)p+3, p[0]-2, number_len); + } + } else + { + strnncpy(number, (char *)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) */ +#if 0 +static void enc_ie_redir_dn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, 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->QI_ELEMENT(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)); + } +} +#endif + +#if 0 +static void dec_ie_redir_dn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, 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->QI_ELEMENT(redir_dn)) */ +/* p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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, (char *)p+3, p[0]-2, number_len); + } else + { + strnncpy(number, (char *)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); +} +#endif + + +/* IE_USERUSER */ +#if 1 +static void enc_ie_useruser(unsigned char **ntmode, msg_t *msg, int protocol, 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+1; + p = msg_put(msg, l+3); + if (nt) + *ntmode = p+1; + else + qi->QI_ELEMENT(useruser) = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_USER_USER; + p[1] = l; + p[2] = protocol; + memcpy(p+3, user, user_len); +} +#endif + +#if 1 +static void dec_ie_useruser(unsigned char *p, Q931_info_t *qi, int *protocol, 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->QI_ELEMENT(useruser)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(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); +} +#endif + +/* IE_DISPLAY */ +static void enc_ie_restart_ind(unsigned char **ntmode, msg_t *msg, unsigned char rind, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + /* if (MISDN_IE_DEBG) printf(" display='%s' (len=%d)\n", display, strlen((char *)display)); */ + + p = msg_put(msg, 3); + if (nt) + *ntmode = p+1; + else + qi->QI_ELEMENT(restart_ind) = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_RESTART_IND; + p[1] = 1; + p[2] = rind; + +} + diff --git a/channels/misdn/isdn_lib.c b/channels/misdn/isdn_lib.c new file mode 100644 index 000000000..b21c794e7 --- /dev/null +++ b/channels/misdn/isdn_lib.c @@ -0,0 +1,4661 @@ +/* + * 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 + */ + +/*! \file + * \brief Interface to mISDN + * \author Christian Richter <crich@beronet.com> + */ + + + +#include <syslog.h> +#include <sys/time.h> +#include <mISDNuser/isdn_debug.h> + +#include "isdn_lib_intern.h" +#include "isdn_lib.h" + +/* + * Define ARRAY_LEN() because I cannot + * #include "asterisk/utils.h" + */ +#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) + +#include "asterisk/causes.h" + +void misdn_join_conf(struct misdn_bchannel *bc, int conf_id); +void misdn_split_conf(struct misdn_bchannel *bc, int conf_id); + +int queue_cleanup_bc(struct misdn_bchannel *bc) ; + +int misdn_lib_get_l2_up(struct misdn_stack *stack); + +struct misdn_stack* get_misdn_stack( void ); + +static int set_chan_in_stack(struct misdn_stack *stack, int channel); + +int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh); + +int misdn_lib_port_is_pri(int port) +{ + struct misdn_stack *stack=get_misdn_stack(); + for ( ; stack; stack=stack->next) { + if (stack->port == port) { + return stack->pri; + } + } + + return -1; +} + +static void misdn_make_dummy(struct misdn_bchannel *dummybc, int port, int l3id, int nt, int channel) +{ + memset (dummybc,0,sizeof(struct misdn_bchannel)); + dummybc->port=port; + dummybc->l3_id=l3id; + dummybc->nt=nt; + dummybc->dummy=1; + dummybc->channel=channel; +} + +int misdn_lib_port_block(int port) +{ + struct misdn_stack *stack=get_misdn_stack(); + for ( ; stack; stack=stack->next) { + if (stack->port == port) { + stack->blocked=1; + return 0; + } + } + return -1; + +} + +int misdn_lib_port_unblock(int port) +{ + struct misdn_stack *stack=get_misdn_stack(); + for ( ; stack; stack=stack->next) { + if (stack->port == port) { + stack->blocked=0; + return 0; + } + } + return -1; + +} + +int misdn_lib_is_port_blocked(int port) +{ + struct misdn_stack *stack=get_misdn_stack(); + for ( ; stack; stack=stack->next) { + if (stack->port == port) { + return stack->blocked; + } + } + return -1; +} + +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; +} + +int misdn_lib_get_maxchans(int port) +{ + struct misdn_stack *stack=get_misdn_stack(); + for ( ; stack; stack=stack->next) { + if (stack->port == port) { + if (stack->pri) + return 30; + else + return 2; + } + } + 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) { + 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, "* Port %d Type %s Prot. %s L2Link %s L1Link:%s Blocked:%d", + stack->port, stack->nt ? "NT" : "TE", stack->ptp ? "PTP" : "PMP", + stack->l2link ? "UP" : "DOWN", stack->l1link ? "UP" : "DOWN", + stack->blocked); + } 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 { + /*! \brief mISDN device handle returned by mISDN_open() */ + int midev; + int midev_nt; /* Not used */ + + 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 + +void misdn_tx_jitter(struct misdn_bchannel *bc, int len); + +struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id); + +struct misdn_bchannel *find_bc_by_confid(unsigned long confid); + +struct misdn_bchannel *stack_holder_find_bychan(struct misdn_stack *stack, int chan); + +int setup_bc(struct misdn_bchannel *bc); + +int manager_isdn_handler(iframe_t *frm ,msg_t *msg); + +int misdn_lib_port_restart(int port); +int misdn_lib_pid_restart(int pid); + +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; + +char tone_425_flip[TONE_425_SIZE]; +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 */ + /* 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); +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); + +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]; + +static 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 char * flip_buf_bits ( char * buf , int len) +{ + int i; + char * start = buf; + + for (i = 0 ; i < len; i++) { + buf[i] = flip_table[(unsigned char)buf[i]]; + } + + return start; +} + + + + +static 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); +} + + +static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg) +{ + iframe_t *frm = (iframe_t *)dmsg->data; + struct misdn_stack *stack=get_stack_by_bc(bc); + + if (!stack) { + cb_log(0,bc->port,"send_msg: IEK!! no stack\n "); + return -1; + } + + frm->addr = (stack->upper_id | FLG_MSG_DOWN); + frm->dinfo = bc->l3_id; + frm->len = (dmsg->len) - mISDN_HEADER_LEN; + + cb_log(4,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo); + + mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC); + free_msg(dmsg); + + return 0; +} + + +static int mypid=1; + + +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 (!bc->early_bconnect) { + /* We have opted to never receive any available inband recorded messages */ + 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; +} + + +static void dump_chan_list(struct misdn_stack *stack) +{ + int i; + + for (i=0; i <= stack->b_num; i++) { + cb_log(6, stack->port, "Idx:%d stack->cchan:%d in_use:%d Chan:%d\n",i,stack->channels[i], stack->bc[i].in_use, i+1); + } +} + + +void misdn_dump_chanlist() +{ + struct misdn_stack *stack=get_misdn_stack(); + for ( ; stack; stack=stack->next) { + dump_chan_list(stack); + } + +} + +int set_chan_in_stack(struct misdn_stack *stack, int channel) +{ + + cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel); + dump_chan_list(stack); + if (channel >=1 && channel <= MAX_BCHANS) { + if (!stack->channels[channel-1]) + stack->channels[channel-1] = 1; + else { + cb_log(4,stack->port,"channel already in use:%d\n", channel ); + return -1; + } + } else { + cb_log(0,stack->port,"couldn't set channel %d in\n", channel ); + return -1; + } + + return 0; +} + + + +static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel, int dec) +{ + int i; + int chan=0; + int bnums = stack->pri ? stack->b_num : stack->b_num - 1; + + if (bc->channel_found) + return 0; + + bc->channel_found=1; + + cb_log(5,stack->port,"find_free_chan: req_chan:%d\n",channel); + + if (channel < 0 || channel > MAX_BCHANS) { + cb_log(0, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel); + return 0; + } + + channel--; + + if (dec) { + for (i = bnums; i >=0; i--) { + if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 D channel ;) and work with chan preselection */ + if (!stack->channels[i]) { + cb_log (3, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1); + chan=i+1; + break; + } + } + } + } else { + for (i = 0; i <= bnums; i++) { + if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 D channel ;) and work with chan preselection */ + if (!stack->channels[i]) { + cb_log (3, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1); + chan=i+1; + break; + } + } + } + } + + if (!chan) { + cb_log (1, stack->port, " !! NO FREE CHAN IN STACK\n"); + dump_chan_list(stack); + bc->out_cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION; + return -1; + } + + if (set_chan_in_stack(stack, chan)<0) { + cb_log (0, stack->port, "Channel Already in use:%d\n", chan); + bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL; + return -1; + } + + bc->channel=chan; + return 0; +} + +static int empty_chan_in_stack(struct misdn_stack *stack, int channel) +{ + if (channel<=0 || channel>MAX_BCHANS) { + cb_log(0,stack?stack->port:0, "empty_chan_in_stack: cannot empty channel %d\n",channel); + return -1; + } + + cb_log (4, stack?stack->port:0, "empty_chan_in_stack: %d\n",channel); + stack->channels[channel-1] = 0; + dump_chan_list(stack); + return 0; +} + +char *bc_state2str(enum bchannel_state state) { + int i; + + struct bchan_state_s { + char *n; + enum bchannel_state s; + } states[] = { + {"BCHAN_CLEANED", BCHAN_CLEANED }, + {"BCHAN_EMPTY", BCHAN_EMPTY}, + {"BCHAN_SETUP", BCHAN_SETUP}, + {"BCHAN_SETUPED", BCHAN_SETUPED}, + {"BCHAN_ACTIVE", BCHAN_ACTIVE}, + {"BCHAN_ACTIVATED", BCHAN_ACTIVATED}, + {"BCHAN_BRIDGE", BCHAN_BRIDGE}, + {"BCHAN_BRIDGED", BCHAN_BRIDGED}, + {"BCHAN_RELEASE", BCHAN_RELEASE}, + {"BCHAN_RELEASED", BCHAN_RELEASED}, + {"BCHAN_CLEAN", BCHAN_CLEAN}, + {"BCHAN_CLEAN_REQUEST", BCHAN_CLEAN_REQUEST}, + {"BCHAN_ERROR", BCHAN_ERROR} + }; + + for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++) + if ( states[i].s == state) + return states[i].n; + + return "UNKNOWN"; +} + +void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state) +{ + cb_log(5,bc->port,"BC_STATE_CHANGE: l3id:%x from:%s to:%s\n", + bc->l3_id, + bc_state2str(bc->bc_state), + bc_state2str(state) ); + + switch (state) { + case BCHAN_ACTIVATED: + if (bc->next_bc_state == BCHAN_BRIDGED) { + misdn_join_conf(bc, bc->conf_id); + bc->next_bc_state = BCHAN_EMPTY; + return; + } + default: + bc->bc_state=state; + break; + } +} + +static void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state) +{ + cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n", + bc_state2str(bc->next_bc_state), + bc_state2str(state) ); + + bc->next_bc_state=state; +} + + +static void empty_bc(struct misdn_bchannel *bc) +{ + bc->dummy=0; + + bc->bframe_len=0; + + bc->cw= 0; + + bc->dec=0; + bc->channel = 0; + + bc->sending_complete = 0; + + bc->restart_channel=0; + + bc->conf_id = 0; + + bc->need_more_infos = 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->generate_tone=0; + bc->tone_cnt=0; + + bc->dnumplan=NUMPLAN_UNKNOWN; + bc->onumplan=NUMPLAN_UNKNOWN; + bc->rnumplan=NUMPLAN_UNKNOWN; + bc->cpnnumplan=NUMPLAN_UNKNOWN; + + + bc->active = 0; + + bc->early_bconnect = 1; + +#ifdef MISDN_1_2 + *bc->pipeline = 0; +#else + bc->ec_enable = 0; + bc->ec_deftaps = 128; +#endif + + bc->orig=0; + + bc->cause = AST_CAUSE_NORMAL_CLEARING; + bc->out_cause = AST_CAUSE_NORMAL_CLEARING; + bc->pres = 0; /* allowed */ + + 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=0x10; + bc->user1=0; + bc->urate=0; + + bc->hdlc=0; + + + bc->info_dad[0] = 0; + bc->display[0] = 0; + bc->infos_pending[0] = 0; + bc->cad[0] = 0; + bc->oad[0] = 0; + bc->dad[0] = 0; + bc->rad[0] = 0; + bc->orig_dad[0] = 0; + bc->uu[0]=0; + bc->uulen=0; + + bc->fac_in.Function = Fac_None; + bc->fac_out.Function = Fac_None; + + bc->te_choose_channel = 0; + bc->channel_found= 0; + + gettimeofday(&bc->last_used, NULL); +} + + +static int clean_up_bc(struct misdn_bchannel *bc) +{ + int ret=0; + unsigned char buff[32]; + struct misdn_stack * stack; + + cb_log(3, bc?bc->port:0, "$$$ CLEANUP CALLED pid:%d\n", bc?bc->pid:-1); + + if (!bc ) return -1; + stack=get_stack_by_bc(bc); + + if (!stack) return -1; + + switch (bc->bc_state ) { + case BCHAN_CLEANED: + cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid); + return -1; + + default: + break; + } + + cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid); + + manager_ec_disable(bc); + + manager_bchannel_deactivate(bc); + + mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + + bc->b_stid = 0; + bc_state_change(bc, BCHAN_CLEANED); + + return ret; +} + + + +static 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], NULL); + empty_chan_in_stack(stack,i+1); + empty_bc(&stack->bc[i]); + clean_up_bc(&stack->bc[i]); + stack->bc[i].in_use = 0; + } + + } +} + +static int new_te_id = 0; + +#define MAXPROCS 0x100 + +static int misdn_lib_get_l1_down(struct misdn_stack *stack) +{ + /* Pull Up L1 */ + iframe_t act; + act.prim = PH_DEACTIVATE | REQUEST; + act.addr = stack->lower_id|FLG_MSG_DOWN; + act.dinfo = 0; + act.len = 0; + + cb_log(1, stack->port, "SENDING PH_DEACTIVATE | REQ\n"); + return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); +} + + +static int misdn_lib_get_l2_down(struct misdn_stack *stack) +{ + + if (stack->ptp && (stack->nt) ) { + msg_t *dmsg; + /* L2 */ + dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0); + + if (stack->nst.manager_l3(&stack->nst, dmsg)) + free_msg(dmsg); + + } else { + iframe_t act; + + act.prim = DL_RELEASE| REQUEST; + act.addr = (stack->upper_id |FLG_MSG_DOWN) ; + + act.dinfo = 0; + act.len = 0; + return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + } + + return 0; +} + + +static int misdn_lib_get_l1_up(struct misdn_stack *stack) +{ + /* Pull Up L1 */ + iframe_t act; + act.prim = PH_ACTIVATE | REQUEST; + act.addr = (stack->upper_id | FLG_MSG_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->nt) ) { + 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 |FLG_MSG_DOWN) ; + + act.dinfo = 0; + act.len = 0; + return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + } + + return 0; +} + +#if 0 +static int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack) +{ + iframe_t act; + + act.prim = DL_ESTABLISH | REQUEST; + act.addr = (stack->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN; + + act.dinfo = 0; + act.len = 0; + return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + return 0; +} +#endif + +static int misdn_lib_get_short_status(struct misdn_stack *stack) +{ + iframe_t act; + + + act.prim = MGR_SHORTSTATUS | REQUEST; + + act.addr = (stack->upper_id | MSG_BROADCAST) ; + + act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL; + + 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 proc_id; + struct misdn_stack *stack; + + stack = get_stack_by_bc(bc); + if (stack->nt) { + if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, 0) < 0) { + return -1; + } + cb_log(4, stack->port, " --> found channel: %d\n", bc->channel); + + for (proc_id = 0; proc_id < MAXPROCS; ++proc_id) { + if (stack->procids[proc_id] == 0) { + break; + } + } /* end for */ + if (proc_id == MAXPROCS) { + cb_log(0, stack->port, "Couldn't Create New ProcId.\n"); + return -1; + } + + stack->procids[proc_id] = 1; + + l3_id = 0xff00 | proc_id; + bc->l3_id = l3_id; + 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 */ + if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, bc->dec) < 0) { + return -1; + } + cb_log(2, stack->port, " --> found channel: %d\n", bc->channel); + } 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 (++new_te_id > 0xffff) { + new_te_id = 0x0001; + } + + l3_id = (entity << 16) | new_te_id; + bc->l3_id = l3_id; + cb_log(3, stack->port, "--> new_l3id %x\n", l3_id); + + /* send message */ + ncr.prim = CC_NEW_CR | REQUEST; + ncr.addr = (stack->upper_id | FLG_MSG_DOWN); + ncr.dinfo = l3_id; + ncr.len = 0; + mISDN_write(midev, &ncr, mISDN_HEADER_LEN + ncr.len, TIMEOUT_1SEC); + } + + return l3_id; +} + + +void misdn_lib_setup_bc(struct misdn_bchannel *bc) +{ + clean_up_bc(bc); + setup_bc(bc); +} + + +int setup_bc(struct misdn_bchannel *bc) +{ + unsigned char buff[1025]; + int midev; + int channel; + int b_stid; + int i; + mISDN_pid_t pid; + int ret; + + struct misdn_stack *stack=get_stack_by_bc(bc); + + if (!stack) { + cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n"); + return -1; + } + + midev = stack->midev; + channel = bc->channel - 1 - (bc->channel > 16); + b_stid = stack->b_stids[channel >= 0 ? channel : 0]; + + switch (bc->bc_state) { + case BCHAN_CLEANED: + break; + default: + cb_log(4, stack->port, "$$$ bc already setup stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) ); + return -1; + } + + cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid); + + /*check if the b_stid is already initialized*/ + for (i=0; i <= stack->b_num; i++) { + if (stack->bc[i].b_stid == b_stid) { + cb_log(0, bc->port, "setup_bc: b_stid:%x already in use !!!\n", b_stid); + return -1; + } + } + + if (b_stid <= 0) { + cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel); + + bc_state_change(bc,BCHAN_ERROR); + 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 */ + + +#define MISDN_DSP +#ifndef MISDN_DSP + bc->nodsp=1; +#endif + if ( bc->hdlc || bc->nodsp) { + 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; + + bc->layer=3; + } else { + 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; + + bc->layer=4; + } + + ret = mISDN_new_layer(midev, &li); + if (ret ) { + cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno)); + + bc_state_change(bc,BCHAN_ERROR); + return(-EINVAL); + } + + bc->layer_id = li.id; + } + + memset(&pid, 0, sizeof(pid)); + + + + cb_log(4, stack->port," --> Channel is %d\n", bc->channel); + + if (bc->nodsp) { + cb_log(2, 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 ( bc->hdlc ) { + cb_log(2, 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)) ; + } else { + cb_log(2, 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)); + + } + + ret = mISDN_set_stack(midev, bc->b_stid, &pid); + + if (ret){ + cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno)); + + mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + + bc_state_change(bc,BCHAN_ERROR); + cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data); + return(-EINVAL); + } + + ret = mISDN_get_setstack_ind(midev, bc->layer_id); + + if (ret) { + cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno)); + mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + + bc_state_change(bc,BCHAN_ERROR); + cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data); + return(-EINVAL); + } + + ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ; + + bc->addr = ret>0? ret : 0; + + if (!bc->addr) { + cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno)); + mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + + bc_state_change(bc,BCHAN_ERROR); + cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data); + return (-EINVAL); + } + + manager_bchannel_activate(bc); + + bc_state_change(bc,BCHAN_ACTIVATED); + + return 0; +} + + + +/** IFACE **/ +static 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(8, port, "Init.BC %d.\n",bidx); + + memset(bc, 0,sizeof(struct misdn_bchannel)); + + bc->send_lock=malloc(sizeof(struct send_lock)); + if (!bc->send_lock) { + return -1; + } + pthread_mutex_init(&bc->send_lock->lock, NULL); + + if (msn) { + int l = sizeof(bc->msn); + strncpy(bc->msn,msn, l); + bc->msn[l-1] = 0; + } + + + empty_bc(bc); + bc_state_change(bc, BCHAN_CLEANED); + + bc->port=stack->port; + bc->nt=stack->nt?1:0; + bc->pri=stack->pri; + + { + ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE); + + if (!ibuf) return -1; + + clear_ibuffer( ibuf); + + ibuf->rsem=malloc(sizeof(sem_t)); + if (!ibuf->rsem) { + return -1; + } + + bc->astbuf=ibuf; + + if (sem_init(ibuf->rsem,1,0)<0) + sem_init(ibuf->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 this port. (ret=%d)\n", __FUNCTION__, ret); + return -1; + } + + stinf = (stack_info_t *)&frm->data.p; + + cb_log(8, port, " --> Child %x\n",stinf->child[bidx]); + } + + return 0; +} + + + +static struct misdn_stack *stack_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; + + struct misdn_stack *stack = malloc(sizeof(struct misdn_stack)); + if (!stack ) return NULL; + + + cb_log(8, port, "Init. Stack.\n"); + + 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); + msg_queue_init(&stack->upqueue); + + /* 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 this port. (ret=%d)\n", __FUNCTION__, 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: + stack->nt=0; + break; + case ISDN_PID_L0_NT_S0: + cb_log(8, port, "NT Stack\n"); + + stack->nt=1; + break; + case ISDN_PID_L0_TE_E1: + cb_log(8, port, "TE S2M Stack\n"); + stack->nt=0; + stack->pri=1; + break; + case ISDN_PID_L0_NT_E1: + cb_log(8, port, "TE S2M Stack\n"); + stack->nt=1; + stack->pri=1; + + break; + default: + cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]); + + } + + if (!stack->nt) { + if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { + stack->ptp = 1; + } else { + stack->ptp = 0; + } + } + + { + int ret; + int nt=stack->nt; + + cb_log(8, port, "Init. Stack.\n"); + + memset(&li, 0, sizeof(li)); + { + int l = sizeof(li.name); + strncpy(li.name,nt?"net l2":"user l4", l); + li.name[l-1] = 0; + } + li.object_id = -1; + li.extentions = 0; + li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20; + li.pid.layermask = ISDN_LAYER((nt?2:4)); + li.st = stack->d_stid; + + + ret = mISDN_new_layer(midev, &li); + if (ret) { + cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4); + return(NULL); + } + + + stack->upper_id = li.id; + ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id); + if (ret) + { + cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4); + return(NULL); + } + + stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3); + if (stack->lower_id < 0) { + cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3); + return(NULL); + } + + stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4); + if (stack->upper_id < 0) { + cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2); + return(NULL); + } + + cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id); + + + /* create nst (nt-mode only) */ + if (nt) { + + 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; + + 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; + + stack->nst.l1_id = stack->lower_id; + stack->nst.l2_id = stack->upper_id; + + msg_queue_init(&stack->nst.down_queue); + + Isdnl2Init(&stack->nst); + Isdnl3Init(&stack->nst); + + } + + if (!stack->nt) { + /*assume L1 is up, we'll get DEACTIVATES soon, for non + * up L1s*/ + stack->l1link=0; + } + stack->l1link=0; + stack->l2link=0; +#if 0 + if (!stack->nt) { + misdn_lib_get_short_status(stack); + } else { + misdn_lib_get_l1_up(stack); + if (!stack->ptp) misdn_lib_get_l1_up(stack); + misdn_lib_get_l2_up(stack); + } +#endif + + misdn_lib_get_short_status(stack); + misdn_lib_get_l1_up(stack); + misdn_lib_get_l2_up(stack); + + } + + cb_log(8,0,"stack_init: port:%d lowerId:%x upperId:%x\n",stack->port,stack->lower_id, stack->upper_id); + + return stack; +} + + +static void stack_destroy(struct misdn_stack *stack) +{ + char buf[1024]; + if (!stack) return; + + if (stack->nt) { + cleanup_Isdnl2(&stack->nst); + cleanup_Isdnl3(&stack->nst); + } + + 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); +} + + +static 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&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack; + + } + + return NULL; +} + + +static 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; +} + +static 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; +} + +static 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); +} + +static 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; +} + + +static struct misdn_bchannel *find_bc_by_addr(unsigned long addr) +{ + 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].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) || stack->bc[i].layer_id== addr ) { + return &stack->bc[i]; + } + } + } + + return NULL; +} + +struct misdn_bchannel *find_bc_by_confid(unsigned long confid) +{ + 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].conf_id==confid ) { + return &stack->bc[i]; + } + } + } + return NULL; +} + + +static struct misdn_bchannel *find_bc_by_channel(int port, int channel) +{ + struct misdn_stack* stack=find_stack_by_port(port); + int i; + + if (!stack) return NULL; + + for (i=0; i<=stack->b_num; i++) { + if ( stack->bc[i].channel== channel ) { + return &stack->bc[i]; + } + } + + return NULL; +} + + + + + +static 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->nt) { + + switch (event) { + + case EVENT_CONNECT_ACKNOWLEDGE: + setup_bc(bc); + + if ( *bc->crypt_key ) { + cb_log(4, stack->port, "ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s\n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad); + 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); + 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; + case EVENT_CONNECT: + + if ( *bc->crypt_key ) { + cb_log(4, stack->port, "ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s\n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad); + manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) ); + } + case EVENT_ALERTING: + case EVENT_PROGRESS: + case EVENT_PROCEEDING: + case EVENT_SETUP_ACKNOWLEDGE: + case EVENT_SETUP: + { + if (bc->channel == 0xff || bc->channel<=0) + bc->channel=0; + + if (find_free_chan_in_stack(stack, bc, bc->channel, 0)<0){ + if (!stack->pri && !stack->ptp) { + bc->cw=1; + break; + } + + if (!bc->channel) + cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n"); + else + cb_log(0, stack->port, "Requested Channel Already in Use releasing this call with cause 34!!!!\n"); + + /* when the channel is already in use, we can't + * simply clear it, we need to make sure that + * it will still be marked as in_use in the + * available channels list.*/ + bc->channel=0; + + misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); + return -1; + } + } + + setup_bc(bc); + break; + + case EVENT_RELEASE_COMPLETE: + case EVENT_RELEASE: + break; + default: + break; + } + } else { /** NT MODE **/ + + } + return 0; +} + +static int handle_cr ( struct misdn_stack *stack, iframe_t *frm) +{ + struct misdn_bchannel *bc; + + if (!stack) return -1; + + switch (frm->prim) { + case CC_NEW_CR|INDICATION: + cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo); + + bc = misdn_lib_get_free_bc(stack->port, 0, 1, 0); + if (!bc) { + cb_log(0, stack->port, " --> !! lib: No free channel!\n"); + return -1; + } + + cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo); + bc->l3_id=frm->dinfo; + 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 find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo); + misdn_make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0); + + bc=&dummybc; + } + + if (bc) { + int channel = bc->channel; + cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo); + + /*bc->pid = 0;*/ + bc->need_disconnect=0; + bc->need_release=0; + bc->need_release_complete=0; + + cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data); + + empty_bc(bc); + clean_up_bc(bc); + + if (channel>0) + empty_chan_in_stack(stack,channel); + bc->in_use=0; + + dump_chan_list(stack); + + if (bc->stack_holder) { + cb_log(4,stack->port, "REMOVING Holder\n"); + stack_holder_remove( stack, bc); + free(bc); + } + } + else { + if (stack->nt) + 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; +} + + +/* Empties 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); + bc->in_use=0; +} + + + + +int misdn_lib_get_port_up (int port) +{ /* Pull Up L1 */ + 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_get_port_down (int port) +{ /* Pull Down L1 */ + struct misdn_stack *stack; + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) { + if (stack->port == port) { + if (stack->l2link) + misdn_lib_get_l2_down(stack); + misdn_lib_get_l1_down(stack); + return 0; + } + } + return 0; +} + +int misdn_lib_port_up(int port, int check) +{ + struct misdn_stack *stack; + + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) { + + if (stack->port == port) { + + if (stack->blocked) { + cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link); + return -1; + } + + if (stack->ptp ) { + + if (stack->l1link && stack->l2link) { + return 1; + } else { + cb_log(1,port, "Port Down L2:%d L1:%d\n", + stack->l2link, stack->l1link); + return 0; + } + } else { + if ( !check || stack->l1link ) + return 1; + else { + cb_log(1,port, "Port down PMP\n"); + return 0; + } + } + } + } + + return -1; +} + + +int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh) +{ + 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 | FLG_MSG_DOWN; + + frm.prim = CC_RELEASE_CR|INDICATION; + cb_log(4, stack->port, " --> CC_RELEASE_CR: Faking Release_cr for %x l3id:%x\n",frm.addr, frm.dinfo); + /** removing procid **/ + if (!bc) { + cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", hh->dinfo); + misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0); + bc=&dummybc; + } + + if (bc) { + if ( (bc->l3_id & 0xff00) == 0xff00) { + cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff); + stack->procids[bc->l3_id&0xff] = 0 ; + } + } + else cb_log(0, stack->port, "Couldn't find BC so I couldn't remove the Process!!!! this is a bad port.\n"); + + if (handle_cr(stack, &frm)<0) { + } + + return 0 ; +} + +int +handle_event_nt(void *dat, void *arg) +{ + manager_t *mgr = (manager_t *)dat; + msg_t *msg = (msg_t *)arg; + mISDNuser_head_t *hh; + int reject=0; + + struct misdn_stack *stack=find_stack_by_mgr(mgr); + int port; + + if (!msg || !mgr) + return(-EINVAL); + + hh=(mISDNuser_head_t*)msg->data; + port=stack->port; + + cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo); + { + switch(hh->prim){ + case CC_RETRIEVE|INDICATION: + { + struct misdn_bchannel *bc; + struct misdn_bchannel *hold_bc; + + iframe_t frm; /* fake te frm to add callref to global callreflist */ + frm.dinfo = hh->dinfo; + + frm.addr=stack->upper_id | FLG_MSG_DOWN; + + frm.prim = CC_NEW_CR|INDICATION; + + if (handle_cr( stack, &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; + } + + bc = find_bc_by_l3id(stack, hh->dinfo); + hold_bc = stack_holder_find(stack, bc->l3_id); + cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id); + + if (hold_bc) { + cb_log(4, stack->port, "REMOVING Holder\n"); + + /*swap the backup to our new channel back*/ + stack_holder_remove(stack, hold_bc); + memcpy(bc, hold_bc, sizeof(struct misdn_bchannel)); + free(hold_bc); + + bc->holded=0; + bc->b_stid=0; + } + + } + + 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, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n",l3id ); + + if (!bc) { cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n"); return 0; } + cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id); + bc->l3_id=l3id; + cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data); + } + free_msg(msg); + return 0; + + case CC_SETUP|INDICATION: + { + struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1, 0); + if (!bc) + ERR_NO_CHANNEL: + { + 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; + } + + cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo); + bc->l3_id=hh->dinfo; + } + break; + + case CC_CONNECT_ACKNOWLEDGE|INDICATION: + break; + + case CC_ALERTING|INDICATION: + case CC_PROCEEDING|INDICATION: + case CC_SETUP_ACKNOWLEDGE|INDICATION: + if(!stack->ptp) break; + case CC_CONNECT|INDICATION: + 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) { + int myprocid=bc->l3_id&0x0000ffff; + hh->dinfo=(hh->dinfo&0xffff0000)|myprocid; + cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause); + reject=1; + } + } + } + break; + + case CC_FACILITY|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) { + int myprocid=bc->l3_id&0x0000ffff; + hh->dinfo=(hh->dinfo&0xffff0000)|myprocid; + cb_log(4,bc->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); + + if (bc) { + cb_log(1, stack->port, "CC_RELEASE|CONFIRM (l3id:%x), sending RELEASE_COMPLETE\n", hh->dinfo); + misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE); + } + } + break; + + case CC_RELEASE|INDICATION: + break; + + case CC_RELEASE_CR|INDICATION: + release_cr(stack, hh); + 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, stack->port, " --> In NEW_CR: didn't found bc ??\n"); return -1;}; + if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) { + cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id); + stack->procids[bc->l3_id&0xff] = 0 ; + } + 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(3, stack->port, "%% GOT L2 Activate Info.\n"); + + if (stack->ptp && stack->l2link) { + cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n"); + cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data); + } + + if (stack->ptp && !stack->restart_sent) { + /* make sure we restart the interface of the + * other side */ + stack->restart_sent=1; + misdn_lib_send_restart(stack->port, -1); + + } + + /* when we get the L2 UP, the L1 is UP definitely too*/ + stack->l1link = 1; + stack->l2link = 1; + stack->l2upcnt=0; + + free_msg(msg); + return 0; + } + break; + + + case DL_RELEASE | INDICATION: + case DL_RELEASE | CONFIRM: + { + if (stack->ptp) { + cb_log(3 , stack->port, "%% GOT L2 DeActivate Info.\n"); + + if (stack->l2upcnt>3) { + cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attempts!!!\n"); + } else { +#if 0 + if (stack->nt) misdn_lib_reinit_nt_stack(stack->port); +#endif + if (stack->l1link) { + misdn_lib_get_l2_up(stack); + stack->l2upcnt++; + } + } + + } else + cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n"); + + stack->l2link = 0; + 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 find BC so temporarily creating dummy BC (l3id:%x).\n", hh->dinfo); + misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0); + bc=&dummybc; + } + if (bc ) { + isdn_msg_parse_event(msgs_g,msg,bc, 1); + + switch (event) { + case EVENT_SETUP: + if (bc->channel<=0 || bc->channel==0xff) + bc->channel=0; + + if (find_free_chan_in_stack(stack,bc, bc->channel,0)<0) + goto ERR_NO_CHANNEL; + break; + case EVENT_RELEASE: + case EVENT_RELEASE_COMPLETE: + { + int channel=bc->channel; + int tmpcause=bc->cause; + empty_bc(bc); + bc->cause=tmpcause; + clean_up_bc(bc); + + if (channel>0) + empty_chan_in_stack(stack,channel); + bc->in_use=0; + } + break; + + default: + break; + } + + 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 { + if (reject) { + switch(bc->cause){ + case AST_CAUSE_USER_BUSY: + cb_log(1, stack->port, "Siemens Busy reject..\n"); + + break; + default: + break; + } + } + 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; +} + + +static 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->nt) 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; +} + + + +void misdn_lib_tone_generator_start(struct misdn_bchannel *bc) +{ + bc->generate_tone=1; +} + +void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc) +{ + bc->generate_tone=0; +} + + +static int do_tone(struct misdn_bchannel *bc, int len) +{ + bc->tone_cnt=len; + + if (bc->generate_tone) { + cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data); + + if ( !bc->nojitter ) { + misdn_tx_jitter(bc,len); + } + + return 1; + } + + return 0; +} + + +#ifdef MISDN_SAVE_DATA +static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2) +{ + char n1[32],n2[32]; + FILE *rx, *tx; + + sprintf(n1,"/tmp/misdn-rx-%d.raw",id); + sprintf(n2,"/tmp/misdn-tx-%d.raw",id); + + rx = fopen(n1,"a+"); + tx = fopen(n2,"a+"); + + if (!rx || !tx) { + cb_log(0,0,"Couldn't open files: %s\n",strerror(errno)); + return ; + } + + fwrite(p1,1,l1,rx); + fwrite(p2,1,l2,tx); + + fclose(rx); + fclose(tx); + +} +#endif + +void misdn_tx_jitter(struct misdn_bchannel *bc, int len) +{ + char buf[4096 + mISDN_HEADER_LEN]; + char *data=&buf[mISDN_HEADER_LEN]; + iframe_t *txfrm= (iframe_t*)buf; + int jlen, r; + + jlen=cb_jb_empty(bc,data,len); + + if (jlen) { +#ifdef MISDN_SAVE_DATA + misdn_save_data((bc->port*100+bc->channel), data, jlen, bc->bframe, bc->bframe_len); +#endif + flip_buf_bits( data, jlen); + + if (jlen < len) { + cb_log(7,bc->port,"Jitterbuffer Underrun.\n"); + } + + txfrm->prim = DL_DATA|REQUEST; + + txfrm->dinfo = 0; + + txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */ + + txfrm->len =jlen; + cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len); + + r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 ); + } else { +#define MISDN_GEN_SILENCE +#ifdef MISDN_GEN_SILENCE + int cnt=len/TONE_SILENCE_SIZE; + int rest=len%TONE_SILENCE_SIZE; + int i; + + for (i=0; i<cnt; i++) { + memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE ); + data +=TONE_SILENCE_SIZE; + } + + if (rest) { + memcpy(data, tone_silence_flip, rest); + } + + txfrm->prim = DL_DATA|REQUEST; + + txfrm->dinfo = 0; + + txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */ + + txfrm->len =len; + cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len); + + r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 ); +#endif + + } +} + +static int handle_bchan(msg_t *msg) +{ + iframe_t *frm= (iframe_t*)msg->data; + struct misdn_bchannel *bc=find_bc_by_addr(frm->addr); + struct misdn_stack *stack; + + if (!bc) { + cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo); + return 0 ; + } + + stack = get_stack_by_bc(bc); + + if (!stack) { + cb_log(0, bc->port,"handle_bchan: STACK not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo); + return 0; + } + + switch (frm->prim) { + + case MGR_SETSTACK| CONFIRM: + cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid); + break; + + case MGR_SETSTACK| INDICATION: + cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid); + break; +#if 0 + AGAIN: + bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer); + if (!bc->addr) { + + if (errno == EAGAIN) { + usleep(1000); + goto AGAIN; + } + + cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno)); + + /* we kill the channel later, when we received some + data. */ + bc->addr= frm->addr; + } else if ( bc->addr < 0) { + cb_log(0, stack->port,"$$$ bc->addr <0 Error:%s\n",strerror(errno)); + bc->addr=0; + } + + cb_log(4, stack->port," --> Got Adr %x\n", bc->addr); + + free_msg(msg); + + + switch(bc->bc_state) { + case BCHAN_SETUP: + bc_state_change(bc,BCHAN_SETUPED); + break; + + case BCHAN_CLEAN_REQUEST: + default: + cb_log(0, stack->port," --> STATE WASN'T SETUP (but %s) in SETSTACK|IND pid:%d\n",bc_state2str(bc->bc_state), bc->pid); + clean_up_bc(bc); + } + return 1; +#endif + + case MGR_DELLAYER| INDICATION: + cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid); + break; + + case MGR_DELLAYER| CONFIRM: + cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid); + + bc->pid=0; + bc->addr=0; + + free_msg(msg); + return 1; + + case PH_ACTIVATE | INDICATION: + case DL_ESTABLISH | INDICATION: + cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid); + + free_msg(msg); + return 1; + + case PH_ACTIVATE | CONFIRM: + case DL_ESTABLISH | CONFIRM: + + cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid); + free_msg(msg); + + return 1; + + case DL_ESTABLISH | REQUEST: + { + char buf[128]; + mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC); + } + free_msg(msg); + return 1; + + case DL_RELEASE|REQUEST: + { + char buf[128]; + mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC); + } + free_msg(msg); + return 1; + + case PH_DEACTIVATE | INDICATION: + case DL_RELEASE | INDICATION: + cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid); + + free_msg(msg); + return 1; + + case PH_DEACTIVATE | CONFIRM: + case DL_RELEASE | CONFIRM: + cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid); + + free_msg(msg); + return 1; + + case PH_CONTROL|INDICATION: + { + unsigned int *cont = (unsigned int *) &frm->data.p; + + cb_log(4, stack->port, "PH_CONTROL: channel:%d oad%d:%s dad%d:%s \n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad); + + 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|REQUEST: + case DL_DATA|REQUEST: + cb_log(0, stack->port, "DL_DATA REQUEST \n"); + do_tone(bc, 64); + + free_msg(msg); + return 1; + + + 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 (!bc->bframe_len) { + cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr); + free_msg(msg); + return 1; + } + + if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) { + cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr); + free_msg(msg); + return 1; + } + +#if MISDN_DEBUG + cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len); + +#endif + + if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) { + int t; + +#ifdef MISDN_B_DEBUG + cb_log(0,bc->port,"do_tone START\n"); +#endif + t=do_tone(bc,frm->len); + +#ifdef MISDN_B_DEBUG + cb_log(0,bc->port,"do_tone STOP (%d)\n",t); +#endif + if ( !t ) { + int i; + + if ( misdn_cap_is_speech(bc->capability)) { + if ( !bc->nojitter ) { +#ifdef MISDN_B_DEBUG + cb_log(0,bc->port,"tx_jitter START\n"); +#endif + misdn_tx_jitter(bc,frm->len); +#ifdef MISDN_B_DEBUG + cb_log(0,bc->port,"tx_jitter STOP\n"); +#endif + } + } + +#ifdef MISDN_B_DEBUG + cb_log(0,bc->port,"EVENT_B_DATA START\n"); +#endif + + i = cb_event(EVENT_BCHAN_DATA, bc, glob_mgr->user_data); +#ifdef MISDN_B_DEBUG + cb_log(0,bc->port,"EVENT_B_DATA STOP\n"); +#endif + + if (i<0) { + cb_log(10,stack->port,"cb_event returned <0\n"); + /*clean_up_bc(bc);*/ + } + } + } + free_msg(msg); + return 1; + } + + + case PH_CONTROL | CONFIRM: + cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr); + 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; + case DL_DATA|RESPONSE: +#if MISDN_DEBUG + cb_log(0, stack->port, "Data response\n"); + +#endif + break; + } + + return 0; +} + + + +static 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 (!stack || !stack->nt) { + 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 \n", err); + } else if (nt_err_cnt < 105){ + cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err); + nt_err_cnt = - 1; + } + } + free_msg(msg); + return 1; + + } + + return 1; +} + + +static int handle_frm(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + + struct misdn_stack *stack=find_stack_by_addr(frm->addr); + + if (!stack || stack->nt) { + return 0; + } + + cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim); + + { + struct misdn_bchannel dummybc; + struct misdn_bchannel *bc; + int ret=handle_cr(stack, frm); + + if (ret<0) { + cb_log(3,stack?stack->port:0,"handle_frm: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr); + + + } + + if(ret) { + free_msg(msg); + return 1; + } + + bc=find_bc_by_l3id(stack, frm->dinfo); + + if (!bc && (frm->prim==(CC_RESTART|CONFIRM)) ) { + misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); + bc=&dummybc; + } + + if (!bc && (frm->prim==(CC_SETUP|INDICATION)) ) { + misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); + dummybc.port=stack->port; + dummybc.l3_id=frm->dinfo; + bc=&dummybc; + + misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); + + free_msg(msg); + return 1; + } + + +handle_frm_bc: + if (bc ) { + enum event_e event = isdn_msg_get_event(msgs_g, msg, 0); + enum event_response_e response=RESPONSE_OK; + int ret; + + isdn_msg_parse_event(msgs_g,msg,bc, 0); + + /** Preprocess some Events **/ + ret = handle_event(bc, event, frm); + if (ret<0) { + cb_log(0,stack->port,"couldn't handle event\n"); + free_msg(msg); + return 1; + } + /* 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, "TOTALLY IGNORING SETUP\n"); + + break; + case RESPONSE_IGNORE_SETUP: + /* I think we should send CC_RELEASE_CR, but am not sure*/ + bc->out_cause = AST_CAUSE_NORMAL_CLEARING; + + case RESPONSE_RELEASE_SETUP: + misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); + if (bc->channel>0) + empty_chan_in_stack(stack, bc->channel); + empty_bc(bc); + bc_state_change(bc,BCHAN_CLEANED); + bc->in_use=0; + + cb_log(0, stack->port, "GOT IGNORE SETUP\n"); + break; + case RESPONSE_OK: + cb_log(4, stack->port, "GOT SETUP OK\n"); + + + break; + default: + break; + } + } + + if (event == EVENT_RELEASE_COMPLETE) { + /* release bchannel only after we've announced the RELEASE_COMPLETE */ + int channel=bc->channel; + int tmpcause=bc->cause; + int tmp_out_cause=bc->out_cause; + empty_bc(bc); + bc->cause=tmpcause; + bc->out_cause=tmp_out_cause; + clean_up_bc(bc); + + if (tmpcause == AST_CAUSE_REQUESTED_CHAN_UNAVAIL) { + cb_log(0,stack->port,"**** Received CAUSE:%d, so not cleaning up channel %d\n", AST_CAUSE_REQUESTED_CHAN_UNAVAIL, channel); + cb_log(0,stack->port,"**** This channel is now no longer available,\nplease try to restart it with 'misdn send restart <port> <channel>'\n"); + set_chan_in_stack(stack, channel); + bc->channel=channel; + misdn_lib_send_restart(stack->port, channel); + } else { + if (channel>0) + empty_chan_in_stack(stack, channel); + } + bc->in_use=0; + } + + if (event == EVENT_RESTART) { + cb_log(0, stack->port, "**** Received RESTART_ACK channel:%d\n", bc->restart_channel); + empty_chan_in_stack(stack, bc->restart_channel); + } + + cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim); + + + free_msg(msg); + return 1; +#endif + + } else { + struct misdn_bchannel dummybc; + cb_log(0, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo); + memset (&dummybc,0,sizeof(dummybc)); + dummybc.port=stack->port; + dummybc.l3_id=frm->dinfo; + bc=&dummybc; + goto handle_frm_bc; + } + } + + cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim); + return 0; +} + + +static int handle_l1(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + struct misdn_stack *stack = find_stack_by_addr(frm->addr); + int i ; + + if (!stack) return 0 ; + + switch (frm->prim) { + case PH_ACTIVATE | CONFIRM: + case PH_ACTIVATE | INDICATION: + cb_log (3, stack->port, "L1: PH L1Link Up!\n"); + stack->l1link=1; + + if (stack->nt) { + + if (stack->nst.l1_l2(&stack->nst, msg)) + free_msg(msg); + + if (stack->ptp) + misdn_lib_get_l2_up(stack); + } else { + free_msg(msg); + } + + for (i=0;i<=stack->b_num; i++) { + if (stack->bc[i].evq != EVENT_NOTHING) { + cb_log(4, stack->port, "Firing 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_ACTIVATE | REQUEST: + free_msg(msg); + cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n"); + return 1; + + case PH_DEACTIVATE | REQUEST: + free_msg(msg); + cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n"); + return 1; + + case PH_DEACTIVATE | CONFIRM: + case PH_DEACTIVATE | INDICATION: + cb_log (3, stack->port, "L1: PH L1Link Down! \n"); + +#if 0 + for (i=0; i<=stack->b_num; i++) { + if (global_state == MISDN_INITIALIZED) { + cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data); + } + } +#endif + + if (stack->nt) { + 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; +} + +static int handle_l2(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + + struct misdn_stack *stack = find_stack_by_addr(frm->addr); + + if (!stack) { + return 0 ; + } + + switch(frm->prim) { + + case DL_ESTABLISH | REQUEST: + cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n"); + return 1; + case DL_RELEASE | REQUEST: + cb_log(1,stack->port,"DL_RELEASE|REQUEST \n"); + return 1; + + case DL_ESTABLISH | INDICATION: + case DL_ESTABLISH | CONFIRM: + { + cb_log (3, stack->port, "L2: L2Link Up! \n"); + if (stack->ptp && stack->l2link) { + cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n"); + cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data); + } + stack->l2link=1; + free_msg(msg); + return 1; + } + break; + + case DL_RELEASE | INDICATION: + case DL_RELEASE | CONFIRM: + { + cb_log (3, stack->port, "L2: L2Link Down! \n"); + stack->l2link=0; + + free_msg(msg); + return 1; + } + break; + } + return 0; +} + +static int handle_mgmt(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + struct misdn_stack *stack; + + if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) { + cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ; + free_msg(msg); + return 1; + } + + stack = find_stack_by_addr(frm->addr); + + if (!stack) { + if (frm->prim == (MGR_DELLAYER|CONFIRM)) { + cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n", + frm->addr) ; + free_msg(msg); + return 1; + } + + return 0; + } + + switch(frm->prim) { + case MGR_SHORTSTATUS | INDICATION: + case MGR_SHORTSTATUS | CONFIRM: + cb_log(5, 0, "MGMT: Short status dinfo %x\n",frm->dinfo); + + switch (frm->dinfo) { + case SSTATUS_L1_ACTIVATED: + cb_log(3, 0, "MGMT: SSTATUS: L1_ACTIVATED \n"); + stack->l1link=1; + + break; + case SSTATUS_L1_DEACTIVATED: + cb_log(3, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n"); + stack->l1link=0; +#if 0 + clear_l3(stack); +#endif + break; + + case SSTATUS_L2_ESTABLISHED: + cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n"); + + /*when the L2 goes UP, L1 needs to be UP too*/ + stack->l1link=1; + stack->l2link=1; + break; + + case SSTATUS_L2_RELEASED: + cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n"); + stack->l2link=0; + break; + } + + free_msg(msg); + return 1; + + case MGR_SETSTACK | INDICATION: + cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo); + free_msg(msg); + return 1; + case MGR_DELLAYER | CONFIRM: + cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ; + free_msg(msg); + return 1; + + } + + /* + 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; +} + + +static msg_t *fetch_msg(int midev) +{ + msg_t *msg=alloc_msg(MAX_MSG_SIZE); + int r; + + if (!msg) { + cb_log(0, 0, "fetch_msg: alloc msg failed !!"); + return NULL; + } + + AGAIN: + r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC); + msg->len=r; + + if (r==0) { + free_msg(msg); /* danger, cause usually freeing in main_loop */ + cb_log(6,0,"Got empty Msg..\n"); + return NULL; + } + + if (r<0) { + if (errno == EAGAIN) { + /*we wait for mISDN here*/ + cb_log(4,0,"mISDN_read wants us to wait\n"); + usleep(5000); + goto AGAIN; + } + + cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno); + } + +#if 0 + if (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION))) + cb_log(0,0,"prim: %x dinfo:%x addr:%x msglen:%d frm->len:%d\n",frm->prim, frm->dinfo, frm->addr, msg->len,frm->len ); +#endif + return msg; +} + +void misdn_lib_isdn_l1watcher(int port) +{ + struct misdn_stack *stack; + + for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next) + ; + + if (stack) { + cb_log(4, port, "Checking L1 State\n"); + if (!stack->l1link) { + cb_log(4, port, "L1 State Down, trying to get it up again\n"); + misdn_lib_get_short_status(stack); + misdn_lib_get_l1_up(stack); + misdn_lib_get_l2_up(stack); + } + } +} + +/* This is a thread */ +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=0; + + while (1) { + msg_t *msg = fetch_msg(midev); + iframe_t *frm; + + + if (!msg) continue; + + frm = (iframe_t*) msg->data; + + /** When we make a call from NT2Ast we get these 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; + + 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, "Entity 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); +} + + + +static int test_inuse(struct misdn_bchannel *bc) +{ + struct timeval now; + gettimeofday(&now, NULL); + if (!bc->in_use) { + if (misdn_lib_port_is_pri(bc->port) && bc->last_used.tv_sec == now.tv_sec ) { + cb_log(2,bc->port, "channel with stid:%x for one second still in use! (n:%d lu:%d)\n", bc->b_stid, (int) now.tv_sec, (int) bc->last_used.tv_sec); + return 1; + } + + + cb_log(3,bc->port, "channel with stid:%x not in use!\n", bc->b_stid); + return 0; + } + + cb_log(2,bc->port, "channel with stid:%x in use!\n", bc->b_stid); + return 1; +} + + +static void prepare_bc(struct misdn_bchannel*bc, int channel) +{ + bc->channel = channel; + bc->channel_preselected = channel?1:0; + bc->in_use = 1; + bc->need_disconnect=1; + bc->need_release=1; + bc->need_release_complete=1; + bc->cause = AST_CAUSE_NORMAL_CLEARING; + + if (++mypid>5000) mypid=1; + bc->pid=mypid; + +#if 0 + bc->addr=0; + bc->b_stid=0; + bc->layer_id=0; +#endif +} + +struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout, int dec) +{ + struct misdn_stack *stack; + int i; + + if (channel < 0 || channel > MAX_BCHANS) { + cb_log(0,port,"Requested channel out of bounds (%d)\n",channel); + return NULL; + } + + usleep(1000); + + for (stack=glob_mgr->stack_list; stack; stack=stack->next) { + + if (stack->port == port) { + int maxnum; + + if (stack->blocked) { + cb_log(0,port,"Port is blocked\n"); + return NULL; + } + + if (channel > 0) { + if (channel <= stack->b_num) { + for (i = 0; i < stack->b_num; i++) { + if ( stack->bc[i].channel == channel) { + if (test_inuse(&stack->bc[i])) { + cb_log(0,port,"Requested channel:%d on port:%d is already in use\n",channel, port); + return NULL; + + } else { + prepare_bc(&stack->bc[i], channel); + return &stack->bc[i]; + } + } + } + } else { + cb_log(0,port,"Requested channel:%d is out of bounds on port:%d\n",channel, port); + return NULL; + } + } + + maxnum = inout && !stack->pri && !stack->ptp ? stack->b_num + 1 : stack->b_num; + + if (dec) { + for (i = maxnum-1; i>=0; i--) { + if (!test_inuse(&stack->bc[i])) { + /* 3. channel on bri means CW*/ + if (!stack->pri && i==stack->b_num) + stack->bc[i].cw=1; + + prepare_bc(&stack->bc[i], channel); + stack->bc[i].dec=1; + return &stack->bc[i]; + } + } + } else { + for (i = 0; i <maxnum; i++) { + if (!test_inuse(&stack->bc[i])) { + /* 3. channel on bri means CW*/ + if (!stack->pri && i==stack->b_num) + stack->bc[i].cw=1; + + prepare_bc(&stack->bc[i], channel); + return &stack->bc[i]; + } + } + } + + cb_log(1,port,"There is no free channel on port (%d)\n",port); + return NULL; + } + } + + cb_log(0,port,"Port is not configured (%d)\n",port); + return NULL; +} + + + + +/* ******************************************************************* */ +/*! + * \internal + * \brief Convert the facility function enum value into a string. + * + * \return String version of the enum value + */ +static const char *fac2str(enum FacFunction facility) +{ + static const struct { + enum FacFunction facility; + char *name; + } arr[] = { +/* *INDENT-OFF* */ + { Fac_None, "Fac_None" }, + { Fac_GetSupportedServices, "Fac_GetSupportedServices" }, + { Fac_Listen, "Fac_Listen" }, + { Fac_Suspend, "Fac_Suspend" }, + { Fac_Resume, "Fac_Resume" }, + { Fac_CFActivate, "Fac_CFActivate" }, + { Fac_CFDeactivate, "Fac_CFDeactivate" }, + { Fac_CFInterrogateParameters, "Fac_CFInterrogateParameters" }, + { Fac_CFInterrogateNumbers, "Fac_CFInterrogateNumbers" }, + { Fac_CD, "Fac_CD" }, + { Fac_AOCDCurrency, "Fac_AOCDCurrency" }, + { Fac_AOCDChargingUnit, "Fac_AOCDChargingUnit" }, +/* *INDENT-ON* */ + }; + + unsigned index; + + for (index = 0; index < ARRAY_LEN(arr); ++index) { + if (arr[index].facility == facility) { + return arr[index].name; + } + } /* end for */ + + return "unknown"; +} /* end fac2str() */ + +void misdn_lib_log_ies(struct misdn_bchannel *bc) +{ + struct misdn_stack *stack; + + if (!bc) return; + + stack = get_stack_by_bc(bc); + + if (!stack) return; + + cb_log(2, stack->port, " --> channel:%d mode:%s cause:%d ocause:%d rad:%s cad:%s\n", bc->channel, stack->nt?"NT":"TE", bc->cause, bc->out_cause, bc->rad, bc->cad); + + cb_log(2, stack->port, + " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c cpnnumplan:%c\n", + bc->info_dad, + bc->onumplan>=0?'0'+bc->onumplan:' ', + bc->dnumplan>=0?'0'+bc->dnumplan:' ', + bc->rnumplan>=0?'0'+bc->rnumplan:' ', + bc->cpnnumplan>=0?'0'+bc->cpnnumplan:' ' + ); + + cb_log(3, stack->port, " --> caps:%s pi:%x keypad:%s sending_complete:%d\n", bearer2str(bc->capability),bc->progress_indicator, bc->keypad, bc->sending_complete); + cb_log(4, stack->port, " --> screen:%d --> pres:%d\n", + bc->screen, bc->pres); + + cb_log(4, stack->port, " --> addr:%x l3id:%x b_stid:%x layer_id:%x\n", bc->addr, bc->l3_id, bc->b_stid, bc->layer_id); + + cb_log(4, stack->port, " --> facility:%s out_facility:%s\n",fac2str(bc->fac_in.Function),fac2str(bc->fac_out.Function)); + + cb_log(5, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1); + + cb_log(5, stack->port, " --> bc:%p h:%d sh:%d\n", bc, bc->holded, bc->stack_holder); +} + + +#define RETURN(a,b) {retval=a; goto b;} + +static void misdn_send_lock(struct misdn_bchannel *bc) +{ + //cb_log(0,bc->port,"Locking bc->pid:%d\n", bc->pid); + if (bc->send_lock) + pthread_mutex_lock(&bc->send_lock->lock); +} + +static void misdn_send_unlock(struct misdn_bchannel *bc) +{ + //cb_log(0,bc->port,"UnLocking bc->pid:%d\n", bc->pid); + if (bc->send_lock) + pthread_mutex_unlock(&bc->send_lock->lock); +} + +int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event ) +{ + msg_t *msg; + int retval=0; + struct misdn_stack *stack; + + if (!bc) RETURN(-1,OUT_POST_UNLOCK); + + stack = get_stack_by_bc(bc); + + if (!stack) { + cb_log(0,bc->port,"SENDEVENT: no Stack for event:%s oad:%s dad:%s \n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad); + RETURN(-1,OUT); + } + + misdn_send_lock(bc); + + + cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->upperid:%x\n",stack->nt, stack->upper_id); + + if ( stack->nt && !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)); + misdn_lib_get_l1_up(stack); + RETURN(0,OUT); + } + + cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s pid:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, bc->pid); + cb_log(4, stack->port, " --> bc_state:%s\n",bc_state2str(bc->bc_state)); + 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"); + + RETURN(-ENOCHAN,OUT); + } + break; + + case EVENT_PROGRESS: + case EVENT_ALERTING: + case EVENT_PROCEEDING: + case EVENT_SETUP_ACKNOWLEDGE: + case EVENT_CONNECT: + if (!stack->nt) break; + + case EVENT_RETRIEVE_ACKNOWLEDGE: + + if (stack->nt) { + if (bc->channel <=0 ) { /* else we have the channel already */ + if (find_free_chan_in_stack(stack, bc, 0, 0)<0) { + cb_log(0, stack->port, " No free channel at the moment\n"); + /*FIXME: add disconnect*/ + RETURN(-ENOCHAN,OUT); + } + } + /* Its that i generate channels */ + } + + retval=setup_bc(bc); + if (retval == -EINVAL) { + cb_log(0,bc->port,"send_event: setup_bc failed\n"); + } + + if (misdn_cap_is_speech(bc->capability)) { + if ((event==EVENT_CONNECT)||(event==EVENT_RETRIEVE_ACKNOWLEDGE)) { + if ( *bc->crypt_key ) { + cb_log(4, stack->port, " --> ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s \n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad); + + manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) ); + } + + if (!bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0); + 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; + + case EVENT_HOLD_ACKNOWLEDGE: + { + struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel)); + if (!holded_bc) { + cb_log(0,bc->port, "Could not allocate holded_bc!!!\n"); + RETURN(-1,OUT); + } + + /*backup the bc*/ + memcpy(holded_bc,bc,sizeof(struct misdn_bchannel)); + holded_bc->holded=1; + bc_state_change(holded_bc,BCHAN_CLEANED); + + stack_holder_add(stack,holded_bc); + + /*kill the bridge and clean the bchannel*/ + if (stack->nt) { + int channel; + if (bc->bc_state == BCHAN_BRIDGED) { + struct misdn_bchannel *bc2; + + misdn_split_conf(bc,bc->conf_id); + bc2 = find_bc_by_confid(bc->conf_id); + if (!bc2) { + cb_log(0,bc->port,"We have no second bc in bridge???\n"); + } else { + misdn_split_conf(bc2,bc->conf_id); + } + } + + channel = bc->channel; + + empty_bc(bc); + clean_up_bc(bc); + + if (channel>0) + empty_chan_in_stack(stack,channel); + + bc->in_use=0; + } + + } + break; + + /* finishing the channel eh ? */ + case EVENT_DISCONNECT: + if (!bc->need_disconnect) { + cb_log(0,bc->port," --> we have already send Disconnect\n"); + RETURN(-1,OUT); + } + + bc->need_disconnect=0; + break; + case EVENT_RELEASE: + if (!bc->need_release) { + cb_log(0,bc->port," --> we have already send Release\n"); + RETURN(-1,OUT); + } + bc->need_disconnect=0; + bc->need_release=0; + break; + case EVENT_RELEASE_COMPLETE: + if (!bc->need_release_complete) { + cb_log(0,bc->port," --> we have already send Release_complete\n"); + RETURN(-1,OUT); + } + bc->need_disconnect=0; + bc->need_release=0; + bc->need_release_complete=0; + + if (!stack->nt) { + /*create cleanup in TE*/ + int channel=bc->channel; + + int tmpcause=bc->cause; + int tmp_out_cause=bc->out_cause; + empty_bc(bc); + bc->cause=tmpcause; + bc->out_cause=tmp_out_cause; + clean_up_bc(bc); + + if (channel>0) + empty_chan_in_stack(stack,channel); + + bc->in_use=0; + } + break; + + case EVENT_CONNECT_ACKNOWLEDGE: + + if ( bc->nt || misdn_cap_is_speech(bc->capability)) { + int retval=setup_bc(bc); + if (retval == -EINVAL){ + cb_log(0,bc->port,"send_event: setup_bc failed\n"); + + } + } + + if (misdn_cap_is_speech(bc->capability)) { + if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0); + 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->nt); + msg_queue_tail(&stack->downqueue, msg); + sem_post(&glob_mgr->new_msg); + +OUT: + misdn_send_unlock(bc); + +OUT_POST_UNLOCK: + return retval; +} + + +static int handle_err(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + + + if (!frm->addr) { + static int cnt=0; + if (!cnt) + cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x\n",frm->prim,frm->dinfo); + cnt++; + if (cnt>100) { + cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x (already more than 100 of them)\n",frm->prim,frm->dinfo); + cnt=0; + } + + free_msg(msg); + return 1; + + } + + switch (frm->prim) { + case MGR_SETSTACK|INDICATION: + return handle_bchan(msg); + break; + + case MGR_SETSTACK|CONFIRM: + case MGR_CLEARSTACK|CONFIRM: + free_msg(msg) ; + return 1; + break; + + case DL_DATA|CONFIRM: + cb_log(4,0,"DL_DATA|CONFIRM\n"); + free_msg(msg); + return 1; + + case PH_CONTROL|CONFIRM: + cb_log(4,0,"PH_CONTROL|CONFIRM\n"); + free_msg(msg); + return 1; + + case DL_DATA|INDICATION: + { + int port=(frm->addr&MASTER_ID_MASK) >> 8; + int channel=(frm->addr&CHILD_ID_MASK) >> 16; + struct misdn_bchannel *bc; + + /*we flush the read buffer here*/ + + cb_log(9,0,"BCHAN DATA without BC: addr:%x port:%d channel:%d\n",frm->addr, port,channel); + + free_msg(msg); + return 1; + + + bc = find_bc_by_channel(port, channel); + + if (!bc) { + struct misdn_stack *stack = find_stack_by_port(port); + + if (!stack) { + cb_log(0,0," --> stack not found\n"); + free_msg(msg); + return 1; + } + + cb_log(0,0," --> bc not found by channel\n"); + if (stack->l2link) + misdn_lib_get_l2_down(stack); + + if (stack->l1link) + misdn_lib_get_l1_down(stack); + + free_msg(msg); + return 1; + } + + cb_log(3,port," --> BC in state:%s\n", bc_state2str(bc->bc_state)); + } + } + + return 0; +} + +#if 0 +static int queue_l2l3(msg_t *msg) +{ + iframe_t *frm= (iframe_t*)msg->data; + struct misdn_stack *stack; + stack=find_stack_by_addr( frm->addr ); + + + if (!stack) { + return 0; + } + + msg_queue_tail(&stack->upqueue, msg); + sem_post(&glob_mgr->new_msg); + return 1; +} +#endif + +int manager_isdn_handler(iframe_t *frm ,msg_t *msg) +{ + + if (frm->dinfo==0xffffffff && frm->prim==(PH_DATA|CONFIRM)) { + cb_log(0,0,"SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n"); + } + + if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) { + static int unhandled_bmsg_count=1000; + if (handle_bchan(msg)) { + return 0 ; + } + + if (unhandled_bmsg_count==1000) { + cb_log(0, 0, "received 1k Unhandled Bchannel Messages: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo); + unhandled_bmsg_count=0; + } + + unhandled_bmsg_count++; + free_msg(msg); + return 0; + } + +#ifdef RECV_FRM_SYSLOG_DEBUG + syslog(LOG_NOTICE,"mISDN recv: P(%02d): ADDR:%x PRIM:%x DINFO:%x\n",stack->port, frm->addr, frm->prim, frm->dinfo); +#endif + + 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_frm_nt(msg)) { + return 0; + } + + if (handle_frm(msg)) { + return 0; + } + + if (handle_err(msg)) { + return 0 ; + } + + cb_log(0, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo); + 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, "misdn_lib_get_port_info: alloc_msg failed!\n"); + return -1; + } + frm=(iframe_t*)msg->data; + if (!stack ) { + cb_log(0, port, "There is no Stack for this port.\n"); + return -1; + } + /* activate bchannel */ + frm->prim = CC_STATUS_ENQUIRY | REQUEST; + + frm->addr = stack->upper_id| FLG_MSG_DOWN; + + frm->dinfo = 0; + frm->len = 0; + + msg_queue_tail(&glob_mgr->activatequeue, msg); + sem_post(&glob_mgr->new_msg); + + + return 0; +} + + +int queue_cleanup_bc(struct misdn_bchannel *bc) +{ + msg_t *msg=alloc_msg(MAX_MSG_SIZE); + iframe_t *frm; + if (!msg) { + cb_log(0, bc->port, "queue_cleanup_bc: alloc_msg failed!\n"); + return -1; + } + frm=(iframe_t*)msg->data; + + /* activate bchannel */ + frm->prim = MGR_CLEARSTACK| REQUEST; + + frm->addr = bc->l3_id; + + frm->dinfo = bc->port; + frm->len = 0; + + msg_queue_tail(&glob_mgr->activatequeue, msg); + sem_post(&glob_mgr->new_msg); + + return 0; + +} + +int misdn_lib_pid_restart(int pid) +{ + struct misdn_bchannel *bc=manager_find_bc_by_pid(pid); + + if (bc) { + manager_clean_bc(bc); + } + return 0; +} + +/*Sends Restart message for every bchannel*/ +int misdn_lib_send_restart(int port, int channel) +{ + struct misdn_stack *stack=find_stack_by_port(port); + struct misdn_bchannel dummybc; + /*default is all channels*/ + cb_log(0, port, "Sending Restarts on this port.\n"); + + misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); + + /*default is all channels*/ + if (channel <0) { + dummybc.channel=-1; + cb_log(0, port, "Restarting and all Interfaces\n"); + misdn_lib_send_event(&dummybc, EVENT_RESTART); + + return 0; + } + + /*if a channel is specified we restart only this one*/ + if (channel >0) { + int cnt; + dummybc.channel=channel; + cb_log(0, port, "Restarting and cleaning channel %d\n",channel); + misdn_lib_send_event(&dummybc, EVENT_RESTART); + /* clean up chan in stack, to be sure we don't think it's + * in use anymore */ + for (cnt=0; cnt<=stack->b_num; cnt++) { + if (stack->bc[cnt].channel == channel) { + empty_bc(&stack->bc[cnt]); + clean_up_bc(&stack->bc[cnt]); + stack->bc[cnt].in_use=0; + } + } + } + + return 0; +} + +/*reinitializes the L2/L3*/ +int misdn_lib_port_restart(int port) +{ + struct misdn_stack *stack=find_stack_by_port(port); + + cb_log(0, port, "Restarting this port.\n"); + 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 failed\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 | FLG_MSG_DOWN; + + frm->dinfo = 0; + frm->len = 0; + msg_queue_tail(&glob_mgr->activatequeue, msg); + sem_post(&glob_mgr->new_msg); + } + + if (stack->nt) + misdn_lib_reinit_nt_stack(stack->port); + + } + + return 0; +} + + + +sem_t handler_started; + +/* This is a thread */ +static 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_CLEARSTACK | REQUEST: + /*a queued bchannel cleanup*/ + { + struct misdn_stack *stack=find_stack_by_port(frm->dinfo); + struct misdn_bchannel *bc; + if (!stack) { + cb_log(0,0,"no stack found with port [%d]!! so we cannot cleanup the bc\n",frm->dinfo); + free_msg(msg); + break; + } + + bc = find_bc_by_l3id(stack, frm->addr); + if (bc) { + cb_log(1,bc->port,"CLEARSTACK queued, cleaning up\n"); + clean_up_bc(bc); + } else { + cb_log(0,stack->port,"bc could not be cleaned correctly !! addr [%x]\n",frm->addr); + } + } + free_msg(msg); + break; + case MGR_SETSTACK | REQUEST : + /* Warning: memory leak here if we get this message */ + 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->upqueue)) ) { + /** Handle L2/3 Signalling after bchans **/ + if (!handle_frm_nt(msg)) { + /* Maybe it's TE */ + if (!handle_frm(msg)) { + /* wow none! */ + cb_log(0,stack->port,"Wow we've got a strange issue while dequeueing a Frame\n"); + } + } + } + + /* Here we should check if we really want to + send all the messages we've queued, lets + assume we've queued a Disconnect, but + received it already from the other side!*/ + + while ( (msg=msg_dequeue(&stack->downqueue)) ) { + if (stack->nt ) { + if (stack->nst.manager_l3(&stack->nst, msg)) + cb_log(0, stack->port, "Error@ Sending Message in NT-Stack.\n"); + + } else { + 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); + else { + if (frm->dinfo == MISDN_ID_GLOBAL) { + struct misdn_bchannel dummybc; + misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); + send_msg(glob_mgr->midev, &dummybc, msg); + } + } + } + } + } + } +} + + +int misdn_lib_maxports_get(void) +{ + /* 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; +} + + +void misdn_lib_nt_keepcalls( int kc) +{ +#ifdef FEATURE_NET_KEEPCALLS + if (kc) { + struct misdn_stack *stack=get_misdn_stack(); + for ( ; stack; stack=stack->next) { + stack->nst.feature |= FEATURE_NET_KEEPCALLS; + } + } +#endif +} + +void misdn_lib_nt_debug_init( int flags, char *file ) +{ + static int init=0; + char *f; + + if (!flags) + f=NULL; + else + f=file; + + if (!init) { + debug_init( flags , f, f, f); + init=1; + } else { + debug_close(); + debug_init( flags , f, f, f); + } +} + + +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_jb_empty = iface->cb_jb_empty; + + glob_mgr = mgr; + + msg_init(); + + misdn_lib_nt_debug_init(0,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 this port since we have only %d ports\n", port_count); + exit(1); + } + stack=stack_init(midev, port, ptp); + + if (!stack) { + perror("stack_init"); + exit(1); + } + + { + 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 (stack && first) { + mgr->stack_list=stack; + first=0; + continue; + } + + if (stack) { + struct misdn_stack * help; + for ( help=mgr->stack_list; help; help=help->next ) + if (help->next == NULL) break; + help->next=stack; + } + + } + + if (sem_init(&handler_started, 1, 0)<0) + sem_init(&handler_started, 0, 0); + + cb_log(8, 0, "Starting Event Handler\n"); + pthread_create( &mgr->event_handler_thread, NULL,(void*)manager_event_handler, mgr); + + sem_wait(&handler_started) ; + cb_log(8, 0, "Starting Event Catcher\n"); + pthread_create( &mgr->event_thread, NULL, (void*)misdn_lib_isdn_event_catcher, mgr); + + cb_log(8, 0, "Event Catcher started\n"); + + global_state= MISDN_INITIALIZED; + + return (mgr == NULL); +} + +void misdn_lib_destroy(void) +{ + 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 this port.\n"); + stack_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) +{ + char buf[128]; + + struct misdn_stack *stack=get_stack_by_bc(bc); + + if (!stack) { + cb_log(0, bc->port, "bchannel_activate: Stack not found !"); + return ; + } + + /* we must activate if we are deactivated */ + clear_ibuffer(bc->astbuf); + + cb_log(5, stack->port, "$$$ Bchan Activated addr %x\n", bc->addr); + + mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_DOWN, DL_ESTABLISH | REQUEST, 0,0, NULL, TIMEOUT_1SEC); + + return ; +} + + +void manager_bchannel_deactivate(struct misdn_bchannel * bc) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + iframe_t dact; + char buf[128]; + + switch (bc->bc_state) { + case BCHAN_ACTIVATED: + break; + case BCHAN_BRIDGED: + misdn_split_conf(bc,bc->conf_id); + break; + default: + cb_log( 4, bc->port,"bchan_deactivate: called but not activated\n"); + return ; + + } + + cb_log(5, stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr); + + bc->generate_tone=0; + + dact.prim = DL_RELEASE | REQUEST; + dact.addr = bc->addr | FLG_MSG_DOWN; + dact.dinfo = 0; + dact.len = 0; + mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_DOWN, DL_RELEASE|REQUEST,0,0,NULL, TIMEOUT_1SEC); + + clear_ibuffer(bc->astbuf); + + bc_state_change(bc,BCHAN_RELEASE); + + return; +} + + +int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + char buf[4096 + mISDN_HEADER_LEN]; + iframe_t *frm = (iframe_t*)buf; + int r; + + switch (bc->bc_state) { + case BCHAN_ACTIVATED: + case BCHAN_BRIDGED: + break; + default: + cb_log(3, bc->port, "BC not yet activated (state:%s)\n",bc_state2str(bc->bc_state)); + return -1; + } + + frm->prim = DL_DATA|REQUEST; + frm->dinfo = 0; + frm->addr = bc->addr | FLG_MSG_DOWN ; + + frm->len = len; + memcpy(&buf[mISDN_HEADER_LEN], data,len); + + if ( misdn_cap_is_speech(bc->capability) ) + flip_buf_bits( &buf[mISDN_HEADER_LEN], len); + else + cb_log(6, stack->port, "Writing %d data bytes\n",len); + + cb_log(9, stack->port, "Writing %d bytes 2 mISDN\n",len); + r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT); + return 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+2*sizeof(int)]; + iframe_t *ctrl = (iframe_t *)buffer; /* preload data */ + unsigned int *d = (unsigned int*)&ctrl->data.p; + /*struct misdn_stack *stack=get_stack_by_bc(bc);*/ + + cb_log(4,bc->port,"ph_control: c1:%x c2:%x\n",c1,c2); + + ctrl->prim = PH_CONTROL | REQUEST; + ctrl->addr = bc->addr | FLG_MSG_DOWN; + ctrl->dinfo = 0; + ctrl->len = sizeof(unsigned int)*2; + *d++ = c1; + *d++ = c2; + mISDN_write(glob_mgr->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC); +} + +/* + * allow live control of channel parameters + */ +void isdn_lib_update_rxgain (struct misdn_bchannel *bc) +{ + manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain); +} + +void isdn_lib_update_txgain (struct misdn_bchannel *bc) +{ + manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain); +} + +void isdn_lib_update_ec (struct misdn_bchannel *bc) +{ +#ifdef MISDN_1_2 + if (*bc->pipeline) +#else + if (bc->ec_enable) +#endif + manager_ec_enable(bc); + else + manager_ec_disable(bc); +} + +void isdn_lib_stop_dtmf (struct misdn_bchannel *bc) +{ + manager_ph_control(bc, DTMF_TONE_STOP, 0); +} + +/* + * 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 int *d = (unsigned int *)&ctrl->data.p; + /*struct misdn_stack *stack=get_stack_by_bc(bc);*/ + + ctrl->prim = PH_CONTROL | REQUEST; + ctrl->addr = bc->addr | FLG_MSG_DOWN; + ctrl->dinfo = 0; + ctrl->len = sizeof(unsigned int) + c2_len; + *d++ = c1; + memcpy(d, c2, c2_len); + mISDN_write(glob_mgr->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->channel>0) + empty_chan_in_stack(stack, bc->channel); + empty_bc(bc); + bc->in_use=0; + + cb_event(EVENT_CLEANUP, bc, NULL); +} + + +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; + holder->next=NULL; + + if (!stack->holding) { + stack->holding = holder; + return; + } + + for (help=stack->holding; + help; + help=help->next) { + if (!help->next) { + help->next=holder; + break; + } + } + +} + +void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder) +{ + struct misdn_bchannel *h1; + + if (!holder->stack_holder) return; + + holder->stack_holder=0; + + 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_bychan(struct misdn_stack *stack, int chan) +{ + struct misdn_bchannel *help; + + cb_log(4,stack?stack->port:0, "*HOLDER: find_bychan %c\n", chan); + + if (!stack) return NULL; + + for (help=stack->holding; + help; + help=help->next) { + if (help->channel == chan) { + cb_log(4,stack->port, "*HOLDER: found_bychan bc\n"); + return help; + } + } + + cb_log(4,stack->port, "*HOLDER: find_bychan nothing\n"); + return NULL; + +} + +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 %lx\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 misdn_lib_send_tone(struct misdn_bchannel *bc, enum tone_e tone) +{ + char buf[mISDN_HEADER_LEN + 128] = ""; + iframe_t *frm = (iframe_t*)buf; + + switch(tone) { + case TONE_DIAL: + manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_DIALTONE); + break; + + case TONE_ALERTING: + manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_RINGING); + break; + + case TONE_HANGUP: + manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_HANGUP); + break; + + case TONE_NONE: + default: + manager_ph_control(bc, TONE_PATT_OFF, TONE_GERMAN_HANGUP); + } + + frm->prim=DL_DATA|REQUEST; + frm->addr=bc->addr|FLG_MSG_DOWN; + frm->dinfo=0; + frm->len=128; + + mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC); +} + + +void manager_ec_enable(struct misdn_bchannel *bc) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + + cb_log(4, stack?stack->port:0,"ec_enable\n"); + + if (!misdn_cap_is_speech(bc->capability)) { + cb_log(1, stack?stack->port:0, " --> no speech? cannot enable EC\n"); + } else { + +#ifdef MISDN_1_2 + if (*bc->pipeline) { + cb_log(3, stack?stack->port:0,"Sending Control PIPELINE_CFG %s\n",bc->pipeline); + manager_ph_control_block(bc, PIPELINE_CFG, bc->pipeline, strlen(bc->pipeline) + 1); + } +#else + int ec_arr[2]; + + if (bc->ec_enable) { + cb_log(3, stack?stack->port:0,"Sending Control ECHOCAN_ON taps:%d\n",bc->ec_deftaps); + + 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]=0; + + manager_ph_control_block(bc, ECHOCAN_ON, ec_arr, sizeof(ec_arr)); + } +#endif + } +} + + + +void manager_ec_disable(struct misdn_bchannel *bc) +{ + struct misdn_stack *stack=get_stack_by_bc(bc); + + cb_log(4, stack?stack->port:0," --> ec_disable\n"); + + if (!misdn_cap_is_speech(bc->capability)) { + cb_log(1, stack?stack->port:0, " --> no speech? cannot disable EC\n"); + return; + } + +#ifdef MISDN_1_2 + manager_ph_control_block(bc, PIPELINE_CFG, "", 0); +#else + if ( ! bc->ec_enable) { + cb_log(3, stack?stack->port:0, "Sending Control ECHOCAN_OFF\n"); + manager_ph_control(bc, ECHOCAN_OFF, 0); + } +#endif +} + +struct misdn_stack* get_misdn_stack() { + return glob_mgr->stack_list; +} + + + +void misdn_join_conf(struct misdn_bchannel *bc, int conf_id) +{ + char data[16] = ""; + + bc_state_change(bc,BCHAN_BRIDGED); + manager_ph_control(bc, CMX_RECEIVE_OFF, 0); + manager_ph_control(bc, CMX_CONF_JOIN, conf_id); + + cb_log(3,bc->port, "Joining bc:%x in conf:%d\n",bc->addr,conf_id); + + misdn_lib_tx2misdn_frm(bc, data, sizeof(data) - 1); +} + + +void misdn_split_conf(struct misdn_bchannel *bc, int conf_id) +{ + bc_state_change(bc,BCHAN_ACTIVATED); + manager_ph_control(bc, CMX_RECEIVE_ON, 0); + manager_ph_control(bc, CMX_CONF_SPLIT, conf_id); + + cb_log(4,bc->port, "Splitting bc:%x in conf:%d\n",bc->addr,conf_id); +} + +void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2) +{ + int conf_id = bc1->pid + 1; + struct misdn_bchannel *bc_list[] = { bc1, bc2, NULL }; + struct misdn_bchannel **bc; + + cb_log(4, bc1->port, "I Send: BRIDGE from:%d to:%d\n",bc1->port,bc2->port); + + for (bc=bc_list; *bc; bc++) { + (*bc)->conf_id=conf_id; + cb_log(4, (*bc)->port, " --> bc_addr:%x\n",(*bc)->addr); + + switch((*bc)->bc_state) { + case BCHAN_ACTIVATED: + misdn_join_conf(*bc,conf_id); + break; + default: + bc_next_state_change(*bc,BCHAN_BRIDGED); + break; + } + } +} + +void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2) +{ + + struct misdn_bchannel *bc_list[]={ + bc1,bc2,NULL + }; + struct misdn_bchannel **bc; + + for (bc=bc_list; *bc; bc++) { + if ( (*bc)->bc_state == BCHAN_BRIDGED){ + misdn_split_conf( *bc, (*bc)->conf_id); + } else { + cb_log( 2, (*bc)->port, "BC not bridged (state:%s) so not splitting it\n",bc_state2str((*bc)->bc_state)); + } + } + +} + + + +void misdn_lib_echo(struct misdn_bchannel *bc, int onoff) +{ + cb_log(3,bc->port, " --> ECHO %s\n", onoff?"ON":"OFF"); + manager_ph_control(bc, onoff?CMX_ECHO_ON:CMX_ECHO_OFF, 0); +} + + + +void misdn_lib_reinit_nt_stack(int port) +{ + struct misdn_stack *stack=find_stack_by_port(port); + + if (stack) { + stack->l2link=0; + stack->blocked=0; + + cleanup_Isdnl3(&stack->nst); + cleanup_Isdnl2(&stack->nst); + + + 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 = glob_mgr->midev; + stack->nst.cardnr = port; + stack->nst.d_stid = stack->d_stid; + + 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; + + stack->nst.l1_id = stack->lower_id; + stack->nst.l2_id = stack->upper_id; + + msg_queue_init(&stack->nst.down_queue); + + Isdnl2Init(&stack->nst); + Isdnl3Init(&stack->nst); + + if (!stack->ptp) + misdn_lib_get_l1_up(stack); + misdn_lib_get_l2_up(stack); + } +} + + diff --git a/channels/misdn/isdn_lib.h b/channels/misdn/isdn_lib.h new file mode 100644 index 000000000..451876888 --- /dev/null +++ b/channels/misdn/isdn_lib.h @@ -0,0 +1,674 @@ +/* + * 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 + */ + +/*! \file + * \brief Interface to mISDN + * + * \author Christian Richter <crich@beronet.com> + */ + +#ifndef TE_LIB +#define TE_LIB + +#include <mISDNuser/suppserv.h> + +/** For initialization usage **/ +/* typedef int ie_nothing_t ;*/ +/** end of init usage **/ + + +/* + * uncomment the following to make chan_misdn create + * record files in /tmp/misdn-{rx|tx}-PortChannel format + * */ + +/*#define MISDN_SAVE_DATA*/ + +#ifdef WITH_BEROEC +typedef int beroec_t; + + +enum beroec_type { + BEROEC_FULLBAND=0, + BEROEC_SUBBAND, + BEROEC_FASTSUBBAND +}; + +void beroec_init(void); +void beroec_exit(void); +beroec_t *beroec_new(int tail, enum beroec_type type, int anti_howl, + int tonedisable, int zerocoeff, int adapt, int nlp); + +void beroec_destroy(beroec_t *ec); +int beroec_cancel_alaw_chunk(beroec_t *ec, + char *send, + char *receive, + int len); + +int beroec_version(void); +#endif + + + +enum tone_e { + TONE_NONE=0, + TONE_DIAL, + TONE_ALERTING, + TONE_FAR_ALERTING, + TONE_BUSY, + TONE_HANGUP, + TONE_CUSTOM, + TONE_FILE +}; + + + +#define MAX_BCHANS 31 + +enum bchannel_state { + BCHAN_CLEANED=0, + BCHAN_EMPTY, + BCHAN_SETUP, + BCHAN_SETUPED, + BCHAN_ACTIVE, + BCHAN_ACTIVATED, + BCHAN_BRIDGE, + BCHAN_BRIDGED, + BCHAN_RELEASE, + BCHAN_RELEASED, + BCHAN_CLEAN, + BCHAN_CLEAN_REQUEST, + BCHAN_ERROR +}; + + +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_RELEASE_SETUP, + RESPONSE_ERR, + RESPONSE_OK +}; + + +enum event_e { + EVENT_NOTHING, + EVENT_TONE_GENERATE, + EVENT_BCHAN_DATA, + EVENT_BCHAN_ACTIVATED, + EVENT_BCHAN_ERROR, + 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_PORT_ALARM, + EVENT_NEW_CHANNEL, + 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 +}; + + + +struct misdn_bchannel { + /*! \brief B channel send locking structure */ + struct send_lock *send_lock; + + /*! \brief TRUE if this is a dummy BC record */ + int dummy; + + /*! \brief TRUE if NT side of protocol (TE otherwise) */ + int nt; + + /*! \brief TRUE if ISDN-PRI (ISDN-BRI otherwise) */ + int pri; + + /*! \brief Logical Layer 1 port associated with this B channel */ + int port; + + /** init stuff **/ + /*! \brief B Channel mISDN driver stack ID */ + int b_stid; + + /* int b_addr; */ + + /*! \brief B Channel mISDN driver layer ID from mISDN_new_layer() */ + int layer_id; + + /*! \brief B channel layer; set to 3 or 4 */ + int layer; + + /* state stuff */ + /*! \brief TRUE if DISCONNECT needs to be sent to clear a call */ + int need_disconnect; + + /*! \brief TRUE if RELEASE needs to be sent to clear a call */ + int need_release; + + /*! \brief TRUE if RELEASE_COMPLETE needs to be sent to clear a call */ + int need_release_complete; + + /*! \brief TRUE if allocate higher B channels first */ + int dec; + + /* var stuff */ + /*! \brief Layer 3 process ID */ + int l3_id; + + /*! \brief B channel process ID (1-5000) */ + int pid; + + /*! \brief Not used. Saved mISDN stack CONNECT_t ces value */ + int ces; + + /*! \brief B channel to restart if received a RESTART message */ + int restart_channel; + + /*! \brief Assigned B channel number B1, B2... 0 if not assigned */ + int channel; + + /*! \brief TRUE if the B channel number is preselected */ + int channel_preselected; + + /*! \brief TRUE if B channel record is in use */ + int in_use; + + /*! \brief Time when empty_bc() last called on this record */ + struct timeval last_used; + + /*! \brief TRUE if call waiting */ + int cw; + + /*! \brief B Channel mISDN driver layer ID from mISDN_get_layerid() */ + int addr; + + /*! \brief B channel speech sample data buffer */ + char *bframe; + + /*! \brief B channel speech sample data buffer size */ + int bframe_len; + int time_usec; /* Not used */ + + /*! \brief Not used. Contents are setup but not used. */ + void *astbuf; + + void *misdnbuf; /* Not used */ + + /*! \brief TRUE if the TE side should choose the B channel to use + * \note This value is user configurable in /etc/asterisk/misdn.conf + */ + int te_choose_channel; + + /*! \brief TRUE if the call progress indicators can indicate an inband audio message for the user to listen to + * \note This value is user configurable in /etc/asterisk/misdn.conf + */ + int early_bconnect; + + /*! \brief Last decoded DTMF digit from mISDN driver */ + int dtmf; + + /*! \brief TRUE if we should produce DTMF tones ourselves + * \note This value is user configurable in /etc/asterisk/misdn.conf + */ + int send_dtmf; + + /*! \brief TRUE if we send SETUP_ACKNOWLEDGE on incoming calls anyway (instead of PROCEEDING). + * + * This requests additional INFORMATION messages, so we can + * wait for digits without issues. + * \note This value is user configurable in /etc/asterisk/misdn.conf + */ + int need_more_infos; + + /*! \brief TRUE if all digits necessary to complete the call are available. + * No more INFORMATION messages are needed. + */ + int sending_complete; + + + /*! \brief TRUE if we will not use jollys dsp */ + int nodsp; + + /*! \brief TRUE if we will not use the jitter buffer system */ + int nojitter; + + /*! \brief Type-of-number in ISDN terms for the dialed/called number + * \note This value is set to "dialplan" in /etc/asterisk/misdn.conf for outgoing calls + */ + enum mISDN_NUMBER_PLAN dnumplan; + + /*! \brief Type-of-number in ISDN terms for the redirecting number which a call diversion or transfer was invoked. + * \note Collected from the incoming SETUP message but not used. + */ + enum mISDN_NUMBER_PLAN rnumplan; + + /*! \brief Type-of-number in ISDN terms for the originating/calling number (Caller-ID) + * \note This value is set to "localdialplan" in /etc/asterisk/misdn.conf for outgoing calls + */ + enum mISDN_NUMBER_PLAN onumplan; + + /*! \brief Type-of-number in ISDN terms for the connected party number + * \note This value is set to "cpndialplan" in /etc/asterisk/misdn.conf for outgoing calls + */ + enum mISDN_NUMBER_PLAN cpnnumplan; + + /*! \brief Progress Indicator IE coding standard field. + * \note Collected from the incoming messages but not used. + */ + int progress_coding; + + /*! \brief Progress Indicator IE location field. + * \note Collected from the incoming messages but not used. + */ + int progress_location; + + /*! \brief Progress Indicator IE progress description field. + * Used to determine if there is an inband audio message present. + */ + int progress_indicator; + + /*! \brief Inbound FACILITY message function type and contents */ + struct FacParm fac_in; + + /*! \brief Outbound FACILITY message function type and contents. + * \note Filled in by misdn facility commands before FACILITY message sent. + */ + struct FacParm fac_out; + + /* storing the current AOCD info here */ + enum FacFunction AOCDtype; + union { + struct FacAOCDCurrency currency; + struct FacAOCDChargingUnit chargingUnit; + } AOCD; + + /*! \brief Event waiting for Layer 1 to come up */ + enum event_e evq; + + /*** CRYPTING STUFF ***/ + int crypt; /* Initialized, Not used */ + int curprx; /* Initialized, Not used */ + int curptx; /* Initialized, Not used */ + + /*! \brief Blowfish encryption key string (secret) */ + char crypt_key[255]; + + int crypt_state; /* Not used */ + /*** CRYPTING STUFF END***/ + + /*! \brief Seems to have been intended for something to do with the jitter buffer. + * \note Used as a boolean. Only initialized to 0 and referenced in a couple places + */ + int active; + int upset; /* Not used */ + + /*! \brief TRUE if tone generator allowed to start */ + int generate_tone; + + /*! \brief Number of tone samples to generate */ + int tone_cnt; + + /*! \brief Current B Channel state */ + enum bchannel_state bc_state; + + /*! \brief This is used as a pending bridge join request for when bc_state becomes BCHAN_ACTIVATED */ + enum bchannel_state next_bc_state; + + /*! \brief Bridging conference ID */ + int conf_id; + + /*! \brief TRUE if this channel is on hold */ + int holded; + + /*! \brief TRUE if this channel is on the misdn_stack->holding list + * \note If TRUE this implies that the structure is also malloced. + */ + int stack_holder; + + /*! \brief Caller ID presentation restriction code + * 0=Allowed, 1=Restricted, 2=Unavailable + * \note It is settable by the misdn_set_opt() application. + */ + int pres; + + /*! \brief Caller ID screening code + * 0=Unscreened, 1=Passed Screen, 2=Failed Screen, 3=Network Number + */ + int screen; + + /*! \brief SETUP message bearer capability field code value */ + int capability; + + /*! \brief Companding ALaw/uLaw encoding (INFO_CODEC_ALAW / INFO_CODEC_ULAW) */ + int law; + + /* V110 Stuff */ + /*! \brief Q.931 Bearer Capability IE Information Transfer Rate field. Initialized to 0x10 (64kbit). Altered by incoming SETUP messages. */ + int rate; + + /*! \brief Q.931 Bearer Capability IE Transfer Mode field. Initialized to 0 (Circuit). Altered by incoming SETUP messages. */ + int mode; + + /*! \brief Q.931 Bearer Capability IE User Information Layer 1 Protocol field code. + * \note Collected from the incoming SETUP message but not used. + */ + int user1; + + /*! \brief Q.931 Bearer Capability IE Layer 1 User Rate field. + * \note Collected from the incoming SETUP message and exported to Asterisk variable MISDN_URATE. + */ + int urate; + + /*! \brief TRUE if call made in digital HDLC mode + * \note This value is user configurable in /etc/asterisk/misdn.conf. + * It is also settable by the misdn_set_opt() application. + */ + int hdlc; + /* V110 */ + + /*! \brief Display message that can be displayed by the user phone. + * \note Maximum displayable length is 34 or 82 octets. + * It is also settable by the misdn_set_opt() application. + */ + char display[84]; + + /*! \brief Not used. Contents are setup but not used. */ + char msn[32]; + + /*! \brief Originating/Calling Phone Number (Address) + * \note This value can be set to "callerid" in /etc/asterisk/misdn.conf for outgoing calls + */ + char oad[32]; + + /*! \brief Redirecting Phone Number (Address) where a call diversion or transfer was invoked */ + char rad[32]; + + /*! \brief Dialed/Called Phone Number (Address) */ + char dad[32]; + + /*! \brief Connected Party/Line Phone Number (Address) */ + char cad[32]; + + /*! \brief Original Dialed/Called Phone Number (Address) before national/international dialing prefix added. + * \note Not used. Contents are setup but not used. + */ + char orig_dad[32]; + + /*! \brief Q.931 Keypad Facility IE contents + * \note Contents exported and imported to Asterisk variable MISDN_KEYPAD + */ + char keypad[32]; + + /*! \brief Current overlap dialing digits to/from INFORMATION messages */ + char info_dad[64]; + + /*! \brief Collected digits to go into info_dad[] while waiting for a SETUP_ACKNOWLEDGE to come in. */ + char infos_pending[64]; + +/* unsigned char info_keypad[32]; */ +/* unsigned char clisub[24]; */ +/* unsigned char cldsub[24]; */ + + /*! \brief User-User information string. + * \note Contents exported and imported to Asterisk variable MISDN_USERUSER + * \note We only support ASCII strings (IA5 characters). + */ + char uu[256]; + + /*! \brief User-User information string length in uu[] */ + int uulen; + + /*! \brief Q.931 Cause for disconnection code (received) + * \note Need to use the AST_CAUSE_xxx code definitions in causes.h + */ + int cause; + + /*! \brief Q.931 Cause for disconnection code (sent) + * \note Need to use the AST_CAUSE_xxx code definitions in causes.h + * \note -1 is used to suppress including the cause code in the RELEASE message. + */ + int out_cause; + + /* struct misdn_bchannel hold_bc; */ + + /** list stuf **/ + +#ifdef MISDN_1_2 + /*! \brief The configuration string for the mISDN dsp pipeline in /etc/asterisk/misdn.conf. */ + char pipeline[128]; +#else + /*! \brief TRUE if the echo cancellor is enabled */ + int ec_enable; + + /*! \brief Number of taps in the echo cancellor when enabled. + * \note This value is user configurable in /etc/asterisk/misdn.conf (echocancel) + */ + int ec_deftaps; +#endif + + /*! \brief TRUE if the channel was allocated from the available B channels */ + int channel_found; + + /*! \brief Who originated the call (ORG_AST, ORG_MISDN) + * \note Set but not used when the misdn_set_opt() application enables echo cancellation. + */ + int orig; + + /*! \brief Tx gain setting (range -8 to 8) + * \note This value is user configurable in /etc/asterisk/misdn.conf. + * It is also settable by the misdn_set_opt() application. + */ + int txgain; + + /*! \brief Rx gain setting (range -8 to 8) + * \note This value is user configurable in /etc/asterisk/misdn.conf. + * It is also settable by the misdn_set_opt() application. + */ + int rxgain; + + /*! \brief Next node in the misdn_stack.holding list */ + 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, ...) + __attribute__ ((format (printf, 3, 4))); +int (*cb_jb_empty)(struct misdn_bchannel *bc, char *buffer, int len); + +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, ...) + __attribute__ ((format (printf, 3, 4))); + int (*cb_jb_empty)(struct misdn_bchannel *bc, char *buffer, int len); +}; + +/***** USER IFACE **********/ + +void misdn_lib_nt_keepcalls(int kc); + +void misdn_lib_nt_debug_init( int flags, char *file ); + +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_isdn_l1watcher(int port); + +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, int inout, int dec); + +void manager_bchannel_activate(struct misdn_bchannel *bc); +void manager_bchannel_deactivate(struct misdn_bchannel * bc); + +int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len); + +void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2); + +void isdn_lib_update_rxgain (struct misdn_bchannel *bc); +void isdn_lib_update_txgain (struct misdn_bchannel *bc); +void isdn_lib_update_ec (struct misdn_bchannel *bc); +void isdn_lib_stop_dtmf (struct misdn_bchannel *bc); + +int misdn_lib_port_restart(int port); +int misdn_lib_pid_restart(int pid); +int misdn_lib_send_restart(int port, int channel); + +int misdn_lib_get_port_info(int port); + +int misdn_lib_is_port_blocked(int port); +int misdn_lib_port_block(int port); +int misdn_lib_port_unblock(int port); + +int misdn_lib_port_is_pri(int port); + +int misdn_lib_port_up(int port, int notcheck); + +int misdn_lib_get_port_down(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); + +void manager_ec_enable(struct misdn_bchannel *bc); +void manager_ec_disable(struct misdn_bchannel *bc); + +void misdn_lib_send_tone(struct misdn_bchannel *bc, enum tone_e tone); + +void get_show_stack_details(int port, char *buf); + + +void misdn_lib_tone_generator_start(struct misdn_bchannel *bc); +void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc); + + +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); + +void misdn_lib_echo(struct misdn_bchannel *bc, int onoff); + +int misdn_lib_is_ptp(int port); +int misdn_lib_get_maxchans(int port); + +void misdn_lib_reinit_nt_stack(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 + + + +char *bc_state2str(enum bchannel_state state); +void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state); + +void misdn_dump_chanlist(void); + +#endif diff --git a/channels/misdn/isdn_lib_intern.h b/channels/misdn/isdn_lib_intern.h new file mode 100644 index 000000000..93d879744 --- /dev/null +++ b/channels/misdn/isdn_lib_intern.h @@ -0,0 +1,142 @@ +#ifndef ISDN_LIB_INTERN +#define ISDN_LIB_INTERN + + +#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" + +#if !defined MISDNUSER_VERSION_CODE || (MISDNUSER_VERSION_CODE < MISDNUSER_VERSION(1, 0, 3)) +#error "You need a newer version of mISDNuser ..." +#endif + + +#define QI_ELEMENT(a) a.off + + +#ifndef mISDNUSER_HEAD_SIZE + +#define mISDNUSER_HEAD_SIZE (sizeof(mISDNuser_head_t)) +/*#define mISDNUSER_HEAD_SIZE (sizeof(mISDN_head_t))*/ +#endif + + +ibuffer_t *astbuf; /* Not used */ +ibuffer_t *misdnbuf; /* Not used */ + +struct send_lock { + pthread_mutex_t lock; +}; + + +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); + 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; + + /*! \brief D Channel mISDN driver stack ID (Parent stack ID) */ + int d_stid; + + /*! /brief Number of B channels supported by this port */ + int b_num; + + /*! \brief B Channel mISDN driver stack IDs (Child stack IDs) */ + int b_stids[MAX_BCHANS + 1]; + + /*! \brief TRUE if Point-To-Point(PTP) (Point-To-Multipoint(PTMP) otherwise) */ + int ptp; + + /*! \brief Number of consecutive times PTP Layer 2 declared down */ + int l2upcnt; + + int l2_id; /* Not used */ + + /*! \brief Lower layer mISDN ID (addr) (Layer 1/3) */ + int lower_id; + + /*! \brief Upper layer mISDN ID (addr) (Layer 2/4) */ + int upper_id; + + /*! \brief TRUE if port is blocked */ + int blocked; + + /*! \brief TRUE if Layer 2 is UP */ + int l2link; + + time_t l2establish; /* Not used */ + + /*! \brief TRUE if Layer 1 is UP */ + int l1link; + + /*! \brief TRUE if restart has been sent to the other side after stack startup */ + int restart_sent; + + /*! \brief mISDN device handle returned by mISDN_open() */ + int midev; + + /*! \brief TRUE if NT side of protocol (TE otherwise) */ + int nt; + + /*! \brief TRUE if ISDN-PRI (ISDN-BRI otherwise) */ + int pri; + + /*! \brief CR Process ID allocation table. TRUE if ID allocated */ + int procids[0x100+1]; + + /*! \brief Queue of Event messages to send to mISDN */ + msg_queue_t downqueue; + msg_queue_t upqueue; /* No code puts anything on this queue */ + int busy; /* Not used */ + + /*! \brief Logical Layer 1 port associated with this stack */ + int port; + + /*! \brief B Channel record pool array */ + struct misdn_bchannel bc[MAX_BCHANS + 1]; + + struct misdn_bchannel* bc_list; /* Not used */ + + /*! \brief Array of B channels in use (a[0] = B1). TRUE if B channel in use */ + int channels[MAX_BCHANS + 1]; + + /*! \brief List of holded channels */ + struct misdn_bchannel *holding; + + /*! \brief Next stack in the list of stacks */ + struct misdn_stack *next; +}; + + +struct misdn_stack* get_stack_by_bc(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); +int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt); +char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt); + + +#endif diff --git a/channels/misdn/isdn_msg_parser.c b/channels/misdn/isdn_msg_parser.c new file mode 100644 index 000000000..a587f8eae --- /dev/null +++ b/channels/misdn/isdn_msg_parser.c @@ -0,0 +1,1348 @@ +/* + * 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" + + +#include "isdn_lib.h" + +#include "ie.c" + + +static void set_channel(struct misdn_bchannel *bc, int channel) +{ + + cb_log(3,bc->port,"set_channel: bc->channel:%d channel:%d\n", bc->channel, channel); + + + if (channel==0xff) { + /* any channel */ + channel=-1; + } + + /* ALERT: is that everytime true ? */ + if (channel > 0 && bc->nt ) { + + if (bc->channel && ( bc->channel != 0xff) ) { + cb_log(0,bc->port,"We already have a channel (%d)\n", bc->channel); + } else { + bc->channel = channel; + cb_event(EVENT_NEW_CHANNEL,bc,NULL); + } + } + + if (channel > 0 && !bc->nt ) { + bc->channel = channel; + cb_event(EVENT_NEW_CHANNEL,bc,NULL); + } +} + +static 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); + + set_channel(bc,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 +} +static 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; +} + +static 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 + + +} + +static 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; +} + + +static 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 +} + +static 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; +} + +static 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, id, sizeof(id)-1, nt,bc); + + bc->onumplan=type; + strcpy(bc->oad, id); + switch (present) { + case 0: + bc->pres=0; /* screened */ + break; + case 1: + bc->pres=1; /* not screened */ + break; + default: + bc->pres=0; + } + switch (screen) { + case 0: + break; + default: + ; + } + } + { + int type, plan; + char number[32]; + dec_ie_called_pn(setup->CALLED_PN, (Q931_info_t *)setup, &type, &plan, number, sizeof(number)-1, nt,bc); + strcpy(bc->dad, number); + bc->dnumplan=type; + } + { + char keypad[32]; + dec_ie_keypad(setup->KEYPAD, (Q931_info_t *)setup, keypad, sizeof(keypad)-1, nt,bc); + strcpy(bc->keypad, keypad); + } + + { + dec_ie_complete(setup->COMPLETE, (Q931_info_t *)setup, &bc->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, id, sizeof(id)-1, nt,bc); + + strcpy(bc->rad, id); + bc->rnumplan=type; + } + { + 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; + break; + case 0: bc->capability=INFO_CAPABILITY_SPEECH; + break; + case 18: bc->capability=INFO_CAPABILITY_VIDEO; + break; + case 8: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; + bc->user1 = user; + bc->urate = urate; + + bc->rate = rate; + bc->mode = mode; + break; + case 9: bc->capability=INFO_CAPABILITY_DIGITAL_RESTRICTED; + break; + default: + 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); + + set_channel(bc,channel); + } + + { + int protocol ; + dec_ie_useruser(setup->USER_USER, (Q931_info_t *)setup, &protocol, bc->uu, &bc->uulen, nt,bc); + if (bc->uulen) cb_log(1,bc->port,"USERUESRINFO:%s\n",bc->uu); + else + cb_log(1,bc->port,"NO USERUESRINFO\n"); + } + + 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' */ +static 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)); + + 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->rad[0]) + enc_ie_redir_nr(&setup->REDIR_NR, msg, 1, 1, bc->pres, bc->screen, 0, bc->rad, nt,bc); + } + + { + if (bc->keypad[0]) + enc_ie_keypad(&setup->KEYPAD, msg, bc->keypad, 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; + mode=bc->mode; + rate=bc->rate; + 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 (bc->sending_complete) { + enc_ie_complete(&setup->COMPLETE,msg, bc->sending_complete, nt, bc); + } + + if (bc->uulen) { + int protocol=4; + enc_ie_useruser(&setup->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc); + cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu); + } + +#if DEBUG + printf("Building SETUP Msg\n"); +#endif + return msg; +} + +static 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)); + + int plan,pres,screen; + + 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); + + dec_ie_connected_pn(connect->CONNECT_PN,(Q931_info_t *)connect, &bc->cpnnumplan, &plan, &pres, &screen, bc->cad, 31, nt, bc); + + /* + cb_log(1,bc->port,"CONNETED PN: %s cpn_dialplan:%d\n", connected_pn, type); + */ + +#if DEBUG + printf("Parsing CONNECT Msg\n"); +#endif +} + +static 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); + + cb_log(6,bc->port,"BUILD_CONNECT: bc:%p bc->l3id:%d, nt:%d\n",bc,bc->l3_id,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=bc->cpnnumplan, plan=1, present=2, screen=0; + enc_ie_connected_pn(&connect->CONNECT_PN, msg, type,plan, present, screen, bc->cad, nt , bc); + } + +#if DEBUG + printf("Building CONNECT Msg\n"); +#endif + return msg; +} + +static 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); + + + set_channel(bc, 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 + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing HOLD Msg\n"); +#endif + + +} + +static 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; +} + +static void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing SUSPEND Msg\n"); +#endif + + +} + +static 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; +} + +static void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing RESUME Msg\n"); +#endif + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing RETRIEVE Msg\n"); +#endif + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static 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 + + +} + +static 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; +} + +static 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; + int cause; + dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &cause, nt,bc); + if (cause>0) bc->cause=cause; + + 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 + + +} + +static 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 (bc->uulen) { + int protocol=4; + enc_ie_useruser(&disconnect->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc); + cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu); + } + +#if DEBUG + printf("Building DISCONNECT Msg\n"); +#endif + return msg; +} + +static 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; + dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &bc->restart_channel, nt,bc); + cb_log(3, stack->port, "CC_RESTART Request on channel:%d on this port.\n", bc->restart_channel); + } + +} + +static 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 + + if (bc->channel > 0) { + enc_ie_channel_id(&restart->CHANNEL_ID, msg, 1,bc->channel, nt,bc); + enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x80, nt, bc); + } else { + enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x87, nt, bc); + } + + cb_log(0,bc->port, "Restarting channel %d\n", bc->channel); + + return msg; +} + +static 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; + int cause; + + dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &cause, nt,bc); + if (cause>0) bc->cause=cause; +#if DEBUG + printf("Parsing RELEASE Msg\n"); +#endif + + +} + +static 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)); + + if (bc->out_cause>= 0) + enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc); + + if (bc->uulen) { + int protocol=4; + enc_ie_useruser(&release->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc); + cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu); + } + +#if DEBUG + printf("Building RELEASE Msg\n"); +#endif + return msg; +} + +static 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; + int cause; + iframe_t *frm = (iframe_t*) msg->data; + + struct misdn_stack *stack=get_stack_by_bc(bc); + mISDNuser_head_t *hh; + hh=(mISDNuser_head_t*)msg->data; + + /*hh=(mISDN_head_t*)msg->data; + mISDN_head_t *hh;*/ + + if (nt) { + if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) { + cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] \n"); + return; + } + } else { + if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) { + cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] \n"); + return; + } + } + dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &cause, nt,bc); + if (cause>0) bc->cause=cause; + +#if DEBUG + printf("Parsing RELEASE_COMPLETE Msg\n"); +#endif +} + +static 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 (bc->uulen) { + int protocol=4; + enc_ie_useruser(&release_complete->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc); + cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu); + } + +#if DEBUG + printf("Building RELEASE_COMPLETE Msg\n"); +#endif + return msg; +} + +static 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*)(msg->data+HEADER_LEN); + Q931_info_t *qi = (Q931_info_t*)(msg->data+HEADER_LEN); + unsigned char *p = NULL; + int err; + +#if DEBUG + printf("Parsing FACILITY Msg\n"); +#endif + + if (!bc->nt) { + if (qi->QI_ELEMENT(facility)) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1; + } else { + p = facility->FACILITY; + } + if (!p) + return; + + err = decodeFac(p, &(bc->fac_in)); + if (err) { + cb_log(1, bc->port, "Decoding FACILITY failed! (%d)\n", err); + } +} + +static msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int len, + HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN; + unsigned char *ie_fac, + fac_tmp[256]; + msg_t *msg =(msg_t*)create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, bc?bc->l3_id:-1, sizeof(FACILITY_t) ,nt); + FACILITY_t *facility = (FACILITY_t*)(msg->data+HEADER_LEN); + Q931_info_t *qi; + +#if DEBUG + printf("Building FACILITY Msg\n"); +#endif + + len = encodeFac(fac_tmp, &(bc->fac_out)); + if (len <= 0) + return NULL; + + ie_fac = msg_put(msg, len); + if (bc->nt) { + facility->FACILITY = ie_fac + 1; + } else { + qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + qi->QI_ELEMENT(facility) = ie_fac - (unsigned char *)qi - sizeof(Q931_info_t); + } + + memcpy(ie_fac, fac_tmp, len); + + if (*bc->display) { + printf("Sending %s as Display\n", bc->display); + enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc); + } + + return msg; +} + +static void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing NOTIFY Msg\n"); +#endif +} + +static 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; +} + +static 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 +} + +static 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; +} + +static 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, number, sizeof(number)-1, nt, bc); + dec_ie_keypad(information->KEYPAD, (Q931_info_t *)information, keypad, sizeof(keypad)-1, nt, bc); + strcpy(bc->info_dad, number); + strcpy(bc->keypad,keypad); + } +#if DEBUG + printf("Parsing INFORMATION Msg\n"); +#endif +} + +static 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; +} + +static 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; + int cause; + + dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &cause, nt,bc); + if (cause>0) bc->cause=cause; + ; + +#if DEBUG + printf("Parsing STATUS Msg\n"); +#endif +} + +static 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; +} + +static void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing STATUS Msg\n"); +#endif +} + +static 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; +} + + +/************************************/ + + + + +/** Msg Array **/ + +struct isdn_msg msgs_g[] = { + {CC_PROCEEDING,L3,EVENT_PROCEEDING, + parse_proceeding,build_proceeding, + "PROCEEDING"}, + {CC_ALERTING,L3,EVENT_ALERTING, + parse_alerting,build_alerting, + "ALERTING"}, + {CC_PROGRESS,L3,EVENT_PROGRESS, + parse_progress,build_progress, + "PROGRESS"}, + {CC_SETUP,L3,EVENT_SETUP, + parse_setup,build_setup, + "SETUP"}, + {CC_CONNECT,L3,EVENT_CONNECT, + parse_connect,build_connect, + "CONNECT"}, + {CC_SETUP_ACKNOWLEDGE,L3,EVENT_SETUP_ACKNOWLEDGE, + parse_setup_acknowledge,build_setup_acknowledge, + "SETUP_ACKNOWLEDGE"}, + {CC_CONNECT_ACKNOWLEDGE ,L3,EVENT_CONNECT_ACKNOWLEDGE , + parse_connect_acknowledge ,build_connect_acknowledge, + "CONNECT_ACKNOWLEDGE "}, + {CC_USER_INFORMATION,L3,EVENT_USER_INFORMATION, + parse_user_information,build_user_information, + "USER_INFORMATION"}, + {CC_SUSPEND_REJECT,L3,EVENT_SUSPEND_REJECT, + parse_suspend_reject,build_suspend_reject, + "SUSPEND_REJECT"}, + {CC_RESUME_REJECT,L3,EVENT_RESUME_REJECT, + parse_resume_reject,build_resume_reject, + "RESUME_REJECT"}, + {CC_HOLD,L3,EVENT_HOLD, + parse_hold,build_hold, + "HOLD"}, + {CC_SUSPEND,L3,EVENT_SUSPEND, + parse_suspend,build_suspend, + "SUSPEND"}, + {CC_RESUME,L3,EVENT_RESUME, + parse_resume,build_resume, + "RESUME"}, + {CC_HOLD_ACKNOWLEDGE,L3,EVENT_HOLD_ACKNOWLEDGE, + parse_hold_acknowledge,build_hold_acknowledge, + "HOLD_ACKNOWLEDGE"}, + {CC_SUSPEND_ACKNOWLEDGE,L3,EVENT_SUSPEND_ACKNOWLEDGE, + parse_suspend_acknowledge,build_suspend_acknowledge, + "SUSPEND_ACKNOWLEDGE"}, + {CC_RESUME_ACKNOWLEDGE,L3,EVENT_RESUME_ACKNOWLEDGE, + parse_resume_acknowledge,build_resume_acknowledge, + "RESUME_ACKNOWLEDGE"}, + {CC_HOLD_REJECT,L3,EVENT_HOLD_REJECT, + parse_hold_reject,build_hold_reject, + "HOLD_REJECT"}, + {CC_RETRIEVE,L3,EVENT_RETRIEVE, + parse_retrieve,build_retrieve, + "RETRIEVE"}, + {CC_RETRIEVE_ACKNOWLEDGE,L3,EVENT_RETRIEVE_ACKNOWLEDGE, + parse_retrieve_acknowledge,build_retrieve_acknowledge, + "RETRIEVE_ACKNOWLEDGE"}, + {CC_RETRIEVE_REJECT,L3,EVENT_RETRIEVE_REJECT, + parse_retrieve_reject,build_retrieve_reject, + "RETRIEVE_REJECT"}, + {CC_DISCONNECT,L3,EVENT_DISCONNECT, + parse_disconnect,build_disconnect, + "DISCONNECT"}, + {CC_RESTART,L3,EVENT_RESTART, + parse_restart,build_restart, + "RESTART"}, + {CC_RELEASE,L3,EVENT_RELEASE, + parse_release,build_release, + "RELEASE"}, + {CC_RELEASE_COMPLETE,L3,EVENT_RELEASE_COMPLETE, + parse_release_complete,build_release_complete, + "RELEASE_COMPLETE"}, + {CC_FACILITY,L3,EVENT_FACILITY, + parse_facility,build_facility, + "FACILITY"}, + {CC_NOTIFY,L3,EVENT_NOTIFY, + parse_notify,build_notify, + "NOTIFY"}, + {CC_STATUS_ENQUIRY,L3,EVENT_STATUS_ENQUIRY, + parse_status_enquiry,build_status_enquiry, + "STATUS_ENQUIRY"}, + {CC_INFORMATION,L3,EVENT_INFORMATION, + parse_information,build_information, + "INFORMATION"}, + {CC_STATUS,L3,EVENT_STATUS, + parse_status,build_status, + "STATUS"}, + {CC_TIMEOUT,L3,EVENT_TIMEOUT, + parse_timeout,build_timeout, + "TIMEOUT"}, + {0,0,0,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){ + mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data; + + 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(10,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_PORT_ALARM_INFO[] = "ALARM"; +char EVENT_NEW_CHANNEL_INFO[] = "NEW_CHANNEL"; +char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA"; +char EVENT_BCHAN_ACTIVATED_INFO[] = "BCHAN_ACTIVATED"; +char EVENT_TONE_GENERATE_INFO[] = "TONE_GENERATE"; +char EVENT_BCHAN_ERROR_INFO[] = "BCHAN_ERROR"; + +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_NEW_CHANNEL) return EVENT_NEW_CHANNEL_INFO; + if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO; + if (event == EVENT_BCHAN_ACTIVATED) return EVENT_BCHAN_ACTIVATED_INFO; + if (event == EVENT_TONE_GENERATE) return EVENT_TONE_GENERATE_INFO; + if (event == EVENT_PORT_ALARM) return EVENT_PORT_ALARM_INFO; + if (event == EVENT_BCHAN_ERROR) return EVENT_BCHAN_ERROR_INFO; + + return NULL; +} + +int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int i=isdn_msg_get_index(msgs, msg, nt); + if(i<0) return -1; + + msgs[i].msg_parser(msgs, msg, bc, nt); + return 0; +} + +msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt) +{ + int i=isdn_msg_get_index_by_event(msgs, event, nt); + if(i<0) return NULL; + + return msgs[i].msg_builder(msgs, bc, nt); +} diff --git a/channels/misdn/portinfo.c b/channels/misdn/portinfo.c new file mode 100644 index 000000000..bcb9f0313 --- /dev/null +++ b/channels/misdn/portinfo.c @@ -0,0 +1,198 @@ + + +#include "isdn_lib.h" +#include "isdn_lib_intern.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; +} |