* removed unnecessary struct elements and functions
authorChristian Richter <christian.richter@beronet.com>
Thu, 2 Feb 2006 21:15:34 +0000 (21:15 +0000)
committerChristian Richter <christian.richter@beronet.com>
Thu, 2 Feb 2006 21:15:34 +0000 (21:15 +0000)
* fixed "RETRIEVE does not work" bug
* fixed some NT Mode bugs
* removed some // comments
* added configureable jitterbuffer
* removed own tone-generator, and use asterisks instead, to support
  asterisks indications
* added more support for hw-bridging, we bridge now every possible call
* fixed some hdlc mode issues, with a patch for chan_zap we can make
  data calls between chan_zap and chan_misdn now
* completely reworked the config engine, works like a charm now
* fixed SetCallerPres - bug
* added Progress and Proceeding passing
* optimized Ringing Indication handling
* added full ast_send_text support (you can setup nice menus with the dialplan
  now)
* added support to read /etc/misdn-init.conf to clarify the NT+PTP Problem
* we compile now channels/misdn if mISDNuser is installed systemwide

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@9114 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/Makefile
channels/chan_misdn.c
channels/chan_misdn_config.c
channels/misdn/Makefile
channels/misdn/chan_misdn_config.h
channels/misdn/isdn_lib.c
channels/misdn/isdn_lib.h
channels/misdn/isdn_msg_parser.c
configs/misdn.conf.sample

index 4eb9329..d772d39 100644 (file)
@@ -69,7 +69,7 @@ ifneq ($(wildcard h323/libchanh323.a),)
   CHANNEL_LIBS+=chan_h323.so
 endif
 
-ifneq ($(wildcard misdn/chan_misdn_lib.a),)
+ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/mISDNuser/mISDNlib.h),)
   CHANNEL_LIBS+=chan_misdn.so
   CFLAGS+=-Imisdn 
 endif
@@ -224,11 +224,18 @@ chan_h323.so: chan_h323.o h323/libchanh323.a
        $(CC) $(SOLINK) -o $@ $< h323/libchanh323.a $(CHANH323LIB) -L$(PWLIBDIR)/lib $(PTLIB) -L$(OPENH323DIR)/lib $(H323LIB) -L/usr/lib -lcrypto -lssl -lexpat
 endif
 
+misdn/chan_misdn_lib.a:
+       make -C misdn
+
 chan_misdn.so: chan_misdn.o chan_misdn_config.o misdn/chan_misdn_lib.a
        $(CC) -shared -Xlinker -x -L/usr/lib -o $@ $^ -lisdnnet -lmISDN
 
 chan_misdn.o: chan_misdn.c
-       $(CC) $(CFLAGS) -DCHAN_MISDN_VERSION=\"0.2.1\" -c $< -o $@
+       $(CC) $(CFLAGS) -DCHAN_MISDN_VERSION=\"0.3.0\" -c $< -o $@
+
+chan_misdn_config.o: chan_misdn_config.c misdn/chan_misdn_config.h
+       $(CC) $(CFLAGS) -DCHAN_MISDN_VERSION=\"0.3.0\" -c $< -o $@
+
 
 install: all
        for x in $(CHANNEL_LIBS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
index c95eddb..6582df2 100644 (file)
@@ -60,8 +60,8 @@
 #include <asterisk/features.h>
 #include <asterisk/stringfields.h>
 
-#include "chan_misdn_config.h"
-#include "isdn_lib.h"
+#include <chan_misdn_config.h>
+#include <isdn_lib.h>
 
 ast_mutex_t release_lock_mutex;
 
@@ -103,6 +103,17 @@ int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
 
 /* BEGIN: chan_misdn.h */
 
+
+enum tone_e {
+       TONE_NONE=0,
+       TONE_DIAL,
+       TONE_ALERTING,
+       TONE_BUSY,
+       TONE_CUSTOM,
+       TONE_FILE
+};
+
+
 enum misdn_chan_state {
        MISDN_NOTHING,          /*!< at beginning */
        MISDN_WAITING4DIGS, /*!<  when waiting for infos */
@@ -161,6 +172,8 @@ struct chan_list {
        int addr;
 
        char context[BUFFERSIZE];
+
+       const struct tone_zone_sound *ts;
        
        struct chan_list *peer;
        struct chan_list *next;
@@ -266,6 +279,7 @@ static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bc
 
 
 
+static int tone_indicate( struct chan_list *cl, enum tone_e tone);
 
 static int start_bc_tones(struct chan_list *cl);
 static int stop_bc_tones(struct chan_list *cl);
@@ -300,25 +314,6 @@ static struct chan_list * get_chan_by_ast_name(char *name)
        return NULL;
 }
 
-static char* tone2str(struct misdn_bchannel *bc)
-{
-       static struct {
-               char name[16];
-               enum tone_e tone;
-       } *tone, tone_buf[] = {
-               {"NOTONE",TONE_NONE},
-               {"DIAL",TONE_DIAL},
-               {"BUSY",TONE_BUSY},
-               {"ALERT",TONE_ALERTING},
-               {"",TONE_NONE}
-       };
-  
-  
-       for (tone=&tone_buf[0]; tone->name[0]; tone++) {
-               if (tone->tone == bc->tone) return tone->name;
-       }
-       return NULL;
-}
 
 static char *bearer2str(int cap) {
        static char *bearers[]={
@@ -529,12 +524,12 @@ 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," ->  VERSION: " CHAN_MISDN_VERSION "\n");
-               
+               ast_cli(fd," -> Version: chan_misdn-" CHAN_MISDN_VERSION "\n");
                for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) {
                        misdn_cfg_get_config_string( 0, elem, buffer, BUFFERSIZE);
                        ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
                }
+               ast_cli(fd, "\n");
        }
 
        if (onlyport < 0) {
@@ -610,6 +605,8 @@ static int misdn_reload (int fd, int argc, char *argv[])
        free_robin_list();
 
        misdn_cfg_reload();
+
+       misdn_cfg_update_ptp();
        
        misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
        
@@ -643,7 +640,6 @@ static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel
                        "  --> ch_addr: %x\n"
                        "  --> bc_addr: %x\n"
                        "  --> bc_l3id: %x\n"
-                       "  --> tone: %s\n"
                        "  --> display: %s\n"
                        "  --> activated: %d\n"
                        "  --> capability: %s\n"
@@ -655,7 +651,6 @@ static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel
                        help->addr,
                        bc->addr,
                        bc?bc->l3_id:-1,
-                       tone2str(bc),
                        bc->display,
                        
                        bc->active,
@@ -1059,6 +1054,89 @@ static struct ast_cli_entry cli_set_crypt_debug =
 /*** CLI END ***/
 
 
+static int update_config (struct chan_list *ch, int orig) 
+{
+       if (!ch) {
+               ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
+               return -1;
+       }
+       
+       struct ast_channel *ast=ch->ast;
+       struct misdn_bchannel *bc=ch->bc;
+       if (! ast || ! bc ) {
+               ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
+               return -1;
+       }
+       
+       int port=bc->port;
+       
+       chan_misdn_log(1,port,"update_config: Getting Config\n");
+
+       
+       int pres, screen;
+                       
+       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);
+                       
+               switch (ast->cid.cid_pres & 0x60){
+                               
+               case AST_PRES_RESTRICTED:
+                       bc->pres=1;
+                       chan_misdn_log(2, port, " --> PRES: Restricted (0x1)\n");
+                       break;
+                               
+                               
+               case AST_PRES_UNAVAILABLE:
+                       bc->pres=2;
+                       chan_misdn_log(2, port, " --> PRES: Unavailable (0x2)\n");
+                       break;
+                               
+               default:
+                       bc->pres=0;
+                       chan_misdn_log(2, port, " --> PRES: Allowed (0x0)\n");
+               }
+                       
+               switch (ast->cid.cid_pres & 0x3){
+                               
+               case AST_PRES_USER_NUMBER_UNSCREENED:
+                       bc->screen=0;
+                       chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
+                       break;
+
+               case AST_PRES_USER_NUMBER_PASSED_SCREEN:
+                       bc->screen=1;
+                       chan_misdn_log(2, port, " --> SCREEN: Passed Screen (0x1)\n");
+                       break;
+               case AST_PRES_USER_NUMBER_FAILED_SCREEN:
+                       bc->screen=2;
+                       chan_misdn_log(2, port, " --> SCREEN: Failed Screen (0x2)\n");
+                       break;
+                               
+               case AST_PRES_NETWORK_NUMBER:
+                       bc->screen=3;
+                       chan_misdn_log(2, port, " --> SCREEN: Network Nr. (0x3)\n");
+                       break;
+                               
+               default:
+                       bc->screen=0;
+                       chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
+               }
+
+                       
+       } else {
+               bc->screen=screen;
+               bc->pres=pres;
+       }
+
+       return 0;
+       
+}
+
 
 static int read_config(struct chan_list *ch, int orig) {
 
@@ -1094,6 +1172,8 @@ static int read_config(struct chan_list *ch, int orig) {
        misdn_cfg_get( port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(int));
        
        misdn_cfg_get( port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(int));
+
+       misdn_cfg_get( port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(int));
        
 
        /*Initialize new Jitterbuffer*/
@@ -1128,6 +1208,7 @@ static int read_config(struct chan_list *ch, int orig) {
        }
        
        misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
+       
        strncpy(ast->context,ch->context,sizeof(ast->context)-1);
        
        {
@@ -1233,67 +1314,7 @@ static int read_config(struct chan_list *ch, int orig) {
                }
 
                
-               /* Will be overridden by asterisk in head! */
-               {
-                       int pres;
-                       
-                       misdn_cfg_get( port, MISDN_CFG_PRES, &pres, sizeof(int));
-                       bc->pres=pres?0:1;
-                       
-               }
-               
-               int def_callingpres;
-               misdn_cfg_get( port, MISDN_CFG_USE_CALLINGPRES, &def_callingpres, sizeof(int));
-               if ( def_callingpres) {
-                       
-                       switch (ast->cid.cid_pres & 0x60){
                                
-                       case AST_PRES_RESTRICTED:
-                               bc->pres=1;
-                               chan_misdn_log(2, port, " --> PRES: Restricted (0x1)\n");
-                               break;
-                               
-                               
-                       case AST_PRES_UNAVAILABLE:
-                               bc->pres=2;
-                               chan_misdn_log(2, port, " --> PRES: Unavailable (0x2)\n");
-                               break;
-                               
-                       default:
-                               bc->pres=0;
-                               chan_misdn_log(2, port, " --> PRES: Allowed (0x0)\n");
-                       }
-                       
-                       switch (ast->cid.cid_pres & 0x3){
-                               
-                       case AST_PRES_USER_NUMBER_UNSCREENED:
-                               bc->screen=0;
-                               chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
-                               break;
-
-                       case AST_PRES_USER_NUMBER_PASSED_SCREEN:
-                               bc->screen=1;
-                               chan_misdn_log(2, port, " --> SCREEN: Passed Screen (0x1)\n");
-                               break;
-                       case AST_PRES_USER_NUMBER_FAILED_SCREEN:
-                               bc->screen=2;
-                               chan_misdn_log(2, port, " --> SCREEN: Failed Screen (0x2)\n");
-                               break;
-                               
-                       case AST_PRES_NETWORK_NUMBER:
-                               bc->screen=3;
-                               chan_misdn_log(2, port, " --> SCREEN: Network Nr. (0x3)\n");
-                               break;
-                               
-                       default:
-                               bc->screen=0;
-                               chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
-                       }
-
-                       
-               }
-
-               
                
        } else { /** ORIGINATOR MISDN **/
                
@@ -1366,8 +1387,18 @@ static int read_config(struct chan_list *ch, int orig) {
                        strcpy(bc->dad,tmp);
                }
                
-               
-               
+               if ( strcmp(bc->dad,ast->exten)) {
+                       ast_copy_string(ast->exten, bc->dad, sizeof(ast->exten));
+               }
+               if ( ast->cid.cid_num && strcmp(ast->cid.cid_num, bc->oad)) {
+                       free(ast->cid.cid_num);
+                       ast->cid.cid_num=NULL;
+                       
+               }
+               if ( !ast->cid.cid_num) {
+                       ast->cid.cid_num=strdup(bc->oad);
+               }
+                       
                pbx_builtin_setvar_helper(ch->ast,"REDIRECTING_NUMBER",bc->rad);
        }
        
@@ -1387,6 +1418,13 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
        struct misdn_bchannel *newbc;
        char *opts=NULL, *ext,*tokb;
        char dest_cp[256];
+       struct ast_channel *bridged;
+       
+       if ( (bridged=ast_bridged_channel(ast)) ) {
+               chan_misdn_log(0,0,"Our Bridged Partner is %s\n",bridged->tech->type);
+       } else {
+               chan_misdn_log(0,0,"No Bridged Partner\n");
+       }
        
        {
                strncpy(dest_cp,dest,sizeof(dest_cp)-1);
@@ -1475,7 +1513,10 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
                        chan_misdn_log(2, port, " --> * Call with flag Digital\n");
                }
                
-               
+
+               /* update screening and presentation */ 
+               update_config(ch,ORG_AST);
+       
                /* Finally The Options Override Everything */
                if (opts)
                        misdn_set_opt_exec(ast,opts);
@@ -1483,9 +1524,7 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
                        chan_misdn_log(1,0,"NO OPTS GIVEN\n");
                
                
-               
                ch->state=MISDN_CALLING;
-
                
                r=misdn_lib_send_event( newbc, EVENT_SETUP );
                
@@ -1504,7 +1543,6 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
        chan_misdn_log(1, port, " --> * SEND: State Dialing pid:%d\n",newbc?newbc->pid:1);
 
        ast_setstate(ast, AST_STATE_DIALING);
-       
        ast->hangupcause=16;
        return 0; 
 }
@@ -1515,9 +1553,7 @@ static int misdn_answer(struct ast_channel *ast)
        struct chan_list *p;
 
        
-       if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
-       p = MISDN_ASTERISK_TECH_PVT(ast) ;
-       
+       if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast)) ) return -1;
        
        chan_misdn_log(1, p? (p->bc? p->bc->port : 0) : 0, "* ANSWER:\n");
        
@@ -1549,10 +1585,12 @@ static int misdn_answer(struct ast_channel *ast)
        }
 
        {
-               const char *async=pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
-               if (async) {
+               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->async=1;
+                       p->bc->nodsp=1;
+                       p->bc->hdlc=0;
+                       p->bc->nojitter=1;
                }
        }
        
@@ -1567,9 +1605,7 @@ static int misdn_digit(struct ast_channel *ast, char digit )
 {
        struct chan_list *p;
        
-       if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
-       p = MISDN_ASTERISK_TECH_PVT(ast) ;
-       
+       if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast))) return -1;
 
        struct misdn_bchannel *bc=p->bc;
        chan_misdn_log(1, bc?bc->port:0, "* IND : Digit %c\n",digit);
@@ -1627,8 +1663,7 @@ static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
 {
        struct chan_list *p;
        
-       if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
-       p = MISDN_ASTERISK_TECH_PVT(ast) ;
+       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 Holded:%d L3id:%x\n", misdn_get_ch_state(p), p->holded, p->l3id);
        
@@ -1645,11 +1680,10 @@ static int misdn_indication(struct ast_channel *ast, int cond)
        struct chan_list *p;
 
   
-       if (!ast || ! MISDN_ASTERISK_PVT(ast)) {
+       if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast))) {
                ast_log(LOG_WARNING, "Returnded -1 in misdn_indication\n");
                return -1;
        }
-       p = MISDN_ASTERISK_TECH_PVT(ast) ;
        
        if (!p->bc ) {
                chan_misdn_log(1, 0, "* IND : Indication from %s\n",ast->exten);
@@ -1657,7 +1691,7 @@ static int misdn_indication(struct ast_channel *ast, int cond)
                return -1;
        }
        
-       chan_misdn_log(1, p->bc->port, "* IND : Indication from %s\n",ast->exten);
+       chan_misdn_log(1, p->bc->port, "* IND : Indication [%d] from %s\n",cond, ast->exten);
        
        switch (cond) {
        case AST_CONTROL_BUSY:
@@ -1668,7 +1702,7 @@ static int misdn_indication(struct ast_channel *ast, int cond)
                p->bc->out_cause=17;
                if (p->state != MISDN_CONNECTED) {
                        misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
-                       manager_send_tone(p->bc, TONE_BUSY);
+                       tone_indicate(p, TONE_BUSY);
                } else {
 
                        chan_misdn_log(-1, p->bc->port, " --> !! Got Busy in Connected State !?! ast:%s\n", ast->name);
@@ -1678,23 +1712,26 @@ static int misdn_indication(struct ast_channel *ast, int cond)
                chan_misdn_log(1, p->bc->port, " --> * IND :\tring pid:%d\n",p->bc?p->bc->pid:-1);
                break;
        case AST_CONTROL_RINGING:
-               if ( p->state == MISDN_ALERTING) {
-                       chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d but I ws Ringing before, so ignoreing it\n",p->bc?p->bc->pid:-1);
-                       break;
+               switch (p->state) {
+                       case MISDN_ALERTING:
+                               chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoreing it\n",p->bc?p->bc->pid:-1);
+                               break;
+                       case MISDN_CONNECTED:
+                               chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n",p->bc?p->bc->pid:-1);
+                               tone_indicate(p, TONE_ALERTING);
+                               break;
+                       default:
+                               p->state=MISDN_ALERTING;
+                               chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d\n",p->bc?p->bc->pid:-1);
+                               misdn_lib_send_event( p->bc, EVENT_ALERTING);
+                               tone_indicate(p, TONE_ALERTING);
+                               chan_misdn_log(1, p->bc->port, " --> * SEND: State Ring pid:%d\n",p->bc?p->bc->pid:-1);
+                               ast_setstate(ast,AST_STATE_RINGING);
                }
-               p->state=MISDN_ALERTING;
-               
-               chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d\n",p->bc?p->bc->pid:-1);
-               
-               misdn_lib_send_event( p->bc, EVENT_ALERTING);
-               
-               manager_send_tone(p->bc, TONE_ALERTING);
-               chan_misdn_log(1, p->bc->port, " --> * SEND: State Ring pid:%d\n",p->bc?p->bc->pid:-1);
-               ast_setstate(ast,AST_STATE_RINGING);
                break;
-               
        case AST_CONTROL_ANSWER:
                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);
@@ -1707,9 +1744,13 @@ static int misdn_indication(struct ast_channel *ast, int cond)
                break;
        case AST_CONTROL_PROGRESS:
                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);
+               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);
 
                p->bc->out_cause=42;
@@ -1721,11 +1762,12 @@ static int misdn_indication(struct ast_channel *ast, int cond)
                }
 
                if (p->bc->nt) {
-                       manager_send_tone(p->bc, TONE_BUSY);
+                       tone_indicate(p, TONE_BUSY);
                }
                break;
        case -1 :
-               chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! 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);
+               tone_indicate(p, TONE_NONE);
                break;
 
        case AST_CONTROL_HOLD:
@@ -1815,7 +1857,7 @@ static int misdn_hangup(struct ast_channel *ast)
                case MISDN_HOLDED:
                case MISDN_DIALING:
                        start_bc_tones(p);
-                       manager_send_tone(bc, TONE_BUSY);
+                       tone_indicate(p, TONE_BUSY);
                        p->state=MISDN_CLEANING;
                        
                        misdn_lib_send_event( bc, EVENT_RELEASE_COMPLETE);
@@ -1826,7 +1868,7 @@ static int misdn_hangup(struct ast_channel *ast)
                        chan_misdn_log(2, bc->port, " --> * State Alerting\n");
 
                        if (p->orginator != ORG_AST) 
-                               manager_send_tone(bc, TONE_BUSY);
+                               tone_indicate(p, TONE_BUSY);
       
                        p->state=MISDN_CLEANING;
                        misdn_lib_send_event( bc, EVENT_DISCONNECT);
@@ -1835,7 +1877,7 @@ static int misdn_hangup(struct ast_channel *ast)
                        /*  Alerting or Disconect */
                        chan_misdn_log(2, bc->port, " --> * State Connected\n");
                        start_bc_tones(p);
-                       manager_send_tone(bc, TONE_BUSY);
+                       tone_indicate(p, TONE_BUSY);
                        misdn_lib_send_event( bc, EVENT_DISCONNECT);
       
                        p->state=MISDN_CLEANING; /* MISDN_HUNGUP_FROM_AST; */
@@ -1918,8 +1960,8 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
                return -1;
        }
        
-       if (ch->bc->tone != TONE_NONE)
-               manager_send_tone(ch->bc,TONE_NONE);
+       /*if (ch->bc->tone != TONE_NONE)
+         tone_indicate(ch,TONE_NONE); */
        
        
        if (ch->holded ) {
@@ -1931,10 +1973,17 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
                chan_misdn_log(5, ch->bc->port, "misdn_write: Returning because notxone\n");
                return 0;
        }
+
+
+       if ( !frame->subclass) {
+               chan_misdn_log(0, ch->bc->port, "misdn_write: * prods us\n");
+               return 0;
+       }
        
        if ( !(frame->subclass & prefformat)) {
-
+               
                chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%d\n", frame->subclass);
+               return -1;
        }
        
        
@@ -1949,6 +1998,13 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
        }
 #endif
 
+       
+       if (!ch->bc->active) {
+               chan_misdn_log(5, ch->bc->port, "BC not active droping: %d frames\n",frame->samples);
+               return 0;
+       }
+       
+       chan_misdn_log(9, ch->bc->port, "Sending :%d bytes 2 MISDN\n",frame->samples);
        /*if speech flip bits*/
        if ( misdn_cap_is_speech(ch->bc->capability) )
                flip_buf_bits(frame->data,frame->samples);
@@ -2063,10 +2119,56 @@ enum ast_bridge_result  misdn_bridge (struct ast_channel *c0,
 
 /** AST INDICATIONS END **/
 
+static int tone_indicate( struct chan_list *cl, enum tone_e tone)
+{
+       const struct tone_zone_sound *ts= NULL;
+       struct ast_channel *ast=cl->ast;
+       
+       chan_misdn_log(0,cl->bc->port,"Tone Indicate:\n");
+       
+       if (!cl->ast) {
+               return 0;
+       }
+       
+       switch (tone) {
+       case TONE_DIAL:
+               chan_misdn_log(0,cl->bc->port," --> Dial\n");
+               ts=ast_get_indication_tone(ast->zone,"dial");
+               misdn_lib_tone_generator_start(cl->bc);
+               break;
+       case TONE_ALERTING:
+               chan_misdn_log(0,cl->bc->port," --> Ring\n");
+               ts=ast_get_indication_tone(ast->zone,"ring");
+               misdn_lib_tone_generator_stop(cl->bc);
+               break;
+       case TONE_BUSY:
+               chan_misdn_log(0,cl->bc->port," --> Busy\n");
+               ts=ast_get_indication_tone(ast->zone,"busy");
+               misdn_lib_tone_generator_stop(cl->bc);
+               break;
+       case TONE_FILE:
+               break;
+
+       case TONE_NONE:
+               chan_misdn_log(0,cl->bc->port," --> None\n");
+               misdn_lib_tone_generator_stop(cl->bc);
+               ast_playtones_stop(ast);
+               break;
+       default:
+               chan_misdn_log(0,cl->bc->port,"Don't know how to handle tone: %d\n",tone);
+       }
+       
+       cl->ts=ts;      
+       
+       if (ts) ast_playtones_start(ast,0, ts->data, 0);
+       
+       return 0;
+}
+
 static int start_bc_tones(struct chan_list* cl)
 {
        manager_bchannel_activate(cl->bc);
-       manager_send_tone(cl->bc ,TONE_NONE);
+       misdn_lib_tone_generator_stop(cl->bc);
        cl->notxtone=0;
        cl->norxtone=0;
        return 0;
@@ -2188,17 +2290,16 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
                                        misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
                                        
                                        if (!strcasecmp(cfg_group, group)) {
-                                               int l1, port_up;
+                                               int port_up;
                                        
-                                               misdn_cfg_get( 0, MISDN_GEN_L1_INFO_OK, &l1, sizeof(l1));
                                                port_up = misdn_lib_port_up(port);
                                                
-                                               if ((l1 && port_up) || !l1)     {
+                                               if ( port_up )  {
                                                        newbc = misdn_lib_get_free_bc(port, robin_channel);
                                                        if (newbc) {
                                                                chan_misdn_log(4, port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
                                                                if (port_up)
-                                                                       chan_misdn_log(4, port, "def_l1:%d, portup:%d\n", l1, port_up);
+                                                                       chan_misdn_log(4, port, "ortup:%d\n",  port_up);
                                                                rr->port = newbc->port;
                                                                rr->channel = newbc->channel;
                                                                break;
@@ -2219,14 +2320,13 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
                                misdn_cfg_get( port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
                                
                                if (!strcasecmp(cfg_group, group)) {
-                                       int l1, port_up;
+                                       int port_up;
                                        
-                                       misdn_cfg_get( 0, MISDN_GEN_L1_INFO_OK, &l1, sizeof(l1));
                                        port_up = misdn_lib_port_up(port);
 
-                                       chan_misdn_log(4, port, "def_l1:%d, portup:%d\n", l1, port_up);
+                                       chan_misdn_log(4, port, "portup:%d\n", port_up);
                                        
-                                       if ((l1 && port_up) || !l1)     {
+                                       if ( port_up )  {
                                                newbc = misdn_lib_get_free_bc(port, 0);
                                                if (newbc)
                                                        break;
@@ -2264,6 +2364,21 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
 }
 
 
+int misdn_send_text (struct ast_channel *chan, const char *text)
+{
+       struct chan_list *tmp=chan->tech_pvt;
+       
+       if (tmp && tmp->bc) {
+               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");
+               return -1;
+       }
+       
+       return 0;
+}
+
 static struct ast_channel_tech misdn_tech = {
        .type="mISDN",
        .description="Channel driver for mISDN Support (Bri/Pri)",
@@ -2278,6 +2393,7 @@ static struct ast_channel_tech misdn_tech = {
        .write=misdn_write,
        .indicate=misdn_indication,
        .fixup=misdn_fixup,
+       .send_text=misdn_send_text,
        .properties=0
 };
 
@@ -2294,6 +2410,7 @@ static struct ast_channel_tech misdn_tech_wo_bridge = {
        .write=misdn_write,
        .indicate=misdn_indication,
        .fixup=misdn_fixup,
+       .send_text=misdn_send_text,
        .properties=0
 };
 
@@ -2379,15 +2496,16 @@ static struct ast_channel *misdn_new(struct chan_list *chlist, int state,  char
 static int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
 {
        struct ast_frame frame;
-       
+
        /* If in hold state we drop frame .. */
        if (tmp->holded ) return 0;
-
+       
        switch(tmp->state) {
        case MISDN_CLEANING:
        case MISDN_EXTCANTMATCH:
-       case MISDN_WAITING4DIGS:
                return 0;
+               
+       case MISDN_WAITING4DIGS:
        default:
                break;
        }
@@ -2473,7 +2591,7 @@ static int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
 #endif
                chan_misdn_log(9, tmp->bc->port, "Queueing %d bytes 2 Asterisk\n",len);
                ast_queue_frame(tmp->ast,&frame);
-               
+
        }  else {
                ast_log (LOG_WARNING, "No ast || ast->pvt || ch\n");
        }
@@ -2586,6 +2704,10 @@ static void release_chan(struct misdn_bchannel *bc) {
        {
                struct chan_list *ch=find_chan_by_bc(cl_te, bc);
                if (!ch) ch=find_chan_by_l3id (cl_te, bc->l3_id);
+               if (!ch)  {
+                       chan_misdn_log(0, bc->port, "release_chan: Ch not found!\n");
+                       return;
+               }
                
                release_lock;
                if (ch->ast) {
@@ -2724,7 +2846,7 @@ static void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch ,
                }
        }
 
-       manager_send_tone(bc,TONE_DIAL);  
+       tone_indicate(ch,TONE_DIAL);  
   
        chan_misdn_log(1, bc->port, "* Starting Ast ctx:%s dad:%s oad:%s with 's' extension\n", ast->context, ast->exten, AST_CID_P(ast));
   
@@ -2732,7 +2854,7 @@ static void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch ,
   
        if (ast_pbx_start(ast)<0) {
                ast=NULL;
-               manager_send_tone(bc,TONE_BUSY);
+               tone_indicate(ch,TONE_BUSY);
 
                if (bc->nt)
                        misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
@@ -2798,7 +2920,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
        if (!ch)
                ch=find_chan_by_l3id(cl_te, bc->l3_id);
        
-       if (event != EVENT_BCHAN_DATA) { /*  Debug Only Non-Bchan */
+       if (event != EVENT_BCHAN_DATA && event != EVENT_TONE_GENERATE) { /*  Debug Only Non-Bchan */
                chan_misdn_log(1, bc->port, "I IND :%s oad:%s dad:%s\n", manager_isdn_get_info(event), bc->oad, bc->dad);
                misdn_lib_log_ies(bc);
        }
@@ -2828,6 +2950,11 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
        
        
        switch (event) {
+
+       case EVENT_BCHAN_ACTIVATED:
+               break;
+               
+               
        case EVENT_NEW_L3ID:
                ch->l3id=bc->l3_id;
                break;
@@ -2863,8 +2990,8 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
        {
                int stop_tone;
                misdn_cfg_get( 0, MISDN_GEN_STOP_TONE, &stop_tone, sizeof(int));
-               if ( stop_tone && bc->tone != TONE_NONE) {
-                       manager_send_tone(bc,TONE_NONE);
+               if ( stop_tone ) {
+                       tone_indicate(ch,TONE_NONE);
                }
                
                if (ch->state == MISDN_WAITING4DIGS ) {
@@ -2887,7 +3014,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        if(!ast_canmatch_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
 
                                chan_misdn_log(-1, bc->port, "Extension can never match, so disconnecting\n");
-                               manager_send_tone(bc,TONE_BUSY);
+                               tone_indicate(ch,TONE_BUSY);
                                ch->state=MISDN_EXTCANTMATCH;
                                bc->out_cause=1;
 
@@ -2900,12 +3027,12 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        if (ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
                                ch->state=MISDN_DIALING;
          
-                               manager_send_tone(bc,TONE_NONE);
+                               tone_indicate(ch,TONE_NONE);
 /*                             chan_misdn_log(1, bc->port, " --> * Starting Ast ctx:%s\n", ch->context);*/
                                if (ast_pbx_start(ch->ast)<0) {
 
                                        chan_misdn_log(-1, bc->port, "ast_pbx_start returned < 0 in INFO\n");
-                                       manager_send_tone(bc,TONE_BUSY);
+                                       tone_indicate(ch,TONE_BUSY);
 
                                        if (bc->nt)
                                                misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
@@ -3027,7 +3154,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        
                        chan_misdn_log(-1, bc->port, "Extension can never match, so disconnecting\n");
 
-                       manager_send_tone(bc,TONE_BUSY);
+                       tone_indicate(ch,TONE_BUSY);
                        ch->state=MISDN_EXTCANTMATCH;
                        bc->out_cause=1;
 
@@ -3041,7 +3168,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                if (ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
                        ch->state=MISDN_DIALING;
                        
-                       if (bc->nt) {
+                       if (bc->nt || (bc->need_more_infos && misdn_lib_is_ptp(bc->port)) ) {
                                int ret; 
                                ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
                        } else {
@@ -3053,7 +3180,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 
                                chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n");
                                chan=NULL;
-                               manager_send_tone(bc,TONE_BUSY);
+                               tone_indicate(ch,TONE_BUSY);
 
                                if (bc->nt)
                                        misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
@@ -3073,9 +3200,9 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        int stop_tone;
                        misdn_cfg_get( 0, MISDN_GEN_STOP_TONE, &stop_tone, sizeof(int));
                        if ( (!ast_strlen_zero(bc->dad)) && stop_tone ) 
-                               manager_send_tone(bc,TONE_NONE);
+                               tone_indicate(ch,TONE_NONE);
                        else
-                               manager_send_tone(bc,TONE_DIAL);
+                               tone_indicate(ch,TONE_DIAL);
        
                        ch->state=MISDN_WAITING4DIGS;
                }
@@ -3116,6 +3243,8 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                     misdn_inband_avail(bc) ) {
                        start_bc_tones(ch);
                }
+
+               ast_queue_control(ch->ast, AST_CONTROL_PROCEEDING);
        }
        break;
        case EVENT_PROGRESS:
@@ -3143,8 +3272,8 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                cb_log(1,bc->port,"Set State Ringing\n");
                
                if ( misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) {
-                        start_bc_tones(ch);
-                }
+                       start_bc_tones(ch);
+               }
        }
        break;
        case EVENT_CONNECT:
@@ -3232,10 +3361,37 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                release_chan(bc);
        }
        break;
-       
-       case EVENT_BCHAN_DATA:
+
+
+       case EVENT_TONE_GENERATE:
        {
+               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,0,"TONE_GEN: len:%d\n");
+               
+               if (!ast->generator) break;
+               
+               tmp = ast->generatordata;
+               ast->generatordata = NULL;
+               generate = ast->generator->generate;
+               res = generate(ast, tmp, tone_len, tone_len);
+               ast->generatordata = tmp;
+               if (res) {
+                       ast_log(LOG_WARNING, "Auto-deactivating generator\n");
+                       ast_deactivate_generator(ast);
+               } else {
+                       bc->tone_cnt=0;
+               }
+               
+       }
+       break;
                
+       case EVENT_BCHAN_DATA:
+       {
                if ( !misdn_cap_is_speech(ch->bc->capability) || bc->nojitter) {
                        misdn_tx2ast_frm(ch, bc->bframe, bc->bframe_len );
                } else {
@@ -3273,7 +3429,6 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                                
                        }
                }
-               
        }
        break;
        case EVENT_TIMEOUT:
@@ -3332,6 +3487,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        break;
                }
 
+#if 0
                {
                        struct chan_list *holded_ch=find_holded(cl_te, bc);
                        if (holded_ch) {
@@ -3341,6 +3497,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                                break;
                        }
                }
+#endif
                
                if (AST_BRIDGED_P(ch->ast)){
                        ch->state = MISDN_HOLDED;
@@ -3400,12 +3557,6 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
  *******************************************/
 
 
-static int clearl3_true ( void ) {
-       int default_clearl3;
-       misdn_cfg_get( 0, MISDN_GEN_CLEAR_L3, &default_clearl3, sizeof(int));
-       return default_clearl3;
-}
-
 static int g_config_initialized=0;
 
 int load_module(void)
@@ -3442,7 +3593,10 @@ int load_module(void)
        ast_mutex_init(&cl_te_lock);
        ast_mutex_init(&release_lock_mutex);
 
+
+       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);
        
@@ -3451,7 +3605,6 @@ int load_module(void)
                        .cb_event = cb_events,
                        .cb_log = chan_misdn_log,
                        .cb_jb_empty = chan_misdn_jb_empty,
-                       .cb_clearl3_true = clearl3_true
                };
                if (misdn_lib_init(ports, &iface, NULL))
                        chan_misdn_log(0, 0, "No te ports initialized\n");
@@ -3722,14 +3875,12 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
       
                case 'h':
                        chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n");
+                       
                        if (strlen(tok) > 1 && tok[1]=='1') {
-                               chan_misdn_log(1, ch->bc->port, "SETOPT: Digital TRANS_DIGITAL\n");
-                               ch->bc->async=1;
-                               ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
-                       } else {
-                               ch->bc->async=0;
-                               ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
-                       }
+                               chan_misdn_log(1, ch->bc->port, "SETOPT: HDLC \n");
+                               ch->bc->hdlc=1;
+                       }  
+                       ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
                        break;
             
                case 's':
@@ -3838,6 +3989,8 @@ int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
 {
     int i, j, rp, wp;
 
+    if (!jb || ! data) return 0;
+
     ast_mutex_lock (&jb->mutexjb);
     
     wp=jb->wp;
@@ -4008,3 +4161,5 @@ void chan_misdn_log(int level, int port, char *tmpl, ...)
                fclose(fp);
        }
 }
+
+
index 317515c..05f3eab 100644 (file)
  * \ingroup channel_drivers
  */
 
-
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 
 #include "chan_misdn_config.h"
 
 #include <asterisk/lock.h>
 #include <asterisk/pbx.h>
 #include <asterisk/strings.h>
-
-
 #include <asterisk/utils.h>
+
 #define AST_LOAD_CFG ast_config_load
 #define AST_DESTROY_CFG ast_config_destroy
 
-#define DEF_ECHOCANCEL 128
-#define DEF_ECHOTRAINING 1
+#define NO_DEFAULT "<>"
+#define NONE 0
+
+#define GEN_CFG 1
+#define PORT_CFG 2
+#define NUM_GEN_ELEMENTS (sizeof(gen_spec) / sizeof(struct misdn_cfg_spec))
+#define NUM_PORT_ELEMENTS (sizeof(port_spec) / sizeof(struct misdn_cfg_spec))
+
+enum misdn_cfg_type {
+       MISDN_CTYPE_STR,
+       MISDN_CTYPE_INT,
+       MISDN_CTYPE_BOOL,
+       MISDN_CTYPE_BOOLINT,
+       MISDN_CTYPE_MSNLIST,
+       MISDN_CTYPE_ASTGROUP
+};
 
 struct msn_list {
        char *msn;
        struct msn_list *next;
 };
 
-struct port_config {
-       char *name;
-       int *rxgain;
-       int *txgain;
-       int *te_choose_channel;
-       char *context;
-       char *language;
-       char *musicclass;
-       char *callerid;
-       char *method;
-       int *dialplan;
-       int *localdialplan; 
-       char *nationalprefix;
-       char *internationalprefix;
-       int *pres;
-       int *always_immediate;
-       int *immediate;
-       int *senddtmf;
-       int *hold_allowed;
-       int *early_bconnect;
-       int *use_callingpres;
-       int *echocancel;
-       int *echocancelwhenbridged;
-       int *echotraining;
-       int *jitterbuffer;
-       int *jitterbuffer_upper_threshold;
-       struct msn_list *msn_list;
-       ast_group_t *callgroup;         /* Call group */
-       ast_group_t *pickupgroup;       /* Pickup group */
+union misdn_cfg_pt {
+       char *str;
+       int *num;
+       struct msn_list *ml;
+       ast_group_t *grp;
+       void *any;
+};
+
+struct misdn_cfg_spec {
+       char name[BUFFERSIZE];
+       enum misdn_cfg_elements elem;
+       enum misdn_cfg_type type;
+       char def[BUFFERSIZE];
+       int boolint_def;
+};
+
+static const struct misdn_cfg_spec port_spec[] = {
+       { "name", MISDN_CFG_GROUPNAME, MISDN_CTYPE_STR, "default", NONE },
+       { "rxgain", MISDN_CFG_RXGAIN, MISDN_CTYPE_INT, "0", NONE },
+       { "txgain", MISDN_CFG_TXGAIN, MISDN_CTYPE_INT, "0", NONE },
+       { "te_choose_channel", MISDN_CFG_TE_CHOOSE_CHANNEL, MISDN_CTYPE_BOOL, "no", NONE },
+       { "context", MISDN_CFG_CONTEXT, MISDN_CTYPE_STR, "default", NONE },
+       { "language", MISDN_CFG_LANGUAGE, MISDN_CTYPE_STR, "en", NONE },
+       { "musicclass", MISDN_CFG_MUSICCLASS, MISDN_CTYPE_STR, "default", NONE },
+       { "callerid", MISDN_CFG_CALLERID, MISDN_CTYPE_STR, "", NONE },
+       { "method", MISDN_CFG_METHOD, MISDN_CTYPE_STR, "standard", NONE },
+       { "dialplan", MISDN_CFG_DIALPLAN, MISDN_CTYPE_INT, "0", NONE },
+       { "localdialplan", MISDN_CFG_LOCALDIALPLAN, MISDN_CTYPE_INT, "0", NONE },
+       { "nationalprefix", MISDN_CFG_NATPREFIX, MISDN_CTYPE_STR, "0", NONE },
+       { "internationalprefix", MISDN_CFG_INTERNATPREFIX, MISDN_CTYPE_STR, "00", NONE },
+       { "presentation", MISDN_CFG_PRES, MISDN_CTYPE_INT, "-1", NONE },
+       { "screen", MISDN_CFG_SCREEN, MISDN_CTYPE_INT, "-1", NONE },
+       { "always_immediate", MISDN_CFG_ALWAYS_IMMEDIATE, MISDN_CTYPE_BOOL, "no", NONE },
+       { "immediate", MISDN_CFG_IMMEDIATE, MISDN_CTYPE_BOOL, "no", NONE },
+       { "senddtmf", MISDN_CFG_SENDDTMF, MISDN_CTYPE_BOOL, "no", NONE },
+       { "hold_allowed", MISDN_CFG_HOLD_ALLOWED, MISDN_CTYPE_BOOL, "no", NONE },
+       { "early_bconnect", MISDN_CFG_EARLY_BCONNECT, MISDN_CTYPE_BOOL, "yes", NONE },
+       { "echocancel", MISDN_CFG_ECHOCANCEL, MISDN_CTYPE_BOOLINT, "0", 128 },
+       { "echocancelwhenbridged", MISDN_CFG_ECHOCANCELWHENBRIDGED, MISDN_CTYPE_BOOL, "no", NONE },
+       { "echotraining", MISDN_CFG_ECHOTRAINING, MISDN_CTYPE_BOOLINT, "1", 1 },
+       { "need_more_infos", MISDN_CFG_NEED_MORE_INFOS, MISDN_CTYPE_BOOL, "0", NONE },
+       { "jitterbuffer", MISDN_CFG_JITTERBUFFER, MISDN_CTYPE_INT, "0", NONE },
+       { "jitterbuffer_upper_threshold", MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, MISDN_CTYPE_INT, "0", NONE },
+       { "callgroup", MISDN_CFG_CALLGROUP, MISDN_CTYPE_ASTGROUP, NO_DEFAULT, NONE },
+       { "pickupgroup", MISDN_CFG_PICKUPGROUP, MISDN_CTYPE_ASTGROUP, NO_DEFAULT, NONE },
+       { "msns", MISDN_CFG_MSNS, MISDN_CTYPE_MSNLIST, NO_DEFAULT, NONE }
 };
 
-struct general_config {
-       int *debug;
-       char *tracefile;
-       int *trace_calls;
-       char *trace_dir;
-       int *bridging;
-       int *stop_tone_after_first_digit;
-       int *append_digits2exten;
-       int *l1_info_ok;
-       int *clear_l3;
-       int *dynamic_crypt;
-       char *crypt_prefix;
-       char *crypt_keys;
+static const struct misdn_cfg_spec gen_spec[] = {
+       { "debug", MISDN_GEN_DEBUG, MISDN_CTYPE_INT, "0", NONE },
+       { "misdn_init", MISDN_GEN_MISDN_INIT, MISDN_CTYPE_STR, "/etc/misdn-init.conf", NONE },
+       { "tracefile", MISDN_GEN_TRACEFILE, MISDN_CTYPE_STR, "/var/log/misdn.trace", NONE },
+       { "bridging", MISDN_GEN_BRIDGING, MISDN_CTYPE_BOOL, "yes", NONE },
+       { "stop_tone_after_first_digit", MISDN_GEN_STOP_TONE, MISDN_CTYPE_BOOL, "yes", NONE },
+       { "append_digits2exten", MISDN_GEN_APPEND_DIGITS2EXTEN, MISDN_CTYPE_BOOL, "yes", NONE },
+       { "dynamic_crypt", MISDN_GEN_DYNAMIC_CRYPT, MISDN_CTYPE_BOOL, "no", NONE },
+       { "crypt_prefix", MISDN_GEN_CRYPT_PREFIX, MISDN_CTYPE_STR, NO_DEFAULT, NONE },
+       { "crypt_keys", MISDN_GEN_CRYPT_KEYS, MISDN_CTYPE_STR, NO_DEFAULT, NONE }
 };
 
 /* array of port configs, default is at position 0. */
-static struct port_config **port_cfg;
+static union misdn_cfg_pt **port_cfg;
 /* max number of available ports, is set on init */
 static int max_ports;
 /* general config */
-static struct general_config *general_cfg;
+static union misdn_cfg_pt *general_cfg;
 /* storing the ptp flag separated to save memory */
 static int *ptp;
+/* maps enum config elements to array positions */
+static int *map;
 
 static ast_mutex_t config_mutex; 
 
+#define CLI_ERROR(name, value, section) ({ \
+       ast_log(LOG_WARNING, "misdn.conf: \"%s=%s\" (section: %s) invalid or out of range. " \
+               "Please edit your misdn.conf and then do a \"misdn reload\".\n", name, value, section); \
+})
+
+static void _enum_array_map (void)
+{
+       int i, j;
+
+       for (i = MISDN_CFG_FIRST + 1; i < MISDN_CFG_LAST; ++i) {
+               if (i == MISDN_CFG_PTP)
+                       continue;
+               for (j = 0; j < NUM_PORT_ELEMENTS; ++j) {
+                       if (port_spec[j].elem == i) {
+                               map[i] = j;
+                               break;
+                       }
+               }
+       }
+       for (i = MISDN_GEN_FIRST + 1; i < MISDN_GEN_LAST; ++i) {
+               for (j = 0; j < NUM_GEN_ELEMENTS; ++j) {
+                       if (gen_spec[j].elem == i) {
+                               map[i] = j;
+                               break;
+                       }
+               }
+       }
+}
+
+static int get_cfg_position (char *name, int type)
+{
+       int i;
 
-static inline void misdn_cfg_lock (void) {
+       switch (type) {
+       case PORT_CFG:
+               for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
+                       if (!strcasecmp(name, port_spec[i].name))
+                               return i;
+               }
+               break;
+       case GEN_CFG:
+               for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
+                       if (!strcasecmp(name, gen_spec[i].name))
+                               return i;
+               }
+       }
+
+       return -1;
+}
+
+static inline void misdn_cfg_lock (void)
+{
        ast_mutex_lock(&config_mutex);
 }
 
-static inline void misdn_cfg_unlock (void) {
+static inline void misdn_cfg_unlock (void)
+{
        ast_mutex_unlock(&config_mutex);
 }
 
-static void free_msn_list (struct msn_list* iter) {
+static void _free_msn_list (struct msn_list* iter)
+{
        if (iter->next)
-               free_msn_list(iter->next);
+               _free_msn_list(iter->next);
        if (iter->msn)
                free(iter->msn);
        free(iter);
 }
 
-static void free_port_cfg (void) {
-
-       struct port_config **free_list = (struct port_config **)calloc(max_ports + 1, sizeof(struct port_config *));
-
+static void _free_port_cfg (void)
+{
        int i, j;
-
-       for (i = 0; i < max_ports; i++) {
-               if (port_cfg[i]) {
-                       for (j = 0; j < max_ports && free_list[j]; j++) {
-                               if (free_list[j] && free_list[j] == port_cfg[i])
-                                       continue; /* already in list */
-                               free_list[j] = port_cfg[i];
+       int gn = map[MISDN_CFG_GROUPNAME];
+       union misdn_cfg_pt* free_list[max_ports + 1];
+       
+       memset(free_list, 0, sizeof(free_list));
+       free_list[0] = port_cfg[0];
+       for (i = 1; i <= max_ports; ++i) {
+               if (port_cfg[i][gn].str) {
+                       /* we always have a groupname in the non-default case, so this is fine */
+                       for (j = 1; j <= max_ports; ++j) {
+                               if (free_list[j] && free_list[j][gn].str == port_cfg[i][gn].str)
+                                       break;
+                               else if (!free_list[j]) {
+                                       free_list[j] = port_cfg[i];
+                                       break;
+                               }
                        }
                }
        }
-
-#define FREE_ELEM(elem) ({ \
-               if (free_list[i]->elem) \
-                       free(free_list[i]->elem); \
-       })
-       
-       for (i = 0; i < max_ports; i++) {
-               if (free_list[i]) {
-                       FREE_ELEM(name);
-                       FREE_ELEM(rxgain);
-                       FREE_ELEM(txgain);
-                       FREE_ELEM(te_choose_channel);
-                       FREE_ELEM(context);
-                       FREE_ELEM(language);
-                       FREE_ELEM(musicclass);
-                       FREE_ELEM(callerid);
-                       FREE_ELEM(method);
-                       FREE_ELEM(dialplan);
-                       FREE_ELEM(localdialplan);
-                       FREE_ELEM(nationalprefix);
-                       FREE_ELEM(internationalprefix);
-                       FREE_ELEM(pres);
-                       FREE_ELEM(always_immediate);
-                       FREE_ELEM(senddtmf);
-                       FREE_ELEM(immediate);
-                       FREE_ELEM(hold_allowed);
-                       FREE_ELEM(early_bconnect);
-                       FREE_ELEM(use_callingpres);
-                       FREE_ELEM(echocancel);
-                       FREE_ELEM(echocancelwhenbridged);
-                       FREE_ELEM(echotraining);
-                       FREE_ELEM(jitterbuffer);
-                       FREE_ELEM(jitterbuffer_upper_threshold);
-                       if (free_list[i]->msn_list)
-                               free_msn_list(free_list[i]->msn_list);
-                       FREE_ELEM(callgroup);
-                       FREE_ELEM(pickupgroup);
-                       free(free_list[i]);
+       for (j = 0; free_list[j]; ++j) {
+               for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
+                       if (free_list[j][i].any) {
+                               if (port_spec[i].type == MISDN_CTYPE_MSNLIST)
+                                       _free_msn_list(free_list[j][i].ml);
+                               else
+                                       free(free_list[j][i].any);
+                       }
                }
        }
-       free(free_list);
 }
 
-static void free_general_cfg (void) {
+static void _free_general_cfg (void)
+{
+       int i;
 
-#define FREE_GEN_ELEM(elem) ({ \
-               if (general_cfg->elem) \
-                       free(general_cfg->elem); \
-       })
-       
-       FREE_GEN_ELEM(debug);
-       FREE_GEN_ELEM(tracefile);
-       FREE_GEN_ELEM(trace_calls);
-       FREE_GEN_ELEM(trace_dir);
-       FREE_GEN_ELEM(bridging);
-       FREE_GEN_ELEM(stop_tone_after_first_digit);
-       FREE_GEN_ELEM(append_digits2exten);
-       FREE_GEN_ELEM(l1_info_ok);
-       FREE_GEN_ELEM(clear_l3);
-       FREE_GEN_ELEM(dynamic_crypt);
-       FREE_GEN_ELEM(crypt_prefix);
-       FREE_GEN_ELEM(crypt_keys);
+       for (i = 0; i < NUM_GEN_ELEMENTS; i++) 
+               if (general_cfg[i].any)
+                       free(general_cfg[i].any);
 }
 
-#define GET_PORTCFG_STRCPY(item) ({ \
-               char *temp; \
-               if (port_cfg[port] && port_cfg[port]->item) \
-                       temp = port_cfg[port]->item; \
-               else \
-                       temp = port_cfg[0]->item; \
-               if (!temp || !memccpy((void *)buf, (void *)temp, '\0', bufsize)) \
-                       memset(buf, 0, 1); \
-       })
-
-#define GET_GENCFG_STRCPY(item) ({ \
-               if (general_cfg && general_cfg->item) { \
-                       if (!memccpy((void *)buf, (void *)general_cfg->item, '\0', bufsize)) \
-                               memset(buf, 0, 1); \
-               } else \
-                       memset(buf, 0, 1); \
-       })
-
-#define GET_PORTCFG_MEMCPY(item) ({ \
-               typeof(port_cfg[0]->item) temp; \
-               if (port_cfg[port] && port_cfg[port]->item) \
-                       temp = port_cfg[port]->item; \
-               else \
-                       temp = port_cfg[0]->item; \
-               if (temp) { \
-                       int l = sizeof(*temp); \
-                       if (l > bufsize) \
-                               memset(buf, 0, bufsize); \
-                       else \
-                               memcpy(buf, temp, l); \
-               } else \
-                       memset(buf, 0, bufsize); \
-       })
-
-#define GET_GENCFG_MEMCPY(item) ({ \
-               if (general_cfg && general_cfg->item) { \
-                       typeof(general_cfg->item) temp = general_cfg->item; \
-                       int l = sizeof(*temp); \
-                       if (l > bufsize) \
-                               memset(buf, 0, bufsize); \
-                       else \
-                               memcpy(buf, temp, l); \
-               } else \
-                       memset(buf, 0, bufsize); \
-       })
-
-void misdn_cfg_get(int port, enum misdn_cfg_elements elem, void *buf, int bufsize) {
-       
-       if (!(elem > MISDN_GEN_FIRST) && !misdn_cfg_is_port_valid(port)) {
+void misdn_cfg_get (int port, enum misdn_cfg_elements elem, void *buf, int bufsize)
+{
+       int place;
+
+       if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
                memset(buf, 0, bufsize);
                ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port);
                return;
        }
 
        misdn_cfg_lock();
-       
-       switch (elem) {
-               
-               /* port config elements */
-                       
-               case MISDN_CFG_PTP:             if (sizeof(ptp[port]) <= bufsize)
-                                                       memcpy(buf, &ptp[port], sizeof(ptp[port]));
-                                               else
-                                                       buf = 0; /* error, should not happen */
-                                               break;
-               case MISDN_CFG_GROUPNAME:       GET_PORTCFG_STRCPY(name);
-                                               break;
-               case MISDN_CFG_RXGAIN:          GET_PORTCFG_MEMCPY(rxgain);
-                                               break;
-               case MISDN_CFG_TXGAIN:          GET_PORTCFG_MEMCPY(txgain);
-                       break;
-               case MISDN_CFG_JITTERBUFFER:    GET_PORTCFG_MEMCPY(jitterbuffer);
-                       break;
-               case MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD:    GET_PORTCFG_MEMCPY(jitterbuffer_upper_threshold);
-                       break;
-case MISDN_CFG_TE_CHOOSE_CHANNEL:
-                                               GET_PORTCFG_MEMCPY(te_choose_channel);
-                                               break;
-               case MISDN_CFG_CONTEXT:         GET_PORTCFG_STRCPY(context);
-                                               break;
-               case MISDN_CFG_LANGUAGE:        GET_PORTCFG_STRCPY(language);
-                       break;
-       case MISDN_CFG_MUSICCLASS:      GET_PORTCFG_STRCPY(musicclass);
-               break;
-       case MISDN_CFG_CALLERID:        GET_PORTCFG_STRCPY(callerid);
-                                               break;
-               case MISDN_CFG_METHOD:          GET_PORTCFG_STRCPY(method);
-                                               break;
-               case MISDN_CFG_DIALPLAN:        GET_PORTCFG_MEMCPY(dialplan);
-                                               break;
-       case MISDN_CFG_LOCALDIALPLAN:   GET_PORTCFG_MEMCPY(localdialplan);
-               break;
-               case MISDN_CFG_NATPREFIX:       GET_PORTCFG_STRCPY(nationalprefix);
-                                               break;
-               case MISDN_CFG_INTERNATPREFIX:
-                                               GET_PORTCFG_STRCPY(internationalprefix);
-                                               break;
-               case MISDN_CFG_PRES:            GET_PORTCFG_MEMCPY(pres);
-                                               break;
-       case MISDN_CFG_ALWAYS_IMMEDIATE:
-                                               GET_PORTCFG_MEMCPY(always_immediate);
-                                               break;
-       case MISDN_CFG_SENDDTMF:
-               GET_PORTCFG_MEMCPY(senddtmf);
-               break;
-               case MISDN_CFG_IMMEDIATE:       GET_PORTCFG_MEMCPY(immediate);
-                                               break;
-               case MISDN_CFG_HOLD_ALLOWED:
-                                               GET_PORTCFG_MEMCPY(hold_allowed);
-                                               break;
-               case MISDN_CFG_EARLY_BCONNECT:
-                                               GET_PORTCFG_MEMCPY(early_bconnect);
-                                               break;
-               case MISDN_CFG_USE_CALLINGPRES:
-                                               GET_PORTCFG_MEMCPY(use_callingpres);
-                                               break;
-               case MISDN_CFG_ECHOCANCEL:
-                                               GET_PORTCFG_MEMCPY(echocancel );
-                                               break;
-               case MISDN_CFG_ECHOCANCELWHENBRIDGED:
-                                               GET_PORTCFG_MEMCPY(echocancelwhenbridged);
-                                               break;
-               case MISDN_CFG_ECHOTRAINING:
-                                               GET_PORTCFG_MEMCPY(echotraining);
-                                               break;
-               case MISDN_CFG_CALLGROUP:       GET_PORTCFG_MEMCPY(callgroup);
-                                               break;
-               case MISDN_CFG_PICKUPGROUP:     GET_PORTCFG_MEMCPY(pickupgroup);
-                                               break;
-               
-               /* general config elements */
-                       
-               case MISDN_GEN_DEBUG:           GET_GENCFG_MEMCPY(debug);
-                                               break;
-               case MISDN_GEN_TRACEFILE:       GET_GENCFG_STRCPY(tracefile);
-                                               break;
-               case MISDN_GEN_TRACE_CALLS: GET_GENCFG_MEMCPY(trace_calls);
-                                               break;
-               case MISDN_GEN_TRACE_DIR:       GET_GENCFG_STRCPY(trace_dir);
-                                               break;
-               case MISDN_GEN_BRIDGING:        GET_GENCFG_MEMCPY(bridging);
-                                               break;
-               case MISDN_GEN_STOP_TONE:       GET_GENCFG_MEMCPY(stop_tone_after_first_digit);
-                                               break;
-               case MISDN_GEN_APPEND_DIGITS2EXTEN: 
-                                               GET_GENCFG_MEMCPY(append_digits2exten);
-                                               break;
-               case MISDN_GEN_L1_INFO_OK:      GET_GENCFG_MEMCPY(l1_info_ok);
-                                               break;
-               case MISDN_GEN_CLEAR_L3:        GET_GENCFG_MEMCPY(clear_l3);
-                                               break;
-               case MISDN_GEN_DYNAMIC_CRYPT:   GET_GENCFG_MEMCPY(dynamic_crypt);
-                                               break;
-               case MISDN_GEN_CRYPT_PREFIX:    GET_GENCFG_STRCPY(crypt_prefix);
-                                               break;
-               case MISDN_GEN_CRYPT_KEYS:      GET_GENCFG_STRCPY(crypt_keys);
-                                               break;
-               default:                        memset(buf, 0, bufsize);
+       if (elem == MISDN_CFG_PTP) {
+               if (!memcpy(buf, &ptp[port], (bufsize > ptp[port]) ? sizeof(ptp[port]) : bufsize))
+                       memset(buf, 0, bufsize);
+       } else {
+               if ((place = map[elem]) < 0) {
+                       memset (buf, 0, bufsize);
+                       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Invalid element (%d) requested.\n", elem);
+               } else {
+                       if (elem < MISDN_CFG_LAST) {
+                               switch (port_spec[place].type) {
+                               case MISDN_CTYPE_STR:
+                                       if (port_cfg[port][place].str) {
+                                               if (!memccpy(buf, port_cfg[port][place].str, 0, bufsize))
+                                                       memset(buf, 0, 1);
+                                       } else if (port_cfg[0][place].str) {
+                                               if (!memccpy(buf, port_cfg[0][place].str, 0, bufsize))
+                                                       memset(buf, 0, 1);
+                                       }
+                                       break;
+                               default:
+                                       if (port_cfg[port][place].any)
+                                               memcpy(buf, port_cfg[port][place].any, bufsize);
+                                       else if (port_cfg[0][place].any)
+                                               memcpy(buf, port_cfg[0][place].any, bufsize);
+                                       else
+                                               memset(buf, 0, bufsize);
+                               }
+                       } else {
+                               switch (gen_spec[place].type) {
+                               case MISDN_CTYPE_STR:
+                                       if (!general_cfg[place].str || !memccpy(buf, general_cfg[place].str, 0, bufsize))
+                                               memset(buf, 0, 1);
+                                       break;
+                               default:
+                                       if (general_cfg[place].any)
+                                               memcpy(buf, general_cfg[place].any, bufsize);
+                                       else
+                                               memset(buf, 0, bufsize);
+                               }
+                       }
+               }
        }
-       
        misdn_cfg_unlock();
 }
 
-int misdn_cfg_is_msn_valid (int port, char* msn) {
-       
+int misdn_cfg_is_msn_valid (int port, char* msn)
+{
+       int re = 0;
+       struct msn_list *iter;
+
        if (!misdn_cfg_is_port_valid(port)) {
                ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port);
                return 0;
        }
-       
-       struct msn_list *iter;
-       
+
        misdn_cfg_lock();
-       
-       if (port_cfg[port]->msn_list)
-               iter = port_cfg[port]->msn_list;
+       if (port_cfg[port][MISDN_CFG_MSNS-1].ml)
+               iter = port_cfg[port][MISDN_CFG_MSNS-1].ml;
        else
-               iter = port_cfg[0]->msn_list;
+               iter = port_cfg[0][MISDN_CFG_MSNS-1].ml;
        for (; iter; iter = iter->next) 
                if (*(iter->msn) == '*' || ast_extension_match(iter->msn, msn)) {
-                       misdn_cfg_unlock();
-                       return 1;
+                       re = 1;
+                       break;
                }
-       
-       misdn_cfg_unlock();
-       
-       return 0;
-}
-
-int misdn_cfg_is_port_valid (int port) {
-       
-       misdn_cfg_lock();
-       
-       if (port < 1 || port > max_ports) {
-               misdn_cfg_unlock();
-               return 0;
-       }
-
-       int valid = (port_cfg[port] != NULL);
-
        misdn_cfg_unlock();
 
-       return valid;
+       return re;
 }
 
-int misdn_cfg_is_group_method (char *group, enum misdn_cfg_method meth) {
+int misdn_cfg_is_port_valid (int port)
+{
+       return (port >= 1 && port <= max_ports);
+}
 
+int misdn_cfg_is_group_method (char *group, enum misdn_cfg_method meth)
+{
        int i, re = 0;
        char *method = NULL;
 
        misdn_cfg_lock();
-       
-       for (i = 0; i < max_ports; i++) {
-               if (port_cfg[i]) {
-                       if (!strcasecmp(port_cfg[i]->name, group))
-                               method = port_cfg[i]->method ? port_cfg[i]->method : port_cfg[0]->method;
+       for (i = 1; i <= max_ports; i++) {
+               if (port_cfg[i] && port_cfg[i][map[MISDN_CFG_GROUPNAME]].str) {
+                       if (!strcasecmp(port_cfg[i][map[MISDN_CFG_GROUPNAME]].str, group))
+                               method = (port_cfg[i][map[MISDN_CFG_METHOD]].str ? 
+                                                 port_cfg[i][map[MISDN_CFG_METHOD]].str : port_cfg[0][map[MISDN_CFG_METHOD]].str);
                }
        }
-
        if (method) {
                switch (meth) {
                case METHOD_STANDARD:           re = !strcasecmp(method, "standard");
@@ -433,22 +357,22 @@ int misdn_cfg_is_group_method (char *group, enum misdn_cfg_method meth) {
                                                                        break;
                }
        }
-
        misdn_cfg_unlock();
 
        return re;
 }
 
-void misdn_cfg_get_ports_string (char *ports) {
-       *ports = 0;
+void misdn_cfg_get_ports_string (char *ports)
+{
        char tmp[16];
-       int l;
-       
-       misdn_cfg_lock();
+       int l, i;
+       int gn = map[MISDN_CFG_GROUPNAME];
+
+       *ports = 0;
 
-       int i = 1;
-       for (; i <= max_ports; i++) {
-               if (port_cfg[i]) {
+       misdn_cfg_lock();
+       for (i = 1; i <= max_ports; i++) {
+               if (port_cfg[i][gn].str) {
                        if (ptp[i])
                                sprintf(tmp, "%dptp,", i);
                        else
@@ -456,583 +380,331 @@ void misdn_cfg_get_ports_string (char *ports) {
                        strcat(ports, tmp);
                }
        }
-       
        misdn_cfg_unlock();
 
        if ((l = strlen(ports)))
                ports[l-1] = 0;
 }
 
-#define GET_CFG_STRING(typestr, type) ({ \
-               if (port_cfg[port] && port_cfg[port]->type) \
-                       snprintf(buf, bufsize, "%s " #typestr ": %s", begin, port_cfg[port]->type); \
-               else \
-                       snprintf(buf, bufsize, "%s " #typestr ": %s", begin, port_cfg[0]->type); \
-       }) \
-
-#define GET_GEN_STRING(typestr, type) ({ \
-               snprintf(buf, bufsize, "%s " #typestr ": %s", begin, general_cfg->type ? general_cfg->type : "not set"); \
-       }) \
-
-#define GET_CFG_INT(typestr, type) ({ \
-               if (port_cfg[port] && port_cfg[port]->type) \
-                       snprintf(buf, bufsize, "%s " #typestr ": %d", begin, *port_cfg[port]->type); \
-               else \
-                       snprintf(buf, bufsize, "%s " #typestr ": %d", begin, *port_cfg[0]->type); \
-       }) \
-
-#define GET_GEN_INT(typestr, type) ({ \
-               snprintf(buf, bufsize, "%s " #typestr ": %d", begin, general_cfg->type ? *general_cfg->type : 0); \
-       }) \
-
-#define GET_CFG_BOOL(typestr, type, yes, no) ({ \
-               int bool; \
-               if (port_cfg[port] && port_cfg[port]->type) \
-                       bool = *port_cfg[port]->type; \
-               else \
-                       bool = *port_cfg[0]->type; \
-               snprintf(buf, bufsize, "%s " #typestr ": %s", begin, bool ? #yes : #no); \
-       }) \
-
-#define GET_CFG_HYBRID(typestr, type, yes, no) ({ \
-               int bool; \
-               if (port_cfg[port] && port_cfg[port]->type) \
-                       bool = *port_cfg[port]->type; \
-               else \
-                       bool = *port_cfg[0]->type; \
-               if (bool == 1 || bool == 0) \
-                       snprintf(buf, bufsize, "%s " #typestr ": %s", begin, bool ? #yes : #no); \
-               else \
-                       snprintf(buf, bufsize, "%s " #typestr ": %d", begin, bool); \
-       }) \
-
-#define GET_GEN_BOOL(typestr, type, yes, no) ({ \
-               snprintf(buf, bufsize, "%s " #typestr ": %s", begin, general_cfg->type ? (*general_cfg->type ? #yes : #no) : "not set"); \
-       }) \
-
-#define GET_CFG_AST_GROUP_T(typestr, type) ({ \
-               ast_group_t *tmp; \
-               if (port_cfg[port] && port_cfg[port]->type) \
-                       tmp = port_cfg[port]->type; \
-               else \
-                       tmp = port_cfg[0]->type; \
-               if (tmp) { \
-                       char tmpbuf[256]; \
-                       snprintf(buf, bufsize, "%s " #typestr ": %s", begin, ast_print_group(tmpbuf, sizeof(tmpbuf), *tmp)); \
-               } else \
-                       snprintf(buf, bufsize, "%s " #typestr ": %s", begin, "none"); \
-       }) \
-
-void misdn_cfg_get_config_string(int port, enum misdn_cfg_elements elem, char* buf, int bufsize) {
-
-       if (!(elem > MISDN_GEN_FIRST) && !misdn_cfg_is_port_valid(port)) {
+void misdn_cfg_get_config_string (int port, enum misdn_cfg_elements elem, char* buf, int bufsize)
+{
+       int place;
+       char tempbuf[BUFFERSIZE] = "";
+       struct msn_list *iter;
+
+       if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
                *buf = 0;
                ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port);
                return;
        }
-       
-       char begin[] = " -> ";
-       
+
+       place = map[elem];
+
        misdn_cfg_lock();
-       
-       switch (elem) {
-               
-               case MISDN_CFG_PTP:             snprintf(buf, bufsize, "%s PTP: %s", begin, ptp[port] ? "yes" : "no");
-                                                                       break;
-               case MISDN_CFG_GROUPNAME:       GET_CFG_STRING(GROUPNAME, name);
-                                                                       break;
-               case MISDN_CFG_RXGAIN:          GET_CFG_INT(RXGAIN, rxgain);
+       if (elem == MISDN_CFG_PTP) {
+               snprintf(buf, bufsize, " -> ptp: %s", ptp[port] ? "yes" : "no");
+       }
+       else if (elem > MISDN_CFG_FIRST && elem < MISDN_CFG_LAST) {
+               switch (port_spec[place].type) {
+               case MISDN_CTYPE_INT:
+               case MISDN_CTYPE_BOOLINT:
+                       if (port_cfg[port][place].num)
+                               snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[port][place].num);
+                       else if (port_cfg[0][place].num)
+                               snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[0][place].num);
+                       else
+                               snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
                        break;
-       case MISDN_CFG_TXGAIN:          GET_CFG_INT(TXGAIN, txgain);
-                                                                       break;
-       case MISDN_CFG_JITTERBUFFER:            GET_CFG_INT(JITTERBUFFER, jitterbuffer);
-                                                                       break;
-                                                                               case MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD:            GET_CFG_INT(JITTERBUFFER_UPPER_THRESHOLD, jitterbuffer_upper_threshold);
-                                                                       break;
-               case MISDN_CFG_TE_CHOOSE_CHANNEL:
-                                               GET_CFG_BOOL(TE_CHOOSE_CHANNEL, te_choose_channel, yes, no);
-                                                                       break;
-               case MISDN_CFG_CONTEXT:         GET_CFG_STRING(CONTEXT, context);
-                                                                       break;
-               case MISDN_CFG_LANGUAGE:        GET_CFG_STRING(LANGUAGE, language);
-                                                                       break;
-       case MISDN_CFG_MUSICCLASS:      GET_CFG_STRING(MUSICCLASS, musicclass);
-                                                                                               break;
-               case MISDN_CFG_CALLERID:        GET_CFG_STRING(CALLERID, callerid);
-                                                                       break;
-               case MISDN_CFG_METHOD:          GET_CFG_STRING(METHOD, method);
-                                                                       break;
-               case MISDN_CFG_DIALPLAN:        GET_CFG_INT(DIALPLAN, dialplan);
-                                                                       break;
-       case MISDN_CFG_LOCALDIALPLAN:   GET_CFG_INT(LOCALDIALPLAN, localdialplan);
-               break;
-               case MISDN_CFG_NATPREFIX:       GET_CFG_STRING(NATIONALPREFIX, nationalprefix);
-                                                                       break;
-               case MISDN_CFG_INTERNATPREFIX:
-                                               GET_CFG_STRING(INTERNATIONALPREFIX, internationalprefix);
-                                                                       break;
-               case MISDN_CFG_PRES:            GET_CFG_BOOL(PRESENTATION, pres, allowed, not_screened);
-                                                                       break;
-       case MISDN_CFG_ALWAYS_IMMEDIATE:
-                                               GET_CFG_BOOL(ALWAYS_IMMEDIATE, always_immediate, yes, no);
-                                                                       break;
-       case MISDN_CFG_SENDDTMF:
-               GET_CFG_BOOL(SENDDTMF, senddtmf, yes, no);
-               break;
-               case MISDN_CFG_IMMEDIATE:       GET_CFG_BOOL(IMMEDIATE, immediate, yes, no);
-                                                                       break;
-               case MISDN_CFG_HOLD_ALLOWED:
-                                               GET_CFG_BOOL(HOLD_ALLOWED, hold_allowed, yes, no);
-                                                                       break;
-               case MISDN_CFG_EARLY_BCONNECT:
-                                               GET_CFG_BOOL(EARLY_BCONNECT, early_bconnect, yes, no);
-                                                                       break;
-               case MISDN_CFG_USE_CALLINGPRES:
-                                               GET_CFG_BOOL(USE_CALLINGPRES, use_callingpres, yes, no);
-                                                                       break;
-               case MISDN_CFG_ECHOCANCEL:      GET_CFG_HYBRID(ECHOCANCEL, echocancel, yes, no);
-                                                                       break;
-               case MISDN_CFG_ECHOCANCELWHENBRIDGED:
-                                               GET_CFG_BOOL(ECHOCANCELWHENBRIDGED, echocancelwhenbridged, yes, no);
-                                                                       break;
-               case MISDN_CFG_ECHOTRAINING:
-                                               GET_CFG_HYBRID(ECHOTRAINING, echotraining, yes, no);
-                                                                       break;
-               case MISDN_CFG_CALLGROUP:       GET_CFG_AST_GROUP_T(CALLINGGROUP, callgroup);
-                                                                       break;
-               case MISDN_CFG_PICKUPGROUP:     GET_CFG_AST_GROUP_T(PICKUPGROUP, pickupgroup);
-                                                                       break;
-               case MISDN_CFG_MSNS:            {
-                                                       char tmpbuffer[BUFFERSIZE];
-                                                       tmpbuffer[0] = 0;
-                                                       struct msn_list *iter;
-                                                       if (port_cfg[port]->msn_list)
-                                                               iter = port_cfg[port]->msn_list;
-                                                       else
-                                                               iter = port_cfg[0]->msn_list;
-                                                       if (iter) {
-                                                               for (; iter; iter = iter->next)
-                                                                       sprintf(tmpbuffer, "%s%s, ", tmpbuffer, iter->msn);
-                                                               tmpbuffer[strlen(tmpbuffer)-2] = 0;
-                                                       }
-                                                       snprintf(buf, bufsize, "%s MSNs: %s", begin, *tmpbuffer ? tmpbuffer : "none"); \
-                                               }
-                                               break;
-               
-               /* general config elements */
-               
-               case MISDN_GEN_DEBUG:           GET_GEN_INT(DEBUG_LEVEL, debug);
-                                                                       break;
-               case MISDN_GEN_TRACEFILE:       GET_GEN_STRING(TRACEFILE, tracefile);
-                                                                       break;
-               case MISDN_GEN_TRACE_CALLS: GET_GEN_BOOL(TRACE_CALLS, trace_calls, true, false);
-                                                                       break;
-               case MISDN_GEN_TRACE_DIR:       GET_GEN_STRING(TRACE_DIR, trace_dir);
-                                                                       break;
-               case MISDN_GEN_BRIDGING:        GET_GEN_BOOL(BRIDGING, bridging, yes, no);
-                                                                       break;
-               case MISDN_GEN_STOP_TONE:       GET_GEN_BOOL(STOP_TONE_AFTER_FIRST_DIGIT, stop_tone_after_first_digit, yes, no);
-                                                                       break;
-               case MISDN_GEN_APPEND_DIGITS2EXTEN: 
-                                               GET_GEN_BOOL(APPEND_DIGITS2EXTEN, append_digits2exten, yes, no);
-                                                                       break;
-               case MISDN_GEN_L1_INFO_OK:      GET_GEN_BOOL(L1_INFO_OK, l1_info_ok, yes, no);
-                                                                       break;
-               case MISDN_GEN_CLEAR_L3:        GET_GEN_BOOL(CLEAR_L3, clear_l3, yes, no);
-                                                                       break;
-               case MISDN_GEN_DYNAMIC_CRYPT:
-                                               GET_GEN_BOOL(DYNAMIC_CRYPT,dynamic_crypt, yes, no);
-                                                                       break;
-               case MISDN_GEN_CRYPT_PREFIX:
-                                               GET_GEN_STRING(CRYPT_PREFIX, crypt_prefix);
-                                                                       break;
-               case MISDN_GEN_CRYPT_KEYS:      GET_GEN_STRING(CRYPT_KEYS, crypt_keys);
-                                                                       break;
-                                                                       
-               default:                        *buf = 0;
-                                                                       break;
+               case MISDN_CTYPE_BOOL:
+                       if (port_cfg[port][place].num)
+                               snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[port][place].num ? "yes" : "no");
+                       else if (port_cfg[0][place].num)
+                               snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[0][place].num ? "yes" : "no");
+                       else
+                               snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
+                       break;
+               case MISDN_CTYPE_ASTGROUP:
+                       if (port_cfg[port][place].grp)
+                               snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, 
+                                                ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[port][place].grp));
+                       else if (port_cfg[0][place].grp)
+                               snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, 
+                                                ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[0][place].grp));
+                       else
+                               snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
+                       break;
+               case MISDN_CTYPE_MSNLIST:
+                       if (port_cfg[port][place].ml)
+                               iter = port_cfg[port][place].ml;
+                       else
+                               iter = port_cfg[0][place].ml;
+                       if (iter) {
+                               for (; iter; iter = iter->next)
+                                       sprintf(tempbuf, "%s%s, ", tempbuf, iter->msn);
+                               tempbuf[strlen(tempbuf)-2] = 0;
+                       }
+                       snprintf(buf, bufsize, " -> msns: %s", *tempbuf ? tempbuf : "none");
+                       break;
+               case MISDN_CTYPE_STR:
+                       if ( port_cfg[port][place].str) {
+                               snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[port][place].str);
+                       } else if (port_cfg[0][place].str) {
+                               snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[0][place].str);
+                       } else {
+                               snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
+                       }
+                       break;
+               }
+       } else if (elem > MISDN_GEN_FIRST && elem < MISDN_GEN_LAST) {
+               switch (gen_spec[place].type) {
+               case MISDN_CTYPE_INT:
+               case MISDN_CTYPE_BOOLINT:
+                       if (general_cfg[place].num)
+                               snprintf(buf, bufsize, " -> %s: %d", gen_spec[place].name, *general_cfg[place].num);
+                       else
+                               snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
+                       break;
+               case MISDN_CTYPE_BOOL:
+                       if (general_cfg[place].num)
+                               snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, *general_cfg[place].num ? "yes" : "no");
+                       else
+                               snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
+                       break;
+               case MISDN_CTYPE_STR:
+                       if ( general_cfg[place].str) {
+                               snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, general_cfg[place].str);
+                       } else {
+                               snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
+                       }
+                       break;
+               default:
+                       snprintf(buf, bufsize, " -> type of %s not handled yet", gen_spec[place].name);
+                       break;
+               }
+       } else {
+               *buf = 0;
+               ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Invalid config element (%d) requested.\n", elem);
        }
-
        misdn_cfg_unlock();
 }
 
-int misdn_cfg_get_next_port (int port) {
+int misdn_cfg_get_next_port (int port)
+{
+       int p = -1;
+       int gn = map[MISDN_CFG_GROUPNAME];
        
        misdn_cfg_lock();
-       
        for (port++; port <= max_ports; port++) {
-               if (port_cfg[port]) {
-                       misdn_cfg_unlock();
-                       return port;
+               if (port_cfg[port][gn].str) {
+                       p = port;
+                       break;
                }
        }
-       
        misdn_cfg_unlock();
-       
-       return -1;
-}
 
-int misdn_cfg_get_next_port_spin (int port) {
+       return p;
+}
 
-       int ret = misdn_cfg_get_next_port(port);
+int misdn_cfg_get_next_port_spin (int port)
+{
+       int p = misdn_cfg_get_next_port(port);
+       return (p > 0) ? p : misdn_cfg_get_next_port(0);
+}
 
-       if (ret > 0)
-               return ret;
+static int _parse (union misdn_cfg_pt *dest, char *value, enum misdn_cfg_type type, int boolint_def)
+{
+       int re = 0;
+       int len, tmp;
+       char *valtmp;
+
+       switch (type) {
+       case MISDN_CTYPE_STR:
+               if ((len = strlen(value))) {
+                       dest->str = (char *)malloc((len + 1) * sizeof(char));
+                       strncpy(dest->str, value, len);
+                       dest->str[len] = 0;
+               } else {
+                       dest->str = (char *)malloc( sizeof(char));
+                       dest->str[0] = 0;
+               }
+               break;
+       case MISDN_CTYPE_INT:
+               if (sscanf(value, "%d", &tmp)) {
+                       dest->num = (int *)malloc(sizeof(int));
+                       memcpy(dest->num, &tmp, sizeof(int));
+               } else
+                       re = -1;
+               break;
+       case MISDN_CTYPE_BOOL:
+               dest->num = (int *)malloc(sizeof(int));
+               *(dest->num) = (ast_true(value) ? 1 : 0);
+               break;
+       case MISDN_CTYPE_BOOLINT:
+               dest->num = (int *)malloc(sizeof(int));
+               if (sscanf(value, "%d", &tmp)) {
+                       memcpy(dest->num, &tmp, sizeof(int));
+               } else {
+                       *(dest->num) = (ast_true(value) ? boolint_def : 0);
+               }
+               break;
+       case MISDN_CTYPE_MSNLIST:
+               for (valtmp = strsep(&value, ","); valtmp; valtmp = strsep(&value, ",")) {
+                       if ((len = strlen(valtmp))) {
+                               struct msn_list *ml = (struct msn_list *)malloc(sizeof(struct msn_list));
+                               ml->msn = (char *)calloc(len+1, sizeof(char));
+                               strncpy(ml->msn, valtmp, len);
+                               ml->next = dest->ml;
+                               dest->ml = ml;
+                       }
+               }
+               break;
+       case MISDN_CTYPE_ASTGROUP:
+               dest->grp = (ast_group_t *)malloc(sizeof(ast_group_t));
+               *(dest->grp) = ast_get_group(value);
+               break;
+       }
 
-       return misdn_cfg_get_next_port(0);
+       return re;
 }
 
-#define PARSE_GEN_INT(item) ({ \
-               if (!strcasecmp(v->name, #item)) { \
-                       int temp; \
-                       if (!sscanf(v->value, "%d", &temp)) { \
-                               ast_log(LOG_WARNING, "Value \"%s\" for \"" #item "\" (generals section) invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", v->value); \
-                       } else { \
-                               general_cfg->item = (int *)malloc(sizeof(int)); \
-                               memcpy(general_cfg->item, &temp, sizeof(int)); \
-                       } \
-                       continue; \
-               } \
-       }) \
-
-#define PARSE_GEN_BOOL(item) ({ \
-               if (!strcasecmp(v->name, #item)) { \
-                       general_cfg->item = (int *)malloc(sizeof(int)); \
-                       *(general_cfg->item) = ast_true(v->value)?1:0; \
-                       continue; \
-               } \
-       })
-
-#define PARSE_GEN_STR(item) ({ \
-               if (!strcasecmp(v->name, #item)) { \
-                       int l = strlen(v->value); \
-                       if (l) { \
-                               general_cfg->item = (char *)calloc(l+1, sizeof(char)); \
-                               strncpy(general_cfg->item,v->value, l); \
-                       } \
-                       continue; \
-               } \
-       })
-
-static void build_general_config(struct ast_variable *v) {
-
-       if (!v) 
-               return;
+static void _build_general_config (struct ast_variable *v)
+{
+       int pos;
 
        for (; v; v = v->next) {
-
-               PARSE_GEN_INT(debug);
-               PARSE_GEN_STR(tracefile);
-               PARSE_GEN_BOOL(trace_calls);
-               PARSE_GEN_STR(trace_dir);
-               PARSE_GEN_BOOL(bridging);
-               PARSE_GEN_BOOL(stop_tone_after_first_digit);
-               PARSE_GEN_BOOL(append_digits2exten);
-               PARSE_GEN_BOOL(l1_info_ok);
-               PARSE_GEN_BOOL(clear_l3);
-               PARSE_GEN_BOOL(dynamic_crypt);
-               PARSE_GEN_STR(crypt_prefix);
-               PARSE_GEN_STR(crypt_keys);
-               
+               if (((pos = get_cfg_position(v->name, GEN_CFG)) < 0) || 
+                       (_parse(&general_cfg[pos], v->value, gen_spec[pos].type, gen_spec[pos].boolint_def) < 0))
+                       CLI_ERROR(v->name, v->value, "general");
        }
 }
 
-#define PARSE_CFG_HYBRID(item, def) ({ \
-               if (!strcasecmp(v->name, #item)) { \
-                       new->item = (int *)malloc(sizeof(int)); \
-                       if (!sscanf(v->value, "%d", new->item)) { \
-                               if (ast_true(v->value)) \
-                                       *new->item = def; \
-                               else \
-                                       *new->item = 0; \
-                       } \
-                       continue; \
-               } \
-       }) \
-
-#define PARSE_CFG_INT(item) ({ \
-               if (!strcasecmp(v->name, #item)) { \
-                       new->item = (int *)malloc(sizeof(int)); \
-                       if (!sscanf(v->value, "%d", new->item)) { \
-                               ast_log(LOG_WARNING, "Value \"%s\" for \"" #item "\" of group \"%s\" invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", v->value, cat); \
-                               free(new->item); \
-                               new->item = NULL; \
-                       } \
-                       continue; \
-               } \
-       }) \
-
-#define PARSE_CFG_BOOL(item) ({ \
-               if (!strcasecmp(v->name, #item)) { \
-                       new->item = (int *)malloc(sizeof(int)); \
-                       *(new->item) = ast_true(v->value)?1:0; \
-                       continue; \
-               } \
-       })
-
-#define PARSE_CFG_STR(item) ({ \
-               if (!strcasecmp(v->name, #item)) { \
-                       int l = strlen(v->value); \
-                       if (l) { \
-                               new->item = (char *)calloc(l+1, sizeof(char)); \
-                               strncpy(new->item,v->value,l); \
-                       } \
-                       continue; \
-               } \
-       })
-
-static void build_port_config(struct ast_variable *v, char *cat) {
+static void _build_port_config (struct ast_variable *v, char *cat)
+{
+       int pos, i;
+       union misdn_cfg_pt cfg_tmp[NUM_PORT_ELEMENTS];
+       int cfg_for_ports[max_ports + 1];
+
        if (!v || !cat)
                return;
 
-       int cfg_for_ports[max_ports + 1];
-       int i = 0;
-       for (; i < (max_ports + 1); i++) {
-               cfg_for_ports[i] = 0;
-       }
-       
-       /* we store the default config at position 0 */
+       memset(cfg_tmp, 0, sizeof(cfg_tmp));
+       memset(cfg_for_ports, 0, sizeof(cfg_for_ports));
+
        if (!strcasecmp(cat, "default")) {
                cfg_for_ports[0] = 1;
        }
 
-       struct port_config* new = (struct port_config *)calloc(1, sizeof(struct port_config));
-       
-       {
-               int l = strlen(cat);
-               new->name = (char *)calloc(l+1, sizeof(char));
-               strncpy(new->name, cat, l);
+       if (((pos = get_cfg_position("name", PORT_CFG)) < 0) || 
+               (_parse(&cfg_tmp[pos], cat, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) {
+               CLI_ERROR(v->name, v->value, cat);
+               return;
        }
-       
-       for (; v; v=v->next) {
+
+       for (; v; v = v->next) {
                if (!strcasecmp(v->name, "ports")) {
-                       char *value;
+                       char *token;
                        char ptpbuf[BUFFERSIZE] = "";
                        int start, end;
-                       for (value = strsep(&v->value, ","); value; value = strsep(&v->value, ","), *ptpbuf = 0) { 
-                               if (!*value)
+                       for (token = strsep(&v->value, ","); token; token = strsep(&v->value, ","), *ptpbuf = 0) { 
+                               if (!*token)
                                        continue;
-                               if (sscanf(value, "%d-%d%s", &start, &end, ptpbuf) >= 2) {
+                               if (sscanf(token, "%d-%d%s", &start, &end, ptpbuf) >= 2) {
                                        for (; start <= end; start++) {
                                                if (start <= max_ports && start > 0) {
                                                        cfg_for_ports[start] = 1;
                                                        ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
                                                } else
-                                                       ast_log(LOG_WARNING, "Port value '%s' of group '%s' invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", value, cat);
+                                                       CLI_ERROR(v->name, v->value, cat);
                                        }
                                } else {
-                                       if (sscanf(value, "%d%s", &start, ptpbuf)) {
+                                       if (sscanf(token, "%d%s", &start, ptpbuf)) {
                                                if (start <= max_ports && start > 0) {
                                                        cfg_for_ports[start] = 1;
                                                        ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
                                                } else
-                                                       ast_log(LOG_WARNING, "Port value '%s' of group '%s' invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", value, cat);
+                                                       CLI_ERROR(v->name, v->value, cat);
                                        } else
-                                               ast_log(LOG_ERROR, "Syntax error parsing token \"msns=%s\" at group '%s'! Please edit your misdn.conf and then do a \"misdn reload\".\n", value, cat);
+                                               CLI_ERROR(v->name, v->value, cat);
                                }
                        }
-                       continue;
+               } else {
+                       if (((pos = get_cfg_position(v->name, PORT_CFG)) < 0) || 
+                               (_parse(&cfg_tmp[pos], v->value, port_spec[pos].type, port_spec[pos].boolint_def) < 0))
+                               CLI_ERROR(v->name, v->value, cat);
                }
-               PARSE_CFG_STR(context);
-               PARSE_CFG_INT(dialplan);
-               PARSE_CFG_INT(localdialplan);
-               PARSE_CFG_STR(nationalprefix);
-               PARSE_CFG_STR(internationalprefix);
-               PARSE_CFG_STR(language);
-               PARSE_CFG_STR(musicclass);
-               if (!strcasecmp(v->name, "presentation")) {
-                       if (v->value && strlen(v->value)) {
-                               new->pres = (int *)malloc(sizeof(int));
-                               if (!strcasecmp(v->value, "allowed")) {
-                                       *(new->pres) = 1;
-                               }
-                               /* TODO: i assume if it is not "allowed", it is "not_screened" */
-                               else {
-                                       *(new->pres) = 0;
-                               }
-                       }
-                       continue;
+       }
+
+       for (i = 0; i < (max_ports + 1); ++i) {
+               if (cfg_for_ports[i]) {
+                       memcpy(port_cfg[i], cfg_tmp, sizeof(cfg_tmp));
                }
-               PARSE_CFG_INT(rxgain);
-               PARSE_CFG_INT(txgain);
-               PARSE_CFG_INT(jitterbuffer);
-               PARSE_CFG_INT(jitterbuffer_upper_threshold);
-               PARSE_CFG_BOOL(te_choose_channel);
-               PARSE_CFG_BOOL(immediate);
-               PARSE_CFG_BOOL(always_immediate);
-               PARSE_CFG_BOOL(senddtmf);
-               PARSE_CFG_BOOL(hold_allowed);
-               PARSE_CFG_BOOL(early_bconnect);
-               PARSE_CFG_BOOL(use_callingpres);
-               PARSE_CFG_HYBRID(echocancel, DEF_ECHOCANCEL);
-               PARSE_CFG_BOOL(echocancelwhenbridged);
-               PARSE_CFG_HYBRID(echotraining, DEF_ECHOTRAINING);
-               PARSE_CFG_STR(callerid);
-               PARSE_CFG_STR(method);
-               if (!strcasecmp(v->name, "msns")) {
-                       char *value;
-                       int l;
-                       for (value = strsep(&v->value, ","); value; value = strsep(&v->value, ",")) {
-                               if ((l = strlen(value))) {
-                                       struct msn_list *ml = (struct msn_list *)calloc(1, sizeof(struct msn_list));
-                                       ml->msn = (char *)calloc(l+1, sizeof(char));
-                                       strncpy(ml->msn,value,l);
-                                       ml->next = new->msn_list;
-                                       new->msn_list = ml;
+       }
+}
+
+void misdn_cfg_update_ptp (void)
+{
+       char misdn_init[BUFFERSIZE];
+       char line[BUFFERSIZE];
+       FILE *fp;
+       char *tok, *p, *end;
+       int port;
+
+       misdn_cfg_get(0, MISDN_GEN_MISDN_INIT, &misdn_init, sizeof(misdn_init));
+
+       if (misdn_init) {
+               fp = fopen(misdn_init, "r");
+               if (fp) {
+                       while(fgets(line, sizeof(line), fp)) {
+                               if (!strncmp(line, "nt_ptp", 6)) {
+                                       for (tok = strtok_r(line,",=", &p);
+                                                tok;
+                                                tok = strtok_r(NULL,",=", &p)) {
+                                               port = strtol(tok, &end, 10);
+                                               if (end != tok && misdn_cfg_is_port_valid(port)) {
+                                                       misdn_cfg_lock();
+                                                       ptp[port] = 1;
+                                                       misdn_cfg_unlock();
+                                               }
+                                       }
                                }
                        }
-                       continue;
-               }
-               if (!strcasecmp(v->name, "callgroup")) {
-                       new->callgroup = (ast_group_t *)malloc(sizeof(ast_group_t));
-                       *(new->callgroup)=ast_get_group(v->value);
-                       continue;
-               }
-               if (!strcasecmp(v->name, "pickupgroup")) {
-                       new->pickupgroup = (ast_group_t *)malloc(sizeof(ast_group_t));
-                       *(new->pickupgroup)=ast_get_group(v->value);
-                       continue;
+                       fclose(fp);
+               } else {
+                       ast_log(LOG_WARNING,"Couldn't open %s: %s\n", misdn_init, strerror(errno));
                }
        }
-       /* store the new config in our array of port configs */
-       for (i = 0; i < (max_ports + 1); i++) {
-               if (cfg_for_ports[i])
-                       port_cfg[i] = new;
-       }
 }
 
+static void _fill_defaults (void)
+{
+       int i;
 
-static void fill_defaults (void) {
-       
-       /* general defaults */
-       if (!general_cfg->debug)
-               general_cfg->debug = (int*)calloc(1, sizeof(int));
-       if (!general_cfg->trace_calls)
-               general_cfg->trace_calls = (int*)calloc(1, sizeof(int));
-       if (!general_cfg->trace_dir) {
-               general_cfg->trace_dir = (char *)malloc(10 * sizeof(char));
-               sprintf(general_cfg->trace_dir, "/var/log/");
-       }
-       if (!general_cfg->bridging) {
-               general_cfg->bridging = (int*)malloc(sizeof(int));
-               *general_cfg->bridging = 1;
+       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
+               if (!port_cfg[0][i].any && strcasecmp(port_spec[i].def, NO_DEFAULT))
+                       _parse(&(port_cfg[0][i]), (char *)port_spec[i].def, port_spec[i].type, port_spec[i].boolint_def);
        }
-       if (!general_cfg->stop_tone_after_first_digit) {
-               general_cfg->stop_tone_after_first_digit = (int*)malloc(sizeof(int));
-               *general_cfg->stop_tone_after_first_digit = 1;
-       }
-       if (!general_cfg->append_digits2exten) {
-               general_cfg->append_digits2exten = (int*)malloc(sizeof(int));
-               *general_cfg->append_digits2exten = 1;
-       }
-       if (!general_cfg->l1_info_ok) {
-               general_cfg->l1_info_ok = (int*)malloc(sizeof(int));
-               *general_cfg->l1_info_ok = 1;
-       }
-       if (!general_cfg->clear_l3)
-               general_cfg->clear_l3 =(int*)calloc(1, sizeof(int));
-       if (!general_cfg->dynamic_crypt)
-               general_cfg->dynamic_crypt = (int*)calloc(1, sizeof(int));
-
-       /* defaults for default port config */
-       if (!port_cfg[0])
-               port_cfg[0] = (struct port_config*)calloc(1, sizeof(struct port_config));
-       if (!port_cfg[0]->name) {
-               port_cfg[0]->name = (char *)malloc(8 * sizeof(char));
-               sprintf(port_cfg[0]->name, "default");
-       }
-       if (!port_cfg[0]->rxgain)
-               port_cfg[0]->rxgain = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->txgain)
-               port_cfg[0]->txgain = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->jitterbuffer)
-               port_cfg[0]->jitterbuffer = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->jitterbuffer_upper_threshold)
-               port_cfg[0]->jitterbuffer_upper_threshold = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->te_choose_channel)
-               port_cfg[0]->te_choose_channel = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->context) {
-               port_cfg[0]->context = (char *)malloc(8 * sizeof(char));
-               sprintf(port_cfg[0]->context, "default");
-       }
-       if (!port_cfg[0]->language) {
-               port_cfg[0]->language = (char *)malloc(3 * sizeof(char));
-               sprintf(port_cfg[0]->language, "en");
-       }
-       if (!port_cfg[0]->musicclass) {
-               port_cfg[0]->musicclass = (char *)malloc(3 * sizeof(char));
-               sprintf(port_cfg[0]->musicclass, "default");
-       }
-       if (!port_cfg[0]->callerid)
-               port_cfg[0]->callerid = (char *)calloc(1, sizeof(char));
-       if (!port_cfg[0]->method) {
-               port_cfg[0]->method = (char *)malloc(9 * sizeof(char));
-               sprintf(port_cfg[0]->method, "standard");
-       }
-       if (!port_cfg[0]->dialplan)
-               port_cfg[0]->dialplan = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->localdialplan)
-               port_cfg[0]->localdialplan = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->nationalprefix) {
-               port_cfg[0]->nationalprefix = (char *)malloc(2 * sizeof(char));
-               sprintf(port_cfg[0]->nationalprefix, "0");
-       }
-       if (!port_cfg[0]->internationalprefix) {
-               port_cfg[0]->internationalprefix = (char *)malloc(3 * sizeof(char));
-               sprintf(port_cfg[0]->internationalprefix, "00");
-       }
-       if (!port_cfg[0]->pres) {
-               port_cfg[0]->pres = (int *)malloc(sizeof(int));
-               *port_cfg[0]->pres = 1;
-       }
-       if (!port_cfg[0]->always_immediate)
-               port_cfg[0]->always_immediate = (int *)calloc(1, sizeof(int));
-       
-       if (!port_cfg[0]->senddtmf)
-               port_cfg[0]->senddtmf = (int *)calloc(1, sizeof(int));
-       
-       if (!port_cfg[0]->immediate)
-               port_cfg[0]->immediate = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->hold_allowed)
-               port_cfg[0]->hold_allowed = (int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->early_bconnect) {
-               port_cfg[0]->early_bconnect = (int *)malloc(sizeof(int));
-               *port_cfg[0]->early_bconnect = 1;
-       }
-       if (!port_cfg[0]->echocancel)
-               port_cfg[0]->echocancel=(int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->echocancelwhenbridged)
-               port_cfg[0]->echocancelwhenbridged=(int *)calloc(1, sizeof(int));
-       if (!port_cfg[0]->echotraining) {
-               port_cfg[0]->echotraining=(int *)malloc(sizeof(int));
-               *port_cfg[0]->echotraining = 1;
-       }
-       if (!port_cfg[0]->use_callingpres) {
-               port_cfg[0]->use_callingpres = (int *)malloc(sizeof(int));
-               *port_cfg[0]->use_callingpres = 1;
-       }
-       if (!port_cfg[0]->msn_list) {
-               port_cfg[0]->msn_list = (struct msn_list *)malloc(sizeof(struct msn_list));
-               port_cfg[0]->msn_list->next = NULL;
-               port_cfg[0]->msn_list->msn = (char *)calloc(2, sizeof(char));
-               *(port_cfg[0]->msn_list->msn) = '*';
+       for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
+               if (!general_cfg[i].any && strcasecmp(gen_spec[i].def, NO_DEFAULT))
+                       _parse(&(general_cfg[i]), (char *)gen_spec[i].def, gen_spec[i].type, gen_spec[i].boolint_def);
        }
 }
 
-void misdn_cfg_reload (void) {
+void misdn_cfg_reload (void)
+{
        misdn_cfg_init (0);
 }
 
-void misdn_cfg_destroy (void) {
-
+void misdn_cfg_destroy (void)
+{
        misdn_cfg_lock();
-       
-       free_port_cfg();
-       free_general_cfg();
-       
+
+       _free_port_cfg();
+       _free_general_cfg();
+
        free(port_cfg);
        free(general_cfg);
        free(ptp);
+       free(map);
 
        misdn_cfg_unlock();
        ast_mutex_destroy(&config_mutex);
@@ -1040,48 +712,58 @@ void misdn_cfg_destroy (void) {
 
 void misdn_cfg_init (int this_max_ports)
 {
-       char config[]="misdn.conf";
-       
+       char config[] = "misdn.conf";
+       char *cat, *p;
+       int i;
        struct ast_config *cfg;
-       cfg = AST_LOAD_CFG(config);
-       if (!cfg) {
+       struct ast_variable *v;
+
+       if (!(cfg = AST_LOAD_CFG(config))) {
                ast_log(LOG_WARNING,"no misdn.conf ?\n");
                return;
        }
 
        misdn_cfg_lock();
-       
+
        if (this_max_ports) {
                /* this is the first run */
                max_ports = this_max_ports;
-               port_cfg = (struct port_config **)calloc(max_ports + 1, sizeof(struct port_config *));
-               general_cfg = (struct general_config*)calloc(1, sizeof(struct general_config));
+               p = (char *)calloc(1, (max_ports + 1) * sizeof(union misdn_cfg_pt *)
+                                                  + (max_ports + 1) * NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt));
+               port_cfg = (union misdn_cfg_pt **)p;
+               p += (max_ports + 1) * sizeof(union misdn_cfg_pt *);
+               for (i = 0; i <= max_ports; ++i) {
+                       port_cfg[i] = (union misdn_cfg_pt *)p;
+                       p += NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt);
+               }
+               general_cfg = (union misdn_cfg_pt *)calloc(1, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
                ptp = (int *)calloc(max_ports + 1, sizeof(int));
+               map = (int *)calloc(MISDN_GEN_LAST + 1, sizeof(int));
+               _enum_array_map();
        }
        else {
-               free_port_cfg();
-               free_general_cfg();
-               port_cfg = memset(port_cfg, 0, sizeof(struct port_config *) * (max_ports + 1));
-               general_cfg = memset(general_cfg, 0, sizeof(struct general_config));
-               ptp = memset(ptp, 0, sizeof(int) * (max_ports + 1));
+               /* misdn reload */
+               _free_port_cfg();
+               _free_general_cfg();
+               memset(port_cfg[0], 0, NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt) * (max_ports + 1));
+               memset(general_cfg, 0, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
+               memset(ptp, 0, sizeof(int) * (max_ports + 1));
        }
-       
-       char *cat;
+
        cat = ast_category_browse(cfg, NULL);
 
        while(cat) {
-               struct ast_variable *v=ast_variable_browse(cfg,cat);
+               v = ast_variable_browse(cfg, cat);
                if (!strcasecmp(cat,"general")) {
-                       build_general_config (v);
+                       _build_general_config(v);
                } else {
-                       build_port_config (v, cat);
+                       _build_port_config(v, cat);
                }
-               cat=ast_category_browse(cfg,cat);
+               cat = ast_category_browse(cfg,cat);
        }
 
-       fill_defaults();
-       
+       _fill_defaults();
+
        misdn_cfg_unlock();
-       
        AST_DESTROY_CFG(cfg);
 }
index bacbf5b..e64a197 100644 (file)
@@ -5,7 +5,7 @@
 #
 
 # Verify those options with main Makefile
-CFLAGS         += -pipe -c
+CFLAGS         = -pipe -c -Wall
 SOURCES                = isdn_lib.c isdn_msg_parser.c 
 OBJDIR         = .
 OBJS           = isdn_lib.o isdn_msg_parser.o fac.o
index b6e309f..8e2b260 100644 (file)
@@ -22,12 +22,9 @@ enum misdn_cfg_elements {
 
        /* port config items */
        MISDN_CFG_FIRST = 0,
-       MISDN_CFG_PTP,                 /* int (bool) */
        MISDN_CFG_GROUPNAME,           /* char[] */
        MISDN_CFG_RXGAIN,              /* int */
        MISDN_CFG_TXGAIN,              /* int */
-       MISDN_CFG_JITTERBUFFER,              /* int */
-       MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD,              /* int */
        MISDN_CFG_TE_CHOOSE_CHANNEL,   /* int (bool) */
        MISDN_CFG_CONTEXT,             /* char[] */
        MISDN_CFG_LANGUAGE,            /* char[] */
@@ -38,32 +35,33 @@ enum misdn_cfg_elements {
        MISDN_CFG_LOCALDIALPLAN,       /* int */
        MISDN_CFG_NATPREFIX,           /* char[] */
        MISDN_CFG_INTERNATPREFIX,      /* char[] */
-       MISDN_CFG_PRES,                /* int (bool) */
+       MISDN_CFG_PRES,                /* int */
+       MISDN_CFG_SCREEN,              /* int */
        MISDN_CFG_ALWAYS_IMMEDIATE,    /* int (bool) */
        MISDN_CFG_IMMEDIATE,           /* int (bool) */
        MISDN_CFG_SENDDTMF,           /* int (bool) */
        MISDN_CFG_HOLD_ALLOWED,        /* int (bool) */
        MISDN_CFG_EARLY_BCONNECT,      /* int (bool) */
-       MISDN_CFG_USE_CALLINGPRES,     /* int (bool) */
        MISDN_CFG_ECHOCANCEL,          /* int */
        MISDN_CFG_ECHOCANCELWHENBRIDGED,  /* int (bool) */
        MISDN_CFG_ECHOTRAINING,        /* int (bool) */
+       MISDN_CFG_NEED_MORE_INFOS,     /* bool */
+       MISDN_CFG_JITTERBUFFER,              /* int */
+       MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD,              /* int */
        MISDN_CFG_CALLGROUP,           /* ast_group_t */
        MISDN_CFG_PICKUPGROUP,         /* ast_group_t */
        MISDN_CFG_MSNS,                /* char[] */
+       MISDN_CFG_PTP,                 /* int (bool) */
        MISDN_CFG_LAST,
        
        /* general config items */
        MISDN_GEN_FIRST,
+       MISDN_GEN_MISDN_INIT,           /* char[] */
        MISDN_GEN_DEBUG,               /* int */
        MISDN_GEN_TRACEFILE,           /* char[] */
-       MISDN_GEN_TRACE_CALLS,         /* int (bool) */
-       MISDN_GEN_TRACE_DIR,           /* char[] */
        MISDN_GEN_BRIDGING,            /* int (bool) */
        MISDN_GEN_STOP_TONE,           /* int (bool) */
        MISDN_GEN_APPEND_DIGITS2EXTEN, /* int (bool) */
-       MISDN_GEN_L1_INFO_OK,          /* int (bool) */
-       MISDN_GEN_CLEAR_L3,            /* int (bool) */
        MISDN_GEN_DYNAMIC_CRYPT,       /* int (bool) */
        MISDN_GEN_CRYPT_PREFIX,        /* char[] */
        MISDN_GEN_CRYPT_KEYS,          /* char[] */
@@ -80,6 +78,8 @@ void misdn_cfg_init(int max_ports);
 void misdn_cfg_reload(void);
 void misdn_cfg_destroy(void);
 
+void misdn_cfg_update_ptp( void );
+
 /* if you requst a general config element, the port value is ignored. if the requested 
  * value is not available, or the buffer is too small, the buffer will be nulled (in 
  * case of a char* only its first byte will be nulled). */
index 8529812..0d24c42 100644 (file)
@@ -12,7 +12,7 @@
  */
 
 #include "isdn_lib_intern.h"
-
+#include <mISDNuser/isdn_debug.h>
 
 void misdn_free_ibuffer(void *ibuf)
 {
@@ -142,6 +142,8 @@ struct misdn_lib {
 
 #define MISDN_DEBUG 0
 
+void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
+
 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
 
 int setup_bc(struct misdn_bchannel *bc);
@@ -388,7 +390,7 @@ void dump_chan_list(struct misdn_stack *stack)
        int i;
 
        for (i=0; i <stack->b_num; i++) {
-               cb_log(3, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
+               cb_log(5, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
        }
 }
 
@@ -433,11 +435,15 @@ int empty_chan_in_stack(struct misdn_stack *stack, int channel)
 
 void empty_bc(struct misdn_bchannel *bc)
 {
+       bc->bframe_len=0;
+
        bc->state=STATE_NOTHING;
        
        bc->channel = 0;
        bc->in_use = 0;
 
+       bc->need_more_infos = 0;
+       
        bc->send_dtmf=0;
        bc->nodsp=0;
        bc->nojitter=0;
@@ -452,8 +458,8 @@ void empty_bc(struct misdn_bchannel *bc)
        
        bc->crypt_key[0] = 0;
        
-       bc->tone=TONE_NONE;
-       bc->tone_cnt2 = bc->tone_cnt=0;
+       bc->generate_tone=0;
+       bc->tone_cnt=0;
   
        bc->dnumplan=NUMPLAN_UNKNOWN;
        bc->onumplan=NUMPLAN_UNKNOWN;
@@ -488,9 +494,9 @@ void empty_bc(struct misdn_bchannel *bc)
        bc->mode=0;
        bc->rate=0;
        bc->user1=0;
-       bc->async=0;
        bc->urate=0;
        
+       bc->hdlc=0;
        
        
        bc->info_dad[0] = 0;
@@ -535,6 +541,14 @@ int clean_up_bc(struct misdn_bchannel *bc)
 
        mISDN_write_frame(stack->midev, buff, bc->addr|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
        
+
+#ifdef ACK_HDLC        
+       if (bc->ack_hdlc ) {
+               free(bc->ack_hdlc);
+               bc->ack_hdlc=NULL;
+       }
+#endif
+       
        
        bc->b_stid = 0;
        
@@ -756,11 +770,10 @@ int setup_bc(struct misdn_bchannel *bc)
        int midev=stack->midev;
        int channel=bc->channel-1-(bc->channel>16);
        int b_stid=stack->b_stids[channel>=0?channel:0];
-       
-       
-       if ( !misdn_cap_is_speech(bc->capability))
+
+       if (bc->hdlc) {
                clean_up_bc(bc);
-       
+       }
        
        if (bc->upset) {
                cb_log(4, stack->port, "$$$ bc already upsetted stid :%x\n", b_stid);
@@ -792,30 +805,30 @@ int setup_bc(struct misdn_bchannel *bc)
 #ifndef MISDN_DSP
                bc->nodsp=1;
 #endif
-               if ( misdn_cap_is_speech(bc->capability) && !bc->nodsp && (bc->async != 1) ) {
-                       cb_log(4, stack->port,"setup_bc: with dsp\n");
+               if ( bc->hdlc || bc->nodsp) {
+                       cb_log(4, stack->port,"setup_bc: without dsp\n");
                        { 
                                int l = sizeof(li.name);
-                               strncpy(li.name, "B L4", l);
+                               strncpy(li.name, "B L3", l);
                                li.name[l-1] = 0;
                        }
-                       li.pid.layermask = ISDN_LAYER((4));
-                       li.pid.protocol[4] = ISDN_PID_L4_B_USER;
-
-                       bc->layer=4;
+                       li.pid.layermask = ISDN_LAYER((3));
+                       li.pid.protocol[3] = ISDN_PID_L3_B_USER;
                        
+                       bc->layer=3;
                } else {
-                       cb_log(4, stack->port,"setup_bc: without dsp\n");
+                       cb_log(4, stack->port,"setup_bc: with dsp\n");
                        { 
                                int l = sizeof(li.name);
-                               strncpy(li.name, "B L3", l);
+                               strncpy(li.name, "B L4", l);
                                li.name[l-1] = 0;
                        }
-                       li.pid.layermask = ISDN_LAYER((3));
-                       li.pid.protocol[3] = ISDN_PID_L3_B_USER;
+                       li.pid.layermask = ISDN_LAYER((4));
+                       li.pid.protocol[4] = ISDN_PID_L4_B_USER
+;
+                       bc->layer=4;
                        
-                       bc->layer=3;
-               }
+               }  
                
                ret = mISDN_new_layer(midev, &li);
                if (ret ) {
@@ -833,29 +846,34 @@ int setup_bc(struct misdn_bchannel *bc)
        
        cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
        
-       
-       if (bc->async == 1 || bc->nodsp) {
-               cb_log(4, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
+       if (bc->nodsp) {
+               cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
                pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
                pid.protocol[2] = ISDN_PID_L2_B_TRANS;
                pid.protocol[3] = ISDN_PID_L3_B_USER;
                pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
                
-       } else if ( misdn_cap_is_speech(bc->capability)) {
-               cb_log(4, stack->port," --> TRANSPARENT Mode\n");
+       } else if ( bc->hdlc ) {
+               cb_log(2, stack->port," --> HDLC Mode\n");
+#ifdef ACK_HDLC
+               bc->ack_hdlc=(sem_t*)malloc(sizeof(sem_t));
+               if ( sem_init((sem_t*)bc->ack_hdlc, 1, 0)<0 )
+                       sem_init((sem_t*)bc->ack_hdlc, 0, 0);
+#endif
+               
+               pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
+               pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
+               pid.protocol[3] = ISDN_PID_L3_B_USER;
+               pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
+       } else {
+               cb_log(2, stack->port," --> TRANSPARENT Mode\n");
                pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
                pid.protocol[2] = ISDN_PID_L2_B_TRANS;
                pid.protocol[3] = ISDN_PID_L3_B_DSP;
                pid.protocol[4] = ISDN_PID_L4_B_USER;
                pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
                
-       } else {
-               cb_log(4, stack->port," --> HDLC Mode\n");
-               pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
-               pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
-               pid.protocol[3] = ISDN_PID_L3_B_USER;
-               pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
-       }
+       } 
        
        ret = mISDN_set_stack(midev, bc->b_stid, &pid);
        
@@ -964,7 +982,6 @@ struct misdn_stack* stack_init( int midev, int port, int ptp )
        if (!stack ) return NULL;
 
 
-       //cb_log(2, "Init. Stack on port:%d\n",port);
        cb_log(4, port, "Init. Stack.\n");
   
        memset(stack,0,sizeof(struct misdn_stack));
@@ -997,7 +1014,6 @@ struct misdn_stack* stack_init( int midev, int port, int ptp )
   
        switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
        case ISDN_PID_L0_TE_S0:
-               //cb_log(2, "TE Stack\n");
                stack->nt=0;
                break;
        case ISDN_PID_L0_NT_S0:
@@ -1120,27 +1136,14 @@ struct misdn_stack* stack_init( int midev, int port, int ptp )
                        Isdnl2Init(&stack->nst);
                        Isdnl3Init(&stack->nst);
                        
-                       //misdn_lib_get_l1_up(stack);
                } 
                
-               
-               
-               if ( stack->nt && stack->ptp) {
-                       //misdn_lib_get_l2_up(stack);
-                       //stack->l2link=0;
-               }
-               
                if (!stack->nt) {
                        /*assume L1 is up, we'll get DEACTIVATES soon, for non
                         * up L1s*/
                        stack->l1link=1;
-                       //misdn_lib_get_l2_up(stack);
-                       //misdn_lib_get_l1_up(stack);
                }
 
-               //misdn_lib_get_short_status(stack);
-               
-               
                misdn_lib_get_l1_up(stack);
                misdn_lib_get_l2_up(stack);
                
@@ -1172,8 +1175,6 @@ struct misdn_stack * find_stack_by_addr(int  addr)
        for (stack=glob_mgr->stack_list;
             stack;
             stack=stack->next) {
-               //cb_log(2,0,"stack->addr:%x addr:%x masked: st->a:%x a:%x\n",stack->upper_id, addr, stack->upper_id&STACK_ID_MASK, addr&STACK_ID_MASK);
-               
                if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
 
        }
@@ -1271,10 +1272,19 @@ int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
                switch (event) {
 
                case EVENT_CONNECT_ACKNOWLEDGE:
+                       
+                       if ( !misdn_cap_is_speech(bc->capability)) {
+                               int ret=setup_bc(bc);
+                               if (ret == -EINVAL){
+                                       cb_log(-1,bc->port,"send_event: setup_bc failed\n");
+                                       
+                               }
+                       }
+                       
                        manager_bchannel_activate(bc);
                        break;
                case EVENT_CONNECT:
-                       
+
                        if ( *bc->crypt_key ) {
                                cb_log(4, stack->port, "ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s\n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
                                
@@ -1294,7 +1304,6 @@ int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
                                bc->channel=find_free_chan_in_stack(stack, 0);
                                if (!bc->channel) {
                                        cb_log(-1, stack->port, "Any Channel Requested, but we have no more!!\n");
-
                                        break;
                                }
                        } 
@@ -1302,7 +1311,6 @@ int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
                        if (!stack->nt) {
                                int ret=setup_bc(bc);
                                if (ret == -EINVAL){
-
                                        cb_log(-1,bc->port,"handle_event: setup_bc failed\n");
                                        misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
                                }
@@ -1575,7 +1583,25 @@ handle_event_nt(void *dat, void *arg)
                }
                break;
 
-
+               case CC_CONNECT_ACKNOWLEDGE|INDICATION:
+#if 0
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+                       if (bc) {
+                               if ( !misdn_cap_is_speech(bc->capability)) {
+                                       int ret=setup_bc(bc);
+                                       if (ret == -EINVAL){
+                                               cb_log(-1,bc->port,"send_event: setup_bc failed\n");
+                                               
+                                       }
+                               }
+                               
+                               manager_bchannel_activate(bc);
+                       }
+               }
+#endif
+               break;
+               
                case CC_CONNECT|INDICATION:
                case CC_ALERTING|INDICATION:
                case CC_PROCEEDING|INDICATION:
@@ -1606,10 +1632,11 @@ handle_event_nt(void *dat, void *arg)
                        struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
                        if (!bc) {
                                bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
-                               if (bc) { //repair reject bug
+                               if (bc) { 
                                        int myprocid=bc->l3_id&0x0000ffff;
                                        hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
-                                       cb_log(4,stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
+                                       cb_log(3,stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
+                                       return 0;
                                }
                        }
                }
@@ -1620,7 +1647,7 @@ handle_event_nt(void *dat, void *arg)
                        struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
                        if (!bc) {
                                bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
-                               if (bc) { //repair reject bug
+                               if (bc) { 
                                        int myprocid=bc->l3_id&0x0000ffff;
                                        hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
                                        cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
@@ -1649,10 +1676,13 @@ handle_event_nt(void *dat, void *arg)
                case CC_RELEASE|CONFIRM:
                {
                        struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
-                       cb_log(4, stack->port, " --> RELEASE CONFIRM, sending RELEASE_COMPLETE\n");
-                       if (bc) misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
+                       cb_log(3, stack->port, " --> RELEASE CONFIRM, doing nothin\n");
+                       cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
+                       empty_chan_in_stack(stack,bc->channel);
+                       empty_bc(bc);
+                       free_msg(msg);
+                       return 0;
                }
-               hh->prim=CC_RELEASE|INDICATION;
                break;  
                case CC_RELEASE|INDICATION:
                        break;
@@ -1750,10 +1780,6 @@ handle_event_nt(void *dat, void *arg)
                        cb_log(4, stack->port, "%% GOT L2 DeActivate Info.\n");
                        stack->l2link = 0;
                        
-                       /** Clean the L3 here **/
-                       if (cb_clearl3_true())
-                               clear_l3(stack);
-                       
                        free_msg(msg);
                        return 0;
                }
@@ -1856,95 +1882,68 @@ int handle_timers(msg_t* msg)
 
 
 
-
-static int do_tone(struct misdn_bchannel *bc, int len)
+void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
 {
-       char buf[4096 + mISDN_HEADER_LEN];
-       iframe_t *frm= (iframe_t*)buf;
-       int  r;
-       struct misdn_stack *stack=get_stack_by_bc(bc);
-       
-       if (bc->tone == TONE_NONE) return 0;
+       bc->generate_tone=1;
+}
 
-       frm->prim = DL_DATA|REQUEST;
-       frm->dinfo = 0;
+void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
+{
+       bc->generate_tone=0;
+}
 
-       frm->addr = bc->addr | FLG_MSG_DOWN ;
 
+static int do_tone(struct misdn_bchannel *bc, int len)
+{
+       bc->tone_cnt=len;
        
-  
-       bc->tone_cnt+=len;
+       if (bc->generate_tone) {
+               cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
 
-       if (bc->tone_cnt < TONE_425_SIZE) return 1;
-       
-       switch(bc->tone) {
-       case TONE_DIAL:
-       {
-               frm->len = TONE_425_SIZE;
-               memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
-      
-               r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
-               if (r<frm->len) {
-                       perror("Error written less than told bytes :(\n");
+               if ( !bc->nojitter ) {
+                       misdn_tx_jitter(bc,len);
                }
+               
+               return 1;
        }
-       break;
+       
+       return 0;
+}
 
-       case TONE_ALERTING:
-               bc->tone_cnt2++;
-    
-               if (bc->tone_cnt2 <= TONE_ALERT_CNT) {
-                       frm->len = TONE_425_SIZE;
-                       memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
-                       r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
-                       if (r<frm->len) {
-                               perror("Error written less than told bytes :(\n");
-                       }
-               } else if (bc->tone_cnt2 <= (TONE_ALERT_SILENCE_CNT)) {
-                       frm->len = TONE_SILENCE_SIZE;
-                       memcpy(&buf[mISDN_HEADER_LEN], tone_silence_flip ,TONE_SILENCE_SIZE);
-                       r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
-               } else {
-                       bc->tone_cnt2=-1;
-               }
-               break;
-       case TONE_BUSY:
-               bc->tone_cnt2++;
-    
-               if (bc->tone_cnt2 <= TONE_BUSY_CNT) {
-                       frm->len = TONE_425_SIZE;
-                       memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
-                       r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
-                       if (r<frm->len) {
-                               perror("Error written less than told bytes :(\n");
-                       }
-               } else if (bc->tone_cnt2 <= (TONE_BUSY_SILENCE_CNT)) {
-                       frm->len = TONE_SILENCE_SIZE;
-                       memcpy(&buf[mISDN_HEADER_LEN], tone_silence_flip ,TONE_SILENCE_SIZE);
-                       r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
-               } else {
-                       bc->tone_cnt2=-1;
+
+
+void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
+{
+       char buf[4096 + mISDN_HEADER_LEN];
+       iframe_t *txfrm= (iframe_t*)buf;
+       int jlen, r;
+       
+       jlen=cb_jb_empty(bc,&buf[mISDN_HEADER_LEN],len);
+       
+       if (jlen) {
+               
+               if (jlen < len) {
+                       cb_log(5,bc->port,"Jitterbuffer Underrun.\n");
                }
-               break;
-       case TONE_FILE:
-               break;
-       case TONE_NONE:
-               return 0;
+               
+               txfrm->prim = DL_DATA|REQUEST;
+               
+               txfrm->dinfo = 0;
+               
+               txfrm->addr = bc->addr|FLG_MSG_DOWN; /*  | IF_DOWN; */
+               
+               txfrm->len =jlen;
+               cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
+               
+               r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
        }
-  
-       bc->tone_cnt -= TONE_425_SIZE ;
-       return 1;
 }
 
-
-
 int handle_bchan(msg_t *msg)
 {
        iframe_t *frm= (iframe_t*)msg->data;
        struct misdn_bchannel *bc;
 
-       //cb_log(0,0,"handle_bchan:  a:%x a&M:%x prim:%x dinfo:%x\n",frm->addr,frm->addr&STACK_ID_MASK, frm->prim, frm->dinfo);
-       
        bc=find_bc_by_addr(frm->addr);
        
        if (!bc) {
@@ -1967,8 +1966,12 @@ int handle_bchan(msg_t *msg)
                bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer);
                if (!bc->addr) {
                        cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno));
-                       return(-EINVAL);
+
+                       /* we kill the channel later, when we received some
+                          data. */
+                       bc->addr= frm->addr;
                }
+               
                cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
                
                bc->upset=2;
@@ -1976,6 +1979,7 @@ int handle_bchan(msg_t *msg)
                free_msg(msg);
                
                manager_bchannel_activate(bc);
+                               
                return 1;
        case MGR_DELLAYER| CONFIRM:
                cb_log(2, stack->port, "BCHAN: MGR_DELLAYER|CNF \n");
@@ -1993,7 +1997,7 @@ int handle_bchan(msg_t *msg)
        case DL_ESTABLISH | CONFIRM:
                cb_log(4, stack->port, "BCHAN: bchan ACT Confirm\n");
                free_msg(msg);
-
+               
                return 1;    
                
        case PH_DEACTIVATE | INDICATION:
@@ -2065,42 +2069,20 @@ int handle_bchan(msg_t *msg)
                                
                                if ( misdn_cap_is_speech(bc->capability)) {
                                        if ( !bc->nojitter ) {
-                                               char buf[4096 + mISDN_HEADER_LEN];
-                                               iframe_t *txfrm= (iframe_t*)buf;
-                                               int len, r;
-                                               
-                                               len=cb_jb_empty(bc,&buf[mISDN_HEADER_LEN],frm->len);
-
-                                               if (len) {
-                                                       
-                                                       if (len < frm->len) {
-                                                               cb_log(0,bc->port,"Jitterbuffer Underrun.\n");
-                                                       }
-                                                       
-                                                       txfrm->prim = DL_DATA|REQUEST;
-                                                       
-                                                       txfrm->dinfo = 0;
-                                                       
-                                                       txfrm->addr = bc->addr|FLG_MSG_DOWN; /*  | IF_DOWN; */
-
-                                                       txfrm->len = len;
-                                                       cb_log(9, stack->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
-                                                       
-                                                       r=mISDN_write(stack->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
-                                               } else {
-                                                       //jb is filling
-                                               }
+                                               misdn_tx_jitter(bc,frm->len);
                                        }
                                }
                                
-                               cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
+                               int i=cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
+                               if (i<0) {
+                                       clean_up_bc(bc);
+                               }
                        }
                }
                free_msg(msg);
                return 1;
        }
 
-
        case PH_DATA | CONFIRM:
        case DL_DATA|CONFIRM:
 #if MISDN_DEBUG
@@ -2109,8 +2091,14 @@ int handle_bchan(msg_t *msg)
 
 #endif
                free_msg(msg);
+
+#ifdef ACK_HDLC
+               if (bc->hdlc) {
+                       cb_log(4,stack->port,"Acknowledge Packet\n");
+                       sem_post( (sem_t*)bc->ack_hdlc);
+               }
+#endif
                return 1;
-               break;
        case DL_DATA|RESPONSE:
 #if MISDN_DEBUG
                cb_log(-1, stack->port, "Data response\n");
@@ -2386,8 +2374,6 @@ int handle_mgmt(msg_t *msg)
                return 0;
        }
        
-       //cb_log(1, 0, "MGMT: Found Stack: addr:%x dinfo:%x prim:%x\n",frm->addr,frm->dinfo, frm->prim) ;
-       
        switch(frm->prim) {
        case MGR_SHORTSTATUS | INDICATION:
                cb_log(2, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
@@ -2491,8 +2477,6 @@ static void misdn_lib_isdn_event_catcher(void *arg)
        int midev= mgr->midev;
        int port=0;
        
-       //cb_log(5, 0, "In event_catcher thread\n");
-       
        while (1) {
                msg_t *msg = fetch_msg(midev); 
                iframe_t *frm;
@@ -2502,9 +2486,6 @@ static void misdn_lib_isdn_event_catcher(void *arg)
                
                frm = (iframe_t*) msg->data;
                
-               //port = frm->addr&CHANNEL_NUMBER;
-               
-               
                /** When we make a call from NT2Ast we get this frames **/
                if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
                        zero_frm++; 
@@ -2641,6 +2622,25 @@ struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel)
        return NULL;
 }
 
+
+char *fac2str (enum facility_type type) {
+       struct arr_el { 
+               enum facility_type p; 
+               char *s ; 
+       } arr[] = {
+               { FACILITY_NONE, "FAC_NONE" },
+               { FACILITY_CALLDEFLECT, "FAC_CALLDEFLECT"},
+               { FACILITY_CENTREX, "FAC_CENTREX"}
+       };
+       
+       int i;
+       
+       for (i=0; i < sizeof(arr)/sizeof( struct arr_el) ; i ++)
+               if ( arr[i].p==type) return arr[i].s;
+       
+       return "FAC_UNKNOWN";
+}
+
 void misdn_lib_log_ies(struct misdn_bchannel *bc)
 {
        if (!bc) return;
@@ -2652,6 +2652,8 @@ void misdn_lib_log_ies(struct misdn_bchannel *bc)
 
        cb_log(2, stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", stack->nt?"NT":"TE", bc->cause, bc->out_cause, bc->rad);
        
+       cb_log(3, stack->port, " --> facility:%s out_facility:%s\n",fac2str(bc->fac_type),fac2str(bc->out_fac_type));
+       
        cb_log(2, stack->port,
               " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c\n",
               bc->info_dad,
@@ -2659,6 +2661,8 @@ void misdn_lib_log_ies(struct misdn_bchannel *bc)
               bc->dnumplan>=0?'0'+bc->dnumplan:' ',
               bc->rnumplan>=0?'0'+bc->rnumplan:' '
                );
+       cb_log(3, stack->port, " --> screen:%d --> pres:%d\n",
+                       bc->screen, bc->pres);
        
        cb_log(2, stack->port, " --> channel:%d caps:%s pi:%x keypad:%s\n", bc->channel, bearer2str(bc->capability),bc->progress_indicator, bc->keypad);
 
@@ -2707,68 +2711,66 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
                
 
        case EVENT_CONNECT:
-       case EVENT_PROCEEDING:
        case EVENT_PROGRESS:
        case EVENT_ALERTING:
+       case EVENT_PROCEEDING:
        case EVENT_SETUP_ACKNOWLEDGE:
        case EVENT_RETRIEVE_ACKNOWLEDGE:
-               
                if (stack->nt) {
                        if (bc->channel <=0 ) { /*  else we have the channel already */
                                bc->channel = find_free_chan_in_stack(stack, 0);
                                if (!bc->channel) {
                                        cb_log(-1, stack->port, " No free channel at the moment\n");
-
+                                       
                                        err=-ENOCHAN;
                                        goto ERR;
                                }
                        }
                        /* Its that i generate channels */
                }
-
-               int ret=setup_bc(bc);
-               if (ret == -EINVAL){
-                       cb_log(-1,bc->port,"send_event: setup_bc failed\n");
-                       
-               }
                
+               if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
+                       int ret=setup_bc(bc);
+                       if (ret == -EINVAL){
+                               cb_log(-1,bc->port,"send_event: setup_bc failed\n");
+                               
+                       }
+               } 
                
-               if ( event == EVENT_CONNECT ) {
+               
+               if ( (event == EVENT_CONNECT ) && misdn_cap_is_speech(bc->capability) ) {
                        if ( *bc->crypt_key ) {
                                cb_log(4, stack->port,  " --> ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s \n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
                                
                                manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
                        }
                        
-                       if ( misdn_cap_is_speech(bc->capability)) {
-                               if (!bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
-                               
-                               if (bc->ec_enable) manager_ec_enable(bc);
-                               
-                               if (bc->txgain != 0) {
-                                       cb_log(4, stack->port,  "--> Changing txgain to %d\n", bc->txgain);
-                                       manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
-                               }
-                               
-                               if ( bc->rxgain != 0 ) {
-                                       cb_log(4, stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
-                                       manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
-                               }
+                       
+                       if (!bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
+                       
+                       if (bc->ec_enable) manager_ec_enable(bc);
+                       
+                       if (bc->txgain != 0) {
+                               cb_log(4, stack->port,  "--> Changing txgain to %d\n", bc->txgain);
+                               manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
+                       }
+                       
+                       if ( bc->rxgain != 0 ) {
+                               cb_log(4, stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
+                               manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
                        }
+                       
                }
                
                if (event == EVENT_RETRIEVE_ACKNOWLEDGE) {
                        manager_bchannel_activate(bc);
                }
-               
                break;
-    
+
        case EVENT_HOLD_ACKNOWLEDGE:
        {
                struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel));
-               
                memcpy(holded_bc,bc,sizeof(struct misdn_bchannel));
-               
                holded_bc->holded=1;
                stack_holder_add(stack,holded_bc);
                
@@ -2790,9 +2792,7 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
                break;
                
        case EVENT_RELEASE_COMPLETE:
-               empty_chan_in_stack(stack,bc->channel);
-               empty_bc(bc);
-               clean_up_bc(bc);
+               /*we do the cleanup in EVENT_CLEANUP*/
                break;
     
        case EVENT_CONNECT_ACKNOWLEDGE:
@@ -2816,11 +2816,8 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
        }
   
        /* Later we should think about sending bchannel data directly to misdn. */
-       
        msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
-
        msg_queue_tail(&stack->downqueue, msg);
-
        sem_post(&glob_mgr->new_msg);
   
        return 0;
@@ -3022,18 +3019,14 @@ void manager_event_handler(void *arg)
                     stack;
                     stack=stack->next ) { 
                        while ( (msg=msg_dequeue(&stack->downqueue)) ) {
-                               
-                               if (stack->nt ){
+                               if (stack->nt ) {
                                        if (stack->nst.manager_l3(&stack->nst, msg))
                                                cb_log(-1, stack->port, "Error@ Sending Message in NT-Stack.\n");
          
                                } else {
-                                       if (msg) {
-                                               iframe_t *frm = (iframe_t *)msg->data;
-                                               struct misdn_bchannel *bc = find_bc_by_l3id(stack, frm->dinfo);
-           
-                                               if (bc) send_msg(glob_mgr->midev, bc, msg);
-                                       }
+                                       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);
                                }
                        }
                }
@@ -3067,13 +3060,16 @@ int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_dat
        cb_log = iface->cb_log;
        cb_event = iface->cb_event;
        cb_jb_empty = iface->cb_jb_empty;
-       cb_clearl3_true = iface->cb_clearl3_true;
        
        glob_mgr = mgr;
   
        msg_init();
        debug_init(0 , NULL, NULL, NULL);
-       
+#if 0
+       int flags=0xff;
+       flags &= ~DBGM_MSG;
+       debug_init( flags , NULL, NULL, NULL);
+#endif 
        if (!portlist || (*portlist == 0) ) return 1;
        
        init_flip_bits();
@@ -3221,8 +3217,12 @@ void manager_bchannel_activate(struct misdn_bchannel *bc)
 {
        msg_t *msg=alloc_msg(MAX_MSG_SIZE);
        iframe_t *frm;
-
+       
        struct misdn_stack *stack=get_stack_by_bc(bc);
+
+       if (!stack) {
+               cb_log(-1, stack->port, "bchannel_activate: Stack not found !");
+       }
        
        if (!msg) {
                cb_log(-1, stack->port, "bchannel_activate: alloc_msg failed !");
@@ -3269,8 +3269,8 @@ void manager_bchannel_deactivate(struct misdn_bchannel * bc)
   
        cb_log(5, stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
   
-       bc->tone=TONE_NONE;
-
+       bc->generate_tone=0;
+       
        dact.prim = DL_RELEASE | REQUEST;
        dact.addr = bc->addr | FLG_MSG_DOWN;
        dact.dinfo = 0;
@@ -3305,23 +3305,18 @@ int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
        
        cb_log(9, stack->port, "Writing %d bytes 2 mISDN\n",len);
        r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
-       
+#ifdef ACK_HDLC
+       if (bc->hdlc && bc->ack_hdlc) {
+               cb_log(4,stack->port,"Awaiting Acknowledge [%d]\n",len);
+               sem_wait((sem_t*)bc->ack_hdlc);
+               cb_log(4,stack->port,"Acknowledged\n");
+       }
+#endif 
        return 0;
 }
 
 
 
-
-void manager_send_tone (struct misdn_bchannel *bc, enum tone_e tone)
-{
-       if (tone != TONE_NONE) manager_bchannel_activate(bc);
-       bc->tone=tone;
-       bc->tone_cnt2=-1;
-       bc->tone_cnt=0;
-}
-
-
-
 /*
  * send control information to the channel (dsp-module)
  */
@@ -3398,6 +3393,7 @@ void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder)
             help=help->next) {
                if (!help->next) {
                        help->next=holder;
+                       break;
                }
        }
   
index a27e7c5..7780424 100644 (file)
@@ -31,15 +31,6 @@ enum bc_state_e {
        STATE_HOLD_ACKNOWLEDGE
 };
 
-
-enum tone_e {
-       TONE_NONE=0,
-       TONE_DIAL,
-       TONE_ALERTING,
-       TONE_BUSY,
-       TONE_FILE
-};
-
 enum misdn_err_e {
        ENOCHAN=1
 };
@@ -66,7 +57,9 @@ enum event_response_e {
 
 enum event_e {
        EVENT_NOTHING,
+       EVENT_TONE_GENERATE,
        EVENT_BCHAN_DATA,
+       EVENT_BCHAN_ACTIVATED,
        EVENT_CLEANUP,
        EVENT_PROCEEDING,
        EVENT_PROGRESS,
@@ -173,6 +166,8 @@ struct misdn_bchannel {
        /* int b_addr; */
        int layer_id;
 
+       void *ack_hdlc;
+       
        int layer;
        
        
@@ -203,6 +198,9 @@ struct misdn_bchannel {
        int dtmf;
        int send_dtmf;
 
+       /* get setup ack */
+       int need_more_infos;
+
        /* wether we should use jollys dsp or not */
        int nodsp;
        
@@ -242,9 +240,8 @@ struct misdn_bchannel {
        int active;
        int upset;
 
-       enum tone_e tone;
+       int generate_tone;
        int tone_cnt;
-       int tone_cnt2;
   
        enum bc_state_e state;
 
@@ -262,7 +259,7 @@ struct misdn_bchannel {
 
        int user1;
        int urate;
-       int async;
+       int hdlc;
        /* V110 */
   
        unsigned char display[84];
@@ -304,7 +301,6 @@ struct misdn_bchannel {
 
 enum event_response_e (*cb_event) (enum event_e event, struct misdn_bchannel *bc, void *user_data);
 void (*cb_log) (int level, int port, char *tmpl, ...);
-int (*cb_clearl3_true)(void);
 int (*cb_jb_empty)(struct misdn_bchannel *bc, char *buffer, int len);
 
 struct misdn_lib_iface {
@@ -312,7 +308,6 @@ struct misdn_lib_iface {
        enum event_response_e (*cb_event)(enum event_e event, struct misdn_bchannel *bc, void *user_data);
        void (*cb_log)(int level, int port, char *tmpl, ...);
        int (*cb_jb_empty)(struct misdn_bchannel *bc, char *buffer, int len);
-       int (*cb_clearl3_true)(void);
 };
 
 /***** USER IFACE **********/
@@ -334,8 +329,6 @@ void manager_bchannel_deactivate(struct misdn_bchannel * bc);
 
 int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len);
 
-void manager_send_tone (struct misdn_bchannel *bc, enum tone_e tone);
-
 void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2);
 
 
@@ -375,6 +368,10 @@ void *misdn_init_ibuffer(int len);
 
 /** Ibuf interface End **/
 
+void misdn_lib_tone_generator_start(struct misdn_bchannel *bc);
+void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc);
+
+
 void misdn_lib_setup_bc(struct misdn_bchannel *bc);
 
 void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2);
index 5cf921a..8d37c69 100644 (file)
 
 #include "fac.h"
 
+
+void set_channel(struct misdn_bchannel *bc, int channel) {
+
+       cb_log(3,bc->port,"set_channel: bc->channel:%d channel:%d\n", bc->channel, channel);
+       
+       
+       if (channel==0xff) {
+               /* any channel */
+               channel=-1;
+       }
+       
+       /*  ALERT: is that everytime true ?  */
+       if (channel > 0 && bc->nt ) {
+               
+               if (bc->channel && ( bc->channel != 0xff) ) {
+                       cb_log(0,bc->port,"We already have a channel (%d)\n", bc->channel);
+               } else {
+                       bc->channel = channel;
+               }
+       }
+       
+       if (channel > 0 && !bc->nt ) 
+               bc->channel = channel;
+}
+
 void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
        int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
        CALL_PROCEEDING_t *proceeding=(CALL_PROCEEDING_t*)((unsigned long)msg->data+ HEADER_LEN);
-       struct misdn_stack *stack=get_stack_by_bc(bc);
+       //struct misdn_stack *stack=get_stack_by_bc(bc);
        
        {
                int  exclusive, channel;
                dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)proceeding, &exclusive, &channel, nt,bc);
-               
-               if (channel==0xff) /* any channel */
-                       channel=-1;
-    
-               /*  ALERT: is that everytime true ?  */
 
-               if (channel > 0 && stack->nt) 
-                       bc->channel = channel;
+               set_channel(bc,channel);
+               
        }
        
        dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)proceeding, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
@@ -197,29 +217,22 @@ void parse_setup (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc,
                dec_ie_bearer(setup->BEARER, (Q931_info_t *)setup, &coding, &capability, &mode, &rate, &multi, &user, &async, &urate, &stopbits, &dbits, &parity, nt,bc);
                switch (capability) {
                case -1: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; 
-//                     cb_log(2, bc->stack->port, " --> cap -1 -> digital\n");
                        break;
                case 0: bc->capability=INFO_CAPABILITY_SPEECH;
-//                     cb_log(2, bc->stack->port, " --> cap speech\n");
                        break;
                case 8: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
                        bc->user1 = user;
-                       bc->async = async;
                        bc->urate = urate;
                        
                        bc->rate = rate;
                        bc->mode = mode;
-                       
-//                     cb_log(2, bc->stack->port, " --> cap unres Digital (user l1 %d, async %d, user rate %d\n", user, async, urate);
                        break;
                case 9: bc->capability=INFO_CAPABILITY_DIGITAL_RESTRICTED;
-//                     cb_log(2, bc->stack->port, " --> cap res Digital\n");
                        break;
                default:
-//                     cb_log(2, bc->stack->port, " --> cap Else\n");
                        break;
                }
-
+               
                switch(user) {
                case 2:
                        bc->law=INFO_CODEC_ULAW;
@@ -237,11 +250,8 @@ void parse_setup (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc,
        {
                int  exclusive, channel;
                dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)setup, &exclusive, &channel, nt,bc);
-               if (channel==0xff) /* any channel */
-                       channel=-1;
-
-               if (channel > 0) 
-                       bc->channel = channel;
+               
+               set_channel(bc,channel);
        }
        
        dec_ie_progress(setup->PROGRESS, (Q931_info_t *)setup, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
@@ -260,9 +270,10 @@ msg_t *build_setup (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
 //     cb_log(2, bc->stack->port, " --> oad %s dad %s channel %d\n",bc->oad, bc->dad,bc->channel);
        if (bc->channel == 0 || bc->channel == ANY_CHANNEL || bc->channel==-1)
                enc_ie_channel_id(&setup->CHANNEL_ID, msg, 0, bc->channel, nt,bc);
-       else
+       else 
                enc_ie_channel_id(&setup->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
-  
+       
+       
        {
                int type=bc->onumplan,plan=1,present=bc->pres,screen=bc->screen;
                enc_ie_calling_pn(&setup->CALLING_PN, msg, type, plan, present,
@@ -281,31 +292,30 @@ msg_t *build_setup (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
        {
                int coding=0, capability, mode=0 /*  2 for packet ! */
                        ,user, rate=0x10;
+
+               switch (bc->law) {
+               case INFO_CODEC_ULAW: user=2;
+                       break;
+               case INFO_CODEC_ALAW: user=3;
+                       break;
+               default:
+                       user=3;
+               }
+               
                switch (bc->capability) {
                case INFO_CAPABILITY_SPEECH: capability = 0;
-//                     cb_log(2, bc->stack->port, " --> Speech\n");
                        break;
                case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: capability = 8;
-//                     cb_log(2, bc->stack->port, " --> cap unres Digital\n");
+                       user=-1;
                        break;
                case INFO_CAPABILITY_DIGITAL_RESTRICTED: capability = 9;
-//                     cb_log(2, bc->stack->port, " --> cap res Digital\n");
+                       user=-1;
                        break;
                default:
-//                     cb_log(2, bc->stack->port, " --> cap Speech\n");
                        capability=bc->capability; 
                }
                
-               switch (bc->law) {
-               case INFO_CODEC_ULAW: user=2;
-//                     cb_log(2, bc->stack->port, " --> Codec Ulaw\n");
-                       break;
-               case INFO_CODEC_ALAW: user=3;
-//                     cb_log(2, bc->stack->port, " --> Codec Alaw\n");
-                       break;
-               default:
-                       user=3;
-               }
+               
     
                enc_ie_bearer(&setup->BEARER, msg, coding, capability, mode, rate, -1, user, nt,bc);
        }
@@ -366,11 +376,8 @@ void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_b
                int  exclusive, channel;
                dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)setup_acknowledge, &exclusive, &channel, nt,bc);
 
-               if (channel==0xff) /* any channel */
-                       channel=-1;
 
-               if (channel > 0) 
-                       bc->channel = channel;
+               set_channel(bc, channel);
        }
        
        dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)setup_acknowledge, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
@@ -1164,7 +1171,7 @@ int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int
        for (i=0; i< msgs_max; i++) 
                if ( event == msgs[i].event) return i;
 
-       cb_log(4,0, "get_index: EVENT NOT FOUND!!\n");
+       cb_log(10,0, "get_index: event not found!\n");
        
        return -1;
 }
@@ -1189,6 +1196,8 @@ char EVENT_DTMF_TONE_INFO[] = "DTMF_TONE";
 char EVENT_NEW_L3ID_INFO[] = "NEW_L3ID";
 char EVENT_NEW_BC_INFO[] = "NEW_BC";
 char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA";
+char EVENT_BCHAN_ACTIVATED_INFO[] = "BCHAN_ACTIVATED";
+char EVENT_TONE_GENERATE_INFO[] = "TONE_GENERATE";
 
 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
 {
@@ -1201,6 +1210,8 @@ char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
        if (event == EVENT_NEW_L3ID) return EVENT_NEW_L3ID_INFO;
        if (event == EVENT_NEW_BC) return EVENT_NEW_BC_INFO;
        if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO;
+       if (event == EVENT_BCHAN_ACTIVATED) return EVENT_BCHAN_ACTIVATED_INFO;
+       if (event == EVENT_TONE_GENERATE) return EVENT_TONE_GENERATE_INFO;
        
        return NULL;
 }
index d10f33b..b3f82b6 100644 (file)
@@ -8,6 +8,10 @@
 ;
 
 [general] 
+;
+; Sets the Path to the misdn-init.conf (for nt_ptp mode checking)
+;
+misdn_init=/etc/misdn-init.conf
 
 ; set debugging flag: 
 ;   0 - No Debug
@@ -55,20 +59,6 @@ stop_tone_after_first_digit=yes
 ;
 append_digits2exten=yes
 
-; set this to yes if you have jollys mISDN which sends correct L1 Infos
-;
-; default value: yes
-;
-l1_info_ok=yes
-
-; set this to yes if you want to clear the l3 in case the l2 deactivates
-; some environments have a flickering l2 which causes this option to 
-; damage active calls .. highly experimental
-; 
-; default value: no
-;
-clear_l3=no
-
 ;;; CRYPTION STUFF
 
 ; Wether to look for dynamic crypting attempt
@@ -151,6 +141,16 @@ txgain=0
 ;
 te_choose_channel=no
 
+
+;
+; Send Setup_Acknowledge on incoming calls anyway (instead of PROCEEDING), 
+; this requests additional Infos, so we can waitfordigits 
+; without much issues. This works only for PTP Ports
+; 
+; default value: no
+;
+need_more_infos=no
+
 ; set the method to use for channel selection:
 ;   standard    - always choose the first free channel with the lowest number
 ;   round_robin - use the round robin algorithm to select a channel. use this
@@ -177,14 +177,6 @@ method=standard
 dialplan=0
 localdialplan=0
 
-; This is only for asterisk head and will result in only considering 
-; misdn.confs and misdn_set_opts callingpresentation informations if set to no.
-; Otherwise asterisks callingpresentation overwrites misdn.confs settings.
-;
-; default value: yes
-;
-use_callingpres=yes
-
 
 
 ;
@@ -223,17 +215,17 @@ early_bconnect=yes
 ;callgroup=1
 ;pickupgroup=1
 
-; Allows/Screens Callerid
-;
-; possible values: allowed,not_screened
-;
-; be aware, if you set to allowed you need to set a correct 
-; callerid in the dialplan or set it here in the misdn.conf
-; Some Telcos don't care about wrong callerids, others do !
-;
-; default value: allowed
+
 ;
-;presentation=not_screened
+; these are the exact isdn screening and presentation indicators
+; if -1 is given for both values the presentation indicators are used
+; from asterisks SetCallerPres application.
+; s=0, p=0 -> callerid presented not screened
+; s=1, p=1 -> callerid presented but screened (the remote end does not see it!)
+; 
+; defaule values s=-1, p=-1
+presentation=-1
+screen=-1
 
 ; this enables echocancellation, with the given number of taps
 ; be aware, move this setting only to outgoing portgroups!
@@ -276,10 +268,12 @@ ports=1,2
 context=Intern
 
 [internPP]
-; if you want to have pp Protocol on one nt Port, you need 
-; to add a ptp directly after the portnumber, you can still add 
-; more ports and multiple ptp adds in your config.
-ports=3ptp
+;
+; adding the postfix 'ptp' to a port number is obsolete now, chan_misdn
+; parses /etc/misdn-init.conf and sets the ptp mode to the corresponding
+; configs. For backwards compatibility you can still set ptp here.
+;
+ports=3
        
 [first_extern]
 ; again port defs