From d220c24906819fc40922678865d12f68400bd096 Mon Sep 17 00:00:00 2001 From: rmudgett Date: Tue, 30 Sep 2008 23:55:24 +0000 Subject: channels/chan_misdn.c channels/misdn/isdn_lib.c * Miscellaneous other fixes from trunk to make merging easier later. ........ r145200 | rmudgett | 2008-09-30 16:00:54 -0500 (Tue, 30 Sep 2008) | 7 lines * Miscellaneous formatting changes to make v1.4 and trunk more merge compatible in the mISDN area. channels/chan_misdn.c * Eliminated redundant code in cb_events() EVENT_SETUP ........ r144257 | crichter | 2008-09-24 03:42:55 -0500 (Wed, 24 Sep 2008) | 9 lines improved helptext of misdn_set_opt. ........ r142181 | rmudgett | 2008-09-09 12:30:52 -0500 (Tue, 09 Sep 2008) | 1 line Cleaned up comment ........ r138738 | rmudgett | 2008-08-18 16:07:28 -0500 (Mon, 18 Aug 2008) | 30 lines channels/chan_misdn.c * Made bearer2str() use allowed_bearers_array[] * Made use the causes.h defines instead of hardcoded numbers. * Made use Asterisk presentation indicator values if either of the mISDN presentation or screen options are negative. * Updated the misdn_set_opt application option descriptions. * Renamed the awkward Caller ID presentation misdn_set_opt application option value not_screened to restricted. Deprecated the not_screened option value. channels/misdn/isdn_lib.c * Made use the causes.h defines instead of hardcoded numbers. * Fixed some spelling errors and typos. * Added all defined facility code strings to fac2str(). channels/misdn/isdn_lib.h * Added doxygen comments to struct misdn_bchannel. channels/misdn/isdn_lib_intern.h * Added doxygen comments to struct misdn_stack. channels/misdn_config.c configs/misdn.conf.sample * Updated the mISDN presentation and screen parameter descriptions. doc/misdn.txt (doc/tex/misdn.tex) * Updated the misdn_set_opt application option descriptions. * Fixed some spelling errors and typos. git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@145293 f38db490-d61c-443f-a65b-d21fe96a405b --- channels/chan_misdn.c | 3654 ++++++++++++++++++-------------------- channels/misdn/isdn_lib.c | 367 ++-- channels/misdn/isdn_lib.h | 305 +++- channels/misdn/isdn_lib_intern.h | 76 +- channels/misdn_config.c | 16 +- 5 files changed, 2257 insertions(+), 2161 deletions(-) (limited to 'channels') diff --git a/channels/chan_misdn.c b/channels/chan_misdn.c index 1e704d3ef..c4dc1c88c 100644 --- a/channels/chan_misdn.c +++ b/channels/chan_misdn.c @@ -21,8 +21,11 @@ * \file * * \brief the chan_misdn channel driver for Asterisk + * * \author Christian Richter * + * \extref MISDN http://www.misdn.org/ + * * \ingroup channel_drivers */ @@ -72,13 +75,14 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/term.h" #include "asterisk/sched.h" #include "asterisk/stringfields.h" +#include "asterisk/causes.h" #include "chan_misdn_config.h" #include "isdn_lib.h" -char global_tracefile[BUFFERSIZE+1]; +char global_tracefile[BUFFERSIZE + 1]; -static int g_config_initialized=0; +static int g_config_initialized = 0; struct misdn_jb{ int size; @@ -94,17 +98,17 @@ struct misdn_jb{ -/* allocates the jb-structure and initialize the elements*/ +/*! \brief allocates the jb-structure and initialize the elements */ struct misdn_jb *misdn_jb_init(int size, int upper_threshold); -/* frees the data and destroys the given jitterbuffer struct */ +/*! \brief frees the data and destroys the given jitterbuffer struct */ void misdn_jb_destroy(struct misdn_jb *jb); -/* fills the jitterbuffer with len data returns < 0 if there was an +/*! \brief fills the jitterbuffer with len data returns < 0 if there was an error (buffer overrun). */ int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len); -/* gets len bytes out of the jitterbuffer if available, else only the +/*! \brief gets len bytes out of the jitterbuffer if available, else only the available data is returned and the return value indicates the number of data. */ int misdn_jb_empty(struct misdn_jb *jb, char *data, int len); @@ -132,9 +136,8 @@ enum misdn_chan_state { MISDN_RELEASED, /*!< when connected */ MISDN_BRIDGED, /*!< when bridged */ MISDN_CLEANING, /*!< when hangup from * but we were connected before */ - MISDN_HUNGUP_FROM_MISDN, /*!< when DISCONNECT/RELEASE/REL_COMP cam from misdn */ - MISDN_HUNGUP_FROM_AST, /*!< when DISCONNECT/RELEASE/REL_COMP came out of */ - /* misdn_hangup */ + MISDN_HUNGUP_FROM_MISDN, /*!< when DISCONNECT/RELEASE/REL_COMP came from misdn */ + MISDN_HUNGUP_FROM_AST, /*!< when DISCONNECT/RELEASE/REL_COMP came out of misdn_hangup */ MISDN_HOLDED, /*!< if this chan is holded */ MISDN_HOLD_DISCONNECT, /*!< if this chan is holded */ @@ -150,17 +153,16 @@ struct hold_info { struct chan_list { - char allowed_bearers[BUFFERSIZE+1]; + char allowed_bearers[BUFFERSIZE + 1]; enum misdn_chan_state state; int need_queue_hangup; int need_hangup; int need_busy; + int originator; int noautorespond_on_setup; - int originator; - int norxtone; int notxtone; @@ -174,7 +176,7 @@ struct chan_list { char ast_rd_buf[4096]; struct ast_frame frame; - int faxdetect; /* 0:no 1:yes 2:yes+nojump */ + int faxdetect; /*!< 0:no 1:yes 2:yes+nojump */ int faxdetect_timeout; struct timeval faxdetect_tv; int faxhandled; @@ -246,11 +248,13 @@ static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame static inline void free_robin_list_r (struct robin_list *r) { - if (r) { - if (r->next) free_robin_list_r(r->next); - if (r->group) free(r->group); - free(r); - } + if (r) { + if (r->next) + free_robin_list_r(r->next); + if (r->group) + free(r->group); + free(r); + } } static void free_robin_list ( void ) @@ -267,7 +271,7 @@ static struct robin_list* get_robin_position (char *group) if (!strcasecmp(iter->group, group)) return iter; } - new = (struct robin_list *)calloc(1, sizeof(struct robin_list)); + new = (struct robin_list *) calloc(1, sizeof(struct robin_list)); new->group = strndup(group, strlen(group)); new->port = 0; new->channel = 0; @@ -280,13 +284,14 @@ static struct robin_list* get_robin_position (char *group) } -/* the main schedule context for stuff like l1 watcher, overlap dial, ... */ +/*! \brief the main schedule context for stuff like l1 watcher, overlap dial, ... */ static struct sched_context *misdn_tasks = NULL; static pthread_t misdn_tasks_thread; static int *misdn_ports; -static void chan_misdn_log(int level, int port, char *tmpl, ...); +static void chan_misdn_log(int level, int port, char *tmpl, ...) + __attribute__ ((format (printf, 3, 4))); static struct ast_channel *misdn_new(struct chan_list *cl, int state, char *exten, char *callerid, int format, int port, int c); static void send_digit_to_chan(struct chan_list *cl, char digit ); @@ -297,7 +302,7 @@ static int pbx_start_chan(struct chan_list *ch); #define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt #define MISDN_ASTERISK_PVT(ast) 1 -#include +#include "asterisk/strings.h" /* #define MISDN_DEBUG 1 */ @@ -305,7 +310,7 @@ static const char misdn_type[] = "mISDN"; static int tracing = 0 ; -/* Only alaw and mulaw is allowed for now */ +/*! \brief Only alaw and mulaw is allowed for now */ static int prefformat = AST_FORMAT_ALAW ; /* AST_FORMAT_SLINEAR ; AST_FORMAT_ULAW | */ static int *misdn_debug; @@ -390,51 +395,35 @@ static struct chan_list * get_chan_by_ast_name(char *name) struct allowed_bearers { - int cap; - int val; - char *name; - int deprecated; + char *name; /*!< Bearer capability name string used in /etc/misdn.conf allowed_bearers */ + char *display; /*!< Bearer capability displayable name */ + int cap; /*!< SETUP message bearer capability field code value */ + int deprecated; /*!< TRUE if this entry is deprecated. (Misspelled or bad name to use) */ }; -static struct allowed_bearers allowed_bearers_array[]= { - {INFO_CAPABILITY_SPEECH,1,"speech"}, - {INFO_CAPABILITY_AUDIO_3_1K,2,"3_1khz"}, - {INFO_CAPABILITY_DIGITAL_UNRESTRICTED,4,"digital_unrestricted"}, - {INFO_CAPABILITY_DIGITAL_RESTRICTED,8,"digital_restricted"}, - {INFO_CAPABILITY_DIGITAL_RESTRICTED,8,"digital_restriced", 1}, /* Allow misspelling for backwards compatibility */ - {INFO_CAPABILITY_VIDEO,16,"video"} +/* *INDENT-OFF* */ +static const struct allowed_bearers allowed_bearers_array[]= { + /* Name, Displayable Name Bearer Capability, Deprecated */ + { "speech", "Speech", INFO_CAPABILITY_SPEECH, 0 }, + { "3_1khz", "3.1KHz Audio", INFO_CAPABILITY_AUDIO_3_1K, 0 }, + { "digital_unrestricted", "Unrestricted Digital", INFO_CAPABILITY_DIGITAL_UNRESTRICTED, 0 }, + { "digital_restricted", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 0 }, + { "digital_restriced", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 1 }, /* Allow misspelling for backwards compatibility */ + { "video", "Video", INFO_CAPABILITY_VIDEO, 0 } }; +/* *INDENT-ON* */ -static char *bearer2str(int cap) { - static char *bearers[]={ - "Speech", - "Audio 3.1k", - "Unres Digital", - "Res Digital", - "Video", - "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; - case INFO_CAPABILITY_VIDEO: - return bearers[4]; - break; - default: - return bearers[5]; - break; - } +static const char *bearer2str(int cap) +{ + unsigned index; + + for (index = 0; index < ARRAY_LEN(allowed_bearers_array); ++index) { + if (allowed_bearers_array[index].cap == cap) { + return allowed_bearers_array[index].display; + } + } /* end for */ + + return "Unknown Bearer"; } @@ -443,7 +432,7 @@ static void print_facility(struct FacParm *fac, struct misdn_bchannel *bc) switch (fac->Function) { case Fac_CD: chan_misdn_log(1,bc->port," --> calldeflect to: %s, screened: %s\n", fac->u.CDeflection.DeflectedToNumber, - fac->u.CDeflection.PresentationAllowed ? "yes" : "no"); + fac->u.CDeflection.PresentationAllowed ? "yes" : "no"); break; case Fac_AOCDCurrency: if (fac->u.AOCDcur.chargeNotAvailable) @@ -451,13 +440,13 @@ static void print_facility(struct FacParm *fac, struct misdn_bchannel *bc) else if (fac->u.AOCDcur.freeOfCharge) chan_misdn_log(1,bc->port," --> AOCD currency: free of charge\n"); else if (fac->u.AOCDchu.billingId >= 0) - chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%d billingId:%d\n", - fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier, - (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId); + chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s billingId:%d\n", + fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier, + (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId); else - chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%d\n", - fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier, - (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total"); + chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s\n", + fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier, + (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total"); break; case Fac_AOCDChargingUnit: if (fac->u.AOCDchu.chargeNotAvailable) @@ -466,13 +455,14 @@ static void print_facility(struct FacParm *fac, struct misdn_bchannel *bc) chan_misdn_log(1,bc->port," --> AOCD charging unit: free of charge\n"); else if (fac->u.AOCDchu.billingId >= 0) chan_misdn_log(1,bc->port," --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s billingId:%d\n", - fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDchu.billingId); + fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDchu.billingId); else chan_misdn_log(1,bc->port," --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s\n", - fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total"); + fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total"); break; default: - chan_misdn_log(1,bc->port," --> unknown\n"); + chan_misdn_log(1,bc->port," --> unknown facility\n"); + break; } } @@ -687,7 +677,7 @@ static int misdn_overlap_dial_task (const void *data) } else { misdn_overlap_dial_task_disconnect: hanguptone_indicate(ch); - ch->bc->out_cause=1; + ch->bc->out_cause = AST_CAUSE_UNALLOCATED; ch->state=MISDN_CLEANING; misdn_lib_send_event(ch->bc, EVENT_DISCONNECT); } @@ -729,10 +719,9 @@ static void send_digit_to_chan(struct chan_list *cl, char digit ) else { /* not handled */ ast_log(LOG_DEBUG, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name); - - } } + /*** CLI HANDLING ***/ static int misdn_set_debug(int fd, int argc, char *argv[]) { @@ -744,52 +733,55 @@ static int misdn_set_debug(int fd, int argc, char *argv[]) level = atoi(argv[3]); switch (argc) { - case 4: - case 5: { - int i; - int only = 0; - if (argc == 5) { - if (strncasecmp(argv[4], "only", strlen(argv[4]))) - return RESULT_SHOWUSAGE; - else - only = 1; - } - for (i=0; i<=max_ports; i++) { - misdn_debug[i] = level; - misdn_debug_only[i] = only; - } - ast_cli(fd, "changing debug level for all ports to %d%s\n",misdn_debug[0], only?" (only)":""); - } - break; - case 6: - case 7: { - int port; - if (strncasecmp(argv[4], "port", strlen(argv[4]))) - return RESULT_SHOWUSAGE; - port = atoi(argv[5]); - if (port <= 0 || port > max_ports) { - switch (max_ports) { - case 0: - ast_cli(fd, "port number not valid! no ports available so you won't get lucky with any number here...\n"); - break; - case 1: - ast_cli(fd, "port number not valid! only port 1 is available.\n"); - break; - default: - ast_cli(fd, "port number not valid! only ports 1 to %d are available.\n", max_ports); - } - return 0; - } - if (argc == 7) { - if (strncasecmp(argv[6], "only", strlen(argv[6]))) - return RESULT_SHOWUSAGE; - else - misdn_debug_only[port] = 1; - } else - misdn_debug_only[port] = 0; - misdn_debug[port] = level; - ast_cli(fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port]?" (only)":"", port); + case 4: + case 5: + { + int i; + int only = 0; + if (argc == 5) { + if (strncasecmp(argv[4], "only", strlen(argv[4]))) + return RESULT_SHOWUSAGE; + else + only = 1; + } + + for (i = 0; i <= max_ports; i++) { + misdn_debug[i] = level; + misdn_debug_only[i] = only; + } + ast_cli(fd, "changing debug level for all ports to %d%s\n",misdn_debug[0], only?" (only)":""); + } + break; + case 6: + case 7: + { + int port; + if (strncasecmp(argv[4], "port", strlen(argv[4]))) + return RESULT_SHOWUSAGE; + port = atoi(argv[5]); + if (port <= 0 || port > max_ports) { + switch (max_ports) { + case 0: + ast_cli(fd, "port number not valid! no ports available so you won't get lucky with any number here...\n"); + break; + case 1: + ast_cli(fd, "port number not valid! only port 1 is available.\n"); + break; + default: + ast_cli(fd, "port number not valid! only ports 1 to %d are available.\n", max_ports); } + return 0; + } + if (argc == 7) { + if (strncasecmp(argv[6], "only", strlen(argv[6]))) + return RESULT_SHOWUSAGE; + else + misdn_debug_only[port] = 1; + } else + misdn_debug_only[port] = 0; + misdn_debug[port] = level; + ast_cli(fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port]?" (only)":"", port); + } } return 0; } @@ -920,7 +912,7 @@ static int misdn_show_config (int fd, int argc, char *argv[]) if (argc >= 4) { if (!strcmp(argv[3], "description")) { if (argc == 5) { - enum misdn_cfg_elements elem = misdn_cfg_get_elem (argv[4]); + enum misdn_cfg_elements elem = misdn_cfg_get_elem(argv[4]); if (elem == MISDN_CFG_FIRST) ast_cli(fd, "Unknown element: %s\n", argv[4]); else @@ -953,9 +945,9 @@ static int misdn_show_config (int fd, int argc, char *argv[]) } if (argc == 3 || onlyport == 0) { - ast_cli(fd,"Misdn General-Config: \n"); + ast_cli(fd, "Misdn General-Config:\n"); for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) { - misdn_cfg_get_config_string( 0, elem, buffer, BUFFERSIZE); + misdn_cfg_get_config_string(0, elem, buffer, BUFFERSIZE); ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : ""); } ast_cli(fd, "\n"); @@ -966,7 +958,7 @@ static int misdn_show_config (int fd, int argc, char *argv[]) for (; port > 0; port = misdn_cfg_get_next_port(port)) { ast_cli(fd, "\n[PORT %d]\n", port); for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) { - misdn_cfg_get_config_string( port, elem, buffer, BUFFERSIZE); + misdn_cfg_get_config_string(port, elem, buffer, BUFFERSIZE); ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : ""); } ast_cli(fd, "\n"); @@ -985,13 +977,14 @@ static int misdn_show_config (int fd, int argc, char *argv[]) ast_cli(fd, "Port %d is not active!\n", onlyport); } } + return 0; } struct state_struct { enum misdn_chan_state state; - char txt[255] ; -} ; + char txt[255]; +}; static struct state_struct state_array[] = { {MISDN_NOTHING,"NOTHING"}, /* at beginning */ @@ -1017,18 +1010,19 @@ static struct state_struct state_array[] = { {MISDN_HUNGUP_FROM_AST,"HUNGUP_FROM_AST"} /* when DISCONNECT/RELEASE/REL_COMP came out of misdn_hangup */ }; -static char *misdn_get_ch_state(struct chan_list *p) +static const char *misdn_get_ch_state(struct chan_list *p) { int i; static char state[8]; if( !p) return NULL; - for (i=0; i< sizeof(state_array)/sizeof(struct state_struct); i++) { - if ( state_array[i].state == p->state) return state_array[i].txt; + for (i = 0; i < sizeof(state_array) / sizeof(struct state_struct); i++) { + if (state_array[i].state == p->state) + return state_array[i].txt; } - sprintf(state,"%d",p->state) ; + snprintf(state, sizeof(state), "%d", p->state) ; return state; } @@ -1047,8 +1041,8 @@ static void reload_config(void) free_robin_list(); misdn_cfg_reload(); misdn_cfg_update_ptp(); - misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE); - misdn_cfg_get( 0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(int)); + misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE); + misdn_cfg_get(0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(int)); for (i = 0; i <= max_ports; i++) { misdn_debug[i] = cfg_debug; @@ -1058,24 +1052,24 @@ static void reload_config(void) static int misdn_reload (int fd, int argc, char *argv[]) { - ast_cli(fd, "Reloading mISDN Config\n"); + ast_cli(fd, "Reloading mISDN configuration\n"); reload_config(); return 0; } -static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel* bc) +static void print_bc_info (int fd, struct chan_list *help, struct misdn_bchannel *bc) { - struct ast_channel *ast=help->ast; + struct ast_channel *ast = help->ast; ast_cli(fd, "* Pid:%d Prt:%d Ch:%d Mode:%s Org:%s dad:%s oad:%s rad:%s ctx:%s state:%s\n", bc->pid, bc->port, bc->channel, - bc->nt?"NT":"TE", - help->originator == ORG_AST?"*":"I", - ast?ast->exten:NULL, - ast?ast->cid.cid_num:NULL, + bc->nt ? "NT" : "TE", + help->originator == ORG_AST ? "*" : "I", + ast ? ast->exten : NULL, + ast ? ast->cid.cid_num : NULL, bc->rad, - ast?ast->context:NULL, + ast ? ast->context : NULL, misdn_get_ch_state(help) ); if (misdn_debug[bc->port] > 0) @@ -1100,7 +1094,7 @@ static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel help->l3id, help->addr, bc->addr, - bc?bc->l3_id:-1, + bc ? bc->l3_id : -1, bc->display, bc->active, @@ -1112,21 +1106,23 @@ static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel bc->ec_enable, #endif - help->norxtone,help->notxtone, + help->norxtone, help->notxtone, bc->holded ); - + } static int misdn_show_cls (int fd, int argc, char *argv[]) { - struct chan_list *help=cl_te; + struct chan_list *help; + + help = cl_te; - ast_cli(fd,"Chan List: %p\n",cl_te); + ast_cli(fd, "Channel List: %p\n", cl_te); - for (;help; help=help->next) { - struct misdn_bchannel *bc=help->bc; - struct ast_channel *ast=help->ast; + for (; help; help = help->next) { + struct misdn_bchannel *bc = help->bc; + struct ast_channel *ast = help->ast; if (!ast) { if (!bc) { ast_cli(fd, "chan_list obj. with l3id:%x has no bc and no ast Leg\n", help->l3id); @@ -1135,7 +1131,9 @@ static int misdn_show_cls (int fd, int argc, char *argv[]) ast_cli(fd, "bc with pid:%d has no Ast Leg\n", bc->pid); continue; } - if (misdn_debug[0] > 2) ast_cli(fd, "Bc:%p Ast:%p\n", bc, ast); + + if (misdn_debug[0] > 2) + ast_cli(fd, "Bc:%p Ast:%p\n", bc, ast); if (bc) { print_bc_info(fd, help, bc); } else { @@ -1144,34 +1142,36 @@ static int misdn_show_cls (int fd, int argc, char *argv[]) ast_cli(fd, " --> l3_id: %x\n" " --> dad:%s oad:%s\n" " --> hold_port: %d\n" - " --> hold_channel: %d\n" - - ,help->l3id - ,ast->exten - ,ast->cid.cid_num - ,help->hold_info.port - ,help->hold_info.channel + " --> hold_channel: %d\n", + help->l3id, + ast->exten, + ast->cid.cid_num, + help->hold_info.port, + help->hold_info.channel ); } else { - ast_cli(fd,"* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n", ast->exten, ast->cid.cid_num); + ast_cli(fd, "* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n", ast->exten, ast->cid.cid_num); } } } - + misdn_dump_chanlist(); + return 0; } static int misdn_show_cl (int fd, int argc, char *argv[]) { - struct chan_list *help=cl_te; + struct chan_list *help; if (argc != 4) return RESULT_SHOWUSAGE; - - for (;help; help=help->next) { - struct misdn_bchannel *bc=help->bc; - struct ast_channel *ast=help->ast; + + help = cl_te; + + for (; help; help = help->next) { + struct misdn_bchannel *bc = help->bc; + struct ast_channel *ast = help->ast; if (bc && ast) { if (!strcasecmp(ast->name,argv[3])) { @@ -1180,21 +1180,20 @@ static int misdn_show_cl (int fd, int argc, char *argv[]) } } } - - + return 0; } ast_mutex_t lock; -int MAXTICS=8; +int MAXTICS = 8; static int misdn_set_tics (int fd, int argc, char *argv[]) { if (argc != 4) return RESULT_SHOWUSAGE; - - MAXTICS=atoi(argv[3]); - + + MAXTICS = atoi(argv[3]); + return 0; } @@ -1203,103 +1202,97 @@ static int misdn_show_stacks (int fd, int argc, char *argv[]) int port; ast_cli(fd, "BEGIN STACK_LIST:\n"); - - for (port=misdn_cfg_get_next_port(0); port > 0; - port=misdn_cfg_get_next_port(port)) { + for (port = misdn_cfg_get_next_port(0); port > 0; + port = misdn_cfg_get_next_port(port)) { char buf[128]; - get_show_stack_details(port,buf); - ast_cli(fd," %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port]?"(only)":""); + get_show_stack_details(port, buf); + ast_cli(fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : ""); } - + return 0; } - static int misdn_show_ports_stats (int fd, int argc, char *argv[]) { int port; ast_cli(fd, "Port\tin_calls\tout_calls\n"); - - for (port=misdn_cfg_get_next_port(0); port > 0; - port=misdn_cfg_get_next_port(port)) { - ast_cli(fd,"%d\t%d\t\t%d\n",port,misdn_in_calls[port],misdn_out_calls[port]); + for (port = misdn_cfg_get_next_port(0); port > 0; + port = misdn_cfg_get_next_port(port)) { + ast_cli(fd, "%d\t%d\t\t%d\n", port, misdn_in_calls[port], misdn_out_calls[port]); } - ast_cli(fd,"\n"); - - return 0; + ast_cli(fd, "\n"); + return 0; } - static int misdn_show_port (int fd, int argc, char *argv[]) { int port; char buf[128]; - + if (argc != 4) return RESULT_SHOWUSAGE; - + port = atoi(argv[3]); ast_cli(fd, "BEGIN STACK_LIST:\n"); + get_show_stack_details(port, buf); + ast_cli(fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : ""); - get_show_stack_details(port,buf); - ast_cli(fd," %s Debug:%d%s\n",buf, misdn_debug[port], misdn_debug_only[port]?"(only)":""); - - return 0; } static int misdn_send_cd (int fd, int argc, char *argv[]) { char *channame; - char *nr; - + char *nr; + struct chan_list *tmp; + if (argc != 5) return RESULT_SHOWUSAGE; - - channame = argv[3]; - nr = argv[4]; - - ast_cli(fd, "Sending Calldeflection (%s) to %s\n",nr, channame); + { - struct chan_list *tmp=get_chan_by_ast_name(channame); - + channame = argv[3]; + nr = argv[4]; + + ast_cli(fd, "Sending Calldeflection (%s) to %s\n", nr, channame); + tmp = get_chan_by_ast_name(channame); if (!tmp) { ast_cli(fd, "Sending CD with nr %s to %s failed: Channel does not exist.\n",nr, channame); return 0; - } else { - if (strlen(nr) >= 15) { - ast_cli(fd, "Sending CD with nr %s to %s failed: Number too long (up to 15 digits are allowed).\n",nr, channame); - return 0; - } - tmp->bc->fac_out.Function = Fac_CD; - strncpy((char *)tmp->bc->fac_out.u.CDeflection.DeflectedToNumber, nr, sizeof(tmp->bc->fac_out.u.CDeflection.DeflectedToNumber)); - misdn_lib_send_event(tmp->bc, EVENT_FACILITY); } + + if (strlen(nr) >= 15) { + ast_cli(fd, "Sending CD with nr %s to %s failed: Number too long (up to 15 digits are allowed).\n",nr, channame); + return 0; + } + tmp->bc->fac_out.Function = Fac_CD; + ast_copy_string((char *)tmp->bc->fac_out.u.CDeflection.DeflectedToNumber, nr, sizeof(tmp->bc->fac_out.u.CDeflection.DeflectedToNumber)); + misdn_lib_send_event(tmp->bc, EVENT_FACILITY); } - - return 0; + + return 0; } static int misdn_send_restart(int fd, int argc, char *argv[]) { int port; int channel; - - if ( (argc < 4) || (argc > 5) ) + + if (argc < 4 || argc > 5) return RESULT_SHOWUSAGE; - + port = atoi(argv[3]); - if (argc==5) { + if (argc == 5) { channel = atoi(argv[4]); - misdn_lib_send_restart(port, channel); - } else - misdn_lib_send_restart(port, -1 ); - + misdn_lib_send_restart(port, channel); + } else { + misdn_lib_send_restart(port, -1); + } + return 0; } @@ -1307,105 +1300,96 @@ static int misdn_send_digit (int fd, int argc, char *argv[]) { char *channame; char *msg; - + struct chan_list *tmp; + int i, msglen; + if (argc != 5) return RESULT_SHOWUSAGE; - + channame = argv[3]; msg = argv[4]; + msglen = strlen(msg); - ast_cli(fd, "Sending %s to %s\n",msg, channame); - - { - struct chan_list *tmp=get_chan_by_ast_name(channame); - - if (!tmp) { - ast_cli(fd, "Sending %s to %s failed Channel does not exist\n",msg, channame); - return 0; - } else { + ast_cli(fd, "Sending %s to %s\n", msg, channame); + + tmp = get_chan_by_ast_name(channame); + if (!tmp) { + ast_cli(fd, "Sending %s to %s failed Channel does not exist\n", msg, channame); + return 0; + } #if 1 - int i; - int msglen = strlen(msg); - for (i=0; iast, 250); */ - usleep(250000); - /* res = ast_waitfor(tmp->ast,100); */ - } + for (i = 0; i < msglen; i++) { + ast_cli(fd, "Sending: %c\n", msg[i]); + send_digit_to_chan(tmp, msg[i]); + /* res = ast_safe_sleep(tmp->ast, 250); */ + usleep(250000); + /* res = ast_waitfor(tmp->ast,100); */ + } #else - int res; - res = ast_dtmf_stream(tmp->ast,NULL,msg,250); + ast_dtmf_stream(tmp->ast, NULL, msg, 250); #endif - } - } - - return 0; + + return 0; } static int misdn_toggle_echocancel (int fd, int argc, char *argv[]) { - char *channame; + char *channame; + struct chan_list *tmp; if (argc != 4) return RESULT_SHOWUSAGE; - + channame = argv[3]; ast_cli(fd, "Toggling EchoCancel on %s\n", channame); - { - struct chan_list *tmp=get_chan_by_ast_name(channame); - - if (!tmp) { - ast_cli(fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame); - return 0; - } else { - - tmp->toggle_ec=tmp->toggle_ec?0:1; + tmp = get_chan_by_ast_name(channame); + if (!tmp) { + ast_cli(fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame); + return 0; + } + + tmp->toggle_ec = tmp->toggle_ec ? 0 : 1; - if (tmp->toggle_ec) { + if (tmp->toggle_ec) { #ifdef MISDN_1_2 - update_pipeline_config(tmp->bc); + update_pipeline_config(tmp->bc); #else - update_ec_config(tmp->bc); + update_ec_config(tmp->bc); #endif - manager_ec_enable(tmp->bc); - } else { - manager_ec_disable(tmp->bc); - } - } + manager_ec_enable(tmp->bc); + } else { + manager_ec_disable(tmp->bc); } - - return 0; + + return 0; } static int misdn_send_display (int fd, int argc, char *argv[]) { - char *channame; - char *msg; - + char *channame; + char *msg; + struct chan_list *tmp; + if (argc != 5) return RESULT_SHOWUSAGE; - + channame = argv[3]; msg = argv[4]; - ast_cli(fd, "Sending %s to %s\n",msg, channame); - { - struct chan_list *tmp; - tmp=get_chan_by_ast_name(channame); + ast_cli(fd, "Sending %s to %s\n", msg, channame); + tmp = get_chan_by_ast_name(channame); - if (tmp && tmp->bc) { - ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display)); - misdn_lib_send_event(tmp->bc, EVENT_INFORMATION); - } else { - ast_cli(fd,"No such channel %s\n",channame); - return RESULT_FAILURE; - } + if (tmp && tmp->bc) { + ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display)); + misdn_lib_send_event(tmp->bc, EVENT_INFORMATION); + } else { + ast_cli(fd, "No such channel %s\n", channame); + return RESULT_FAILURE; } - return RESULT_SUCCESS ; + return RESULT_SUCCESS; } static char *complete_ch_helper(const char *line, const char *word, int pos, int state, int rpos) @@ -1443,14 +1427,16 @@ static char *complete_debug_port (const char *line, const char *word, int pos, i return NULL; switch (pos) { - case 4: if (*word == 'p') - return strdup("port"); - else if (*word == 'o') - return strdup("only"); - break; - case 6: if (*word == 'o') - return strdup("only"); - break; + case 4: + if (*word == 'p') + return strdup("port"); + else if (*word == 'o') + return strdup("only"); + break; + case 6: + if (*word == 'o') + return strdup("only"); + break; } return NULL; } @@ -1464,37 +1450,38 @@ static char *complete_show_config (const char *line, const char *word, int pos, int port = 0; switch (pos) { - case 3: if ((!strncmp(word, "description", wordlen)) && (++which > state)) - return strdup("description"); - if ((!strncmp(word, "descriptions", wordlen)) && (++which > state)) - return strdup("descriptions"); - if ((!strncmp(word, "0", wordlen)) && (++which > state)) - return strdup("0"); - while ((port = misdn_cfg_get_next_port(port)) != -1) { - snprintf(buffer, sizeof(buffer), "%d", port); - if ((!strncmp(word, buffer, wordlen)) && (++which > state)) { - return strdup(buffer); - } + case 3: + if ((!strncmp(word, "description", wordlen)) && (++which > state)) + return strdup("description"); + if ((!strncmp(word, "descriptions", wordlen)) && (++which > state)) + return strdup("descriptions"); + if ((!strncmp(word, "0", wordlen)) && (++which > state)) + return strdup("0"); + while ((port = misdn_cfg_get_next_port(port)) != -1) { + snprintf(buffer, sizeof(buffer), "%d", port); + if ((!strncmp(word, buffer, wordlen)) && (++which > state)) { + return strdup(buffer); } - break; + } + break; case 4: - if (strstr(line, "description ")) { - for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) { - if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST)) - continue; - misdn_cfg_get_name(elem, buffer, BUFFERSIZE); - if (!wordlen || !strncmp(word, buffer, wordlen)) { - if (++which > state) - return strdup(buffer); - } + if (strstr(line, "description ")) { + for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) { + if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST)) + continue; + misdn_cfg_get_name(elem, buffer, BUFFERSIZE); + if (!wordlen || !strncmp(word, buffer, wordlen)) { + if (++which > state) + return strdup(buffer); } - } else if (strstr(line, "descriptions ")) { - if ((!wordlen || !strncmp(word, "general", wordlen)) && (++which > state)) - return strdup("general"); - if ((!wordlen || !strncmp(word, "ports", wordlen)) && (++which > state)) - return strdup("ports"); } - break; + } else if (strstr(line, "descriptions ")) { + if ((!wordlen || !strncmp(word, "general", wordlen)) && (++which > state)) + return strdup("general"); + if ((!wordlen || !strncmp(word, "ports", wordlen)) && (++which > state)) + return strdup("ports"); + } + break; } return NULL; } @@ -1550,139 +1537,122 @@ static struct ast_cli_entry chan_misdn_clis[] = { "Usage: misdn set crypt debug \n" } }; -static int update_config (struct chan_list *ch, int orig) +static int update_config(struct chan_list *ch, int orig) { - struct ast_channel *ast=ch->ast; - struct misdn_bchannel *bc=ch->bc; - int port; + struct ast_channel *ast; + struct misdn_bchannel *bc; + int port, hdlc = 0; int pres, screen; - int hdlc=0; if (!ch) { ast_log(LOG_WARNING, "Cannot configure without chanlist\n"); return -1; } - - ast=ch->ast; - bc=ch->bc; - if (! ast || ! bc ) { + + ast = ch->ast; + bc = ch->bc; + if (! ast || ! bc) { ast_log(LOG_WARNING, "Cannot configure without ast || bc\n"); return -1; } - - port=bc->port; - - chan_misdn_log(7,port,"update_config: Getting Config\n"); - misdn_cfg_get( port, MISDN_CFG_HDLC, &hdlc, sizeof(int)); + port = bc->port; + + chan_misdn_log(7, port, "update_config: Getting Config\n"); + + misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int)); if (hdlc) { switch (bc->capability) { case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: case INFO_CAPABILITY_DIGITAL_RESTRICTED: - chan_misdn_log(1,bc->port," --> CONF HDLC\n"); - bc->hdlc=1; + chan_misdn_log(1, bc->port, " --> CONF HDLC\n"); + bc->hdlc = 1; break; } - } - - misdn_cfg_get( port, MISDN_CFG_PRES, &pres, sizeof(int)); - misdn_cfg_get( port, MISDN_CFG_SCREEN, &screen, sizeof(int)); - chan_misdn_log(2,port," --> pres: %d screen: %d\n",pres, screen); - - if ( (pres + screen) < 0 ) { - chan_misdn_log(2,port," --> pres: %x\n", ast->cid.cid_pres); + + misdn_cfg_get(port, MISDN_CFG_PRES, &pres, sizeof(pres)); + misdn_cfg_get(port, MISDN_CFG_SCREEN, &screen, sizeof(screen)); + chan_misdn_log(2, port, " --> pres: %d screen: %d\n", pres, screen); + + if (pres < 0 || screen < 0) { + chan_misdn_log(2, port, " --> pres: %x\n", ast->cid.cid_pres); - switch (ast->cid.cid_pres & 0x60){ - + switch (ast->cid.cid_pres & 0x60) { case AST_PRES_RESTRICTED: - bc->pres=1; - chan_misdn_log(2, port, " --> PRES: Restricted (0x1)\n"); + bc->pres = 1; + chan_misdn_log(2, port, " --> PRES: Restricted (1)\n"); break; - - case AST_PRES_UNAVAILABLE: - bc->pres=2; - chan_misdn_log(2, port, " --> PRES: Unavailable (0x2)\n"); + bc->pres = 2; + chan_misdn_log(2, port, " --> PRES: Unavailable (2)\n"); break; - default: - bc->pres=0; - chan_misdn_log(2, port, " --> PRES: Allowed (0x0)\n"); + bc->pres = 0; + chan_misdn_log(2, port, " --> PRES: Allowed (0)\n"); + break; } - - switch (ast->cid.cid_pres & 0x3){ - + + switch (ast->cid.cid_pres & 0x3) { + default: case AST_PRES_USER_NUMBER_UNSCREENED: - bc->screen=0; - chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n"); + bc->screen = 0; + chan_misdn_log(2, port, " --> SCREEN: Unscreened (0)\n"); break; - case AST_PRES_USER_NUMBER_PASSED_SCREEN: - bc->screen=1; - chan_misdn_log(2, port, " --> SCREEN: Passed Screen (0x1)\n"); + bc->screen = 1; + chan_misdn_log(2, port, " --> SCREEN: Passed Screen (1)\n"); break; case AST_PRES_USER_NUMBER_FAILED_SCREEN: - bc->screen=2; - chan_misdn_log(2, port, " --> SCREEN: Failed Screen (0x2)\n"); + bc->screen = 2; + chan_misdn_log(2, port, " --> SCREEN: Failed Screen (2)\n"); break; - case AST_PRES_NETWORK_NUMBER: - bc->screen=3; - chan_misdn_log(2, port, " --> SCREEN: Network Nr. (0x3)\n"); + bc->screen = 3; + chan_misdn_log(2, port, " --> SCREEN: Network Nr. (3)\n"); break; - - default: - bc->screen=0; - chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n"); } - - } else { - bc->screen=screen; - bc->pres=pres; + bc->screen = screen; + bc->pres = pres; } return 0; - } - - static void config_jitterbuffer(struct chan_list *ch) { - struct misdn_bchannel *bc=ch->bc; - int len=ch->jb_len, threshold=ch->jb_upper_threshold; + struct misdn_bchannel *bc = ch->bc; + int len = ch->jb_len, threshold = ch->jb_upper_threshold; - chan_misdn_log(5,bc->port, "config_jb: Called\n"); + chan_misdn_log(5, bc->port, "config_jb: Called\n"); - if ( ! len ) { - chan_misdn_log(1,bc->port, "config_jb: Deactivating Jitterbuffer\n"); + if (! len) { + chan_misdn_log(1, bc->port, "config_jb: Deactivating Jitterbuffer\n"); bc->nojitter=1; } else { - - if (len <=100 || len > 8000) { - chan_misdn_log(0,bc->port,"config_jb: Jitterbuffer out of Bounds, setting to 1000\n"); - len=1000; + if (len <= 100 || len > 8000) { + chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer out of Bounds, setting to 1000\n"); + len = 1000; } - + if ( threshold > len ) { - chan_misdn_log(0,bc->port,"config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n"); + chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n"); } - + if ( ch->jb) { - cb_log(0,bc->port,"config_jb: We've got a Jitterbuffer Already on this port.\n"); + cb_log(0, bc->port, "config_jb: We've got a Jitterbuffer Already on this port.\n"); misdn_jb_destroy(ch->jb); - ch->jb=NULL; + ch->jb = NULL; } - + ch->jb=misdn_jb_init(len, threshold); if (!ch->jb ) - bc->nojitter=1; + bc->nojitter = 1; } } @@ -1691,16 +1661,16 @@ void debug_numplan(int port, int numplan, char *type) { switch (numplan) { case NUMPLAN_INTERNATIONAL: - chan_misdn_log(2, port, " --> %s: International\n",type); + chan_misdn_log(2, port, " --> %s: International\n", type); break; case NUMPLAN_NATIONAL: - chan_misdn_log(2, port, " --> %s: National\n",type); + chan_misdn_log(2, port, " --> %s: National\n", type); break; case NUMPLAN_SUBSCRIBER: - chan_misdn_log(2, port, " --> %s: Subscriber\n",type); + chan_misdn_log(2, port, " --> %s: Subscriber\n", type); break; case NUMPLAN_UNKNOWN: - chan_misdn_log(2, port, " --> %s: Unknown\n",type); + chan_misdn_log(2, port, " --> %s: Unknown\n", type); break; /* Maybe we should cut off the prefix if present ? */ default: @@ -1710,8 +1680,6 @@ void debug_numplan(int port, int numplan, char *type) } - - #ifdef MISDN_1_2 static int update_pipeline_config(struct misdn_bchannel *bc) { @@ -1724,9 +1692,9 @@ static int update_pipeline_config(struct misdn_bchannel *bc) misdn_cfg_get(bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int)); if (ec == 1) - snprintf(bc->pipeline, sizeof(bc->pipeline) - 1, "mg2ec"); + ast_copy_string(bc->pipeline, "mg2ec", sizeof(bc->pipeline)); else if (ec > 1) - snprintf(bc->pipeline, sizeof(bc->pipeline) - 1, "mg2ec(deftaps=%d)", ec); + snprintf(bc->pipeline, sizeof(bc->pipeline), "mg2ec(deftaps=%d)", ec); return 0; } @@ -1734,15 +1702,15 @@ static int update_pipeline_config(struct misdn_bchannel *bc) static int update_ec_config(struct misdn_bchannel *bc) { int ec; - int port=bc->port; - - misdn_cfg_get( port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int)); - - if (ec == 1 ) { - bc->ec_enable=1; - } else if ( ec > 1 ) { - bc->ec_enable=1; - bc->ec_deftaps=ec; + int port = bc->port; + + misdn_cfg_get(port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int)); + + if (ec == 1) { + bc->ec_enable = 1; + } else if (ec > 1) { + bc->ec_enable = 1; + bc->ec_deftaps = ec; } return 0; @@ -1750,87 +1718,86 @@ static int update_ec_config(struct misdn_bchannel *bc) #endif -static int read_config(struct chan_list *ch, int orig) +static int read_config(struct chan_list *ch, int orig) { struct ast_channel *ast; struct misdn_bchannel *bc; int port; - char lang[BUFFERSIZE+1]; - char localmusicclass[BUFFERSIZE+1]; - char faxdetect[BUFFERSIZE+1]; int hdlc = 0; + char lang[BUFFERSIZE + 1]; + char localmusicclass[BUFFERSIZE + 1]; + char faxdetect[BUFFERSIZE + 1]; + char buf[256]; + char buf2[256]; + ast_group_t pg; + ast_group_t cg; if (!ch) { ast_log(LOG_WARNING, "Cannot configure without chanlist\n"); return -1; } - ast=ch->ast; - bc=ch->bc; - if (! ast || ! bc ) { + ast = ch->ast; + bc = ch->bc; + if (! ast || ! bc) { ast_log(LOG_WARNING, "Cannot configure without ast || bc\n"); return -1; } - port=bc->port; - + port = bc->port; + chan_misdn_log(1, port, "read_config: Getting Config\n"); - chan_misdn_log(1,port,"read_config: Getting Config\n"); - - misdn_cfg_get( port, MISDN_CFG_LANGUAGE, lang, BUFFERSIZE); + misdn_cfg_get(port, MISDN_CFG_LANGUAGE, lang, BUFFERSIZE); ast_string_field_set(ast, language, lang); - misdn_cfg_get( port, MISDN_CFG_MUSICCLASS, localmusicclass, BUFFERSIZE); + misdn_cfg_get(port, MISDN_CFG_MUSICCLASS, localmusicclass, BUFFERSIZE); ast_string_field_set(ast, musicclass, localmusicclass); - - - misdn_cfg_get( port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(int)); - misdn_cfg_get( port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(int)); - - misdn_cfg_get( port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(int)); - - misdn_cfg_get( port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(int)); - misdn_cfg_get( port, MISDN_CFG_ASTDTMF, &ch->ast_dsp, sizeof(int)); + misdn_cfg_get(port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(int)); + misdn_cfg_get(port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(int)); + + misdn_cfg_get(port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(int)); + + misdn_cfg_get(port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(int)); + + misdn_cfg_get(port, MISDN_CFG_ASTDTMF, &ch->ast_dsp, sizeof(int)); if (ch->ast_dsp) { - ch->ignore_dtmf=1; + ch->ignore_dtmf = 1; } - misdn_cfg_get( port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(int)); - misdn_cfg_get( port, MISDN_CFG_NTTIMEOUT, &ch->nttimeout, sizeof(int)); - - misdn_cfg_get( port, MISDN_CFG_NOAUTORESPOND_ON_SETUP, &ch->noautorespond_on_setup, sizeof(int)); - - misdn_cfg_get( port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(int)); + misdn_cfg_get(port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(int)); + misdn_cfg_get(port, MISDN_CFG_NTTIMEOUT, &ch->nttimeout, sizeof(int)); + + misdn_cfg_get(port, MISDN_CFG_NOAUTORESPOND_ON_SETUP, &ch->noautorespond_on_setup, sizeof(int)); + + misdn_cfg_get(port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(int)); + + misdn_cfg_get(port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, BUFFERSIZE); + + misdn_cfg_get(port, MISDN_CFG_FAXDETECT, faxdetect, BUFFERSIZE); + + misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int)); - misdn_cfg_get( port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, BUFFERSIZE); - - misdn_cfg_get( port, MISDN_CFG_FAXDETECT, faxdetect, BUFFERSIZE); - - misdn_cfg_get( port, MISDN_CFG_HDLC, &hdlc, sizeof(int)); - if (hdlc) { switch (bc->capability) { case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: case INFO_CAPABILITY_DIGITAL_RESTRICTED: - chan_misdn_log(1,bc->port," --> CONF HDLC\n"); - bc->hdlc=1; + chan_misdn_log(1, bc->port, " --> CONF HDLC\n"); + bc->hdlc = 1; break; } } /*Initialize new Jitterbuffer*/ - { - misdn_cfg_get( port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(int)); - misdn_cfg_get( port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(int)); - - config_jitterbuffer(ch); - } - - misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context)); - - ast_copy_string (ast->context,ch->context,sizeof(ast->context)); + misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(int)); + misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(int)); + + config_jitterbuffer(ch); + + misdn_cfg_get(bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context)); + + ast_copy_string(ast->context, ch->context, sizeof(ast->context)); #ifdef MISDN_1_2 update_pipeline_config(bc); @@ -1844,121 +1811,98 @@ static int read_config(struct chan_list *ch, int orig) misdn_cfg_get( bc->port, MISDN_CFG_EARLY_BCONNECT, &eb3, sizeof(int)); bc->early_bconnect=eb3; } + + misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg)); + misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg)); + + chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n", ast_print_group(buf, sizeof(buf), cg), ast_print_group(buf2, sizeof(buf2), pg)); + ast->pickupgroup = pg; + ast->callgroup = cg; - port=bc->port; - - { - char buf[256]; - ast_group_t pg,cg; - - misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg)); - misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg)); - - chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg)); - ast->pickupgroup=pg; - ast->callgroup=cg; - } - - if ( orig == ORG_AST) { - misdn_cfg_get( port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(int)); - + if (orig == ORG_AST) { + char callerid[BUFFERSIZE + 1]; + + misdn_cfg_get(port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(int)); + if (strstr(faxdetect, "outgoing") || strstr(faxdetect, "both")) { if (strstr(faxdetect, "nojump")) - ch->faxdetect=2; + ch->faxdetect = 2; else - ch->faxdetect=1; + ch->faxdetect = 1; } - { - char callerid[BUFFERSIZE+1]; - misdn_cfg_get( port, MISDN_CFG_CALLERID, callerid, BUFFERSIZE); - if ( ! ast_strlen_zero(callerid) ) { - chan_misdn_log(1, port, " --> * Setting Cid to %s\n", callerid); - { - int l = sizeof(bc->oad); - strncpy(bc->oad,callerid, l); - bc->oad[l-1] = 0; - } - - } - - - misdn_cfg_get( port, MISDN_CFG_DIALPLAN, &bc->dnumplan, sizeof(int)); - misdn_cfg_get( port, MISDN_CFG_LOCALDIALPLAN, &bc->onumplan, sizeof(int)); - misdn_cfg_get( port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int)); - debug_numplan(port, bc->dnumplan,"TON"); - debug_numplan(port, bc->onumplan,"LTON"); - debug_numplan(port, bc->cpnnumplan,"CTON"); + misdn_cfg_get(port, MISDN_CFG_CALLERID, callerid, BUFFERSIZE); + if ( ! ast_strlen_zero(callerid) ) { + chan_misdn_log(1, port, " --> * Setting Cid to %s\n", callerid); + ast_copy_string(bc->oad, callerid, sizeof(bc->oad)); } + misdn_cfg_get(port, MISDN_CFG_DIALPLAN, &bc->dnumplan, sizeof(int)); + misdn_cfg_get(port, MISDN_CFG_LOCALDIALPLAN, &bc->onumplan, sizeof(int)); + misdn_cfg_get(port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int)); + debug_numplan(port, bc->dnumplan, "TON"); + debug_numplan(port, bc->onumplan, "LTON"); + debug_numplan(port, bc->cpnnumplan, "CTON"); + ch->overlap_dial = 0; } else { /** ORIGINATOR MISDN **/ - char prefix[BUFFERSIZE+1]=""; + char prefix[BUFFERSIZE + 1] = ""; + if (strstr(faxdetect, "incoming") || strstr(faxdetect, "both")) { if (strstr(faxdetect, "nojump")) - ch->faxdetect=2; + ch->faxdetect = 2; else - ch->faxdetect=1; + ch->faxdetect = 1; } - - misdn_cfg_get( port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int)); - debug_numplan(port, bc->cpnnumplan,"CTON"); - - switch( bc->onumplan ) { + + misdn_cfg_get(port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int)); + debug_numplan(port, bc->cpnnumplan, "CTON"); + + switch (bc->onumplan) { case NUMPLAN_INTERNATIONAL: - misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE); + misdn_cfg_get(bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE); break; - + case NUMPLAN_NATIONAL: - misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE); + misdn_cfg_get(bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE); break; default: break; } - - { - int l = strlen(prefix) + strlen(bc->oad); - char *tmp = alloca(l+1); - strcpy(tmp,prefix); - strcat(tmp,bc->oad); - strcpy(bc->oad,tmp); - } - + + ast_copy_string(buf, bc->oad, sizeof(buf)); + snprintf(bc->oad, sizeof(bc->oad), "%s%s", prefix, buf); + if (!ast_strlen_zero(bc->dad)) { - ast_copy_string(bc->orig_dad,bc->dad, sizeof(bc->orig_dad)); + ast_copy_string(bc->orig_dad, bc->dad, sizeof(bc->orig_dad)); } - + if ( ast_strlen_zero(bc->dad) && !ast_strlen_zero(bc->keypad)) { - ast_copy_string(bc->dad,bc->keypad, sizeof(bc->dad)); + ast_copy_string(bc->dad, bc->keypad, sizeof(bc->dad)); } prefix[0] = 0; - - switch( bc->dnumplan ) { + + switch (bc->dnumplan) { case NUMPLAN_INTERNATIONAL: - misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE); + misdn_cfg_get(bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE); break; case NUMPLAN_NATIONAL: - misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE); + misdn_cfg_get(bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE); break; default: break; } - - { - int l = strlen(prefix) + strlen(bc->dad); - char *tmp = alloca(l+1); - strcpy(tmp,prefix); - strcat(tmp,bc->dad); - strcpy(bc->dad,tmp); - } - - if ( strcmp(bc->dad,ast->exten)) { + + ast_copy_string(buf, bc->dad, sizeof(buf)); + snprintf(bc->dad, sizeof(bc->dad), "%s%s", prefix, buf); + + if (strcmp(bc->dad, ast->exten)) { ast_copy_string(ast->exten, bc->dad, sizeof(ast->exten)); } - + ast_set_callerid(ast, bc->oad, NULL, bc->oad); - + if ( !ast_strlen_zero(bc->rad) ) { if (ast->cid.cid_rdnis) free(ast->cid.cid_rdnis); @@ -1972,7 +1916,7 @@ static int read_config(struct chan_list *ch, int orig) ch->overlap_dial_task = -1; if (ch->faxdetect || ch->ast_dsp) { - misdn_cfg_get( port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout)); + misdn_cfg_get(port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout)); if (!ch->dsp) ch->dsp = ast_dsp_new(); if (ch->dsp) { @@ -1982,7 +1926,7 @@ static int read_config(struct chan_list *ch, int orig) ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT ); } if (!ch->trans) - ch->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW); + ch->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW); } /* AOCD initialization */ @@ -1998,21 +1942,23 @@ static int read_config(struct chan_list *ch, int orig) static int misdn_call(struct ast_channel *ast, char *dest, int timeout) { - int port=0; + int port = 0; int r; int exceed; - struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast); + int bridging; + struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(ast); struct misdn_bchannel *newbc; - char *opts=NULL, *ext; + char *opts = NULL, *ext, *tokb; + char *dest_cp = ast_strdupa(dest); - { - ext = ast_strdupa(dest); - strsep(&ext,"/"); + ext = strtok_r(dest_cp, "/", &tokb); + + if (ext) { + ext = strtok_r(NULL, "/", &tokb); if (ext) { - opts=ext; - strsep(&opts,"/"); - } else { - ast_log(LOG_WARNING, "Malformed dialstring\n"); + opts = strtok_r(NULL, "/", &tokb); + } else { + chan_misdn_log(0, 0, "misdn_call: No Extension given!\n"); return -1; } } @@ -2024,126 +1970,108 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout) if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest ) { ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name); - ast->hangupcause=41; + ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE; ast_setstate(ast, AST_STATE_DOWN); return -1; } if (!ch) { ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name); - ast->hangupcause=41; + ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE; ast_setstate(ast, AST_STATE_DOWN); return -1; } - newbc=ch->bc; + newbc = ch->bc; if (!newbc) { ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name); - ast->hangupcause=41; + ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE; ast_setstate(ast, AST_STATE_DOWN); return -1; } - port=newbc->port; + port = newbc->port; - if ((exceed=add_out_calls(port))) { + if ((exceed = add_out_calls(port))) { char tmp[16]; - sprintf(tmp,"%d",exceed); - pbx_builtin_setvar_helper(ast,"MAX_OVERFLOW",tmp); + snprintf(tmp, sizeof(tmp), "%d", exceed); + pbx_builtin_setvar_helper(ast, "MAX_OVERFLOW", tmp); return -1; } - chan_misdn_log(1, port, "* CALL: %s\n",dest); + chan_misdn_log(1, port, "* CALL: %s\n", dest); - chan_misdn_log(2, port, " --> * dad:%s tech:%s ctx:%s\n",ast->exten,ast->name, ast->context); + chan_misdn_log(2, port, " --> * dad:%s tech:%s ctx:%s\n", ast->exten, ast->name, ast->context); - chan_misdn_log(3, port, " --> * adding2newbc ext %s\n",ast->exten); + chan_misdn_log(3, port, " --> * adding2newbc ext %s\n", ast->exten); if (ast->exten) { - int l = sizeof(newbc->dad); - strncpy(ast->exten,ext,sizeof(ast->exten)); - - strncpy(newbc->dad,ext,l); - - newbc->dad[l-1] = 0; + ast_copy_string(ast->exten, ext, sizeof(ast->exten)); + ast_copy_string(newbc->dad, ext, sizeof(newbc->dad)); } - if (ast->cid.cid_rdnis) - strcpy(newbc->rad, ast->cid.cid_rdnis); - else - newbc->rad[0]=0; + ast_copy_string(newbc->rad, S_OR(ast->cid.cid_rdnis, ""), sizeof(newbc->rad)); - chan_misdn_log(3, port, " --> * adding2newbc callerid %s\n",ast->cid.cid_num); - if (ast_strlen_zero(newbc->oad) && ast->cid.cid_num ) { + chan_misdn_log(3, port, " --> * adding2newbc callerid %s\n", ast->cid.cid_num); + if (ast_strlen_zero(newbc->oad) && !ast_strlen_zero(ast->cid.cid_num)) { + ast_copy_string(newbc->oad, ast->cid.cid_num, sizeof(newbc->oad)); + } - if (ast->cid.cid_num) { - int l = sizeof(newbc->oad); - strncpy(newbc->oad,ast->cid.cid_num, l); - newbc->oad[l-1] = 0; - } + newbc->capability = ast->transfercapability; + pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY", ast_transfercapability2str(newbc->capability)); + if ( ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) { + chan_misdn_log(2, port, " --> * Call with flag Digital\n"); } - { - int bridging; - struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast); - if (!ch) { ast_verbose("No chan_list in misdn_call\n"); return -1;} - - newbc->capability=ast->transfercapability; - pbx_builtin_setvar_helper(ast,"TRANSFERCAPABILITY",ast_transfercapability2str(newbc->capability)); - if ( ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) { - chan_misdn_log(2, port, " --> * Call with flag Digital\n"); - } + /* update screening and presentation */ + update_config(ch, ORG_AST); + /* fill in some ies from channel vary*/ + import_ch(ast, newbc, ch); - /* update screening and presentation */ - update_config(ch,ORG_AST); - - /* fill in some ies from channel vary*/ - import_ch(ast, newbc, ch); - - /* Finally The Options Override Everything */ - if (opts) - misdn_set_opt_exec(ast,opts); - else - chan_misdn_log(2,port,"NO OPTS GIVEN\n"); + /* Finally The Options Override Everything */ + if (opts) + misdn_set_opt_exec(ast, opts); + else + chan_misdn_log(2, port, "NO OPTS GIVEN\n"); - /*check for bridging*/ - misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int)); - if (bridging && ch->other_ch) { + /*check for bridging*/ + misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging)); + if (bridging && ch->other_ch) { #ifdef MISDN_1_2 - chan_misdn_log(1, port, "Disabling EC (aka Pipeline) on both Sides\n"); - *ch->bc->pipeline=0; - *ch->other_ch->bc->pipeline=0; + chan_misdn_log(1, port, "Disabling EC (aka Pipeline) on both Sides\n"); + *ch->bc->pipeline = 0; + *ch->other_ch->bc->pipeline = 0; #else - chan_misdn_log(1, port, "Disabling EC on both Sides\n"); - ch->bc->ec_enable=0; - ch->other_ch->bc->ec_enable=0; + chan_misdn_log(1, port, "Disabling EC on both Sides\n"); + ch->bc->ec_enable = 0; + ch->other_ch->bc->ec_enable = 0; #endif - } - - r=misdn_lib_send_event( newbc, EVENT_SETUP ); - - /** we should have l3id after sending setup **/ - ch->l3id=newbc->l3_id; } - + + r = misdn_lib_send_event( newbc, EVENT_SETUP ); + + /** we should have l3id after sending setup **/ + ch->l3id = newbc->l3_id; + if ( r == -ENOCHAN ) { chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n"); - chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n",newbc?newbc->pid:-1); - ast->hangupcause=34; + chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n", newbc ? newbc->pid : -1); + ast->hangupcause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION; ast_setstate(ast, AST_STATE_DOWN); return -1; } - chan_misdn_log(2, port, " --> * SEND: State Dialing pid:%d\n",newbc?newbc->pid:1); + chan_misdn_log(2, port, " --> * SEND: State Dialing pid:%d\n", newbc ? newbc->pid : 1); ast_setstate(ast, AST_STATE_DIALING); - ast->hangupcause=16; + ast->hangupcause = AST_CAUSE_NORMAL_CLEARING; - if (newbc->nt) stop_bc_tones(ch); - - ch->state=MISDN_CALLING; + if (newbc->nt) + stop_bc_tones(ch); + ch->state = MISDN_CALLING; + return 0; } @@ -2151,11 +2079,11 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout) static int misdn_answer(struct ast_channel *ast) { struct chan_list *p; + const char *tmp; + if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) return -1; - if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast)) ) return -1; - - chan_misdn_log(1, p? (p->bc? p->bc->port : 0) : 0, "* ANSWER:\n"); + chan_misdn_log(1, p ? (p->bc ? p->bc->port : 0) : 0, "* ANSWER:\n"); if (!p) { ast_log(LOG_WARNING, " --> Channel not connected ??\n"); @@ -2168,43 +2096,33 @@ static int misdn_answer(struct ast_channel *ast) ast_queue_hangup(ast); } - { - const char *tmp_key = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY"); - - if (tmp_key ) { - chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n"); - { - int l = sizeof(p->bc->crypt_key); - strncpy(p->bc->crypt_key,tmp_key, l); - p->bc->crypt_key[l-1] = 0; - } - } else { - chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n"); - } - + tmp = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY"); + if (!ast_strlen_zero(tmp)) { + chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n"); + ast_copy_string(p->bc->crypt_key, tmp, sizeof(p->bc->crypt_key)); + } else { + chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n"); } - { - const char *nodsp=pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS"); - if (nodsp) { - chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n"); - p->bc->nodsp=1; - p->bc->hdlc=0; - p->bc->nojitter=1; - } + tmp = pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS"); + if (!ast_strlen_zero(tmp) && ast_true(tmp)) { + chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n"); + p->bc->nodsp = 1; + p->bc->hdlc = 0; + p->bc->nojitter = 1; } - + p->state = MISDN_CONNECTED; stop_indicate(p); if ( ast_strlen_zero(p->bc->cad) ) { chan_misdn_log(2,p->bc->port," --> empty cad using dad\n"); - ast_copy_string(p->bc->cad,p->bc->dad,sizeof(p->bc->cad)); + ast_copy_string(p->bc->cad, p->bc->dad, sizeof(p->bc->cad)); } misdn_lib_send_event( p->bc, EVENT_CONNECT); start_bc_tones(p); - + return 0; } @@ -2218,11 +2136,11 @@ static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int dur { struct chan_list *p; struct misdn_bchannel *bc; - - if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast))) return -1; - bc=p->bc; - chan_misdn_log(1, bc?bc->port:0, "* IND : Digit %c\n",digit); + if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) return -1; + + bc = p->bc; + chan_misdn_log(1, bc ? bc->port : 0, "* IND : Digit %c\n", digit); if (!bc) { ast_log(LOG_WARNING, " --> !! Got Digit Event without having bchannel Object\n"); @@ -2230,7 +2148,7 @@ static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int dur } switch (p->state ) { - case MISDN_CALLING: + case MISDN_CALLING: { int l; char buf[8]; @@ -2241,7 +2159,7 @@ static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int dur strncat(bc->infos_pending, buf, l - strlen(bc->infos_pending) - 1); } break; - case MISDN_CALLING_ACKNOWLEDGE: + case MISDN_CALLING_ACKNOWLEDGE: { bc->info_dad[0]=digit; bc->info_dad[1]=0; @@ -2259,8 +2177,7 @@ static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int dur misdn_lib_send_event( bc, EVENT_INFORMATION); } break; - - default: + default: /* Do not send Digits in CONNECTED State, when * the other side is too mISDN. */ if (p->other_ch ) @@ -2270,7 +2187,7 @@ static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int dur send_digit_to_chan(p,digit); break; } - + return 0; } @@ -2278,13 +2195,13 @@ static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int dur static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast) { struct chan_list *p; - + if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast) )) return -1; - - chan_misdn_log(1, p->bc?p->bc->port:0, "* IND: Got Fixup State:%s L3id:%x\n", misdn_get_ch_state(p), p->l3id); - - p->ast = ast ; - + + chan_misdn_log(1, p->bc ? p->bc->port : 0, "* IND: Got Fixup State:%s L3id:%x\n", misdn_get_ch_state(p), p->l3id); + + p->ast = ast; + return 0; } @@ -2294,26 +2211,25 @@ static int misdn_indication(struct ast_channel *ast, int cond, const void *data, { struct chan_list *p; - if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast))) { ast_log(LOG_WARNING, "Returned -1 in misdn_indication\n"); return -1; } if (!p->bc ) { - chan_misdn_log(1, 0, "* IND : Indication from %s\n",ast->exten); + chan_misdn_log(1, 0, "* IND : Indication from %s\n", ast->exten); ast_log(LOG_WARNING, "Private Pointer but no bc ?\n"); return -1; } - chan_misdn_log(5, p->bc->port, "* IND : Indication [%d] from %s\n",cond, ast->exten); + chan_misdn_log(5, p->bc->port, "* IND : Indication [%d] from %s\n", cond, ast->exten); switch (cond) { case AST_CONTROL_BUSY: - chan_misdn_log(1, p->bc->port, "* IND :\tbusy pid:%d\n",p->bc?p->bc->pid:-1); - ast_setstate(ast,AST_STATE_BUSY); + chan_misdn_log(1, p->bc->port, "* IND :\tbusy pid:%d\n", p->bc ? p->bc->pid : -1); + ast_setstate(ast, AST_STATE_BUSY); - p->bc->out_cause=17; + p->bc->out_cause = AST_CAUSE_USER_BUSY; if (p->state != MISDN_CONNECTED) { start_bc_tones(p); misdn_lib_send_event( p->bc, EVENT_DISCONNECT); @@ -2321,75 +2237,69 @@ static int misdn_indication(struct ast_channel *ast, int cond, const void *data, chan_misdn_log(-1, p->bc->port, " --> !! Got Busy in Connected State !?! ast:%s\n", ast->name); } return -1; - break; case AST_CONTROL_RING: - chan_misdn_log(1, p->bc->port, "* IND :\tring pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, "* IND :\tring pid:%d\n", p->bc ? p->bc->pid : -1); return -1; - break; - case AST_CONTROL_RINGING: - chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n", p->bc ? p->bc->pid : -1); switch (p->state) { - case MISDN_ALERTING: - chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n",p->bc?p->bc->pid:-1); - break; - case MISDN_CONNECTED: - chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n",p->bc?p->bc->pid:-1); - return -1; - break; - default: - p->state=MISDN_ALERTING; - chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d\n",p->bc?p->bc->pid:-1); - misdn_lib_send_event( p->bc, EVENT_ALERTING); - - if (p->other_ch && p->other_ch->bc) { - if (misdn_inband_avail(p->other_ch->bc)) { - chan_misdn_log(2,p->bc->port, " --> other End is mISDN and has inband info available\n"); - break; - } + case MISDN_ALERTING: + chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n", p->bc ? p->bc->pid : -1); + break; + case MISDN_CONNECTED: + chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n", p->bc ? p->bc->pid : -1); + return -1; + default: + p->state = MISDN_ALERTING; + chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d\n", p->bc ? p->bc->pid : -1); + misdn_lib_send_event( p->bc, EVENT_ALERTING); - if (!p->other_ch->bc->nt) { - chan_misdn_log(2,p->bc->port, " --> other End is mISDN TE so it has inband info for sure (?)\n"); - break; - } + if (p->other_ch && p->other_ch->bc) { + if (misdn_inband_avail(p->other_ch->bc)) { + chan_misdn_log(2, p->bc->port, " --> other End is mISDN and has inband info available\n"); + break; } - chan_misdn_log(3, p->bc->port, " --> * SEND: State Ring pid:%d\n",p->bc?p->bc->pid:-1); - ast_setstate(ast,AST_STATE_RINGING); - - if ( !p->bc->nt && (p->originator==ORG_MISDN) && !p->incoming_early_audio ) - chan_misdn_log(2,p->bc->port, " --> incoming_early_audio off\n"); - else - return -1; + if (!p->other_ch->bc->nt) { + chan_misdn_log(2, p->bc->port, " --> other End is mISDN TE so it has inband info for sure (?)\n"); + break; + } + } + + chan_misdn_log(3, p->bc->port, " --> * SEND: State Ring pid:%d\n", p->bc ? p->bc->pid : -1); + ast_setstate(ast, AST_STATE_RINGING); + + if (!p->bc->nt && (p->originator == ORG_MISDN) && !p->incoming_early_audio) + chan_misdn_log(2, p->bc->port, " --> incoming_early_audio off\n"); + else + return -1; } break; case AST_CONTROL_ANSWER: - chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n", p->bc ? p->bc->pid : -1); start_bc_tones(p); break; case AST_CONTROL_TAKEOFFHOOK: - chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n", p->bc ? p->bc->pid : -1); return -1; - break; case AST_CONTROL_OFFHOOK: - chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n", p->bc ? p->bc->pid : -1); return -1; - break; case AST_CONTROL_FLASH: - chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n", p->bc ? p->bc->pid : -1); break; case AST_CONTROL_PROGRESS: - chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n", p->bc ? p->bc->pid : -1); misdn_lib_send_event( p->bc, EVENT_PROGRESS); break; case AST_CONTROL_PROCEEDING: - chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n", p->bc ? p->bc->pid : -1); misdn_lib_send_event( p->bc, EVENT_PROCEEDING); break; case AST_CONTROL_CONGESTION: - chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n", p->bc ? p->bc->pid : -1); - p->bc->out_cause=42; + p->bc->out_cause = AST_CAUSE_SWITCH_CONGESTION; start_bc_tones(p); misdn_lib_send_event( p->bc, EVENT_DISCONNECT); @@ -2398,25 +2308,23 @@ static int misdn_indication(struct ast_channel *ast, int cond, const void *data, } break; case -1 : - chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n",p->bc?p->bc->pid:-1); - + chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n", p->bc ? p->bc->pid : -1); + stop_indicate(p); if (p->state == MISDN_CONNECTED) start_bc_tones(p); - break; - case AST_CONTROL_HOLD: ast_moh_start(ast,data,ast->musicclass); - chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n", p->bc ? p->bc->pid : -1); break; case AST_CONTROL_UNHOLD: ast_moh_stop(ast); - chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n",p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n", p->bc ? p->bc->pid : -1); break; default: - chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n",cond,p->bc?p->bc->pid:-1); + chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n", cond, p->bc ? p->bc->pid : -1); } return 0; @@ -2425,18 +2333,19 @@ static int misdn_indication(struct ast_channel *ast, int cond, const void *data, static int misdn_hangup(struct ast_channel *ast) { struct chan_list *p; - struct misdn_bchannel *bc=NULL; + struct misdn_bchannel *bc = NULL; + const char *varcause = NULL; ast_log(LOG_DEBUG, "misdn_hangup(%s)\n", ast->name); - + if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast) ) ) return -1; - + if (!p) { chan_misdn_log(3, 0, "misdn_hangup called, without chan_list obj.\n"); return 0 ; } - - bc=p->bc; + + bc = p->bc; if (bc) { const char *tmp=pbx_builtin_getvar_helper(ast,"MISDN_USERUSER"); @@ -2447,8 +2356,8 @@ static int misdn_hangup(struct ast_channel *ast) } } - MISDN_ASTERISK_TECH_PVT(ast)=NULL; - p->ast=NULL; + MISDN_ASTERISK_TECH_PVT(ast) = NULL; + p->ast = NULL; if (ast->_state == AST_STATE_RESERVED || p->state == MISDN_NOTHING || @@ -2458,7 +2367,7 @@ static int misdn_hangup(struct ast_channel *ast) CLEAN_CH: /* between request and call */ ast_log(LOG_DEBUG, "State Reserved (or nothing) => chanIsAvail\n"); - MISDN_ASTERISK_TECH_PVT(ast)=NULL; + MISDN_ASTERISK_TECH_PVT(ast) = NULL; ast_mutex_lock(&release_lock); cl_dequeue_chan(&cl_te, p); @@ -2474,132 +2383,125 @@ static int misdn_hangup(struct ast_channel *ast) } if (!bc) { - ast_log(LOG_WARNING,"Hangup with private but no bc ? state:%s l3id:%x\n", misdn_get_ch_state(p), p->l3id); + ast_log(LOG_WARNING, "Hangup with private but no bc ? state:%s l3id:%x\n", misdn_get_ch_state(p), p->l3id); goto CLEAN_CH; } - p->need_hangup=0; - p->need_queue_hangup=0; - p->need_busy=0; + p->need_hangup = 0; + p->need_queue_hangup = 0; + p->need_busy = 0; if (!p->bc->nt) stop_bc_tones(p); - - { - const char *varcause=NULL; - bc->out_cause=ast->hangupcause?ast->hangupcause:16; - - if ( (varcause=pbx_builtin_getvar_helper(ast, "HANGUPCAUSE")) || - (varcause=pbx_builtin_getvar_helper(ast, "PRI_CAUSE"))) { - int tmpcause=atoi(varcause); - bc->out_cause=tmpcause?tmpcause:16; - } - - chan_misdn_log(1, bc->port, "* IND : HANGUP\tpid:%d ctx:%s dad:%s oad:%s State:%s\n",p->bc?p->bc->pid:-1, ast->context, ast->exten, ast->cid.cid_num, misdn_get_ch_state(p)); - chan_misdn_log(3, bc->port, " --> l3id:%x\n",p->l3id); - chan_misdn_log(3, bc->port, " --> cause:%d\n",bc->cause); - chan_misdn_log(2, bc->port, " --> out_cause:%d\n",bc->out_cause); - chan_misdn_log(2, bc->port, " --> state:%s\n", misdn_get_ch_state(p)); - - switch (p->state) { - case MISDN_CALLING: - case MISDN_INCOMING_SETUP: - /* This is the only place in misdn_hangup, where we - * can call release_chan, else it might create lot's of trouble - * */ - ast_log(LOG_NOTICE, "release channel, in CALLING/INCOMING_SETUP state.. no other events happened\n"); - release_chan(bc); + bc->out_cause = ast->hangupcause ? ast->hangupcause : AST_CAUSE_NORMAL_CLEARING; - p->state=MISDN_CLEANING; - if (bc->need_release_complete) - misdn_lib_send_event( bc, EVENT_RELEASE_COMPLETE); - break; - case MISDN_HOLDED: - case MISDN_DIALING: - start_bc_tones(p); + if ((varcause = pbx_builtin_getvar_helper(ast, "HANGUPCAUSE")) || + (varcause = pbx_builtin_getvar_helper(ast, "PRI_CAUSE"))) { + int tmpcause = atoi(varcause); + bc->out_cause = tmpcause ? tmpcause : AST_CAUSE_NORMAL_CLEARING; + } + + chan_misdn_log(1, bc->port, "* IND : HANGUP\tpid:%d ctx:%s dad:%s oad:%s State:%s\n", p->bc ? p->bc->pid : -1, ast->context, ast->exten, ast->cid.cid_num, misdn_get_ch_state(p)); + chan_misdn_log(3, bc->port, " --> l3id:%x\n", p->l3id); + chan_misdn_log(3, bc->port, " --> cause:%d\n", bc->cause); + chan_misdn_log(2, bc->port, " --> out_cause:%d\n", bc->out_cause); + chan_misdn_log(2, bc->port, " --> state:%s\n", misdn_get_ch_state(p)); + + switch (p->state) { + case MISDN_INCOMING_SETUP: + case MISDN_CALLING: + /* This is the only place in misdn_hangup, where we + * can call release_chan, else it might create lot's of trouble + * */ + ast_log(LOG_NOTICE, "release channel, in CALLING/INCOMING_SETUP state.. no other events happened\n"); + release_chan(bc); + + p->state = MISDN_CLEANING; + if (bc->need_release_complete) + misdn_lib_send_event( bc, EVENT_RELEASE_COMPLETE); + break; + case MISDN_HOLDED: + case MISDN_DIALING: + start_bc_tones(p); + hanguptone_indicate(p); + + if (bc->need_disconnect) + misdn_lib_send_event( bc, EVENT_DISCONNECT); + break; + case MISDN_CALLING_ACKNOWLEDGE: + start_bc_tones(p); + hanguptone_indicate(p); + + if (bc->need_disconnect) + misdn_lib_send_event( bc, EVENT_DISCONNECT); + break; + + case MISDN_ALERTING: + case MISDN_PROGRESS: + case MISDN_PROCEEDING: + if (p->originator != ORG_AST) hanguptone_indicate(p); - - if (bc->need_disconnect) - misdn_lib_send_event( bc, EVENT_DISCONNECT); - break; - case MISDN_CALLING_ACKNOWLEDGE: + /*p->state=MISDN_CLEANING;*/ + if (bc->need_disconnect) + misdn_lib_send_event( bc, EVENT_DISCONNECT); + break; + case MISDN_CONNECTED: + case MISDN_PRECONNECTED: + /* Alerting or Disconnect */ + if (p->bc->nt) { start_bc_tones(p); hanguptone_indicate(p); - - if (bc->need_disconnect) - misdn_lib_send_event( bc, EVENT_DISCONNECT); - break; - - case MISDN_ALERTING: - case MISDN_PROGRESS: - case MISDN_PROCEEDING: - if (p->originator != ORG_AST) - hanguptone_indicate(p); - - /*p->state=MISDN_CLEANING;*/ - if (bc->need_disconnect) - misdn_lib_send_event( bc, EVENT_DISCONNECT); - break; - case MISDN_CONNECTED: - case MISDN_PRECONNECTED: - /* Alerting or Disconnect */ - if (p->bc->nt) { - start_bc_tones(p); - hanguptone_indicate(p); - p->bc->progress_indicator=8; - } - if (bc->need_disconnect) - misdn_lib_send_event( bc, EVENT_DISCONNECT); + p->bc->progress_indicator = INFO_PI_INBAND_AVAILABLE; + } + if (bc->need_disconnect) + misdn_lib_send_event( bc, EVENT_DISCONNECT); - /*p->state=MISDN_CLEANING;*/ - break; - case MISDN_DISCONNECTED: - if (bc->need_release) - misdn_lib_send_event( bc, EVENT_RELEASE); - p->state=MISDN_CLEANING; /* MISDN_HUNGUP_FROM_AST; */ - break; + /*p->state=MISDN_CLEANING;*/ + break; + case MISDN_DISCONNECTED: + if (bc->need_release) + misdn_lib_send_event( bc, EVENT_RELEASE); + p->state = MISDN_CLEANING; /* MISDN_HUNGUP_FROM_AST; */ + break; - case MISDN_RELEASED: - case MISDN_CLEANING: - p->state=MISDN_CLEANING; - break; + case MISDN_RELEASED: + case MISDN_CLEANING: + p->state = MISDN_CLEANING; + break; - case MISDN_BUSY: - break; - - case MISDN_HOLD_DISCONNECT: - /* need to send release here */ - chan_misdn_log(1, bc->port, " --> cause %d\n",bc->cause); - chan_misdn_log(1, bc->port, " --> out_cause %d\n",bc->out_cause); - - bc->out_cause=-1; + case MISDN_BUSY: + break; + + case MISDN_HOLD_DISCONNECT: + /* need to send release here */ + chan_misdn_log(1, bc->port, " --> cause %d\n", bc->cause); + chan_misdn_log(1, bc->port, " --> out_cause %d\n", bc->out_cause); + + bc->out_cause = -1; + if (bc->need_release) + misdn_lib_send_event(bc, EVENT_RELEASE); + p->state = MISDN_CLEANING; + break; + default: + if (bc->nt) { + bc->out_cause = -1; if (bc->need_release) - misdn_lib_send_event(bc,EVENT_RELEASE); - p->state=MISDN_CLEANING; - break; - default: - if (bc->nt) { - bc->out_cause=-1; - if (bc->need_release) - misdn_lib_send_event(bc, EVENT_RELEASE); - p->state=MISDN_CLEANING; - } else { - if (bc->need_disconnect) - misdn_lib_send_event(bc, EVENT_DISCONNECT); - } + misdn_lib_send_event(bc, EVENT_RELEASE); + p->state = MISDN_CLEANING; + } else { + if (bc->need_disconnect) + misdn_lib_send_event(bc, EVENT_DISCONNECT); } - - p->state=MISDN_CLEANING; - } - - chan_misdn_log(3, bc->port, " --> Channel: %s hanguped new state:%s\n",ast->name,misdn_get_ch_state(p)); - + p->state = MISDN_CLEANING; + + chan_misdn_log(3, bc->port, " --> Channel: %s hanguped new state:%s\n", ast->name, misdn_get_ch_state(p)); + return 0; } @@ -2616,7 +2518,6 @@ static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame return NULL; } - if (!f || (f->frametype != AST_FRAME_DTMF)) return frame; @@ -2655,15 +2556,17 @@ static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, context); } else ast_log(LOG_NOTICE, "Fax detected, but no fax extension ctx:%s exten:%s\n", context, ast->exten); - } else + } else { ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n"); + } break; case 2: ast_verbose(VERBOSE_PREFIX_3 "Not redirecting %s to fax extension, nojump is set.\n", ast->name); break; } - } else + } else { ast_log(LOG_DEBUG, "Fax already handled\n"); + } } if (tmp->ast_dsp && (f->subclass != 'f')) { @@ -2674,7 +2577,7 @@ static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame } -static struct ast_frame *misdn_read(struct ast_channel *ast) +static struct ast_frame *misdn_read(struct ast_channel *ast) { struct chan_list *tmp; fd_set rrfs; @@ -2682,16 +2585,16 @@ static struct ast_frame *misdn_read(struct ast_channel *ast) int len, t; if (!ast) { - chan_misdn_log(1,0,"misdn_read called without ast\n"); + chan_misdn_log(1, 0, "misdn_read called without ast\n"); return NULL; } - if (!(tmp=MISDN_ASTERISK_TECH_PVT(ast))) { - chan_misdn_log(1,0,"misdn_read called without ast->pvt\n"); + if (!(tmp = MISDN_ASTERISK_TECH_PVT(ast))) { + chan_misdn_log(1, 0, "misdn_read called without ast->pvt\n"); return NULL; } - if (!tmp->bc && !(tmp->state==MISDN_HOLDED)) { - chan_misdn_log(1,0,"misdn_read called without bc\n"); + if (!tmp->bc && !(tmp->state == MISDN_HOLDED)) { + chan_misdn_log(1, 0, "misdn_read called without bc\n"); return NULL; } @@ -2726,13 +2629,13 @@ static struct ast_frame *misdn_read(struct ast_channel *ast) return NULL; } - tmp->frame.frametype = AST_FRAME_VOICE; + tmp->frame.frametype = AST_FRAME_VOICE; tmp->frame.subclass = AST_FORMAT_ALAW; tmp->frame.datalen = len; tmp->frame.samples = len; tmp->frame.mallocd = 0; tmp->frame.offset = 0; - tmp->frame.delivery= ast_tv(0,0) ; + tmp->frame.delivery = ast_tv(0,0); tmp->frame.src = NULL; tmp->frame.data = tmp->ast_rd_buf; @@ -2740,22 +2643,22 @@ static struct ast_frame *misdn_read(struct ast_channel *ast) if (tmp->faxdetect_timeout) { if (ast_tvzero(tmp->faxdetect_tv)) { tmp->faxdetect_tv = ast_tvnow(); - chan_misdn_log(2,tmp->bc->port,"faxdetect: starting detection with timeout: %ds ...\n", tmp->faxdetect_timeout); + chan_misdn_log(2, tmp->bc->port, "faxdetect: starting detection with timeout: %ds ...\n", tmp->faxdetect_timeout); return process_ast_dsp(tmp, &tmp->frame); } else { struct timeval tv_now = ast_tvnow(); int diff = ast_tvdiff_ms(tv_now, tmp->faxdetect_tv); if (diff <= (tmp->faxdetect_timeout * 1000)) { - chan_misdn_log(5,tmp->bc->port,"faxdetect: detecting ...\n"); + chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ...\n"); return process_ast_dsp(tmp, &tmp->frame); } else { - chan_misdn_log(2,tmp->bc->port,"faxdetect: stopping detection (time ran out) ...\n"); + chan_misdn_log(2, tmp->bc->port, "faxdetect: stopping detection (time ran out) ...\n"); tmp->faxdetect = 0; return &tmp->frame; } } } else { - chan_misdn_log(5,tmp->bc->port,"faxdetect: detecting ... (no timeout)\n"); + chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ... (no timeout)\n"); return process_ast_dsp(tmp, &tmp->frame); } } else { @@ -2772,7 +2675,7 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame) struct chan_list *ch; int i = 0; - if (!ast || ! (ch=MISDN_ASTERISK_TECH_PVT(ast)) ) return -1; + if (!ast || ! (ch = MISDN_ASTERISK_TECH_PVT(ast)) ) return -1; if (ch->state == MISDN_HOLDED) { chan_misdn_log(7, 0, "misdn_write: Returning because holded\n"); @@ -2790,26 +2693,26 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame) } - if ( !frame->subclass) { + if (!frame->subclass) { chan_misdn_log(4, ch->bc->port, "misdn_write: * prods us\n"); return 0; } - if ( !(frame->subclass & prefformat)) { + if (!(frame->subclass & prefformat)) { chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%d\n", frame->subclass); return 0; } - if ( !frame->samples ) { + if (!frame->samples ) { chan_misdn_log(4, ch->bc->port, "misdn_write: zero write\n"); if (!strcmp(frame->src,"ast_prod")) { chan_misdn_log(1, ch->bc->port, "misdn_write: state (%s) prodded.\n", misdn_get_ch_state(ch)); if (ch->ts) { - chan_misdn_log(4,ch->bc->port,"Starting Playtones\n"); + chan_misdn_log(4, ch->bc->port, "Starting Playtones\n"); misdn_lib_tone_generator_start(ch->bc); } return 0; @@ -2825,49 +2728,46 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame) #if MISDN_DEBUG { - int i, max=5>frame->samples?frame->samples:5; - + int i, max = 5 > frame->samples ? frame->samples : 5; + printf("write2mISDN %p %d bytes: ", p, frame->samples); - - for (i=0; i< max ; i++) printf("%2.2x ",((char*) frame->data)[i]); + + for (i = 0; i < max; i++) + printf("%2.2x ", ((char*) frame->data)[i]); printf ("\n"); } #endif - switch (ch->bc->bc_state) { - case BCHAN_ACTIVATED: - case BCHAN_BRIDGED: - break; - default: + case BCHAN_ACTIVATED: + case BCHAN_BRIDGED: + break; + default: if (!ch->dropped_frame_cnt) - chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n",frame->samples,ch->bc->addr, ast->exten, ast->cid.cid_num,misdn_get_ch_state( ch), ch->bc->bc_state, ch->bc->l3_id); + chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n", frame->samples, ch->bc->addr, ast->exten, ast->cid.cid_num, misdn_get_ch_state( ch), ch->bc->bc_state, ch->bc->l3_id); ch->dropped_frame_cnt++; if (ch->dropped_frame_cnt > 100) { - ch->dropped_frame_cnt=0; - chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x dropped > 100 frames!\n",frame->samples,ch->bc->addr); - + ch->dropped_frame_cnt = 0; + chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x dropped > 100 frames!\n", frame->samples, ch->bc->addr); } return 0; } - chan_misdn_log(9, ch->bc->port, "Sending :%d bytes 2 MISDN\n",frame->samples); + chan_misdn_log(9, ch->bc->port, "Sending :%d bytes to MISDN\n", frame->samples); if ( !ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability) ) { /* Buffered Transmit (triggered by read from isdn side)*/ - if (misdn_jb_fill(ch->jb,frame->data,frame->samples) < 0) { + if (misdn_jb_fill(ch->jb, frame->data, frame->samples) < 0) { if (ch->bc->active) - cb_log(0,ch->bc->port,"Misdn Jitterbuffer Overflow.\n"); + cb_log(0, ch->bc->port, "Misdn Jitterbuffer Overflow.\n"); } } else { /*transmit without jitterbuffer*/ - i=misdn_lib_tx2misdn_frm(ch->bc, frame->data, frame->samples); + i = misdn_lib_tx2misdn_frm(ch->bc, frame->data, frame->samples); } - - return 0; } @@ -2881,78 +2781,76 @@ static enum ast_bridge_result misdn_bridge (struct ast_channel *c0, int timeoutms) { - struct chan_list *ch1,*ch2; + struct chan_list *ch1, *ch2; struct ast_channel *carr[2], *who; - int to=-1; + int to = -1; struct ast_frame *f; int p1_b, p2_b; int bridging; - ch1=get_chan_by_ast(c0); - ch2=get_chan_by_ast(c1); + ch1 = get_chan_by_ast(c0); + ch2 = get_chan_by_ast(c1); - carr[0]=c0; - carr[1]=c1; + carr[0] = c0; + carr[1] = c1; - if (ch1 && ch2 ) ; - else + if (!(ch1 && ch2)) return -1; misdn_cfg_get(ch1->bc->port, MISDN_CFG_BRIDGING, &p1_b, sizeof(int)); misdn_cfg_get(ch2->bc->port, MISDN_CFG_BRIDGING, &p2_b, sizeof(int)); - - if ( ! p1_b || ! p2_b) { + + if (! p1_b || ! p2_b) { ast_log(LOG_NOTICE, "Falling back to Asterisk bridging\n"); return AST_BRIDGE_FAILED; } - misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int)); + misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(int)); if (bridging) { /* trying to make a mISDN_dsp conference */ - chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid +1); - misdn_lib_bridge(ch1->bc,ch2->bc); + chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid + 1); + misdn_lib_bridge(ch1->bc, ch2->bc); } - + if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name); chan_misdn_log(1, ch1->bc->port, "* Making Native Bridge between %s and %s\n", ch1->bc->oad, ch2->bc->oad); - if (! (flags&AST_BRIDGE_DTMF_CHANNEL_0) ) - ch1->ignore_dtmf=1; + if (! (flags & AST_BRIDGE_DTMF_CHANNEL_0) ) + ch1->ignore_dtmf = 1; - if (! (flags&AST_BRIDGE_DTMF_CHANNEL_1) ) - ch2->ignore_dtmf=1; + if (! (flags & AST_BRIDGE_DTMF_CHANNEL_1) ) + ch2->ignore_dtmf = 1; - while(1) { - to=-1; + for (;/*ever*/;) { + to = -1; who = ast_waitfor_n(carr, 2, &to); if (!who) { - ast_log(LOG_NOTICE,"misdn_bridge: empty read, breaking out\n"); + ast_log(LOG_NOTICE, "misdn_bridge: empty read, breaking out\n"); break; } f = ast_read(who); - + if (!f || f->frametype == AST_FRAME_CONTROL) { /* got hangup .. */ if (!f) - chan_misdn_log(4,ch1->bc->port,"Read Null Frame\n"); + chan_misdn_log(4, ch1->bc->port, "Read Null Frame\n"); else - chan_misdn_log(4,ch1->bc->port,"Read Frame Control class:%d\n",f->subclass); - - *fo=f; - *rc=who; - + chan_misdn_log(4, ch1->bc->port, "Read Frame Control class:%d\n", f->subclass); + + *fo = f; + *rc = who; break; } if ( f->frametype == AST_FRAME_DTMF ) { - chan_misdn_log(1,0,"Read DTMF %d from %s\n",f->subclass, who->exten); + chan_misdn_log(1, 0, "Read DTMF %d from %s\n", f->subclass, who->exten); - *fo=f; - *rc=who; + *fo = f; + *rc = who; break; } @@ -2965,19 +2863,17 @@ static enum ast_bridge_result misdn_bridge (struct ast_channel *c0, #endif if (who == c0) { - ast_write(c1,f); + ast_write(c1, f); } else { - ast_write(c0,f); + ast_write(c0, f); } - } - - chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid +1); - - misdn_lib_split_bridge(ch1->bc,ch2->bc); - - + + chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid + 1); + + misdn_lib_split_bridge(ch1->bc, ch2->bc); + return AST_BRIDGE_COMPLETE; } @@ -2985,31 +2881,31 @@ static enum ast_bridge_result misdn_bridge (struct ast_channel *c0, static int dialtone_indicate(struct chan_list *cl) { - const struct ind_tone_zone_sound *ts= NULL; - struct ast_channel *ast=cl->ast; - int nd=0; + const struct ind_tone_zone_sound *ts = NULL; + struct ast_channel *ast = cl->ast; + int nd = 0; if (!ast) { - chan_misdn_log(0,cl->bc->port,"No Ast in dialtone_indicate\n"); + chan_misdn_log(0, cl->bc->port, "No Ast in dialtone_indicate\n"); return -1; } - misdn_cfg_get( cl->bc->port, MISDN_CFG_NODIALTONE, &nd, sizeof(nd)); + misdn_cfg_get(cl->bc->port, MISDN_CFG_NODIALTONE, &nd, sizeof(nd)); if (nd) { - chan_misdn_log(1,cl->bc->port,"Not sending Dialtone, because config wants it\n"); + chan_misdn_log(1, cl->bc->port, "Not sending Dialtone, because config wants it\n"); return 0; } - chan_misdn_log(3,cl->bc->port," --> Dial\n"); - ts=ast_get_indication_tone(ast->zone,"dial"); - cl->ts=ts; + chan_misdn_log(3, cl->bc->port, " --> Dial\n"); + ts = ast_get_indication_tone(ast->zone, "dial"); + cl->ts = ts; if (ts) { - cl->notxtone=0; - cl->norxtone=0; + cl->notxtone = 0; + cl->norxtone = 0; /* This prods us in misdn_write */ - ast_playtones_start(ast,0, ts->data, 0); + ast_playtones_start(ast, 0, ts->data, 0); } return 0; @@ -3017,26 +2913,26 @@ static int dialtone_indicate(struct chan_list *cl) static int hanguptone_indicate(struct chan_list *cl) { - misdn_lib_send_tone(cl->bc,TONE_HANGUP); + misdn_lib_send_tone(cl->bc, TONE_HANGUP); return 0; } static int stop_indicate(struct chan_list *cl) { - struct ast_channel *ast=cl->ast; + struct ast_channel *ast = cl->ast; if (!ast) { - chan_misdn_log(0,cl->bc->port,"No Ast in stop_indicate\n"); + chan_misdn_log(0, cl->bc->port, "No Ast in stop_indicate\n"); return -1; } - chan_misdn_log(3,cl->bc->port," --> None\n"); + chan_misdn_log(3, cl->bc->port, " --> None\n"); misdn_lib_tone_generator_stop(cl->bc); ast_playtones_stop(ast); - cl->ts=NULL; + cl->ts = NULL; /*ast_deactivate_generator(ast);*/ - + return 0; } @@ -3044,8 +2940,8 @@ static int stop_indicate(struct chan_list *cl) static int start_bc_tones(struct chan_list* cl) { misdn_lib_tone_generator_stop(cl->bc); - cl->notxtone=0; - cl->norxtone=0; + cl->notxtone = 0; + cl->norxtone = 0; return 0; } @@ -3053,8 +2949,8 @@ static int stop_bc_tones(struct chan_list *cl) { if (!cl) return -1; - cl->notxtone=1; - cl->norxtone=1; + cl->notxtone = 1; + cl->norxtone = 1; return 0; } @@ -3062,77 +2958,70 @@ static int stop_bc_tones(struct chan_list *cl) static struct chan_list *init_chan_list(int orig) { - struct chan_list *cl=malloc(sizeof(struct chan_list)); - + struct chan_list *cl; + + cl = calloc(1, sizeof(struct chan_list)); if (!cl) { chan_misdn_log(-1, 0, "misdn_request: malloc failed!"); return NULL; } - memset(cl,0,sizeof(struct chan_list)); + cl->originator = orig; + cl->need_queue_hangup = 1; + cl->need_hangup = 1; + cl->need_busy = 1; + cl->overlap_dial_task = -1; - cl->originator=orig; - cl->need_queue_hangup=1; - cl->need_hangup=1; - cl->need_busy=1; - cl->overlap_dial_task=-1; - return cl; - } static struct ast_channel *misdn_request(const char *type, int format, void *data, int *cause) - { struct ast_channel *tmp = NULL; - char group[BUFFERSIZE+1]=""; + char group[BUFFERSIZE + 1] = ""; char buf[128]; - char buf2[128], *ext=NULL, *port_str; - char *tokb=NULL, *p=NULL; - int channel=0, port=0; + char buf2[128], *ext = NULL, *port_str; + char *tokb = NULL, *p = NULL; + int channel = 0, port = 0; struct misdn_bchannel *newbc = NULL; - int dec=0; - - struct chan_list *cl=init_chan_list(ORG_AST); - - sprintf(buf,"%s/%s",misdn_type,(char*)data); - ast_copy_string(buf2,data, 128); - - port_str=strtok_r(buf2,"/", &tokb); + int dec = 0; + + struct chan_list *cl = init_chan_list(ORG_AST); + + snprintf(buf, sizeof(buf), "%s/%s", misdn_type, (char*)data); + ast_copy_string(buf2, data, 128); - ext=strtok_r(NULL,"/", &tokb); + port_str = strtok_r(buf2, "/", &tokb); + + ext = strtok_r(NULL, "/", &tokb); if (port_str) { - if (port_str[0]=='g' && port_str[1]==':' ) { + if (port_str[0] == 'g' && port_str[1] == ':' ) { /* We make a group call lets checkout which ports are in my group */ port_str += 2; - strncpy(group, port_str, BUFFERSIZE); - group[127] = 0; - chan_misdn_log(2, 0, " --> Group Call group: %s\n",group); - } - else if ((p = strchr(port_str, ':'))) { + ast_copy_string(group, port_str, sizeof(group)); + chan_misdn_log(2, 0, " --> Group Call group: %s\n", group); + } else if ((p = strchr(port_str, ':'))) { /* we have a preselected channel */ *p = 0; channel = atoi(++p); port = atoi(port_str); chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel); - } - else { + } else { port = atoi(port_str); } } else { - ast_log(LOG_WARNING, " --> ! IND : CALL dad:%s WITHOUT PORT/Group, check extension.conf\n",ext); + ast_log(LOG_WARNING, " --> ! IND : CALL dad:%s WITHOUT PORT/Group, check extensions.conf\n", ext); return NULL; } if (misdn_cfg_is_group_method(group, METHOD_STANDARD_DEC)) { - chan_misdn_log(4, port, " --> STARTING STANDARDDEC...\n"); - dec=1; + chan_misdn_log(4, port, " --> STARTING STANDARD DEC...\n"); + dec = 1; } if (!ast_strlen_zero(group)) { - - char cfg_group[BUFFERSIZE+1]; + char cfg_group[BUFFERSIZE + 1]; struct robin_list *rr = NULL; if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) { @@ -3203,21 +3092,21 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat rr->channel = chan_bak; } } else { - for (port=misdn_cfg_get_next_port(0); port > 0; - port=misdn_cfg_get_next_port(port)) { - - misdn_cfg_get( port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE); + for (port = misdn_cfg_get_next_port(0); port > 0; + port = misdn_cfg_get_next_port(port)) { + + misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE); - chan_misdn_log(3,port, "Group [%s] Port [%d]\n", group, port); + chan_misdn_log(3, port, "Group [%s] Port [%d]\n", group, port); if (!strcasecmp(cfg_group, group)) { int port_up; int check; misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(int)); port_up = misdn_lib_port_up(port, check); - + chan_misdn_log(4, port, "portup:%d\n", port_up); - - if ( port_up>0 ) { + + if (port_up > 0) { newbc = misdn_lib_get_free_bc(port, 0, 0, dec); if (newbc) break; @@ -3237,18 +3126,18 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat } } else { /* 'Normal' Port dial * Port dial */ if (channel) - chan_misdn_log(1, port," --> preselected_channel: %d\n",channel); + chan_misdn_log(1, port, " --> preselected_channel: %d\n", channel); newbc = misdn_lib_get_free_bc(port, channel, 0, dec); if (!newbc) { - ast_log(LOG_WARNING, "Could not create channel on port:%d with extensions:%s\n",port,ext); + ast_log(LOG_WARNING, "Could not create channel on port:%d with extensions:%s\n", port, ext); return NULL; } } /* create ast_channel and link all the objects together */ - cl->bc=newbc; + cl->bc = newbc; tmp = misdn_new(cl, AST_STATE_RESERVED, ext, NULL, format, port, channel); if (!tmp) { @@ -3265,18 +3154,18 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat read_config(cl, ORG_AST); /* important */ - cl->need_hangup=0; + cl->need_hangup = 0; return tmp; } -static int misdn_send_text (struct ast_channel *chan, const char *text) +static int misdn_send_text(struct ast_channel *chan, const char *text) { - struct chan_list *tmp=chan->tech_pvt; + struct chan_list *tmp = chan->tech_pvt; if (tmp && tmp->bc) { - ast_copy_string(tmp->bc->display,text,sizeof(tmp->bc->display)); + ast_copy_string(tmp->bc->display, text, sizeof(tmp->bc->display)); misdn_lib_send_event(tmp->bc, EVENT_INFORMATION); } else { ast_log(LOG_WARNING, "No chan_list but send_text request?\n"); @@ -3287,85 +3176,87 @@ static int misdn_send_text (struct ast_channel *chan, const char *text) } static struct ast_channel_tech misdn_tech = { - .type="mISDN", - .description="Channel driver for mISDN Support (Bri/Pri)", - .capabilities= AST_FORMAT_ALAW , - .requester=misdn_request, - .send_digit_begin=misdn_digit_begin, - .send_digit_end=misdn_digit_end, - .call=misdn_call, - .bridge=misdn_bridge, - .hangup=misdn_hangup, - .answer=misdn_answer, - .read=misdn_read, - .write=misdn_write, - .indicate=misdn_indication, - .fixup=misdn_fixup, - .send_text=misdn_send_text, - .properties=0 + .type = "mISDN", + .description = "Channel driver for mISDN Support (Bri/Pri)", + .capabilities = AST_FORMAT_ALAW , + .requester = misdn_request, + .send_digit_begin = misdn_digit_begin, + .send_digit_end = misdn_digit_end, + .call = misdn_call, + .bridge = misdn_bridge, + .hangup = misdn_hangup, + .answer = misdn_answer, + .read = misdn_read, + .write = misdn_write, + .indicate = misdn_indication, + .fixup = misdn_fixup, + .send_text = misdn_send_text, + .properties = 0 }; static struct ast_channel_tech misdn_tech_wo_bridge = { - .type="mISDN", - .description="Channel driver for mISDN Support (Bri/Pri)", - .capabilities=AST_FORMAT_ALAW , - .requester=misdn_request, - .send_digit_begin=misdn_digit_begin, - .send_digit_end=misdn_digit_end, - .call=misdn_call, - .hangup=misdn_hangup, - .answer=misdn_answer, - .read=misdn_read, - .write=misdn_write, - .indicate=misdn_indication, - .fixup=misdn_fixup, - .send_text=misdn_send_text, - .properties=0 + .type = "mISDN", + .description = "Channel driver for mISDN Support (Bri/Pri)", + .capabilities = AST_FORMAT_ALAW , + .requester = misdn_request, + .send_digit_begin = misdn_digit_begin, + .send_digit_end = misdn_digit_end, + .call = misdn_call, + .hangup = misdn_hangup, + .answer = misdn_answer, + .read = misdn_read, + .write = misdn_write, + .indicate = misdn_indication, + .fixup = misdn_fixup, + .send_text = misdn_send_text, + .properties = 0 }; -static int glob_channel=0; +static int glob_channel = 0; static void update_name(struct ast_channel *tmp, int port, int c) { - int chan_offset=0; + int chan_offset = 0; int tmp_port = misdn_cfg_get_next_port(0); - for (; tmp_port > 0; tmp_port=misdn_cfg_get_next_port(tmp_port)) { - if (tmp_port == port) break; - chan_offset+=misdn_lib_port_is_pri(tmp_port)?30:2; + for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) { + if (tmp_port == port) + break; + chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2; } - if (c<0) c=0; + if (c < 0) + c = 0; ast_string_field_build(tmp, name, "%s/%d-u%d", - misdn_type, chan_offset+c, glob_channel++); + misdn_type, chan_offset + c, glob_channel++); - chan_misdn_log(3,port," --> updating channel name to [%s]\n",tmp->name); - + chan_misdn_log(3, port, " --> updating channel name to [%s]\n", tmp->name); } static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char *exten, char *callerid, int format, int port, int c) { struct ast_channel *tmp; char *cid_name = 0, *cid_num = 0; - int chan_offset=0; + int chan_offset = 0; int tmp_port = misdn_cfg_get_next_port(0); + int bridging; - for (; tmp_port > 0; tmp_port=misdn_cfg_get_next_port(tmp_port)) { - if (tmp_port == port) break; - chan_offset+=misdn_lib_port_is_pri(tmp_port)?30:2; + for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) { + if (tmp_port == port) + break; + chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2; } - if (c<0) c=0; + if (c < 0) + c = 0; - - if (callerid) + if (callerid) { ast_callerid_parse(callerid, &cid_name, &cid_num); + } tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", 0, "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++); - if (tmp) { - int bridging; - chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s\n",exten,callerid); - + chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s\n", exten, callerid); + tmp->nativeformats = prefformat; tmp->readformat = format; @@ -3374,44 +3265,39 @@ static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char tmp->rawwriteformat = format; tmp->tech_pvt = chlist; - - misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int)); - + + misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(int)); + if (bridging) tmp->tech = &misdn_tech; else tmp->tech = &misdn_tech_wo_bridge; - + tmp->writeformat = format; tmp->readformat = format; tmp->priority=1; - + if (exten) - ast_copy_string(tmp->exten, exten, sizeof(tmp->exten)); + ast_copy_string(tmp->exten, exten, sizeof(tmp->exten)); else - chan_misdn_log(1,0,"misdn_new: no exten given.\n"); - + chan_misdn_log(1, 0, "misdn_new: no exten given.\n"); + if (callerid) /* Don't use ast_set_callerid() here because it will * generate a needless NewCallerID event */ tmp->cid.cid_ani = ast_strdup(cid_num); - { - if (pipe(chlist->pipe)<0) - perror("Pipe failed\n"); - - tmp->fds[0]=chlist->pipe[0]; - - } - + if (pipe(chlist->pipe) < 0) + perror("Pipe failed\n"); + tmp->fds[0] = chlist->pipe[0]; + if (state == AST_STATE_RING) tmp->rings = 1; else tmp->rings = 0; - } else { - chan_misdn_log(-1,0,"Unable to allocate channel structure\n"); + chan_misdn_log(-1, 0, "Unable to allocate channel structure\n"); } return tmp; @@ -3419,53 +3305,52 @@ static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc) { - struct chan_list *help=list; - for (;help; help=help->next) { + struct chan_list *help = list; + for (; help; help = help->next) { if (help->bc == bc) return help; } - - chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad); - + + chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n", bc->oad, bc->dad); + return NULL; } static struct chan_list *find_chan_by_pid(struct chan_list *list, int pid) { - struct chan_list *help=list; - for (;help; help=help->next) { + struct chan_list *help = list; + for (; help; help = help->next) { if ( help->bc && (help->bc->pid == pid) ) return help; } - - chan_misdn_log(6, 0, "$$$ find_chan: No channel found for pid:%d\n",pid); - + + chan_misdn_log(6, 0, "$$$ find_chan: No channel found for pid:%d\n", pid); + return NULL; } static struct chan_list *find_holded(struct chan_list *list, struct misdn_bchannel *bc) { - struct chan_list *help=list; + struct chan_list *help = list; if (bc->pri) return NULL; - - chan_misdn_log(6, bc->port, "$$$ find_holded: channel:%d oad:%s dad:%s\n",bc->channel, bc->oad,bc->dad); - for (;help; help=help->next) { - chan_misdn_log(4, bc->port, "$$$ find_holded: --> holded:%d channel:%d\n",help->state==MISDN_HOLDED, help->hold_info.channel); - if ( (help->state == MISDN_HOLDED) && - (help->hold_info.port == bc->port) ) + + chan_misdn_log(6, bc->port, "$$$ find_holded: channel:%d oad:%s dad:%s\n", bc->channel, bc->oad, bc->dad); + for (;help; help = help->next) { + chan_misdn_log(4, bc->port, "$$$ find_holded: --> holded:%d channel:%d\n", help->state == MISDN_HOLDED, help->hold_info.channel); + if ((help->state == MISDN_HOLDED) && + (help->hold_info.port == bc->port)) return help; - } - chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad); - + } + chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n", bc->oad, bc->dad); + return NULL; } static struct chan_list *find_holded_l3(struct chan_list *list, unsigned long l3_id, int w) - { - struct chan_list *help=list; + struct chan_list *help = list; - for (;help; help=help->next) { + for (; help; help = help->next) { if ( (help->state == MISDN_HOLDED) && (help->l3id == l3_id) ) @@ -3477,29 +3362,29 @@ static struct chan_list *find_holded_l3(struct chan_list *list, unsigned long l3 static void cl_queue_chan(struct chan_list **list, struct chan_list *chan) { - chan_misdn_log(4, chan->bc? chan->bc->port : 0, "* Queuing chan %p\n",chan); + chan_misdn_log(4, chan->bc ? chan->bc->port : 0, "* Queuing chan %p\n", chan); ast_mutex_lock(&cl_te_lock); if (!*list) { *list = chan; } else { - struct chan_list *help=*list; - for (;help->next; help=help->next); - help->next=chan; + struct chan_list *help = *list; + for (; help->next; help = help->next); + help->next = chan; } - chan->next=NULL; + chan->next = NULL; ast_mutex_unlock(&cl_te_lock); } static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan) { + struct chan_list *help; + if (chan->dsp) ast_dsp_free(chan->dsp); if (chan->trans) ast_translator_free_path(chan->trans); - - ast_mutex_lock(&cl_te_lock); if (!*list) { ast_mutex_unlock(&cl_te_lock); @@ -3507,19 +3392,16 @@ static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan) } if (*list == chan) { - *list=(*list)->next; + *list = (*list)->next; ast_mutex_unlock(&cl_te_lock); - return ; + return; } - { - struct chan_list *help=*list; - for (;help->next; help=help->next) { - if (help->next == chan) { - help->next=help->next->next; - ast_mutex_unlock(&cl_te_lock); - return; - } + for (help = *list; help->next; help = help->next) { + if (help->next == chan) { + help->next = help->next->next; + ast_mutex_unlock(&cl_te_lock); + return; } } @@ -3531,56 +3413,55 @@ static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan) static int pbx_start_chan(struct chan_list *ch) { - int ret=ast_pbx_start(ch->ast); + int ret = ast_pbx_start(ch->ast); - if (ret>=0) - ch->need_hangup=0; + if (ret >= 0) + ch->need_hangup = 0; else - ch->need_hangup=1; + ch->need_hangup = 1; return ret; } static void hangup_chan(struct chan_list *ch) { - int port=ch?ch->bc?ch->bc->port:0:0; + int port = ch ? (ch->bc ? ch->bc->port : 0) : 0; if (!ch) { - cb_log(1,0,"Cannot hangup chan, no ch\n"); + cb_log(1, 0, "Cannot hangup chan, no ch\n"); return; } - cb_log(5,port,"hangup_chan called\n"); + cb_log(5, port, "hangup_chan called\n"); - if (ch->need_hangup) - { - cb_log(2,port," --> hangup\n"); - send_cause2ast(ch->ast,ch->bc,ch); - ch->need_hangup=0; - ch->need_queue_hangup=0; + if (ch->need_hangup) { + cb_log(2, port, " --> hangup\n"); + send_cause2ast(ch->ast, ch->bc, ch); + ch->need_hangup = 0; + ch->need_queue_hangup = 0; if (ch->ast) ast_hangup(ch->ast); return; } if (!ch->need_queue_hangup) { - cb_log(2,port," --> No need to queue hangup\n"); + cb_log(2, port, " --> No need to queue hangup\n"); } - ch->need_queue_hangup=0; + ch->need_queue_hangup = 0; if (ch->ast) { - send_cause2ast(ch->ast,ch->bc,ch); + send_cause2ast(ch->ast, ch->bc, ch); if (ch->ast) ast_queue_hangup(ch->ast); - cb_log(2,port," --> queue_hangup\n"); + cb_log(2, port, " --> queue_hangup\n"); } else { - cb_log(1,port,"Cannot hangup chan, no ast\n"); + cb_log(1, port, "Cannot hangup chan, no ast\n"); } } /** Isdn asks us to release channel, pendant to misdn_hangup **/ static void release_chan(struct misdn_bchannel *bc) { - struct ast_channel *ast=NULL; + struct ast_channel *ast = NULL; ast_mutex_lock(&release_lock); { @@ -3590,20 +3471,20 @@ static void release_chan(struct misdn_bchannel *bc) { ast_mutex_unlock(&release_lock); return; } - + if (ch->ast) { - ast=ch->ast; + ast = ch->ast; } - - chan_misdn_log(5, bc->port, "release_chan: bc with l3id: %x\n",bc->l3_id); - + + chan_misdn_log(5, bc->port, "release_chan: bc with l3id: %x\n", bc->l3_id); + /*releasing jitterbuffer*/ if (ch->jb ) { misdn_jb_destroy(ch->jb); - ch->jb=NULL; + ch->jb = NULL; } else { if (!bc->nojitter) - chan_misdn_log(5,bc->port,"Jitterbuffer already destroyed.\n"); + chan_misdn_log(5, bc->port, "Jitterbuffer already destroyed.\n"); } if (ch->overlap_dial) { @@ -3619,62 +3500,58 @@ static void release_chan(struct misdn_bchannel *bc) { } else { misdn_in_calls[bc->port]--; } - + if (ch) { - close(ch->pipe[0]); close(ch->pipe[1]); - if (ast && MISDN_ASTERISK_TECH_PVT(ast)) { - chan_misdn_log(1, bc->port, "* RELEASING CHANNEL pid:%d ctx:%s dad:%s oad:%s state: %s\n",bc?bc->pid:-1, ast->context, ast->exten,ast->cid.cid_num,misdn_get_ch_state(ch)); + chan_misdn_log(1, bc->port, "* RELEASING CHANNEL pid:%d ctx:%s dad:%s oad:%s state: %s\n", bc ? bc->pid : -1, ast->context, ast->exten, ast->cid.cid_num, misdn_get_ch_state(ch)); chan_misdn_log(3, bc->port, " --> * State Down\n"); - MISDN_ASTERISK_TECH_PVT(ast)=NULL; - - + MISDN_ASTERISK_TECH_PVT(ast) = NULL; + if (ast->_state != AST_STATE_RESERVED) { chan_misdn_log(3, bc->port, " --> Setting AST State to down\n"); ast_setstate(ast, AST_STATE_DOWN); } } - - ch->state=MISDN_CLEANING; + + ch->state = MISDN_CLEANING; cl_dequeue_chan(&cl_te, ch); - + free(ch); } else { /* chan is already cleaned, so exiting */ } } - ast_mutex_unlock(&release_lock); -} /*** release end **/ +} static void misdn_transfer_bc(struct chan_list *tmp_ch, struct chan_list *holded_chan) { - chan_misdn_log(4,0,"TRANSFERING %s to %s\n",holded_chan->ast->name, tmp_ch->ast->name); - - tmp_ch->state=MISDN_HOLD_DISCONNECT; - + chan_misdn_log(4, 0, "TRANSFERRING %s to %s\n", holded_chan->ast->name, tmp_ch->ast->name); + + tmp_ch->state = MISDN_HOLD_DISCONNECT; + ast_moh_stop(ast_bridged_channel(holded_chan->ast)); holded_chan->state=MISDN_CONNECTED; - //misdn_lib_transfer(holded_chan->bc); + /* misdn_lib_transfer(holded_chan->bc); */ ast_channel_masquerade(holded_chan->ast, ast_bridged_channel(tmp_ch->ast)); } -static void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct ast_channel *ast) +static void do_immediate_setup(struct misdn_bchannel *bc, struct chan_list *ch, struct ast_channel *ast) { char predial[256]=""; char *p = predial; struct ast_frame fr; - + strncpy(predial, ast->exten, sizeof(predial) -1 ); - - ch->state=MISDN_DIALING; + + ch->state = MISDN_DIALING; if (!ch->noautorespond_on_setup) { if (bc->nt) { @@ -3694,10 +3571,10 @@ static void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , chan_misdn_log(1, bc->port, "* Starting Ast ctx:%s dad:%s oad:%s with 's' extension\n", ast->context, ast->exten, ast->cid.cid_num); - strncpy(ast->exten,"s", 2); + strncpy(ast->exten, "s", 2); - if (pbx_start_chan(ch)<0) { - ast=NULL; + if (pbx_start_chan(ch) < 0) { + ast = NULL; hangup_chan(ch); hanguptone_indicate(ch); @@ -3710,14 +3587,14 @@ static void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , while (!ast_strlen_zero(p) ) { fr.frametype = AST_FRAME_DTMF; - fr.subclass = *p ; - fr.src=NULL; - fr.data = NULL ; + fr.subclass = *p; + fr.src = NULL; + fr.data = NULL; fr.datalen = 0; - fr.samples = 0 ; - fr.mallocd =0 ; - fr.offset= 0 ; - fr.delivery= ast_tv(0,0) ; + fr.samples = 0; + fr.mallocd = 0; + fr.offset = 0; + fr.delivery = ast_tv(0,0); if (ch->ast && MISDN_ASTERISK_PVT(ch->ast) && MISDN_ASTERISK_TECH_PVT(ch->ast)) { ast_queue_frame(ch->ast, &fr); @@ -3728,134 +3605,131 @@ static void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , -static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc, struct chan_list *ch) { +static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch) { if (!ast) { - chan_misdn_log(1,0,"send_cause2ast: No Ast\n"); + chan_misdn_log(1, 0, "send_cause2ast: No Ast\n"); return; } if (!bc) { - chan_misdn_log(1,0,"send_cause2ast: No BC\n"); + chan_misdn_log(1, 0, "send_cause2ast: No BC\n"); return; } if (!ch) { - chan_misdn_log(1,0,"send_cause2ast: No Ch\n"); + chan_misdn_log(1, 0, "send_cause2ast: No Ch\n"); return; } - - ast->hangupcause=bc->cause; - - switch ( bc->cause) { - - case 1: /** Congestion Cases **/ - case 2: - case 3: - case 4: - case 22: - case 27: + + ast->hangupcause = bc->cause; + + switch (bc->cause) { + + case AST_CAUSE_UNALLOCATED: + case AST_CAUSE_NO_ROUTE_TRANSIT_NET: + case AST_CAUSE_NO_ROUTE_DESTINATION: + case 4: /* Send special information tone */ + case AST_CAUSE_NUMBER_CHANGED: + case AST_CAUSE_DESTINATION_OUT_OF_ORDER: + /* Congestion Cases */ /* * Not Queueing the Congestion anymore, since we want to hear * the inband message * - chan_misdn_log(1, bc?bc->port:0, " --> * SEND: Queue Congestion pid:%d\n", bc?bc->pid:-1); - ch->state=MISDN_BUSY; + chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Congestion pid:%d\n", bc ? bc->pid : -1); + ch->state = MISDN_BUSY; ast_queue_control(ast, AST_CONTROL_CONGESTION); */ break; - - case 21: - case 17: /* user busy */ - - ch->state=MISDN_BUSY; - + + case AST_CAUSE_CALL_REJECTED: + case AST_CAUSE_USER_BUSY: + ch->state = MISDN_BUSY; + if (!ch->need_busy) { - chan_misdn_log(1,bc?bc->port:0, "Queued busy already\n"); + chan_misdn_log(1, bc ? bc->port : 0, "Queued busy already\n"); break; } - - chan_misdn_log(1, bc?bc->port:0, " --> * SEND: Queue Busy pid:%d\n", bc?bc->pid:-1); + + chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Busy pid:%d\n", bc ? bc->pid : -1); ast_queue_control(ast, AST_CONTROL_BUSY); - ch->need_busy=0; + ch->need_busy = 0; break; } } - - void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch) { const char *tmp; - tmp=pbx_builtin_getvar_helper(chan,"MISDN_PID"); + + tmp = pbx_builtin_getvar_helper(chan, "MISDN_PID"); if (tmp) { - ch->other_pid=atoi(tmp); - chan_misdn_log(3,bc->port," --> IMPORT_PID: importing pid:%s\n",tmp); - if (ch->other_pid >0) { - ch->other_ch=find_chan_by_pid(cl_te,ch->other_pid); - if (ch->other_ch) ch->other_ch->other_ch=ch; + ch->other_pid = atoi(tmp); + chan_misdn_log(3, bc->port, " --> IMPORT_PID: importing pid:%s\n", tmp); + if (ch->other_pid > 0) { + ch->other_ch = find_chan_by_pid(cl_te, ch->other_pid); + if (ch->other_ch) + ch->other_ch->other_ch = ch; } } - tmp=pbx_builtin_getvar_helper(chan,"MISDN_ADDRESS_COMPLETE"); + tmp = pbx_builtin_getvar_helper(chan, "MISDN_ADDRESS_COMPLETE"); if (tmp && (atoi(tmp) == 1)) { - bc->sending_complete=1; + bc->sending_complete = 1; } - - tmp=pbx_builtin_getvar_helper(chan,"MISDN_USERUSER"); + + tmp = pbx_builtin_getvar_helper(chan, "MISDN_USERUSER"); if (tmp) { ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", tmp); - strcpy(bc->uu, tmp); - bc->uulen=strlen(bc->uu); + ast_copy_string(bc->uu, tmp, sizeof(bc->uu)); + bc->uulen = strlen(bc->uu); } - tmp=pbx_builtin_getvar_helper(chan,"MISDN_KEYPAD"); + tmp = pbx_builtin_getvar_helper(chan, "MISDN_KEYPAD"); if (tmp) { - strncpy(bc->keypad,tmp,sizeof(bc->keypad)); - bc->keypad[sizeof(bc->keypad)-1]=0; + ast_copy_string(bc->keypad, tmp, sizeof(bc->keypad)); } - - } void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch) { char tmp[32]; - chan_misdn_log(3,bc->port," --> EXPORT_PID: pid:%d\n",bc->pid); - sprintf(tmp,"%d",bc->pid); - pbx_builtin_setvar_helper(chan,"_MISDN_PID",tmp); + chan_misdn_log(3, bc->port, " --> EXPORT_PID: pid:%d\n", bc->pid); + snprintf(tmp, sizeof(tmp), "%d", bc->pid); + pbx_builtin_setvar_helper(chan, "_MISDN_PID", tmp); if (bc->sending_complete) { - sprintf(tmp,"%d",bc->sending_complete); - pbx_builtin_setvar_helper(chan,"MISDN_ADDRESS_COMPLETE",tmp); + snprintf(tmp, sizeof(tmp), "%d", bc->sending_complete); + pbx_builtin_setvar_helper(chan, "MISDN_ADDRESS_COMPLETE", tmp); } if (bc->urate) { - sprintf(tmp,"%d",bc->urate); - pbx_builtin_setvar_helper(chan,"MISDN_URATE",tmp); + snprintf(tmp, sizeof(tmp), "%d", bc->urate); + pbx_builtin_setvar_helper(chan, "MISDN_URATE", tmp); } if (bc->uulen && (bc->uulen < sizeof(bc->uu))) { - bc->uu[bc->uulen]=0; - pbx_builtin_setvar_helper(chan,"MISDN_USERUSER",bc->uu); + bc->uu[bc->uulen] = 0; + pbx_builtin_setvar_helper(chan, "MISDN_USERUSER", bc->uu); } - if (bc->keypad[0]) - pbx_builtin_setvar_helper(chan,"MISDN_KEYPAD",bc->keypad); + if (!ast_strlen_zero(bc->keypad)) + pbx_builtin_setvar_helper(chan, "MISDN_KEYPAD", bc->keypad); } int add_in_calls(int port) { int max_in_calls; - misdn_cfg_get( port, MISDN_CFG_MAX_IN, &max_in_calls, sizeof(max_in_calls)); + misdn_cfg_get(port, MISDN_CFG_MAX_IN, &max_in_calls, sizeof(max_in_calls)); misdn_in_calls[port]++; - if (max_in_calls >=0 && max_in_calls= 0 && max_in_calls < misdn_in_calls[port]) { + ast_log(LOG_NOTICE, "Marking Incoming Call on port[%d]\n", port); + return misdn_in_calls[port] - max_in_calls; } return 0; @@ -3865,12 +3739,11 @@ int add_out_calls(int port) { int max_out_calls; - misdn_cfg_get( port, MISDN_CFG_MAX_OUT, &max_out_calls, sizeof(max_out_calls)); - + misdn_cfg_get(port, MISDN_CFG_MAX_OUT, &max_out_calls, sizeof(max_out_calls)); - if (max_out_calls >=0 && max_out_calls<=misdn_out_calls[port]) { - ast_log(LOG_NOTICE,"Rejecting Outgoing Call on port[%d]\n",port); - return (misdn_out_calls[port]+1)-max_out_calls; + if (max_out_calls >= 0 && max_out_calls <= misdn_out_calls[port]) { + ast_log(LOG_NOTICE, "Rejecting Outgoing Call on port[%d]\n", port); + return (misdn_out_calls[port] + 1) - max_out_calls; } misdn_out_calls[port]++; @@ -3879,12 +3752,12 @@ int add_out_calls(int port) } static void start_pbx(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan) { - if (pbx_start_chan(ch)<0) { + if (pbx_start_chan(ch) < 0) { hangup_chan(ch); chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n"); if (bc->nt) { hanguptone_indicate(ch); - misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE ); + misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE); } else misdn_lib_send_event(bc, EVENT_RELEASE); } @@ -3905,58 +3778,57 @@ static enum event_response_e cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) { int msn_valid; - struct chan_list *ch=find_chan_by_bc(cl_te, bc); + struct chan_list *ch = find_chan_by_bc(cl_te, bc); if (event != EVENT_BCHAN_DATA && event != EVENT_TONE_GENERATE) { /* Debug Only Non-Bchan */ - int debuglevel=1; - if ( event==EVENT_CLEANUP && !user_data) - debuglevel=5; + int debuglevel = 1; + if ( event == EVENT_CLEANUP && !user_data) + debuglevel = 5; - chan_misdn_log(debuglevel, bc->port, "I IND :%s oad:%s dad:%s pid:%d state:%s\n", manager_isdn_get_info(event), bc->oad, bc->dad, bc->pid, ch?misdn_get_ch_state(ch):"none"); - if (debuglevel==1) { + chan_misdn_log(debuglevel, bc->port, "I IND :%s oad:%s dad:%s pid:%d state:%s\n", manager_isdn_get_info(event), bc->oad, bc->dad, bc->pid, ch ? misdn_get_ch_state(ch) : "none"); + if (debuglevel == 1) { misdn_lib_log_ies(bc); - chan_misdn_log(4,bc->port," --> bc_state:%s\n",bc_state2str(bc->bc_state)); + chan_misdn_log(4, bc->port, " --> bc_state:%s\n", bc_state2str(bc->bc_state)); } } if (!ch) { switch(event) { - case EVENT_SETUP: - case EVENT_DISCONNECT: - case EVENT_PORT_ALARM: - case EVENT_RETRIEVE: - case EVENT_NEW_BC: - case EVENT_FACILITY: - break; - case EVENT_RELEASE_COMPLETE: - chan_misdn_log(1, bc->port, " --> no Ch, so we've already released.\n"); - break; - case EVENT_CLEANUP: - case EVENT_TONE_GENERATE: - case EVENT_BCHAN_DATA: - return -1; - - default: - chan_misdn_log(1,bc->port, "Chan not existing at the moment bc->l3id:%x bc:%p event:%s port:%d channel:%d\n",bc->l3_id, bc, manager_isdn_get_info( event), bc->port,bc->channel); - return -1; + case EVENT_SETUP: + case EVENT_DISCONNECT: + case EVENT_PORT_ALARM: + case EVENT_RETRIEVE: + case EVENT_NEW_BC: + case EVENT_FACILITY: + break; + case EVENT_RELEASE_COMPLETE: + chan_misdn_log(1, bc->port, " --> no Ch, so we've already released.\n"); + break; + case EVENT_CLEANUP: + case EVENT_TONE_GENERATE: + case EVENT_BCHAN_DATA: + return -1; + default: + chan_misdn_log(1, bc->port, "Chan not existing at the moment bc->l3id:%x bc:%p event:%s port:%d channel:%d\n", bc->l3_id, bc, manager_isdn_get_info(event), bc->port, bc->channel); + return -1; } } - if (ch ) { + if (ch) { switch (event) { case EVENT_TONE_GENERATE: - break; + break; case EVENT_DISCONNECT: case EVENT_RELEASE: case EVENT_RELEASE_COMPLETE: case EVENT_CLEANUP: case EVENT_TIMEOUT: if (!ch->ast) - chan_misdn_log(3,bc->port,"ast_hangup already called, so we have no ast ptr anymore in event(%s)\n",manager_isdn_get_info(event)); + chan_misdn_log(3, bc->port, "ast_hangup already called, so we have no ast ptr anymore in event(%s)\n", manager_isdn_get_info(event)); break; default: - if ( !ch->ast || !MISDN_ASTERISK_PVT(ch->ast) || !MISDN_ASTERISK_TECH_PVT(ch->ast)) { - if (event!=EVENT_BCHAN_DATA) + if (!ch->ast || !MISDN_ASTERISK_PVT(ch->ast) || !MISDN_ASTERISK_TECH_PVT(ch->ast)) { + if (event != EVENT_BCHAN_DATA) ast_log(LOG_NOTICE, "No Ast or No private Pointer in Event (%d:%s)\n", event, manager_isdn_get_info(event)); return -1; } @@ -3967,14 +3839,14 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) switch (event) { case EVENT_PORT_ALARM: { - int boa=0; - misdn_cfg_get( bc->port, MISDN_CFG_ALARM_BLOCK, &boa, sizeof(int)); + int boa = 0; + misdn_cfg_get(bc->port, MISDN_CFG_ALARM_BLOCK, &boa, sizeof(int)); if (boa) { - cb_log(1,bc->port," --> blocking\n"); + cb_log(1, bc->port, " --> blocking\n"); misdn_lib_port_block(bc->port); } } - break; + break; case EVENT_BCHAN_ACTIVATED: break; @@ -3989,32 +3861,33 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) case EVENT_NEW_BC: if (!ch) { - ch=find_holded(cl_te,bc); + ch = find_holded(cl_te,bc); } if (!ch) { - ast_log(LOG_WARNING,"NEW_BC without chan_list?\n"); + ast_log(LOG_WARNING, "NEW_BC without chan_list?\n"); break; } if (bc) - ch->bc=(struct misdn_bchannel*)user_data; + ch->bc = (struct misdn_bchannel *)user_data; break; case EVENT_DTMF_TONE: { /* sending INFOS as DTMF-Frames :) */ struct ast_frame fr; - memset(&fr, 0 , sizeof(fr)); + + memset(&fr, 0, sizeof(fr)); fr.frametype = AST_FRAME_DTMF; fr.subclass = bc->dtmf ; - fr.src=NULL; - fr.data = NULL ; + fr.src = NULL; + fr.data = NULL; fr.datalen = 0; - fr.samples = 0 ; - fr.mallocd =0 ; - fr.offset= 0 ; - fr.delivery= ast_tv(0,0) ; + fr.samples = 0; + fr.mallocd = 0; + fr.offset = 0; + fr.delivery = ast_tv(0,0); if (!ch->ignore_dtmf) { chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf); @@ -4023,50 +3896,45 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) chan_misdn_log(2, bc->port, " --> Ignoring DTMF:%c due to bridge flags\n", bc->dtmf); } } - break; + break; case EVENT_STATUS: break; case EVENT_INFORMATION: { - int l; - if ( ch->state != MISDN_CONNECTED ) stop_indicate(ch); - if (!ch->ast) break; + if (!ch->ast) + break; if (ch->state == MISDN_WAITING4DIGS ) { /* Ok, incomplete Setup, waiting till extension exists */ if (ast_strlen_zero(bc->info_dad) && ! ast_strlen_zero(bc->keypad)) { chan_misdn_log(1, bc->port, " --> using keypad as info\n"); - strcpy(bc->info_dad,bc->keypad); + ast_copy_string(bc->info_dad, bc->keypad, sizeof(bc->info_dad)); } - l = sizeof(bc->dad); - strncat(bc->dad,bc->info_dad, l - strlen(bc->dad) - 1); - - l = sizeof(ch->ast->exten); - strncpy(ch->ast->exten, bc->dad, l); - ch->ast->exten[l-1] = 0; + strncat(bc->dad, bc->info_dad, sizeof(bc->dad) - strlen(bc->dad) - 1); + ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten)); /* Check for Pickup Request first */ if (!strcmp(ch->ast->exten, ast_pickup_ext())) { if (ast_pickup_call(ch->ast)) { hangup_chan(ch); } else { - struct ast_channel *chan=ch->ast; + struct ast_channel *chan = ch->ast; ch->state = MISDN_CALLING_ACKNOWLEDGE; ast_setstate(chan, AST_STATE_DOWN); hangup_chan(ch); - ch->ast=NULL; + ch->ast = NULL; break; } } - if(!ast_canmatch_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) { + if (!ast_canmatch_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) { if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->oad)) { - ast_log(LOG_WARNING, "Extension can never match, So jumping to 'i' extension. port(%d)\n",bc->port); + ast_log(LOG_WARNING, "Extension can never match, So jumping to 'i' extension. port(%d)\n", bc->port); strcpy(ch->ast->exten, "i"); ch->state = MISDN_DIALING; @@ -4074,16 +3942,16 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) break; } - ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d)." - "maybe you want to add an 'i' extension to catch this case.\n", + ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d).\n" + "\tMaybe you want to add an 'i' extension to catch this case.\n", bc->port); if (bc->nt) hanguptone_indicate(ch); - ch->state=MISDN_EXTCANTMATCH; - bc->out_cause=1; + ch->state = MISDN_EXTCANTMATCH; + bc->out_cause = AST_CAUSE_UNALLOCATED; - misdn_lib_send_event(bc, EVENT_DISCONNECT ); + misdn_lib_send_event(bc, EVENT_DISCONNECT); break; } @@ -4105,28 +3973,25 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) } } else { /* sending INFOS as DTMF-Frames :) */ - int digits; struct ast_frame fr; + int digits; + memset(&fr, 0, sizeof(fr)); fr.frametype = AST_FRAME_DTMF; fr.subclass = bc->info_dad[0] ; - fr.src=NULL; - fr.data = NULL ; + fr.src = NULL; + fr.data = NULL; fr.datalen = 0; - fr.samples = 0 ; - fr.mallocd =0 ; - fr.offset= 0 ; - fr.delivery= ast_tv(0,0) ; + fr.samples = 0; + fr.mallocd = 0; + fr.offset = 0; + fr.delivery = ast_tv(0,0); - misdn_cfg_get( 0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(int)); + misdn_cfg_get(0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(int)); if (ch->state != MISDN_CONNECTED ) { if (digits) { - int l = sizeof(bc->dad); - strncat(bc->dad, bc->info_dad, l - strlen(bc->dad) - 1); - l = sizeof(ch->ast->exten); - strncpy(ch->ast->exten, bc->dad, l); - ch->ast->exten[l-1] = 0; - + strncat(bc->dad, bc->info_dad, sizeof(bc->dad) - strlen(bc->dad) - 1); + ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten)); ast_cdr_update(ch->ast); } @@ -4134,46 +3999,28 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) } } } - break; + break; case EVENT_SETUP: { - struct chan_list *ch=find_chan_by_bc(cl_te, bc); + struct chan_list *ch = find_chan_by_bc(cl_te, bc); + struct ast_channel *chan; + int exceed; + int pres, screen; + int ai; + int im; + if (ch) { switch (ch->state) { - case MISDN_NOTHING: - ch=NULL; + case MISDN_NOTHING: + ch = NULL; break; - default: + default: chan_misdn_log(1, bc->port, " --> Ignoring Call we have already one\n"); return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE; /* Ignore MSNs which are not in our List */ } } - } - msn_valid = misdn_cfg_is_msn_valid(bc->port, bc->dad); - if (!bc->nt && ! msn_valid) { - chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n"); - return RESPONSE_IGNORE_SETUP; /* Ignore MSNs which are not in our List */ - } - - - if (bc->cw) { - int cause; - chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n"); - misdn_cfg_get( bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause)); - bc->out_cause=cause?cause:16; - return RESPONSE_RELEASE_SETUP; - } - - print_bearer(bc); - - { - struct chan_list *ch; - struct ast_channel *chan; - int exceed; - int pres,screen; - int msn_valid = misdn_cfg_is_msn_valid(bc->port, bc->dad); - int ai, im; + msn_valid = misdn_cfg_is_msn_valid(bc->port, bc->dad); if (!bc->nt && ! msn_valid) { chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n"); return RESPONSE_IGNORE_SETUP; /* Ignore MSNs which are not in our List */ @@ -4182,23 +4029,26 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) if (bc->cw) { int cause; chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n"); - misdn_cfg_get( bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause)); - bc->out_cause=cause?cause:16; + misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause)); + bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING; return RESPONSE_RELEASE_SETUP; } print_bearer(bc); - ch=init_chan_list(ORG_MISDN); + ch = init_chan_list(ORG_MISDN); - if (!ch) { chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n"); return 0;} + if (!ch) { + chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n"); + return 0; + } ch->bc = bc; - ch->l3id=bc->l3_id; - ch->addr=bc->addr; + ch->l3id = bc->l3_id; + ch->addr = bc->addr; ch->originator = ORG_MISDN; - chan=misdn_new(ch, AST_STATE_RESERVED,bc->dad, bc->oad, AST_FORMAT_ALAW, bc->port, bc->channel); + chan = misdn_new(ch, AST_STATE_RESERVED, bc->dad, bc->oad, AST_FORMAT_ALAW, bc->port, bc->channel); if (!chan) { misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); @@ -4208,64 +4058,71 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) ch->ast = chan; - if ((exceed=add_in_calls(bc->port))) { + if ((exceed = add_in_calls(bc->port))) { char tmp[16]; - sprintf(tmp,"%d",exceed); - pbx_builtin_setvar_helper(chan,"MAX_OVERFLOW",tmp); + snprintf(tmp, sizeof(tmp), "%d", exceed); + pbx_builtin_setvar_helper(chan, "MAX_OVERFLOW", tmp); } read_config(ch, ORG_MISDN); export_ch(chan, bc, ch); - ch->ast->rings=1; + ch->ast->rings = 1; ast_setstate(ch->ast, AST_STATE_RINGING); switch (bc->pres) { - case 1: - pres=AST_PRES_RESTRICTED; chan_misdn_log(2,bc->port," --> PRES: Restricted (1)\n"); - break; - case 2: - pres=AST_PRES_UNAVAILABLE; chan_misdn_log(2,bc->port," --> PRES: Restricted (2)\n"); - break; - default: - pres=AST_PRES_ALLOWED; chan_misdn_log(2,bc->port," --> PRES: Restricted (%d)\n", bc->pres); + case 1: + pres = AST_PRES_RESTRICTED; + chan_misdn_log(2, bc->port, " --> PRES: Restricted (1)\n"); + break; + case 2: + pres = AST_PRES_UNAVAILABLE; + chan_misdn_log(2, bc->port, " --> PRES: Unavailable (2)\n"); + break; + default: + pres = AST_PRES_ALLOWED; + chan_misdn_log(2, bc->port, " --> PRES: Allowed (%d)\n", bc->pres); + break; } switch (bc->screen) { - case 0: - screen=AST_PRES_USER_NUMBER_UNSCREENED; chan_misdn_log(2,bc->port," --> SCREEN: Unscreened (0)\n"); - break; - case 1: - screen=AST_PRES_USER_NUMBER_PASSED_SCREEN; chan_misdn_log(2,bc->port," --> SCREEN: Passed screen (1)\n"); - break; - case 2: - screen=AST_PRES_USER_NUMBER_FAILED_SCREEN; chan_misdn_log(2,bc->port," --> SCREEN: failed screen (2)\n"); - break; - case 3: - screen=AST_PRES_NETWORK_NUMBER; chan_misdn_log(2,bc->port," --> SCREEN: Network Number (3)\n"); - break; - default: - screen=AST_PRES_USER_NUMBER_UNSCREENED; chan_misdn_log(2,bc->port," --> SCREEN: Unscreened (%d)\n",bc->screen); + default: + case 0: + screen = AST_PRES_USER_NUMBER_UNSCREENED; + chan_misdn_log(2, bc->port, " --> SCREEN: Unscreened (%d)\n", bc->screen); + break; + case 1: + screen = AST_PRES_USER_NUMBER_PASSED_SCREEN; + chan_misdn_log(2, bc->port, " --> SCREEN: Passed screen (1)\n"); + break; + case 2: + screen = AST_PRES_USER_NUMBER_FAILED_SCREEN; + chan_misdn_log(2, bc->port, " --> SCREEN: failed screen (2)\n"); + break; + case 3: + screen = AST_PRES_NETWORK_NUMBER; + chan_misdn_log(2, bc->port, " --> SCREEN: Network Number (3)\n"); + break; } - chan->cid.cid_pres=pres+screen; + chan->cid.cid_pres = pres | screen; pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability)); - chan->transfercapability=bc->capability; + chan->transfercapability = bc->capability; switch (bc->capability) { - case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: - pbx_builtin_setvar_helper(chan,"CALLTYPE","DIGITAL"); - break; - default: - pbx_builtin_setvar_helper(chan,"CALLTYPE","SPEECH"); + case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: + pbx_builtin_setvar_helper(chan, "CALLTYPE", "DIGITAL"); + break; + default: + pbx_builtin_setvar_helper(chan, "CALLTYPE", "SPEECH"); } /** queue new chan **/ - cl_queue_chan(&cl_te, ch) ; + cl_queue_chan(&cl_te, ch); - if (!strstr(ch->allowed_bearers,"all")) { + if (!strstr(ch->allowed_bearers, "all")) { int i; for (i = 0; i < ARRAY_LEN(allowed_bearers_array); ++i) { @@ -4284,7 +4141,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) /* We did not find the bearer capability */ chan_misdn_log(0, bc->port, "Bearer capability not allowed: %s(%d)\n", bearer2str(bc->capability), bc->capability); - bc->out_cause = 88; + bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION; ch->state = MISDN_EXTCANTMATCH; misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE); @@ -4306,7 +4163,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) ch->state = MISDN_CALLING_ACKNOWLEDGE; ast_setstate(chan, AST_STATE_DOWN); hangup_chan(ch); - ch->ast=NULL; + ch->ast = NULL; break; } } @@ -4315,38 +4172,38 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) added support for s extension hope it will help those poor cretains which haven't overlap dial. */ - misdn_cfg_get( bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai)); - if ( ai ) { - do_immediate_setup(bc, ch , chan); + misdn_cfg_get(bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai)); + if (ai) { + do_immediate_setup(bc, ch, chan); break; } /* check if we should jump into s when we have no dad */ - misdn_cfg_get( bc->port, MISDN_CFG_IMMEDIATE, &im, sizeof(im)); - if ( im && ast_strlen_zero(bc->dad) ) { - do_immediate_setup(bc, ch , chan); + misdn_cfg_get(bc->port, MISDN_CFG_IMMEDIATE, &im, sizeof(im)); + if (im && ast_strlen_zero(bc->dad)) { + do_immediate_setup(bc, ch, chan); break; } - chan_misdn_log(5,bc->port,"CONTEXT:%s\n",ch->context); + chan_misdn_log(5, bc->port, "CONTEXT:%s\n", ch->context); if(!ast_canmatch_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) { if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->oad)) { - ast_log(LOG_WARNING, "Extension can never match, So jumping to 'i' extension. port(%d)\n",bc->port); + ast_log(LOG_WARNING, "Extension can never match, So jumping to 'i' extension. port(%d)\n", bc->port); strcpy(ch->ast->exten, "i"); misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE); - ch->state=MISDN_DIALING; + ch->state = MISDN_DIALING; start_pbx(ch, bc, chan); break; } - ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d)." - "maybe you want to add an 'i' extension to catch this case.\n", + ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d).\n" + "\tMaybe you want to add an 'i' extension to catch this case.\n", bc->port); if (bc->nt) hanguptone_indicate(ch); - ch->state=MISDN_EXTCANTMATCH; - bc->out_cause=1; + ch->state = MISDN_EXTCANTMATCH; + bc->out_cause = AST_CAUSE_UNALLOCATED; if (bc->nt) misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE ); @@ -4415,7 +4272,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) else misdn_lib_send_event(bc, EVENT_PROCEEDING); - ch->state=MISDN_DIALING; + ch->state = MISDN_DIALING; start_pbx(ch, bc, chan); break; } @@ -4427,28 +4284,17 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) ch->state = MISDN_CALLING_ACKNOWLEDGE; if (bc->channel) - update_name(ch->ast,bc->port,bc->channel); - - if (!ast_strlen_zero(bc->infos_pending)) { - /* TX Pending Infos */ - - { - int l = sizeof(bc->dad); - strncat(bc->dad, bc->infos_pending, l - strlen(bc->dad) - 1); - } + update_name(ch->ast,bc->port,bc->channel); - if (!ch->ast) break; - { - int l = sizeof(ch->ast->exten); - strncpy(ch->ast->exten, bc->dad, l); - ch->ast->exten[l-1] = 0; - } - { - int l = sizeof(bc->info_dad); - strncpy(bc->info_dad, bc->infos_pending, l); - bc->info_dad[l-1] = 0; - } - strncpy(bc->infos_pending,"", 1); + if (!ast_strlen_zero(bc->infos_pending)) { + /* TX Pending Infos */ + strncat(bc->dad, bc->infos_pending, sizeof(bc->dad) - strlen(bc->dad) - 1); + + if (!ch->ast) + break; + ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten)); + ast_copy_string(bc->info_dad, bc->infos_pending, sizeof(bc->info_dad)); + ast_copy_string(bc->infos_pending, "", sizeof(bc->infos_pending)); misdn_lib_send_event(bc, EVENT_INFORMATION); } @@ -4457,23 +4303,24 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) case EVENT_PROCEEDING: { if (bc->channel) - update_name(ch->ast,bc->port,bc->channel); + update_name(ch->ast, bc->port, bc->channel); - if ( misdn_cap_is_speech(bc->capability) && + if (misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc) ) { start_bc_tones(ch); } ch->state = MISDN_PROCEEDING; - if (!ch->ast) break; + if (!ch->ast) + break; ast_queue_control(ch->ast, AST_CONTROL_PROCEEDING); } break; case EVENT_PROGRESS: if (bc->channel) - update_name(ch->ast,bc->port,bc->channel); + update_name(ch->ast, bc->port, bc->channel); if (!bc->nt ) { if ( misdn_cap_is_speech(bc->capability) && @@ -4482,9 +4329,10 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) start_bc_tones(ch); } - ch->state=MISDN_PROGRESS; + ch->state = MISDN_PROGRESS; - if (!ch->ast) break; + if (!ch->ast) + break; ast_queue_control(ch->ast, AST_CONTROL_PROGRESS); } break; @@ -4493,24 +4341,25 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) case EVENT_ALERTING: { if (bc->channel) - update_name(ch->ast,bc->port,bc->channel); + update_name(ch->ast, bc->port, bc->channel); ch->state = MISDN_ALERTING; - if (!ch->ast) break; + if (!ch->ast) + break; ast_queue_control(ch->ast, AST_CONTROL_RINGING); ast_setstate(ch->ast, AST_STATE_RINGING); - cb_log(7,bc->port," --> Set State Ringing\n"); + cb_log(7, bc->port, " --> Set State Ringing\n"); - if ( misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) { - cb_log(1,bc->port,"Starting Tones, we have inband Data\n"); + if (misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) { + cb_log(1, bc->port, "Starting Tones, we have inband Data\n"); start_bc_tones(ch); } else { - cb_log(3,bc->port," --> We have no inband Data, the other end must create ringing\n"); + cb_log(3, bc->port, " --> We have no inband Data, the other end must create ringing\n"); if (ch->far_alerting) { - cb_log(1,bc->port," --> The other end can not do ringing eh ?.. we must do all ourself.."); + cb_log(1, bc->port, " --> The other end can not do ringing eh ?.. we must do all ourself.."); start_bc_tones(ch); /*tone_indicate(ch, TONE_FAR_ALERTING);*/ } @@ -4520,21 +4369,23 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) case EVENT_CONNECT: { struct ast_channel *bridged; + /*we answer when we've got our very new L3 ID from the NT stack */ - misdn_lib_send_event(bc,EVENT_CONNECT_ACKNOWLEDGE); + misdn_lib_send_event(bc, EVENT_CONNECT_ACKNOWLEDGE); - if (!ch->ast) break; + if (!ch->ast) + break; - bridged=ast_bridged_channel(ch->ast); + bridged = ast_bridged_channel(ch->ast); stop_indicate(ch); - if (bridged && !strcasecmp(bridged->tech->type,"mISDN")) { - struct chan_list *bridged_ch=MISDN_ASTERISK_TECH_PVT(bridged); + if (bridged && !strcasecmp(bridged->tech->type, "mISDN")) { + struct chan_list *bridged_ch = MISDN_ASTERISK_TECH_PVT(bridged); - chan_misdn_log(1,bc->port," --> copying cpndialplan:%d and cad:%s to the A-Channel\n",bc->cpnnumplan,bc->cad); + chan_misdn_log(1, bc->port, " --> copying cpndialplan:%d and cad:%s to the A-Channel\n", bc->cpnnumplan, bc->cad); if (bridged_ch) { - bridged_ch->bc->cpnnumplan=bc->cpnnumplan; - ast_copy_string(bridged_ch->bc->cad,bc->cad,sizeof(bc->cad)); + bridged_ch->bc->cpnnumplan = bc->cpnnumplan; + ast_copy_string(bridged_ch->bc->cad, bc->cad, sizeof(bridged_ch->bc->cad)); } } } @@ -4549,68 +4400,67 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) break; case EVENT_CONNECT_ACKNOWLEDGE: { - ch->l3id=bc->l3_id; - ch->addr=bc->addr; - + ch->l3id = bc->l3_id; + ch->addr = bc->addr; + start_bc_tones(ch); - + ch->state = MISDN_CONNECTED; } break; case EVENT_DISCONNECT: - /*we might not have an ch->ast ptr here anymore*/ - if (ch) { - struct chan_list *holded_ch=find_holded(cl_te, bc); - - chan_misdn_log(3,bc->port," --> org:%d nt:%d, inbandavail:%d state:%d\n", ch->originator, bc->nt, misdn_inband_avail(bc), ch->state); - if ( ch->originator==ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) { - /* If there's inband information available (e.g. a - recorded message saying what was wrong with the - dialled number, or perhaps even giving an - alternative number, then play it instead of - immediately releasing the call */ - chan_misdn_log(1,bc->port, " --> Inband Info Avail, not sending RELEASE\n"); + /*we might not have an ch->ast ptr here anymore*/ + if (ch) { + struct chan_list *holded_ch = find_holded(cl_te, bc); + + chan_misdn_log(3, bc->port, " --> org:%d nt:%d, inbandavail:%d state:%d\n", ch->originator, bc->nt, misdn_inband_avail(bc), ch->state); + if (ch->originator == ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) { + /* If there's inband information available (e.g. a + recorded message saying what was wrong with the + dialled number, or perhaps even giving an + alternative number, then play it instead of + immediately releasing the call */ + chan_misdn_log(1, bc->port, " --> Inband Info Avail, not sending RELEASE\n"); - ch->state=MISDN_DISCONNECTED; - start_bc_tones(ch); + ch->state = MISDN_DISCONNECTED; + start_bc_tones(ch); + + if (ch->ast) { + ch->ast->hangupcause = bc->cause; + if (bc->cause == AST_CAUSE_USER_BUSY) + ast_queue_control(ch->ast, AST_CONTROL_BUSY); + } + ch->need_busy = 0; + break; + } - if (ch->ast) { - ch->ast->hangupcause=bc->cause; - if (bc->cause == 17) - ast_queue_control(ch->ast, AST_CONTROL_BUSY); + /*Check for holded channel, to implement transfer*/ + if (holded_ch && holded_ch != ch && ch->ast && ch->state == MISDN_CONNECTED) { + cb_log(1, bc->port, " --> found holded ch\n"); + misdn_transfer_bc(ch, holded_ch) ; } - ch->need_busy=0; - break; - } - - /*Check for holded channel, to implement transfer*/ - if ( holded_ch && - holded_ch != ch && - ch->ast && - ch->state == MISDN_CONNECTED ) { - cb_log(1,bc->port," --> found holded ch\n"); - misdn_transfer_bc(ch, holded_ch) ; - } - bc->need_disconnect=0; - - stop_bc_tones(ch); - hangup_chan(ch); - } else { - /* ch=find_holded_l3(cl_te, bc->l3_id,1); - if (ch) { + bc->need_disconnect = 0; + + stop_bc_tones(ch); hangup_chan(ch); +#if 0 + } else { + ch = find_holded_l3(cl_te, bc->l3_id,1); + if (ch) { + hangup_chan(ch); + } +#endif } - */ - } - bc->out_cause=-1; - if (bc->need_release) misdn_lib_send_event(bc,EVENT_RELEASE); - break; + bc->out_cause = -1; + if (bc->need_release) + misdn_lib_send_event(bc, EVENT_RELEASE); + break; case EVENT_RELEASE: { - bc->need_disconnect=0; - bc->need_release=0; + bc->need_disconnect = 0; + bc->need_release = 0; hangup_chan(ch); release_chan(bc); @@ -4618,15 +4468,15 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) break; case EVENT_RELEASE_COMPLETE: { - bc->need_disconnect=0; - bc->need_release=0; - bc->need_release_complete=0; + bc->need_disconnect = 0; + bc->need_release = 0; + bc->need_release_complete = 0; stop_bc_tones(ch); hangup_chan(ch); - if(ch) - ch->state=MISDN_CLEANING; + if (ch) + ch->state = MISDN_CLEANING; release_chan(bc); } @@ -4636,11 +4486,11 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) { stop_bc_tones(ch); - switch(ch->state) { - case MISDN_CALLING: - bc->cause=27; /* Destination out of order */ + switch (ch->state) { + case MISDN_CALLING: + bc->cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER; break; - default: + default: break; } @@ -4651,27 +4501,27 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) case EVENT_TONE_GENERATE: { - int tone_len=bc->tone_cnt; - struct ast_channel *ast=ch->ast; + int tone_len = bc->tone_cnt; + struct ast_channel *ast = ch->ast; void *tmp; int res; int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples); - chan_misdn_log(9,bc->port,"TONE_GEN: len:%d\n"); + chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n", tone_len); - if (!ast) break; + if (!ast) + break; + + if (!ast->generator) + break; - if (!ast->generator) break; - - - tmp = ast->generatordata; ast->generatordata = NULL; generate = ast->generator->generate; - if (tone_len <0 || tone_len > 512 ) { - ast_log(LOG_NOTICE, "TONE_GEN: len was %d, set to 128\n",tone_len); - tone_len=128; + if (tone_len < 0 || tone_len > 512 ) { + ast_log(LOG_NOTICE, "TONE_GEN: len was %d, set to 128\n", tone_len); + tone_len = 128; } res = generate(ast, tmp, tone_len, tone_len); @@ -4681,58 +4531,52 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) ast_log(LOG_WARNING, "Auto-deactivating generator\n"); ast_deactivate_generator(ast); } else { - bc->tone_cnt=0; + bc->tone_cnt = 0; } } break; - + case EVENT_BCHAN_DATA: { - if ( !misdn_cap_is_speech(ch->bc->capability) ) { + if (!misdn_cap_is_speech(ch->bc->capability)) { struct ast_frame frame; /*In Data Modes we queue frames*/ frame.frametype = AST_FRAME_VOICE; /*we have no data frames yet*/ frame.subclass = AST_FORMAT_ALAW; frame.datalen = bc->bframe_len; - frame.samples = bc->bframe_len ; - frame.mallocd =0 ; - frame.offset= 0 ; - frame.delivery= ast_tv(0,0) ; + frame.samples = bc->bframe_len; + frame.mallocd = 0; + frame.offset = 0; + frame.delivery = ast_tv(0,0); frame.src = NULL; - frame.data = bc->bframe ; - + frame.data = bc->bframe; + if (ch->ast) - ast_queue_frame(ch->ast,&frame); + ast_queue_frame(ch->ast, &frame); } else { - int t; fd_set wrfs; - struct timeval tv; - tv.tv_sec=0; - tv.tv_usec=0; - - + struct timeval tv = { 0, 0 }; + int t; + FD_ZERO(&wrfs); - FD_SET(ch->pipe[1],&wrfs); - - t=select(FD_SETSIZE,NULL,&wrfs,NULL,&tv); + FD_SET(ch->pipe[1], &wrfs); + + t = select(FD_SETSIZE, NULL, &wrfs, NULL, &tv); if (!t) { chan_misdn_log(9, bc->port, "Select Timed out\n"); break; } - if (t<0) { - chan_misdn_log(-1, bc->port, "Select Error (err=%s)\n",strerror(errno)); + if (t < 0) { + chan_misdn_log(-1, bc->port, "Select Error (err=%s)\n", strerror(errno)); break; } - if (FD_ISSET(ch->pipe[1],&wrfs)) { - int ret; - chan_misdn_log(9, bc->port, "writing %d bytes 2 asterisk\n",bc->bframe_len); - ret=write(ch->pipe[1], bc->bframe, bc->bframe_len); - - if (ret<=0) { - chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n",strerror(errno)); + if (FD_ISSET(ch->pipe[1], &wrfs)) { + chan_misdn_log(9, bc->port, "writing %d bytes to asterisk\n", bc->bframe_len); + if (write(ch->pipe[1], bc->bframe, bc->bframe_len) <= 0) { + chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n", strerror(errno)); stop_bc_tones(ch); hangup_chan(ch); @@ -4745,51 +4589,51 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) } break; case EVENT_TIMEOUT: - { + { if (ch && bc) - chan_misdn_log(1,bc->port,"--> state: %s\n",misdn_get_ch_state(ch)); + chan_misdn_log(1, bc->port, "--> state: %s\n", misdn_get_ch_state(ch)); switch (ch->state) { - case MISDN_DIALING: - case MISDN_PROGRESS: - if (bc->nt && !ch->nttimeout) break; + case MISDN_DIALING: + case MISDN_PROGRESS: + if (bc->nt && !ch->nttimeout) + break; - case MISDN_CALLING: - case MISDN_ALERTING: - case MISDN_PROCEEDING: - case MISDN_CALLING_ACKNOWLEDGE: - if (bc->nt) { - bc->progress_indicator=8; - hanguptone_indicate(ch); - } + case MISDN_CALLING: + case MISDN_ALERTING: + case MISDN_PROCEEDING: + case MISDN_CALLING_ACKNOWLEDGE: + if (bc->nt) { + bc->progress_indicator = INFO_PI_INBAND_AVAILABLE; + hanguptone_indicate(ch); + } - bc->out_cause=1; - misdn_lib_send_event(bc,EVENT_DISCONNECT); + bc->out_cause = AST_CAUSE_UNALLOCATED; + misdn_lib_send_event(bc, EVENT_DISCONNECT); break; - case MISDN_WAITING4DIGS: - if (bc->nt) { - bc->progress_indicator=8; - bc->out_cause=1; - hanguptone_indicate(ch); - misdn_lib_send_event(bc,EVENT_DISCONNECT); - } else { - bc->out_cause=16; - misdn_lib_send_event(bc,EVENT_RELEASE); - } + case MISDN_WAITING4DIGS: + if (bc->nt) { + bc->progress_indicator = INFO_PI_INBAND_AVAILABLE; + bc->out_cause = AST_CAUSE_UNALLOCATED; + hanguptone_indicate(ch); + misdn_lib_send_event(bc, EVENT_DISCONNECT); + } else { + bc->out_cause = AST_CAUSE_NORMAL_CLEARING; + misdn_lib_send_event(bc, EVENT_RELEASE); + } break; - - case MISDN_CLEANING: - chan_misdn_log(1,bc->port," --> in state cleaning .. so ignoring, the stack should clean it for us\n"); + case MISDN_CLEANING: + chan_misdn_log(1,bc->port," --> in state cleaning .. so ignoring, the stack should clean it for us\n"); break; - default: - misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); - } + default: + misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); } - break; + } + break; /****************************/ @@ -4798,9 +4642,10 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) case EVENT_RETRIEVE: { struct ast_channel *hold_ast; + if (!ch) { chan_misdn_log(4, bc->port, " --> no CH, searching in holded\n"); - ch=find_holded_l3(cl_te, bc->l3_id,1); + ch = find_holded_l3(cl_te, bc->l3_id, 1); } if (!ch) { @@ -4810,19 +4655,19 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) } /*remember the channel again*/ - ch->bc=bc; + ch->bc = bc; ch->state = MISDN_CONNECTED; - ch->hold_info.port=0; - ch->hold_info.channel=0; - - hold_ast=ast_bridged_channel(ch->ast); - + ch->hold_info.port = 0; + ch->hold_info.channel = 0; + + hold_ast = ast_bridged_channel(ch->ast); + if (hold_ast) { ast_moh_stop(hold_ast); } - if ( misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) { + if (misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) { chan_misdn_log(4, bc->port, " --> RETRIEVE_ACK failed\n"); misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT); } @@ -4833,19 +4678,19 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) { int hold_allowed; struct ast_channel *bridged; - misdn_cfg_get( bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(int)); - + + misdn_cfg_get(bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(int)); + if (!hold_allowed) { chan_misdn_log(-1, bc->port, "Hold not allowed this port.\n"); misdn_lib_send_event(bc, EVENT_HOLD_REJECT); break; } - - bridged=ast_bridged_channel(ch->ast); + bridged = ast_bridged_channel(ch->ast); if (bridged) { - chan_misdn_log(2,bc->port,"Bridge Partner is of type: %s\n",bridged->tech->type); + chan_misdn_log(2, bc->port, "Bridge Partner is of type: %s\n", bridged->tech->type); ch->state = MISDN_HOLDED; ch->l3id = bc->l3_id; @@ -4856,9 +4701,9 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) ast_moh_start(bridged, NULL, NULL); /*forget the channel now*/ - ch->bc=NULL; - ch->hold_info.port=bc->port; - ch->hold_info.channel=bc->channel; + ch->bc = NULL; + ch->hold_info.port = bc->port; + ch->hold_info.channel = bc->channel; } else { misdn_lib_send_event(bc, EVENT_HOLD_REJECT); @@ -4879,36 +4724,39 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) switch (bc->fac_in.Function) { case Fac_CD: { - struct ast_channel *bridged=ast_bridged_channel(ch->ast); + struct ast_channel *bridged = ast_bridged_channel(ch->ast); struct chan_list *ch_br; if (bridged && MISDN_ASTERISK_TECH_PVT(bridged)) { - ch_br=MISDN_ASTERISK_TECH_PVT(bridged); - /*ch->state=MISDN_FACILITY_DEFLECTED;*/ + ch_br = MISDN_ASTERISK_TECH_PVT(bridged); + /*ch->state = MISDN_FACILITY_DEFLECTED;*/ if (ch_br->bc) { if (ast_exists_extension(bridged, ch->context, (char *)bc->fac_in.u.CDeflection.DeflectedToNumber, 1, bc->oad)) { - ch_br->state=MISDN_DIALING; + ch_br->state = MISDN_DIALING; if (pbx_start_chan(ch_br) < 0) { chan_misdn_log(-1, ch_br->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n"); } } } - } misdn_lib_send_event(bc, EVENT_DISCONNECT); } break; case Fac_AOCDCurrency: - bc->AOCDtype = Fac_AOCDCurrency; - memcpy(&(bc->AOCD.currency), &(bc->fac_in.u.AOCDcur), sizeof(struct FacAOCDCurrency)); - export_aoc_vars(ch->originator, ch->ast, bc); + { + bc->AOCDtype = Fac_AOCDCurrency; + memcpy(&(bc->AOCD.currency), &(bc->fac_in.u.AOCDcur), sizeof(struct FacAOCDCurrency)); + export_aoc_vars(ch->originator, ch->ast, bc); + } break; case Fac_AOCDChargingUnit: - bc->AOCDtype = Fac_AOCDChargingUnit; - memcpy(&(bc->AOCD.chargingUnit), &(bc->fac_in.u.AOCDchu), sizeof(struct FacAOCDChargingUnit)); - export_aoc_vars(ch->originator, ch->ast, bc); + { + bc->AOCDtype = Fac_AOCDChargingUnit; + memcpy(&(bc->AOCD.chargingUnit), &(bc->fac_in.u.AOCDchu), sizeof(struct FacAOCDChargingUnit)); + export_aoc_vars(ch->originator, ch->ast, bc); + } break; default: - chan_misdn_log(0, bc->port," --> not yet handled: facility type:%p\n", bc->fac_in.Function); + chan_misdn_log(0, bc->port," --> not yet handled: facility type:%d\n", bc->fac_in.Function); } break; @@ -4919,11 +4767,10 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) stop_bc_tones(ch); release_chan(bc); } - break; - + default: - chan_misdn_log(1,0, "Got Unknown Event\n"); + chan_misdn_log(1, 0, "Got Unknown Event\n"); break; } @@ -4948,7 +4795,8 @@ static int unload_module(void) misdn_tasks_destroy(); - if (!g_config_initialized) return 0; + if (!g_config_initialized) + return 0; ast_cli_unregister_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry)); @@ -4959,7 +4807,6 @@ static int unload_module(void) ast_channel_unregister(&misdn_tech); - free_robin_list(); misdn_cfg_destroy(); misdn_lib_destroy(); @@ -4976,12 +4823,19 @@ static int unload_module(void) static int load_module(void) { int i, port; - - char ports[256]=""; + int ntflags = 0, ntkc = 0; + char ports[256] = ""; + char tempbuf[BUFFERSIZE + 1]; + char ntfile[BUFFERSIZE + 1]; + struct misdn_lib_iface iface = { + .cb_event = cb_events, + .cb_log = chan_misdn_log, + .cb_jb_empty = chan_misdn_jb_empty, + }; - max_ports=misdn_lib_maxports_get(); + max_ports = misdn_lib_maxports_get(); - if (max_ports<=0) { + if (max_ports <= 0) { ast_log(LOG_ERROR, "Unable to initialize mISDN\n"); return AST_MODULE_LOAD_DECLINE; } @@ -4990,88 +4844,89 @@ static int load_module(void) ast_log(LOG_ERROR, "Unable to initialize misdn_config.\n"); return AST_MODULE_LOAD_DECLINE; } - g_config_initialized=1; + g_config_initialized = 1; - misdn_debug = (int *)malloc(sizeof(int) * (max_ports+1)); - misdn_ports = (int *)malloc(sizeof(int) * (max_ports+1)); - misdn_cfg_get( 0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(int)); + misdn_debug = (int *) malloc(sizeof(int) * (max_ports + 1)); + if (!misdn_debug) { + ast_log(LOG_ERROR, "Out of memory for misdn_debug\n"); + return AST_MODULE_LOAD_DECLINE; + } + misdn_ports = (int *) malloc(sizeof(int) * (max_ports + 1)); + if (!misdn_ports) { + ast_log(LOG_ERROR, "Out of memory for misdn_ports\n"); + return AST_MODULE_LOAD_DECLINE; + } + misdn_cfg_get(0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(int)); for (i = 1; i <= max_ports; i++) { misdn_debug[i] = misdn_debug[0]; misdn_ports[i] = i; } *misdn_ports = 0; - misdn_debug_only = (int *)calloc(max_ports + 1, sizeof(int)); + misdn_debug_only = (int *) calloc(max_ports + 1, sizeof(int)); - { - char tempbuf[BUFFERSIZE+1]; - misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, tempbuf, BUFFERSIZE); - if (strlen(tempbuf)) - tracing = 1; - } - - misdn_in_calls = (int *)malloc(sizeof(int) * (max_ports+1)); - misdn_out_calls = (int *)malloc(sizeof(int) * (max_ports+1)); + misdn_cfg_get(0, MISDN_GEN_TRACEFILE, tempbuf, BUFFERSIZE); + if (!ast_strlen_zero(tempbuf)) + tracing = 1; + + misdn_in_calls = (int *) malloc(sizeof(int) * (max_ports + 1)); + misdn_out_calls = (int *) malloc(sizeof(int) * (max_ports + 1)); - for (i=1; i <= max_ports; i++) { - misdn_in_calls[i]=0; - misdn_out_calls[i]=0; + for (i = 1; i <= max_ports; i++) { + misdn_in_calls[i] = 0; + misdn_out_calls[i] = 0; } - + ast_mutex_init(&cl_te_lock); ast_mutex_init(&release_lock); misdn_cfg_update_ptp(); misdn_cfg_get_ports_string(ports); - if (strlen(ports)) - chan_misdn_log(0, 0, "Got: %s from get_ports\n",ports); - - { - int ntflags=0, ntkc=0; - char ntfile[BUFFERSIZE+1]; - struct misdn_lib_iface iface = { - .cb_event = cb_events, - .cb_log = chan_misdn_log, - .cb_jb_empty = chan_misdn_jb_empty, - }; - - if (misdn_lib_init(ports, &iface, NULL)) - chan_misdn_log(0, 0, "No te ports initialized\n"); - - misdn_cfg_get( 0, MISDN_GEN_NTDEBUGFLAGS, &ntflags, sizeof(int)); - misdn_cfg_get( 0, MISDN_GEN_NTDEBUGFILE, &ntfile, BUFFERSIZE); - misdn_lib_nt_debug_init(ntflags,ntfile); + if (!ast_strlen_zero(ports)) + chan_misdn_log(0, 0, "Got: %s from get_ports\n", ports); + if (misdn_lib_init(ports, &iface, NULL)) + chan_misdn_log(0, 0, "No te ports initialized\n"); - misdn_cfg_get( 0, MISDN_GEN_NTKEEPCALLS, &ntkc, sizeof(int)); - misdn_lib_nt_keepcalls(ntkc); - } + misdn_cfg_get(0, MISDN_GEN_NTDEBUGFLAGS, &ntflags, sizeof(int)); + misdn_cfg_get(0, MISDN_GEN_NTDEBUGFILE, &ntfile, BUFFERSIZE); + misdn_lib_nt_debug_init(ntflags, ntfile); - { - if (ast_channel_register(&misdn_tech)) { - ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type); - unload_module(); - return -1; - } + misdn_cfg_get( 0, MISDN_GEN_NTKEEPCALLS, &ntkc, sizeof(int)); + misdn_lib_nt_keepcalls(ntkc); + + if (ast_channel_register(&misdn_tech)) { + ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type); + unload_module(); + return -1; } ast_cli_register_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry)); - + ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt", - "misdn_set_opt(::..):\n" - "Sets mISDN opts. and optargs\n" - "\n" - "The available options are:\n" - " d - Send display text on called phone, text is the optparam\n" - " n - disable DSP on channel, disables: Echocancel, DTMF Detection and Volume Control.\n" - " h - make digital outgoing call\n" - " h1 - make digital outgoing call, enable HDLC Controller\n" - " c - make crypted outgoing call, param is keyindex\n" - " e - perform echo cancelation on this channel,\n" - " takes taps as arguments (32,64,128,256)\n" - " s - send Non Inband DTMF as inband\n" - " vr - rxgain control\n" - " vt - txgain control\n" - " i - Ignore detected dtmf tones, don't signal them to asterisk, they will be transported inband.\n" + "misdn_set_opt(::...):\n" + "Sets mISDN opts. and optargs\n" + "\n" + "The available options are:\n" + " a - Have Asterisk detect DTMF tones on called channel\n" + " c - Make crypted outgoing call, optarg is keyindex\n" + " d - Send display text to called phone, text is the optarg\n" + " e - Perform echo cancelation on this channel,\n" + " takes taps as optarg (32,64,128,256)\n" + " e! - Disable echo cancelation on this channel\n" + " f - Enable fax detection\n" + " h - Make digital outgoing call\n" + " h1 - Make HDLC mode digital outgoing call\n" + " i - Ignore detected DTMF tones, don't signal them to Asterisk,\n" + " they will be transported inband.\n" + " jb - Set jitter buffer length, optarg is length\n" + " jt - Set jitter buffer upper threshold, optarg is threshold\n" + " jn - Disable jitter buffer\n" + " n - disable DSP on channel, disables: Echocancel, DTMF Detection and Volume Control.\n" + " p - Caller ID presentation,\n" + " optarg is either 'allowed' or 'restricted'\n" + " s - Send Non-inband DTMF as inband\n" + " vr - Rx gain control, optarg is gain\n" + " vt - Tx gain control, optarg is gain\n" ); @@ -5103,7 +4958,7 @@ static int load_module(void) ); - misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE); + misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE); /* start the l1 watchers */ @@ -5115,7 +4970,7 @@ static int load_module(void) misdn_tasks_add(l1timeout * 1000, misdn_l1_task, &misdn_ports[port]); } } - + chan_misdn_log(0, 0, "-- mISDN Channel Driver Registered --\n"); return 0; @@ -5137,9 +4992,9 @@ static int misdn_facility_exec(struct ast_channel *chan, void *data) struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan); char *tok, *tokb; - chan_misdn_log(0,0,"TYPE: %s\n",chan->tech->type); + chan_misdn_log(0, 0, "TYPE: %s\n", chan->tech->type); - if (strcasecmp(chan->tech->type,"mISDN")) { + if (strcasecmp(chan->tech->type, "mISDN")) { ast_log(LOG_WARNING, "misdn_facility makes only sense with chan_misdn channels!\n"); return -1; } @@ -5148,44 +5003,43 @@ static int misdn_facility_exec(struct ast_channel *chan, void *data) ast_log(LOG_WARNING, "misdn_facility Requires arguments\n"); return -1; } - - tok=strtok_r((char*)data,"|", &tokb) ; - + + tok = strtok_r((char*) data, "|", &tokb) ; + if (!tok) { ast_log(LOG_WARNING, "misdn_facility Requires arguments\n"); return -1; } - - if (!strcasecmp(tok,"calldeflect")) { - tok=strtok_r(NULL,"|", &tokb) ; + + if (!strcasecmp(tok, "calldeflect")) { + tok = strtok_r(NULL, "|", &tokb) ; if (!tok) { ast_log(LOG_WARNING, "Facility: Call Defl Requires arguments\n"); } - + if (strlen(tok) >= sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber)) { ast_log(LOG_WARNING, "Facility: Number argument too long (up to 15 digits are allowed). Ignoring.\n"); return 0; } ch->bc->fac_out.Function = Fac_CD; - strncpy((char *)ch->bc->fac_out.u.CDeflection.DeflectedToNumber, tok, sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber)); + ast_copy_string((char *)ch->bc->fac_out.u.CDeflection.DeflectedToNumber, tok, sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber)); misdn_lib_send_event(ch->bc, EVENT_FACILITY); } else { - chan_misdn_log(1, ch->bc->port, "Unknown Facility: %s\n",tok); + chan_misdn_log(1, ch->bc->port, "Unknown Facility: %s\n", tok); } - + return 0; - } static int misdn_check_l2l1(struct ast_channel *chan, void *data) { - char group[BUFFERSIZE+1]; + char group[BUFFERSIZE + 1]; char *port_str; - int port_up; + int port = 0; int timeout; - int dowait=0; - int port=0; + int dowait = 0; + int port_up; AST_DECLARE_APP_ARGS(args, AST_APP_ARG(grouppar); @@ -5205,22 +5059,21 @@ static int misdn_check_l2l1(struct ast_channel *chan, void *data) } /*ast_log(LOG_NOTICE, "Arguments: group/port '%s' timeout '%s'\n", args.grouppar, args.timeout);*/ - timeout=atoi(args.timeout); - port_str=args.grouppar; - if (port_str[0]=='g' && port_str[1]==':' ) { + timeout = atoi(args.timeout); + port_str = args.grouppar; + + if (port_str[0] == 'g' && port_str[1] == ':' ) { /* We make a group call lets checkout which ports are in my group */ port_str += 2; - strncpy(group, port_str, BUFFERSIZE); - group[BUFFERSIZE-1] = 0; - chan_misdn_log(2, 0, "Checking Ports in group: %s\n",group); + ast_copy_string(group, port_str, sizeof(group)); + chan_misdn_log(2, 0, "Checking Ports in group: %s\n", group); for ( port = misdn_cfg_get_next_port(port); port > 0; - port = misdn_cfg_get_next_port(port)) - { - char cfg_group[BUFFERSIZE+1]; - - chan_misdn_log(2,0,"trying port %d\n",port); + port = misdn_cfg_get_next_port(port)) { + char cfg_group[BUFFERSIZE + 1]; + + chan_misdn_log(2, 0, "trying port %d\n", port); misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE); @@ -5230,7 +5083,7 @@ static int misdn_check_l2l1(struct ast_channel *chan, void *data) if (!port_up) { chan_misdn_log(2, 0, " --> port '%d'\n", port); misdn_lib_get_port_up(port); - dowait=1; + dowait = 1; } } } @@ -5241,13 +5094,12 @@ static int misdn_check_l2l1(struct ast_channel *chan, void *data) port_up = misdn_lib_port_up(port, 1); if (!port_up) { misdn_lib_get_port_up(port); - dowait=1; + dowait = 1; } - } if (dowait) { - chan_misdn_log(2, 0, "Waiting for '%d' seconds\n",timeout); + chan_misdn_log(2, 0, "Waiting for '%d' seconds\n", timeout); sleep(timeout); } @@ -5257,13 +5109,13 @@ static int misdn_check_l2l1(struct ast_channel *chan, void *data) static int misdn_set_opt_exec(struct ast_channel *chan, void *data) { struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan); - char *tok,*tokb; - int keyidx=0; - int rxgain=0; - int txgain=0; - int change_jitter=0; - - if (strcasecmp(chan->tech->type,"mISDN")) { + char *tok, *tokb; + int keyidx = 0; + int rxgain = 0; + int txgain = 0; + int change_jitter = 0; + + if (strcasecmp(chan->tech->type, "mISDN")) { ast_log(LOG_WARNING, "misdn_set_opt makes only sense with chan_misdn channels!\n"); return -1; } @@ -5273,159 +5125,159 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data) return -1; } - for (tok=strtok_r((char*)data, ":",&tokb); + for (tok = strtok_r((char*) data, ":", &tokb); tok; - tok=strtok_r(NULL,":",&tokb) ) { - char keys[4096]; - char *key, *tmp; - int i; - int neglect=0; - + tok = strtok_r(NULL, ":", &tokb) ) { + int neglect = 0; + if (tok[0] == '!' ) { - neglect=1; + neglect = 1; tok++; } switch(tok[0]) { case 'd' : - ast_copy_string(ch->bc->display,++tok,84); - chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n",ch->bc->display); + ast_copy_string(ch->bc->display, ++tok, sizeof(ch->bc->display)); + chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n", ch->bc->display); break; case 'n': chan_misdn_log(1, ch->bc->port, "SETOPT: No DSP\n"); - ch->bc->nodsp=1; + ch->bc->nodsp = 1; break; case 'j': chan_misdn_log(1, ch->bc->port, "SETOPT: jitter\n"); tok++; - change_jitter=1; - + change_jitter = 1; + switch ( tok[0] ) { - case 'b' : - ch->jb_len=atoi(++tok); - chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d\n",ch->jb_len); + case 'b': + ch->jb_len = atoi(++tok); + chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d\n", ch->jb_len); break; case 't' : - ch->jb_upper_threshold=atoi(++tok); - chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d\n",ch->jb_upper_threshold); + ch->jb_upper_threshold = atoi(++tok); + chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d\n", ch->jb_upper_threshold); break; - case 'n': - ch->bc->nojitter=1; + ch->bc->nojitter = 1; chan_misdn_log(1, ch->bc->port, " --> nojitter\n"); break; - default: - ch->jb_len=4000; - ch->jb_upper_threshold=0; - chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d (default)\n",ch->jb_len); - chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d (default)\n",ch->jb_upper_threshold); + ch->jb_len = 4000; + ch->jb_upper_threshold = 0; + chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d (default)\n", ch->jb_len); + chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d (default)\n", ch->jb_upper_threshold); } - break; - case 'v': tok++; - switch ( tok[0] ) { + switch (tok[0]) { case 'r' : - rxgain=atoi(++tok); - if (rxgain<-8) rxgain=-8; - if (rxgain>8) rxgain=8; - ch->bc->rxgain=rxgain; - chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n",rxgain); + rxgain = atoi(++tok); + if (rxgain < -8) + rxgain = -8; + if (rxgain > 8) + rxgain = 8; + ch->bc->rxgain = rxgain; + chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", rxgain); break; case 't': - txgain=atoi(++tok); - if (txgain<-8) txgain=-8; - if (txgain>8) txgain=8; - ch->bc->txgain=txgain; - chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n",txgain); + txgain = atoi(++tok); + if (txgain < -8) + txgain = -8; + if (txgain > 8) + txgain = 8; + ch->bc->txgain = txgain; + chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", txgain); break; } break; case 'c': - keyidx=atoi(++tok); - key=NULL; - misdn_cfg_get( 0, MISDN_GEN_CRYPT_KEYS, keys, sizeof(keys)); + keyidx = atoi(++tok); + { + char keys[4096]; + char *key = NULL, *tmp = keys; + int i; + misdn_cfg_get(0, MISDN_GEN_CRYPT_KEYS, keys, sizeof(keys)); - tmp=keys; + for (i = 0; i < keyidx; i++) { + key = strsep(&tmp, ","); + } - for (i=0; ibc->crypt_key, key, sizeof(ch->bc->crypt_key)); + } - if (key) { - ast_copy_string(ch->bc->crypt_key, key, sizeof(ch->bc->crypt_key)); + chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n", ch->bc->crypt_key); + break; } - - chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n",ch->bc->crypt_key); - break; - case 'e': chan_misdn_log(1, ch->bc->port, "SETOPT: EchoCancel\n"); if (neglect) { chan_misdn_log(1, ch->bc->port, " --> disabled\n"); #ifdef MISDN_1_2 - *ch->bc->pipeline=0; + *ch->bc->pipeline = 0; #else - ch->bc->ec_enable=0; + ch->bc->ec_enable = 0; #endif } else { #ifdef MISDN_1_2 update_pipeline_config(ch->bc); #else - ch->bc->ec_enable=1; - ch->bc->orig=ch->originator; + ch->bc->ec_enable = 1; + ch->bc->orig = ch->originator; tok++; if (*tok) { - ch->bc->ec_deftaps=atoi(tok); + ch->bc->ec_deftaps = atoi(tok); } #endif } break; - case 'h': chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n"); - if (strlen(tok) > 1 && tok[1]=='1') { + if (strlen(tok) > 1 && tok[1] == '1') { chan_misdn_log(1, ch->bc->port, "SETOPT: HDLC \n"); if (!ch->bc->hdlc) { - ch->bc->hdlc=1; + ch->bc->hdlc = 1; } - } - ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; + } + ch->bc->capability = INFO_CAPABILITY_DIGITAL_UNRESTRICTED; break; case 's': chan_misdn_log(1, ch->bc->port, "SETOPT: Send DTMF\n"); - ch->bc->send_dtmf=1; + ch->bc->send_dtmf = 1; break; case 'f': chan_misdn_log(1, ch->bc->port, "SETOPT: Faxdetect\n"); - ch->faxdetect=1; + ch->faxdetect = 1; misdn_cfg_get(ch->bc->port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout)); break; case 'a': chan_misdn_log(1, ch->bc->port, "SETOPT: AST_DSP (for DTMF)\n"); - ch->ast_dsp=1; + ch->ast_dsp = 1; break; case 'p': - chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n",&tok[1]); + chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n", &tok[1]); /* CRICH: callingpres!!! */ - if (strstr(tok,"allowed") ) { - ch->bc->pres=0; - } else if (strstr(tok,"not_screened")) { - ch->bc->pres=1; + if (strstr(tok,"allowed")) { + ch->bc->pres = 0; + } else if (strstr(tok, "restricted")) { + ch->bc->pres = 1; + } else if (strstr(tok, "not_screened")) { + chan_misdn_log(0, ch->bc->port, "SETOPT: callerpres: not_screened is deprecated\n"); + ch->bc->pres = 1; } break; case 'i' : @@ -5439,18 +5291,20 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data) if (change_jitter) config_jitterbuffer(ch); - - + if (ch->faxdetect || ch->ast_dsp) { - if (!ch->dsp) ch->dsp = ast_dsp_new(); - if (ch->dsp) ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT); - if (!ch->trans) ch->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW); + if (!ch->dsp) + ch->dsp = ast_dsp_new(); + if (ch->dsp) + ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT); + if (!ch->trans) + ch->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW); } if (ch->ast_dsp) { - chan_misdn_log(1,ch->bc->port,"SETOPT: with AST_DSP we deactivate mISDN_dsp\n"); - ch->bc->nodsp=1; - ch->bc->nojitter=1; + chan_misdn_log(1, ch->bc->port, "SETOPT: with AST_DSP we deactivate mISDN_dsp\n"); + ch->bc->nodsp = 1; + ch->bc->nojitter = 1; } return 0; @@ -5459,7 +5313,7 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data) int chan_misdn_jb_empty ( struct misdn_bchannel *bc, char *buf, int len) { - struct chan_list *ch=find_chan_by_bc(cl_te, bc); + struct chan_list *ch = find_chan_by_bc(cl_te, bc); if (ch && ch->jb) { return misdn_jb_empty(ch->jb, buf, len); @@ -5478,35 +5332,42 @@ int chan_misdn_jb_empty ( struct misdn_bchannel *bc, char *buf, int len) /* allocates the jb-structure and initialize the elements*/ struct misdn_jb *misdn_jb_init(int size, int upper_threshold) { - int i; - struct misdn_jb *jb = (struct misdn_jb*) malloc(sizeof(struct misdn_jb)); - jb->size = size; - jb->upper_threshold = upper_threshold; - jb->wp = 0; - jb->rp = 0; - jb->state_full = 0; - jb->state_empty = 0; - jb->bytes_wrote = 0; - jb->samples = (char *)malloc(size*sizeof(char)); - - if (!jb->samples) { - chan_misdn_log(-1,0,"No free Mem for jb->samples\n"); - return NULL; - } - - jb->ok = (char *)malloc(size*sizeof(char)); + int i; + struct misdn_jb *jb; - if (!jb->ok) { - chan_misdn_log(-1,0,"No free Mem for jb->ok\n"); + jb = malloc(sizeof(struct misdn_jb)); + if (!jb) { + chan_misdn_log(-1, 0, "No free Mem for jb\n"); return NULL; - } + } + jb->size = size; + jb->upper_threshold = upper_threshold; + jb->wp = 0; + jb->rp = 0; + jb->state_full = 0; + jb->state_empty = 0; + jb->bytes_wrote = 0; + jb->samples = malloc(size * sizeof(char)); + if (!jb->samples) { + free(jb); + chan_misdn_log(-1, 0, "No free Mem for jb->samples\n"); + return NULL; + } + + jb->ok = malloc(size * sizeof(char)); + if (!jb->ok) { + free(jb->samples); + free(jb); + chan_misdn_log(-1, 0, "No free Mem for jb->ok\n"); + return NULL; + } - for(i=0; iok[i]=0; + for (i = 0; i < size; i++) + jb->ok[i] = 0; - ast_mutex_init(&jb->mutexjb); + ast_mutex_init(&jb->mutexjb); - return jb; + return jb; } /* frees the data and destroys the given jitterbuffer struct */ @@ -5522,61 +5383,58 @@ void misdn_jb_destroy(struct misdn_jb *jb) error (buffer overflow). */ int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len) { - int i, j, rp, wp; + int i, j, rp, wp; - if (!jb || ! data) return 0; + if (!jb || ! data) + return 0; - ast_mutex_lock (&jb->mutexjb); - - wp=jb->wp; - rp=jb->rp; + ast_mutex_lock(&jb->mutexjb); - for(i=0; isamples[wp]=data[i]; - jb->ok[wp]=1; - wp = (wp!=jb->size-1 ? wp+1 : 0); - - if(wp==jb->rp) - jb->state_full=1; - } - - if(wp>=rp) - jb->state_buffer=wp-rp; - else - jb->state_buffer= jb->size-rp+wp; - chan_misdn_log(9,0,"misdn_jb_fill: written:%d | Buffer status:%d p:%x\n",len,jb->state_buffer,jb); - - if(jb->state_full) - { - jb->wp=wp; - - rp=wp; - for(j=0; jupper_threshold; j++) - rp = (rp!=0 ? rp-1 : jb->size-1); - jb->rp=rp; - jb->state_full=0; - jb->state_empty=1; - - ast_mutex_unlock (&jb->mutexjb); + wp = jb->wp; + rp = jb->rp; - return -1; - } + for (i = 0; i < len; i++) { + jb->samples[wp] = data[i]; + jb->ok[wp] = 1; + wp = (wp != jb->size - 1) ? wp + 1 : 0; - if(!jb->state_empty) - { - jb->bytes_wrote+=len; - if(jb->bytes_wrote>=jb->upper_threshold) - { - jb->state_empty=1; - jb->bytes_wrote=0; + if (wp == jb->rp) + jb->state_full = 1; } - } - jb->wp=wp; - ast_mutex_unlock (&jb->mutexjb); - - return 0; + if (wp >= rp) + jb->state_buffer = wp - rp; + else + jb->state_buffer = jb->size - rp + wp; + chan_misdn_log(9, 0, "misdn_jb_fill: written:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb); + + if (jb->state_full) { + jb->wp = wp; + + rp = wp; + for (j = 0; j < jb->upper_threshold; j++) + rp = (rp != 0) ? rp - 1 : jb->size - 1; + jb->rp = rp; + jb->state_full = 0; + jb->state_empty = 1; + + ast_mutex_unlock(&jb->mutexjb); + + return -1; + } + + if (!jb->state_empty) { + jb->bytes_wrote += len; + if (jb->bytes_wrote >= jb->upper_threshold) { + jb->state_empty = 1; + jb->bytes_wrote = 0; + } + } + jb->wp = wp; + + ast_mutex_unlock(&jb->mutexjb); + + return 0; } /* gets len bytes out of the jitterbuffer if available, else only the @@ -5584,52 +5442,45 @@ available data is returned and the return value indicates the number of data. */ int misdn_jb_empty(struct misdn_jb *jb, char *data, int len) { - int i, wp, rp, read=0; + int i, wp, rp, read = 0; - ast_mutex_lock (&jb->mutexjb); + ast_mutex_lock(&jb->mutexjb); - rp=jb->rp; - wp=jb->wp; + rp = jb->rp; + wp = jb->wp; - if(jb->state_empty) - { - for(i=0; irp=rp; - jb->state_empty=0; + if (jb->state_empty) { + for (i = 0; i < len; i++) { + if (wp == rp) { + jb->rp = rp; + jb->state_empty = 0; - ast_mutex_unlock (&jb->mutexjb); - - return read; - } - else - { - if(jb->ok[rp]==1) - { - data[i]=jb->samples[rp]; - jb->ok[rp]=0; - rp=(rp!=jb->size-1 ? rp+1 : 0); - read+=1; + ast_mutex_unlock(&jb->mutexjb); + + return read; + } else { + if (jb->ok[rp] == 1) { + data[i] = jb->samples[rp]; + jb->ok[rp] = 0; + rp = (rp != jb->size - 1) ? rp + 1 : 0; + read += 1; + } + } } - } - } - if(wp >= rp) - jb->state_buffer=wp-rp; - else - jb->state_buffer= jb->size-rp+wp; - chan_misdn_log(9,0,"misdn_jb_empty: read:%d | Buffer status:%d p:%x\n",len,jb->state_buffer,jb); - - jb->rp=rp; - } - else - chan_misdn_log(9,0,"misdn_jb_empty: Wait...requested:%d p:%x\n",len,jb); - - ast_mutex_unlock (&jb->mutexjb); + if (wp >= rp) + jb->state_buffer = wp - rp; + else + jb->state_buffer = jb->size - rp + wp; + chan_misdn_log(9, 0, "misdn_jb_empty: read:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb); + + jb->rp = rp; + } else + chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%p\n", len, jb); + + ast_mutex_unlock(&jb->mutexjb); - return read; + return read; } @@ -5650,21 +5501,21 @@ static void chan_misdn_log(int level, int port, char *tmpl, ...) if (! ((0 <= port) && (port <= max_ports))) { ast_log(LOG_WARNING, "cb_log called with out-of-range port number! (%d)\n", port); - port=0; - level=-1; + port = 0; + level = -1; } - - sprintf(port_buf,"P[%2d] ",port); - + + snprintf(port_buf, sizeof(port_buf), "P[%2d] ", port); + va_start(ap, tmpl); - vsnprintf( buf, 1023, tmpl, ap ); + vsnprintf(buf, sizeof(buf), tmpl, ap); va_end(ap); if (level == -1) - ast_log(LOG_WARNING, buf); + ast_log(LOG_WARNING, "%s", buf); else if (misdn_debug_only[port] ? - (level==1 && misdn_debug[port]) || (level==misdn_debug[port]) + (level == 1 && misdn_debug[port]) || (level == misdn_debug[port]) : level <= misdn_debug[port]) { ast_console_puts(port_buf); @@ -5673,12 +5524,13 @@ static void chan_misdn_log(int level, int port, char *tmpl, ...) if ((level <= misdn_debug[0]) && !ast_strlen_zero(global_tracefile) ) { time_t tm = time(NULL); - char *tmp=ctime(&tm),*p; - - FILE *fp= fopen(global_tracefile, "a+"); - - p=strchr(tmp,'\n'); - if (p) *p=':'; + char *tmp = ctime(&tm), *p; + + FILE *fp = fopen(global_tracefile, "a+"); + + p = strchr(tmp, '\n'); + if (p) + *p = ':'; if (!fp) { ast_console_puts("Error opening Tracefile: [ "); @@ -5690,9 +5542,9 @@ static void chan_misdn_log(int level, int port, char *tmpl, ...) return ; } - fputs(tmp,fp); + fputs(tmp, fp); fputs(" ", fp); - fputs(port_buf,fp); + fputs(port_buf, fp); fputs(" ", fp); fputs(buf, fp); @@ -5701,7 +5553,7 @@ static void chan_misdn_log(int level, int port, char *tmpl, ...) } AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for mISDN Support (BRI/PRI)", - .load = load_module, - .unload = unload_module, - .reload = reload, - ); + .load = load_module, + .unload = unload_module, + .reload = reload, +); diff --git a/channels/misdn/isdn_lib.c b/channels/misdn/isdn_lib.c index dc666cc87..02df1b000 100644 --- a/channels/misdn/isdn_lib.c +++ b/channels/misdn/isdn_lib.c @@ -11,6 +11,12 @@ * the GNU General Public License */ +/*! \file + * \brief Interface to mISDN + * \author Christian Richter + */ + + #include #include @@ -19,6 +25,14 @@ #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); @@ -44,7 +58,7 @@ int misdn_lib_port_is_pri(int port) return -1; } -static void make_dummy(struct misdn_bchannel *dummybc, int port, int l3id, int nt, int channel) +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; @@ -115,17 +129,16 @@ int misdn_lib_get_maxchans(int port) } -struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc) +struct misdn_stack *get_stack_by_bc(struct misdn_bchannel *bc) { - struct misdn_stack *stack=get_misdn_stack(); + struct misdn_stack *stack = get_misdn_stack(); - if (!bc) return NULL; + if (!bc) + return NULL; - for ( ; stack; stack=stack->next) { - int i; - for (i=0; i <=stack->b_num; i++) { - if ( bc->port == stack->port) return stack; - } + for ( ; stack; stack = stack->next) { + if (bc->port == stack->port) + return stack; } return NULL; @@ -141,12 +154,13 @@ void get_show_stack_details(int port, char *buf) } 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); - + 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; } - } @@ -171,8 +185,9 @@ static enum global_states global_state=MISDN_INITIALIZING; struct misdn_lib { + /*! \brief mISDN device handle returned by mISDN_open() */ int midev; - int midev_nt; + int midev_nt; /* Not used */ pthread_t event_thread; pthread_t event_handler_thread; @@ -240,9 +255,6 @@ void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holde struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id); /* from isdn_lib.h */ -int init_bc(struct misdn_stack * stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime); -struct misdn_stack* stack_init(int midev, int port, int ptp); -void stack_destroy(struct misdn_stack* stack); /* user iface */ int te_lib_init( void ) ; /* returns midev */ void te_lib_destroy(int midev) ; @@ -382,10 +394,8 @@ msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode) static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg) { - struct misdn_stack *stack; - iframe_t *frm; - frm = (iframe_t *)dmsg->data; - stack=get_stack_by_bc(bc); + 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 "); @@ -419,8 +429,10 @@ int misdn_cap_is_speech(int cap) int misdn_inband_avail(struct misdn_bchannel *bc) { - /*if ! early_bconnect we have never inband available*/ - if ( ! bc->early_bconnect ) return 0; + 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: @@ -478,8 +490,8 @@ int set_chan_in_stack(struct misdn_stack *stack, int channel) static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel, int dec) { int i; - int bnums; int chan=0; + int bnums = stack->pri ? stack->b_num : stack->b_num - 1; if (bc->channel_found) return 0; @@ -488,7 +500,6 @@ static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchan 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; @@ -496,11 +507,9 @@ static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchan channel--; - bnums=stack->pri?stack->b_num:stack->b_num-1; - if (dec) { for (i = bnums; i >=0; i--) { - if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */ + 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; @@ -510,7 +519,7 @@ static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchan } } else { for (i = 0; i <= bnums; i++) { - if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */ + 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; @@ -523,13 +532,13 @@ static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchan if (!chan) { cb_log (1, stack->port, " !! NO FREE CHAN IN STACK\n"); dump_chan_list(stack); - bc->out_cause=34; + 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=44; + bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL; return -1; } @@ -664,9 +673,9 @@ static void empty_bc(struct misdn_bchannel *bc) bc->orig=0; - bc->cause=16; - bc->out_cause=16; - bc->pres=0 ; /* screened */ + bc->cause = AST_CAUSE_NORMAL_CLEARING; + bc->out_cause = AST_CAUSE_NORMAL_CLEARING; + bc->pres = 0; /* allowed */ bc->evq=EVENT_NOTHING; @@ -890,7 +899,7 @@ static int create_process (int midev, struct misdn_bchannel *bc) { if (stack->procids[i]==0) break; if (i== MAXPROCS) { - cb_log(0, stack->port, "Couldnt Create New ProcId.\n"); + cb_log(0, stack->port, "Couldn't Create New ProcId.\n"); return -1; } stack->procids[i]=1; @@ -952,13 +961,12 @@ void misdn_lib_setup_bc(struct misdn_bchannel *bc) int setup_bc(struct misdn_bchannel *bc) { unsigned char buff[1025]; - int midev, i; + int midev; int channel; int b_stid; - + int i; mISDN_pid_t pid; int ret; - struct misdn_stack *stack=get_stack_by_bc(bc); @@ -967,22 +975,21 @@ int setup_bc(struct misdn_bchannel *bc) return -1; } - midev=stack->midev; - channel=bc->channel-1-(bc->channel>16); - b_stid=stack->b_stids[channel>=0?channel:0]; + 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 upsetted stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) ); + 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 alread initialized*/ + /*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); @@ -996,10 +1003,9 @@ int setup_bc(struct misdn_bchannel *bc) bc_state_change(bc,BCHAN_ERROR); return 1; } - - + bc->b_stid = b_stid; - + { layer_info_t li; memset(&li, 0, sizeof(li)); @@ -1033,10 +1039,9 @@ int setup_bc(struct misdn_bchannel *bc) li.name[l-1] = 0; } li.pid.layermask = ISDN_LAYER((4)); - li.pid.protocol[4] = ISDN_PID_L4_B_USER -; + li.pid.protocol[4] = ISDN_PID_L4_B_USER; + bc->layer=4; - } ret = mISDN_new_layer(midev, &li); @@ -1125,7 +1130,7 @@ int setup_bc(struct misdn_bchannel *bc) /** IFACE **/ -int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime) +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; @@ -1138,7 +1143,9 @@ int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, in 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) { @@ -1163,6 +1170,9 @@ int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, in clear_ibuffer( ibuf); ibuf->rsem=malloc(sizeof(sem_t)); + if (!ibuf->rsem) { + return -1; + } bc->astbuf=ibuf; @@ -1189,7 +1199,7 @@ int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, in -struct misdn_stack* stack_init( int midev, int port, int ptp ) +static struct misdn_stack *stack_init(int midev, int port, int ptp) { int ret; unsigned char buff[1025]; @@ -1374,7 +1384,7 @@ struct misdn_stack* stack_init( int midev, int port, int ptp ) } -void stack_destroy(struct misdn_stack* stack) +static void stack_destroy(struct misdn_stack *stack) { char buf[1024]; if (!stack) return; @@ -1601,14 +1611,15 @@ static int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_ static int handle_cr ( struct misdn_stack *stack, iframe_t *frm) { - struct misdn_bchannel* bc; + 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); + 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; @@ -1632,8 +1643,8 @@ static int handle_cr ( struct misdn_stack *stack, iframe_t *frm) struct misdn_bchannel dummybc; if (!bc) { - cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo); - make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0); + 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; } @@ -1659,7 +1670,7 @@ static int handle_cr ( struct misdn_stack *stack, iframe_t *frm) dump_chan_list(stack); if (bc->stack_holder) { - cb_log(4,stack->port, "REMOVEING Holder\n"); + cb_log(4,stack->port, "REMOVING Holder\n"); stack_holder_remove( stack, bc); free(bc); } @@ -1678,7 +1689,7 @@ static int handle_cr ( struct misdn_stack *stack, iframe_t *frm) } -/*Emptys bc if it's reserved (no SETUP out yet)*/ +/* 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); @@ -1787,11 +1798,11 @@ int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh) frm.addr=stack->upper_id | FLG_MSG_DOWN; frm.prim = CC_RELEASE_CR|INDICATION; - cb_log(4, stack->port, " --> CC_RELEASE_CR: Faking Realease_cr for %x l3id:%x\n",frm.addr, frm.dinfo); + 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 found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo); - make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0); + 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; } @@ -1801,7 +1812,7 @@ int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh) stack->procids[bc->l3_id&0xff] = 0 ; } } - else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n"); + 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) { } @@ -1833,6 +1844,7 @@ handle_event_nt(void *dat, void *arg) { 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; @@ -1849,16 +1861,16 @@ handle_event_nt(void *dat, void *arg) return 0; } - bc=find_bc_by_l3id(stack, hh->dinfo); - hold_bc=stack_holder_find(stack,bc->l3_id); + 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, "REMOVEING Holder\n"); + 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)); + memcpy(bc, hold_bc, sizeof(struct misdn_bchannel)); free(hold_bc); bc->holded=0; @@ -2032,7 +2044,7 @@ handle_event_nt(void *dat, void *arg) 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 Attemps!!!\n"); + 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); @@ -2064,16 +2076,14 @@ handle_event_nt(void *dat, void *arg) bc=find_bc_by_l3id(stack, hh->dinfo); if (!bc) { - cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo); - make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0); + 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) { - int channel; - int tmpcause; case EVENT_SETUP: if (bc->channel<=0 || bc->channel==0xff) bc->channel=0; @@ -2084,8 +2094,8 @@ handle_event_nt(void *dat, void *arg) case EVENT_RELEASE: case EVENT_RELEASE_COMPLETE: { - channel=bc->channel; - tmpcause=bc->cause; + int channel=bc->channel; + int tmpcause=bc->cause; empty_bc(bc); bc->cause=tmpcause; clean_up_bc(bc); @@ -2105,7 +2115,7 @@ handle_event_nt(void *dat, void *arg) } else { if (reject) { switch(bc->cause){ - case 17: + case AST_CAUSE_USER_BUSY: cb_log(1, stack->port, "Siemens Busy reject..\n"); break; @@ -2214,12 +2224,13 @@ static int do_tone(struct misdn_bchannel *bc, int len) 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); - FILE *rx=fopen(n1,"a+"); - FILE *tx=fopen(n2,"a+"); + rx = fopen(n1,"a+"); + tx = fopen(n2,"a+"); if (!rx || !tx) { cb_log(0,0,"Couldn't open files: %s\n",strerror(errno)); @@ -2298,15 +2309,15 @@ void misdn_tx_jitter(struct misdn_bchannel *bc, int len) static int handle_bchan(msg_t *msg) { iframe_t *frm= (iframe_t*)msg->data; - struct misdn_stack *stack; 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); + 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); @@ -2513,7 +2524,7 @@ static int handle_bchan(msg_t *msg) cb_log(0,bc->port,"EVENT_B_DATA START\n"); #endif - i=cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data); + 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 @@ -2622,12 +2633,12 @@ static int handle_frm(msg_t *msg) bc=find_bc_by_l3id(stack, frm->dinfo); if (!bc && (frm->prim==(CC_RESTART|CONFIRM)) ) { - make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); + misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); bc=&dummybc; } if (!bc && (frm->prim==(CC_SETUP|INDICATION)) ) { - make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); + misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); dummybc.port=stack->port; dummybc.l3_id=frm->dinfo; bc=&dummybc; @@ -2641,14 +2652,14 @@ static int handle_frm(msg_t *msg) handle_frm_bc: if (bc ) { - int ret; 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); + ret = handle_event(bc, event, frm); if (ret<0) { cb_log(0,stack->port,"couldn't handle event\n"); free_msg(msg); @@ -2666,12 +2677,12 @@ handle_frm_bc: switch (response) { case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE: - cb_log(0, stack->port, "TOTALY IGNORING SETUP \n"); + 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=16; + bc->out_cause = AST_CAUSE_NORMAL_CLEARING; case RESPONSE_RELEASE_SETUP: misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); @@ -2694,7 +2705,7 @@ handle_frm_bc: } if (event == EVENT_RELEASE_COMPLETE) { - /* release bchannel only after we've anounced the 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; @@ -2703,8 +2714,8 @@ handle_frm_bc: bc->out_cause=tmp_out_cause; clean_up_bc(bc); - if (tmpcause == 44) { - cb_log(0,stack->port,"**** Received CAUSE:44, so not cleaning up channel %d\n", channel); + 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 '\n"); set_chan_in_stack(stack, channel); bc->channel=channel; @@ -2730,7 +2741,7 @@ handle_frm_bc: } else { struct misdn_bchannel dummybc; - cb_log(0, stack->port, " --> Didn't find BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo); + 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; @@ -2771,7 +2782,7 @@ static int handle_l1(msg_t *msg) for (i=0;i<=stack->b_num; i++) { if (stack->bc[i].evq != EVENT_NOTHING) { - cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0)); + 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; } @@ -2865,8 +2876,8 @@ static int handle_l2(msg_t *msg) static int handle_mgmt(msg_t *msg) { - struct misdn_stack *stack; 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") ; @@ -2874,7 +2885,7 @@ static int handle_mgmt(msg_t *msg) return 1; } - stack=find_stack_by_addr(frm->addr); + stack = find_stack_by_addr(frm->addr); if (!stack) { if (frm->prim == (MGR_DELLAYER|CONFIRM)) { @@ -2960,7 +2971,7 @@ static msg_t *fetch_msg(int midev) msg->len=r; if (r==0) { - free_msg(msg); /* danger, cauz usualy freeing in main_loop */ + free_msg(msg); /* danger, cause usually freeing in main_loop */ cb_log(6,0,"Got empty Msg..\n"); return NULL; } @@ -3001,6 +3012,7 @@ void misdn_lib_isdn_l1watcher(int port) } } +/* This is a thread */ static void misdn_lib_isdn_event_catcher(void *arg) { struct misdn_lib *mgr = arg; @@ -3017,7 +3029,7 @@ static void misdn_lib_isdn_event_catcher(void *arg) frm = (iframe_t*) msg->data; - /** When we make a call from NT2Ast we get this frames **/ + /** 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); @@ -3050,13 +3062,11 @@ static void misdn_lib_isdn_event_catcher(void *arg) /** App Interface **/ int te_lib_init() { - char buff[1025]; + char buff[1025] = ""; iframe_t *frm=(iframe_t*)buff; int midev=mISDN_open(); int ret; - memset(buff,0,1025); - if (midev<=0) return midev; /* create entity for layer 3 TE-mode */ @@ -3083,7 +3093,7 @@ void te_lib_destroy(int midev) char buf[1024]; mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC); - cb_log(4, 0, "Entetity deleted\n"); + cb_log(4, 0, "Entity deleted\n"); mISDN_close(midev); cb_log(4, 0, "midev closed\n"); } @@ -3124,7 +3134,7 @@ static int test_inuse(struct misdn_bchannel *bc) 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, now.tv_sec, bc->last_used.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; } @@ -3146,7 +3156,7 @@ static void prepare_bc(struct misdn_bchannel*bc, int channel) bc->need_disconnect=1; bc->need_release=1; bc->need_release_complete=1; - bc->cause=16; + bc->cause = AST_CAUSE_NORMAL_CLEARING; if (++mypid>5000) mypid=1; bc->pid=mypid; @@ -3174,6 +3184,7 @@ struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout, i if (stack->port == port) { int maxnum; + if (stack->blocked) { cb_log(0,port,"Port is blocked\n"); return NULL; @@ -3199,7 +3210,7 @@ struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout, i } } - maxnum=inout&&!stack->pri&&!stack->ptp?stack->b_num+1:stack->b_num; + maxnum = inout && !stack->pri && !stack->ptp ? stack->b_num + 1 : stack->b_num; if (dec) { for (i = maxnum-1; i>=0; i--) { @@ -3236,30 +3247,55 @@ struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout, i } -static char *fac2str (enum FacFunction func) + + +/* ******************************************************************* */ +/*! + * \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) { - struct arr_el { - enum FacFunction p; - char *s ; + static const struct { + enum FacFunction facility; + char *name; } arr[] = { +/* *INDENT-OFF* */ { Fac_None, "Fac_None" }, - { Fac_CD, "Fac_CD"}, + { 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* */ }; - int i; - - for (i=0; i < sizeof(arr)/sizeof( struct arr_el) ; i ++) - if ( arr[i].p==func) return arr[i].s; + 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); + stack = get_stack_by_bc(bc); if (!stack) return; @@ -3284,22 +3320,20 @@ void misdn_lib_log_ies(struct misdn_bchannel *bc) 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:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder); + cb_log(5, stack->port, " --> bc:%p h:%d sh:%d\n", bc, bc->holded, bc->stack_holder); } -void misdn_send_lock(struct misdn_bchannel *bc); -void misdn_send_unlock(struct misdn_bchannel *bc); #define RETURN(a,b) {retval=a; goto b;} -void misdn_send_lock(struct misdn_bchannel *bc) +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); } -void misdn_send_unlock(struct misdn_bchannel *bc) +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) @@ -3309,12 +3343,12 @@ void misdn_send_unlock(struct misdn_bchannel *bc) int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event ) { msg_t *msg; - struct misdn_stack *stack; int retval=0; + struct misdn_stack *stack; if (!bc) RETURN(-1,OUT_POST_UNLOCK); - stack=get_stack_by_bc(bc); + 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); @@ -3324,7 +3358,7 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event ) misdn_send_lock(bc); - cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->uperid:%x\n",stack->nt, stack->upper_id); + 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 **/ @@ -3416,8 +3450,9 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event ) 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); + bc2 = find_bc_by_confid(bc->conf_id); if (!bc2) { cb_log(0,bc->port,"We have no second bc in bridge???\n"); } else { @@ -3425,7 +3460,7 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event ) } } - channel=bc->channel; + channel = bc->channel; empty_bc(bc); clean_up_bc(bc); @@ -3466,7 +3501,7 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event ) bc->need_release_complete=0; if (!stack->nt) { - /*create clenaup in TE*/ + /*create cleanup in TE*/ int channel=bc->channel; int tmpcause=bc->cause; @@ -3568,22 +3603,22 @@ static int handle_err(msg_t *msg) case DL_DATA|INDICATION: { - struct misdn_bchannel *bc; 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) ; + free_msg(msg); return 1; - bc=find_bc_by_channel( port , channel); + bc = find_bc_by_channel(port, channel); if (!bc) { - struct misdn_stack *stack=find_stack_by_port( port ); + struct misdn_stack *stack = find_stack_by_port(port); if (!stack) { cb_log(0,0," --> stack not found\n"); @@ -3696,7 +3731,7 @@ int misdn_lib_get_port_info(int port) iframe_t *frm; struct misdn_stack *stack=find_stack_by_port(port); if (!msg) { - cb_log(0, port, "misgn_lib_get_port: alloc_msg failed!\n"); + cb_log(0, port, "misdn_lib_get_port_info: alloc_msg failed!\n"); return -1; } frm=(iframe_t*)msg->data; @@ -3725,7 +3760,7 @@ 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, "misgn_lib_get_port: alloc_msg failed!\n"); + cb_log(0, bc->port, "queue_cleanup_bc: alloc_msg failed!\n"); return -1; } frm=(iframe_t*)msg->data; @@ -3755,14 +3790,15 @@ int misdn_lib_pid_restart(int pid) return 0; } -/*Sends Restart message for every bchnanel*/ +/*Sends Restart message for every bchannel*/ int misdn_lib_send_restart(int port, int channel) { - struct misdn_bchannel dummybc; 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"); - make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); + misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); /*default is all channels*/ if (channel <0) { @@ -3836,6 +3872,7 @@ int misdn_lib_port_restart(int port) sem_t handler_started; +/* This is a thread */ static void manager_event_handler(void *arg) { sem_post(&handler_started); @@ -3859,15 +3896,15 @@ static void manager_event_handler(void *arg) case MGR_CLEARSTACK | REQUEST: /*a queued bchannel cleanup*/ { - struct misdn_bchannel *bc; 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); + bc = find_bc_by_l3id(stack, frm->addr); if (bc) { cb_log(1,bc->port,"CLEARSTACK queued, cleaning up\n"); clean_up_bc(bc); @@ -3878,6 +3915,7 @@ static void manager_event_handler(void *arg) 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); @@ -3913,11 +3951,12 @@ static void manager_event_handler(void *arg) } 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); + if (bc) + send_msg(glob_mgr->midev, bc, msg); else { if (frm->dinfo == MISDN_ID_GLOBAL) { struct misdn_bchannel dummybc; - make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); + misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0); send_msg(glob_mgr->midev, &dummybc, msg); } } @@ -3928,8 +3967,10 @@ static void manager_event_handler(void *arg) } -int misdn_lib_maxports_get() { /** BE AWARE WE HAVE NO CB_LOG HERE! **/ - +int misdn_lib_maxports_get(void) +{ + /* BE AWARE WE HAVE NO cb_log() HERE! */ + int i = mISDN_open(); int max=0; @@ -3958,7 +3999,7 @@ void misdn_lib_nt_keepcalls( int kc) void misdn_lib_nt_debug_init( int flags, char *file ) { - int static init=0; + static int init=0; char *f; if (!flags) @@ -4037,7 +4078,7 @@ int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_dat stack=stack_init(midev, port, ptp); if (!stack) { - perror("init_stack"); + perror("stack_init"); exit(1); } @@ -4084,7 +4125,7 @@ int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_dat return (mgr == NULL); } -void misdn_lib_destroy() +void misdn_lib_destroy(void) { struct misdn_stack *help; int i; @@ -4146,9 +4187,9 @@ void manager_bchannel_activate(struct misdn_bchannel *bc) void manager_bchannel_deactivate(struct misdn_bchannel * bc) { - char buf[128]; - iframe_t dact; struct misdn_stack *stack=get_stack_by_bc(bc); + iframe_t dact; + char buf[128]; switch (bc->bc_state) { case BCHAN_ACTIVATED: @@ -4182,10 +4223,10 @@ void manager_bchannel_deactivate(struct misdn_bchannel * bc) int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len) { - char buf[4096 + mISDN_HEADER_LEN]; - iframe_t *frm; - int r; 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: @@ -4196,8 +4237,6 @@ int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len) return -1; } - frm= (iframe_t*)buf; - frm->prim = DL_DATA|REQUEST; frm->dinfo = 0; frm->addr = bc->addr | FLG_MSG_DOWN ; @@ -4309,9 +4348,6 @@ void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder) cb_log(4,stack->port, "*HOLDER: add %x\n",holder->l3_id); holder->stack_holder=1; - - if (!stack ) return ; - holder->next=NULL; if (!stack->holding) { @@ -4382,7 +4418,7 @@ struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned lon { struct misdn_bchannel *help; - cb_log(4,stack?stack->port:0, "*HOLDER: find %x\n",l3id); + cb_log(4,stack?stack->port:0, "*HOLDER: find %lx\n",l3id); if (!stack) return NULL; @@ -4403,8 +4439,8 @@ struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned lon void misdn_lib_send_tone(struct misdn_bchannel *bc, enum tone_e tone) { - char buf[mISDN_HEADER_LEN+128]; - iframe_t *frm; + char buf[mISDN_HEADER_LEN + 128] = ""; + iframe_t *frm = (iframe_t*)buf; switch(tone) { case TONE_DIAL: @@ -4424,9 +4460,6 @@ void misdn_lib_send_tone(struct misdn_bchannel *bc, enum tone_e tone) manager_ph_control(bc, TONE_PATT_OFF, TONE_GERMAN_HANGUP); } - frm=(iframe_t*)buf; - memset(buf,0,mISDN_HEADER_LEN+128); - frm->prim=DL_DATA|REQUEST; frm->addr=bc->addr|FLG_MSG_DOWN; frm->dinfo=0; @@ -4514,8 +4547,7 @@ struct misdn_stack* get_misdn_stack() { void misdn_join_conf(struct misdn_bchannel *bc, int conf_id) { - char data[16]; - int len=15; + char data[16] = ""; bc_state_change(bc,BCHAN_BRIDGED); manager_ph_control(bc, CMX_RECEIVE_OFF, 0); @@ -4523,10 +4555,7 @@ void misdn_join_conf(struct misdn_bchannel *bc, int conf_id) cb_log(3,bc->port, "Joining bc:%x in conf:%d\n",bc->addr,conf_id); - memset(data,0,15); - - misdn_lib_tx2misdn_frm(bc, data, len); - + misdn_lib_tx2misdn_frm(bc, data, sizeof(data) - 1); } @@ -4539,16 +4568,14 @@ void misdn_split_conf(struct misdn_bchannel *bc, int 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) +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 - }; + 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); diff --git a/channels/misdn/isdn_lib.h b/channels/misdn/isdn_lib.h index 8e95b5303..451876888 100644 --- a/channels/misdn/isdn_lib.h +++ b/channels/misdn/isdn_lib.h @@ -11,6 +11,12 @@ * the GNU General Public License */ +/*! \file + * \brief Interface to mISDN + * + * \author Christian Richter + */ + #ifndef TE_LIB #define TE_LIB @@ -21,9 +27,9 @@ /** end of init usage **/ -/* +/* * uncomment the following to make chan_misdn create - * record files in /tmp/misdn-{rx|tx}-PortChannel format + * record files in /tmp/misdn-{rx|tx}-PortChannel format * */ /*#define MISDN_SAVE_DATA*/ @@ -44,9 +50,9 @@ 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 beroec_cancel_alaw_chunk(beroec_t *ec, + char *send, + char *receive, int len); int beroec_version(void); @@ -97,7 +103,7 @@ enum mISDN_NUMBER_PLAN { NUMPLAN_NATIONAL=0x2, NUMPLAN_SUBSCRIBER=0x4, NUMPLAN_UNKNOWN=0x0 -}; +}; enum event_response_e { @@ -152,7 +158,7 @@ enum event_e { EVENT_PORT_ALARM, EVENT_NEW_CHANNEL, EVENT_UNKNOWN -}; +}; enum ie_name_e { @@ -186,7 +192,7 @@ enum { /* progress indicators */ enum { /*CODECS*/ INFO_CODEC_ULAW=2, INFO_CODEC_ALAW=3 -}; +}; enum layer_e { @@ -194,85 +200,173 @@ enum layer_e { 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*/ + + /* 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**/ + + /* 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; - char * bframe; + /*! \brief B channel speech sample data buffer */ + char *bframe; + + /*! \brief B channel speech sample data buffer size */ int bframe_len; - int time_usec; - - + int time_usec; /* Not used */ + + /*! \brief Not used. Contents are setup but not used. */ void *astbuf; - void *misdnbuf; + 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; - - /* dtmf digit */ + + /*! \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; - /* get setup ack */ + /*! \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; - /* may there be more infos ?*/ + /*! \brief TRUE if all digits necessary to complete the call are available. + * No more INFORMATION messages are needed. + */ int sending_complete; - /* wether we should use jollys dsp or not */ + /*! \brief TRUE if we will not use jollys dsp */ int nodsp; - - /* wether we should use our jitter buf system or not */ + + /*! \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 */ @@ -281,102 +375,205 @@ struct misdn_bchannel { struct FacAOCDCurrency currency; struct FacAOCDChargingUnit chargingUnit; } AOCD; - + + /*! \brief Event waiting for Layer 1 to come up */ enum event_e evq; - + /*** CRYPTING STUFF ***/ - - int crypt; - int curprx; - int curptx; + 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; - - /*char ast_dtmf_buf[255]; - char misdn_dtmf_buf[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; + 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 **/ + + /* 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, ...); +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, ...); + 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); }; @@ -429,7 +626,7 @@ 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); diff --git a/channels/misdn/isdn_lib_intern.h b/channels/misdn/isdn_lib_intern.h index 050c86fee..93d879744 100644 --- a/channels/misdn/isdn_lib_intern.h +++ b/channels/misdn/isdn_lib_intern.h @@ -26,8 +26,8 @@ #endif -ibuffer_t *astbuf; -ibuffer_t *misdnbuf; +ibuffer_t *astbuf; /* Not used */ +ibuffer_t *misdnbuf; /* Not used */ struct send_lock { pthread_mutex_t lock; @@ -36,15 +36,14 @@ struct send_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); @@ -55,57 +54,78 @@ 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; + 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; - + + 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; - int busy; - + 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; - + + 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]; - - struct misdn_bchannel *holding; /* Queue which holds holded channels :) */ - + /*! \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); diff --git a/channels/misdn_config.c b/channels/misdn_config.c index 473325162..5125058ea 100644 --- a/channels/misdn_config.c +++ b/channels/misdn_config.c @@ -196,19 +196,19 @@ static const struct misdn_cfg_spec port_spec[] = { { "presentation", MISDN_CFG_PRES, MISDN_CTYPE_INT, "-1", NONE, "These (presentation and screen) are the exact isdn screening and presentation\n" "\tindicators.\n" - "\tIf -1 is given for both values, the presentation indicators are used from\n" - "\tAsterisks SetCallerPres application.\n" + "\tIf -1 is given for either value, the presentation indicators are used from\n" + "\tAsterisk's SetCallerPres application.\n" "\n" - "\tscreen=0, presentation=0 -> callerid presented not screened\n" - "\tscreen=1, presentation=1 -> callerid presented but screened (the remote end doesn't see it!)" }, + "\tscreen=0, presentation=0 -> callerid presented\n" + "\tscreen=1, presentation=1 -> callerid restricted (the remote end doesn't see it!)" }, { "screen", MISDN_CFG_SCREEN, MISDN_CTYPE_INT, "-1", NONE, "These (presentation and screen) are the exact isdn screening and presentation\n" "\tindicators.\n" - "\tIf -1 is given for both values, the presentation indicators are used from\n" - "\tAsterisks SetCallerPres application.\n" + "\tIf -1 is given for either value, the presentation indicators are used from\n" + "\tAsterisk's SetCallerPres application.\n" "\n" - "\tscreen=0, presentation=0 -> callerid presented not screened\n" - "\tscreen=1, presentation=1 -> callerid presented but screened (the remote end doesn't see it!)" }, + "\tscreen=0, presentation=0 -> callerid presented\n" + "\tscreen=1, presentation=1 -> callerid restricted (the remote end doesn't see it!)" }, { "always_immediate", MISDN_CFG_ALWAYS_IMMEDIATE, MISDN_CTYPE_BOOL, "no", NONE, "Enable this to get into the s dialplan-extension.\n" "\tThere you can use DigitTimeout if you can't or don't want to use\n" -- cgit v1.2.3