committed head of chan_misdn with a lot of new features. Most important of all: chan_...
authorChristian Richter <christian.richter@beronet.com>
Fri, 9 Dec 2005 11:01:18 +0000 (11:01 +0000)
committerChristian Richter <christian.richter@beronet.com>
Fri, 9 Dec 2005 11:01:18 +0000 (11:01 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@7413 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/chan_misdn.c
channels/chan_misdn_config.c
channels/misdn/Makefile
channels/misdn/chan_misdn_config.h
channels/misdn/fac.c [new file with mode: 0644]
channels/misdn/fac.h [new file with mode: 0644]
channels/misdn/ie.c
channels/misdn/isdn_lib.c
channels/misdn/isdn_lib.h
channels/misdn/isdn_lib_intern.h
channels/misdn/isdn_msg_parser.c

index 1abe043..9d3b0c0 100644 (file)
@@ -68,6 +68,38 @@ ast_mutex_t release_lock_mutex;
 #define release_unlock ast_mutex_unlock(&release_lock_mutex)
 
 
+char global_tracefile[BUFFERSIZE];
+
+
+struct misdn_jb{
+       int size;
+       int upper_threshold;
+       char *samples, *ok;
+       int wp,rp;
+       int state_empty;
+       int state_full;
+       int state_buffer;
+       int bytes_wrote;
+       ast_mutex_t mutexjb;
+};
+
+/* allocates the jb-structure and initialise the elements*/
+struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
+
+/* frees the data and destroys the given jitterbuffer struct */
+void misdn_jb_destroy(struct misdn_jb *jb);
+
+/* fills the jitterbuffer with len data returns < 0 if there was an
+error (bufferoverun). */
+int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
+
+/* gets len bytes out of the jitterbuffer if available, else only the
+available data is returned and the return value indicates the number
+of data. */
+int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
+
+
+
 /* BEGIN: chan_misdn.h */
 
 enum misdn_chan_state {
@@ -113,6 +145,8 @@ struct chan_list {
        int faxhandled;
 
        int ast_dsp;
+
+       struct misdn_jb *jb;
        
        struct ast_dsp *dsp;
        struct ast_trans_pvt *trans;
@@ -124,6 +158,8 @@ struct chan_list {
 
        unsigned int l3id;
        int addr;
+
+       char context[BUFFERSIZE];
        
        struct chan_list *peer;
        struct chan_list *next;
@@ -142,11 +178,11 @@ static struct robin_list *robin = NULL;
 
 static inline void free_robin_list_r (struct robin_list *r)
 {
-       if (r) {
-               if (r->next) free_robin_list_r(r->next);
-               if (r->group) free(r->group);
-               free(r);
-       }
+        if (r) {
+                if (r->next) free_robin_list_r(r->next);
+                if (r->group) free(r->group);
+                free(r);
+        }
 }
 
 static void free_robin_list ( void )
@@ -155,7 +191,7 @@ static void free_robin_list ( void )
        robin = NULL;
 }
 
-struct robin_list* get_robin_position (char *group) 
+static struct robin_list* get_robin_position (char *group) 
 {
        struct robin_list *iter = robin;
        for (; iter; iter = iter->next) {
@@ -173,8 +209,11 @@ struct robin_list* get_robin_position (char *group)
        return robin;
 }
 
-struct ast_channel *misdn_new(struct chan_list *cl, int state, char * name, char * context, char *exten, char *callerid, int format, int port, int c);
-void send_digit_to_chan(struct chan_list *cl, char digit );
+
+static void chan_misdn_log(int level, int port, char *tmpl, ...);
+
+static struct ast_channel *misdn_new(struct chan_list *cl, int state,  char *exten, char *callerid, int format, int port, int c);
+static void send_digit_to_chan(struct chan_list *cl, char digit );
 
 
 #define AST_CID_P(ast) ast->cid.cid_num
@@ -186,46 +225,46 @@ void send_digit_to_chan(struct chan_list *cl, char digit );
 #define MISDN_ASTERISK_PVT(ast) 1
 #define MISDN_ASTERISK_TYPE(ast) ast->tech->type
 
-/* END: chan_misdn.h */
-
 #include <asterisk/strings.h>
 
 /* #define MISDN_DEBUG 1 */
 
-static  char *desc = "Channel driver for mISDN Support (Bri/Pri)";
-static  char *type = "mISDN";
+static char *desc = "Channel driver for mISDN Support (Bri/Pri)";
+static char *misdn_type = "mISDN";
 
-int tracing = 0 ;
+static int tracing = 0 ;
 
 static int usecnt=0;
 
-char **misdn_key_vector=NULL;
-int misdn_key_vector_size=0;
+static char **misdn_key_vector=NULL;
+static int misdn_key_vector_size=0;
 
 /* Only alaw and mulaw is allowed for now */
 static int prefformat =  AST_FORMAT_ALAW ; /*  AST_FORMAT_SLINEAR ;  AST_FORMAT_ULAW | */
 
 static ast_mutex_t usecnt_lock; 
 
-int *misdn_debug;
-int *misdn_debug_only;
-int max_ports;
+static int *misdn_debug;
+static int *misdn_debug_only;
+static int max_ports;
 
 struct chan_list dummy_cl;
 
 struct chan_list *cl_te=NULL;
 ast_mutex_t cl_te_lock;
 
-enum event_response_e
+static enum event_response_e
 cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
 
-void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc);
+static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc);
+
+static void cl_queue_chan(struct chan_list **list, struct chan_list *chan);
+static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan);
+static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc);
+
+
+
 
-void cl_queue_chan(struct chan_list **list, struct chan_list *chan);
-void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan);
-struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc);
-void chan_misdn_log(int level, int port, char *tmpl, ...);
-void chan_misdn_trace_call(struct ast_channel *chan, int debug, char *tmpl, ...);
 
 static int start_bc_tones(struct chan_list *cl);
 static int stop_bc_tones(struct chan_list *cl);
@@ -234,6 +273,8 @@ static void release_chan(struct misdn_bchannel *bc);
 static int misdn_set_opt_exec(struct ast_channel *chan, void *data);
 static int misdn_facility_exec(struct ast_channel *chan, void *data);
 
+int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len);
+
 /*************** Helpers *****************/
 
 static struct chan_list * get_chan_by_ast(struct ast_channel *ast)
@@ -306,6 +347,24 @@ static char *bearer2str(int cap) {
        }
 }
 
+
+static void print_facility( struct misdn_bchannel *bc)
+{
+       switch (bc->fac_type) {
+       case FACILITY_CALLDEFLECT:
+               chan_misdn_log(2,bc->port," --> calldeflect: %s\n",
+                              bc->fac.calldeflect_nr);
+               break;
+       case FACILITY_CENTREX:
+               chan_misdn_log(2,bc->port," --> centrex: %s\n",
+                              bc->fac.cnip);
+               break;
+       default:
+               chan_misdn_log(2,bc->port," --> unknown\n");
+               
+       }
+}
+
 static void print_bearer(struct misdn_bchannel *bc) 
 {
        
@@ -322,7 +381,7 @@ static void print_bearer(struct misdn_bchannel *bc)
 }
 /*************** Helpers END *************/
 
-void send_digit_to_chan(struct chan_list *cl, char digit )
+static void send_digit_to_chan(struct chan_list *cl, char digit )
 {
        static const char* dtmf_tones[] = {
                "!941+1336/100,!0/100", /* 0 */
@@ -416,10 +475,9 @@ static int misdn_set_debug(int fd, int argc, char *argv[])
        return 0;
 }
 
-
 static int misdn_set_crypt_debug(int fd, int argc, char *argv[])
 {
-       if (argc != 5 )return RESULT_SHOWUSAGE; 
+       if (argc != 5) return RESULT_SHOWUSAGE; 
 
        return 0;
 }
@@ -505,14 +563,12 @@ static int misdn_show_config (int fd, int argc, char *argv[])
        return 0;
 }
 
-
-
 struct state_struct {
        enum misdn_chan_state state;
        char txt[255] ;
 } ;
 
-struct state_struct state_array[] = {
+static struct state_struct state_array[] = {
        {MISDN_NOTHING,"NOTHING"}, /* at beginning */
        {MISDN_WAITING4DIGS,"WAITING4DIGS"}, /*  when waiting for infos */
        {MISDN_EXTCANTMATCH,"EXTCANTMATCH"}, /*  when asterisk couldnt match our ext */
@@ -531,10 +587,7 @@ struct state_struct state_array[] = {
        /* misdn_hangup */
 };
 
-
-
-
-char *misdn_get_ch_state(struct chan_list *p) 
+static char *misdn_get_ch_state(struct chan_list *p) 
 {
        int i;
        if( !p) return NULL;
@@ -551,19 +604,14 @@ static int misdn_reload (int fd, int argc, char *argv[])
        int i, cfg_debug;
        
        ast_cli(fd, "Reloading mISDN Config\n");
-       chan_misdn_log(0, 0, "Dynamic Crypting Activation is not support during reload at the moment\n");
+       chan_misdn_log(-1, 0, "Dynamic Crypting Activation is not support during reload at the moment\n");
        
        free_robin_list();
 
        misdn_cfg_reload();
-
-       {
-               char tempbuf[BUFFERSIZE];
-               misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, tempbuf, BUFFERSIZE);
-               if (strlen(tempbuf))
-                       tracing = 1;
-       }
-
+       
+       misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
+       
        misdn_cfg_get( 0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(int));
        for (i = 0;  i <= max_ports; i++) {
                misdn_debug[i] = cfg_debug;
@@ -578,6 +626,7 @@ static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel
        struct ast_channel *ast=help->ast;
        ast_cli(fd,
                "* Pid:%d Prt:%d Ch:%d Mode:%s Org:%s dad:%s oad:%s ctx:%s state:%s\n",
+
                bc->pid, bc->port, bc->channel,
                bc->nt?"NT":"TE",
                help->orginator == ORG_AST?"*":"I",
@@ -617,7 +666,6 @@ static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel
   
 }
 
-
 static int misdn_show_cls (int fd, int argc, char *argv[])
 {
        struct chan_list *help=cl_te;
@@ -642,8 +690,6 @@ static int misdn_show_cls (int fd, int argc, char *argv[])
        return 0;
 }
 
-
-
 static int misdn_show_cl (int fd, int argc, char *argv[])
 {
        struct chan_list *help=cl_te;
@@ -680,13 +726,12 @@ static int misdn_set_tics (int fd, int argc, char *argv[])
        return 0;
 }
 
-
-
 static int misdn_show_stacks (int fd, int argc, char *argv[])
 {
        int port;
        
        ast_cli(fd, "BEGIN STACK_LIST:\n");
+
        for (port=misdn_cfg_get_next_port(0); port > 0;
             port=misdn_cfg_get_next_port(port)) {
                char buf[128];
@@ -746,8 +791,6 @@ static int misdn_send_cd (int fd, int argc, char *argv[])
        return 0; 
 }
 
-
-
 static int misdn_send_digit (int fd, int argc, char *argv[])
 {
        char *channame; 
@@ -819,8 +862,6 @@ static int misdn_toggle_echocancel (int fd, int argc, char *argv[])
        return 0; 
 }
 
-
-
 static int misdn_send_display (int fd, int argc, char *argv[])
 {
        char *channame; 
@@ -851,9 +892,6 @@ static int misdn_send_display (int fd, int argc, char *argv[])
        return RESULT_SUCCESS ;
 }
 
-
-
-
 static char *complete_ch_helper(char *line, char *word, int pos, int state, int rpos)
 {
        struct ast_channel *c;
@@ -909,7 +947,6 @@ static struct ast_cli_entry cli_send_cd =
   complete_ch
 };
 
-
 static struct ast_cli_entry cli_send_digit =
 { {"misdn","send","digit", NULL},
   misdn_send_digit,
@@ -920,7 +957,6 @@ static struct ast_cli_entry cli_send_digit =
   complete_ch
 };
 
-
 static struct ast_cli_entry cli_toggle_echocancel =
 { {"misdn","toggle","echocancel", NULL},
   misdn_toggle_echocancel,
@@ -929,8 +965,6 @@ static struct ast_cli_entry cli_toggle_echocancel =
   complete_ch
 };
 
-
-
 static struct ast_cli_entry cli_send_display =
 { {"misdn","send","display", NULL},
   misdn_send_display,
@@ -941,7 +975,6 @@ static struct ast_cli_entry cli_send_display =
   complete_ch
 };
 
-
 static struct ast_cli_entry cli_show_config =
 { {"misdn","show","config", NULL},
   misdn_show_config,
@@ -949,7 +982,6 @@ static struct ast_cli_entry cli_show_config =
   "Usage: misdn show config [port | 0]\n       use 0 to only print the general config.\n"
 };
  
-
 static struct ast_cli_entry cli_reload =
 { {"misdn","reload", NULL},
   misdn_reload,
@@ -964,7 +996,6 @@ static struct ast_cli_entry cli_set_tics =
   "\n"
 };
 
-
 static struct ast_cli_entry cli_show_cls =
 { {"misdn","show","channels", NULL},
   misdn_show_cls,
@@ -989,7 +1020,6 @@ static struct ast_cli_entry cli_restart_port =
   "Usage: misdn restart port\n"
 };
 
-
 static struct ast_cli_entry cli_port_up =
 { {"misdn","port","up", NULL},
   misdn_port_up,
@@ -997,7 +1027,6 @@ static struct ast_cli_entry cli_port_up =
   "Usage: misdn port up <port>\n"
 };
 
-
 static struct ast_cli_entry cli_show_stacks =
 { {"misdn","show","stacks", NULL},
   misdn_show_stacks,
@@ -1012,8 +1041,6 @@ static struct ast_cli_entry cli_show_port =
   "Usage: misdn show port <port>\n"
 };
 
-
-
 static struct ast_cli_entry cli_set_debug =
 { {"misdn","set","debug", NULL},
   misdn_set_debug,
@@ -1031,245 +1058,444 @@ static struct ast_cli_entry cli_set_crypt_debug =
 /*** CLI END ***/
 
 
-/*****************************/
-/*** AST Indications Start ***/
-/*****************************/
 
-static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
-{
-       int port=0;
-       int r;
-       struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast);
-       struct misdn_bchannel *newbc;
-       char *opts=NULL, *ext=NULL;
-       char dest_cp[256];
-       char *tokb;
-       
-       {
-               strncpy(dest_cp,dest,sizeof(dest_cp)-1);
-               dest_cp[sizeof(dest_cp)]=0;
-               
-               ext=strtok_r(dest_cp,"/",&tokb);
-               
-               if (ext) {
-                       ext=strtok_r(NULL,"/",&tokb);
-                       if (ext) {
-                               opts=strtok_r(NULL,"/",&tokb);
-                       } else {
-                               chan_misdn_log(-1,0,"misdn_call: No Extension given!\n");
-                               return -1;
-                       }
-               }
-               
-               
-       }
+static int read_config(struct chan_list *ch, int orig) {
 
-       if (!ast) {
-               ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
+       if (!ch) {
+               ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
                return -1;
        }
 
-       if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest  ) {
-               ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
-               ast->hangupcause=41;
-               ast_setstate(ast, AST_STATE_DOWN);
+       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,"read_config: Getting Config\n");
 
+       char lang[BUFFERSIZE];
+       
 
-       if (!ch) {
-               ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
-               ast->hangupcause=41;
-               ast_setstate(ast, AST_STATE_DOWN);
-               return -1;
-       }
+       misdn_cfg_get( port, MISDN_CFG_LANGUAGE, lang, BUFFERSIZE);
+       ast_copy_string(ast->language, lang, sizeof(ast->language));
+       ast->language[sizeof(ast->language)]=0;
+
+
+       char musicclass[BUFFERSIZE];
        
-       newbc=ch->bc;
+       misdn_cfg_get( port, MISDN_CFG_MUSICCLASS, musicclass, BUFFERSIZE);
+       ast_copy_string(ast->musicclass, musicclass, sizeof(ast->musicclass));
+       ast->musicclass[sizeof(ast->musicclass)]=0;
        
-       if (!newbc) {
-               ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
-               ast->hangupcause=41;
-               ast_setstate(ast, AST_STATE_DOWN);
-               return -1;
+       
+       misdn_cfg_get( port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(int));
+       misdn_cfg_get( port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(int));
+       
+       misdn_cfg_get( port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(int));
+       
+
+       /*Initialize new Jitterbuffer*/
+       {
+               int jb_len,jb_upper_threshold;
+               misdn_cfg_get( port, MISDN_CFG_JITTERBUFFER, &jb_len, sizeof(int));
+               misdn_cfg_get( port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &jb_upper_threshold, sizeof(int));
+
+               if ( ! jb_len ) {
+                       chan_misdn_log(1,bc->port, "read_config: Deactivating Jitterbuffer\n");
+                       bc->nojitter=1;
+               } else {
+               
+                       if (jb_len <=100 || jb_len > 8000) {
+                               chan_misdn_log(-1,bc->port,"read_config: Jitterbuffer out of Bounds, setting to 1000\n");
+                               jb_len=1000;
+                       }
+                       
+                       if ( jb_upper_threshold > jb_len ) {
+                               chan_misdn_log(-1,bc->port,"read_config: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n");
+                       }
+                       
+                       
+                       if ( ch->jb) {
+                               cb_log(0,bc->port,"read_config: We've got a Jitterbuffer Already on this port.\n");
+                               misdn_jb_destroy(ch->jb);
+                               ch->jb=NULL;
+                       }
+                       
+                       ch->jb=misdn_jb_init(jb_len, jb_upper_threshold);
+               }
        }
        
-       port=newbc->port;
+       misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
+       strncpy(ast->context,ch->context,sizeof(ast->context)-1);
        
-       ast_copy_string(newbc->dad, ext, sizeof(newbc->dad));
-       ast_copy_string(ast->exten, ext, sizeof(ast->exten));
+       {
+               int ec, ectr;
+               
+               misdn_cfg_get( port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
+               
+               misdn_cfg_get( port, MISDN_CFG_ECHOTRAINING, &ectr, sizeof(int));
+               if (ec == 1 ) {
+                       bc->ec_enable=1;
+               } else if ( ec > 1 ) {
+                       bc->ec_enable=1;
+                       bc->ec_deftaps=ec;
+               }
+               
+               if ( ectr >= 0 ) {
+                       bc->ec_training=ectr;
+               }
+       }
        
        
-       chan_misdn_log(1, 0, "* CALL: %s\n",dest);
+       {
+               int eb3;
+               
+               misdn_cfg_get( bc->port, MISDN_CFG_EARLY_BCONNECT, &eb3, sizeof(int));
+               bc->early_bconnect=eb3;
+       }
        
-       chan_misdn_log(1, port, " --> * dad:%s tech:%s ctx:%s\n",ast->exten,ast->name, ast->context);
+       port=bc->port;
        
+
        {
-               char context[BUFFERSIZE];
+               char buf[256];
+               ast_group_t pg,cg;
                
-               misdn_cfg_get( port, MISDN_CFG_CONTEXT, context, sizeof(ast->context));
-               {
-                       int l = sizeof(ast->context);
-                       strncpy(ast->context,context, l);
-                       ast->context[l-1] = 0;
-               }
-               chan_misdn_log(2, port, " --> * Setting Context to %s\n",context);
-               misdn_cfg_get( port, MISDN_CFG_LANGUAGE, ast->language, BUFFERSIZE);
+               misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
+               misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
                
-               misdn_cfg_get( port, MISDN_CFG_TXGAIN, &newbc->txgain, sizeof(int));
-               misdn_cfg_get( port, MISDN_CFG_RXGAIN, &newbc->rxgain, sizeof(int));
+               chan_misdn_log(2, port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg));
+               ast->pickupgroup=pg;
+               ast->callgroup=cg;
+       }
+       
+       
+       if ( orig  == ORG_AST) {
+               misdn_cfg_get( port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(int));
                
-               misdn_cfg_get( port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(newbc->te_choose_channel), sizeof(int));
                
-
                {
                        char callerid[BUFFERSIZE];
                        misdn_cfg_get( port, MISDN_CFG_CALLERID, callerid, BUFFERSIZE);
                        if ( ! ast_strlen_zero(callerid) ) {
                                chan_misdn_log(1, port, " --> * Setting Cid to %s\n", callerid);
                                {
-                                       int l = sizeof(newbc->oad);
-                                       strncpy(newbc->oad,callerid, l);
-                                       newbc->oad[l-1] = 0;
+                                       int l = sizeof(bc->oad);
+                                       strncpy(bc->oad,callerid, l);
+                                       bc->oad[l-1] = 0;
                                }
-                               
-                               misdn_cfg_get( port, MISDN_CFG_DIALPLAN, &newbc->dnumplan, sizeof(int));
-                               switch (newbc->dnumplan) {
-                               case NUMPLAN_INTERNATIONAL:
-                               case NUMPLAN_NATIONAL:
-                               case NUMPLAN_SUBSCRIBER:
-                               case NUMPLAN_UNKNOWN:
-                                       /* Maybe we should cut off the prefix if present ? */
-                                       break;
-                               default:
+
+                       }
+
+                       
+                       misdn_cfg_get( port, MISDN_CFG_DIALPLAN, &bc->dnumplan, sizeof(int));
+                       switch (bc->dnumplan) {
+                       case NUMPLAN_INTERNATIONAL:
+                               chan_misdn_log(2, port, " --> TON: International\n");
+                               break;
+                       case NUMPLAN_NATIONAL:
+                               chan_misdn_log(2, port, " --> TON: National\n");
+                               break;
+                       case NUMPLAN_SUBSCRIBER:
+                               chan_misdn_log(2, port, " --> TON: Subscriber\n");
+                               break;
+                       case NUMPLAN_UNKNOWN:
+                               chan_misdn_log(2, port, " --> TON: Unknown\n");
+                               break;
+                               /* Maybe we should cut off the prefix if present ? */
+                       default:
+                               chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
+                               break;
+                       }
+
+
+                       misdn_cfg_get( port, MISDN_CFG_LOCALDIALPLAN, &bc->onumplan, sizeof(int));
+                       switch (bc->onumplan) {
+                       case NUMPLAN_INTERNATIONAL:
+                               chan_misdn_log(2, port, " --> TON: International\n");
+                               break;
+                       case NUMPLAN_NATIONAL:
+                               chan_misdn_log(2, port, " --> TON: National\n");
+                               break;
+                       case NUMPLAN_SUBSCRIBER:
+                               chan_misdn_log(2, port, " --> TON: Subscriber\n");
+                               break;
+                       case NUMPLAN_UNKNOWN:
+                               chan_misdn_log(2, port, " --> TON: Unknown\n");
+                               break;
+                               /* Maybe we should cut off the prefix if present ? */
+                       default:
                                        chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
                                        break;
-                               }
-                               
                        }
                }
+
                
                /* Will be overridden by asterisk in head! */
                {
                        int pres;
                        
                        misdn_cfg_get( port, MISDN_CFG_PRES, &pres, sizeof(int));
-                       newbc->pres=pres?0:1;
+                       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:
-                               newbc->pres=1;
+                               bc->pres=1;
+                               chan_misdn_log(2, port, " --> PRES: Restricted (0x1)\n");
                                break;
                                
+                               
                        case AST_PRES_UNAVAILABLE:
-                               newbc->pres=2;
+                               bc->pres=2;
+                               chan_misdn_log(2, port, " --> PRES: Unavailable (0x2)\n");
                                break;
-                       
+                               
                        default:
-                               newbc->pres=0;
+                               bc->pres=0;
+                               chan_misdn_log(2, port, " --> PRES: Allowed (0x0)\n");
                        }
                        
                        switch (ast->cid.cid_pres & 0x3){
                                
                        case AST_PRES_USER_NUMBER_UNSCREENED:
-                               newbc->screen=0;
+                               bc->screen=0;
+                               chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
                                break;
 
                        case AST_PRES_USER_NUMBER_PASSED_SCREEN:
-                               newbc->screen=1;
+                               bc->screen=1;
+                               chan_misdn_log(2, port, " --> SCREEN: Passed Screen (0x1)\n");
                                break;
                        case AST_PRES_USER_NUMBER_FAILED_SCREEN:
-                               newbc->screen=2;
+                               bc->screen=2;
+                               chan_misdn_log(2, port, " --> SCREEN: Failed Screen (0x2)\n");
                                break;
                                
                        case AST_PRES_NETWORK_NUMBER:
-                               newbc->screen=3;
+                               bc->screen=3;
+                               chan_misdn_log(2, port, " --> SCREEN: Network Nr. (0x3)\n");
                                break;
                                
                        default:
-                               newbc->screen=0;
+                               bc->screen=0;
+                               chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
                        }
+
+                       
                }
 
                
-               {
-                       int ec, ectr;
+               
+       } else { /** ORIGINATOR MISDN **/
+               
+               char prefix[BUFFERSIZE]="";
+               switch( bc->onumplan ) {
+               case NUMPLAN_INTERNATIONAL:
+                       misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
+                       break;
                        
-                       misdn_cfg_get( port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
+               case NUMPLAN_NATIONAL:
+                       misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
+                       break;
                        
-                       misdn_cfg_get( port, MISDN_CFG_ECHOTRAINING, &ectr, sizeof(int));
-                       if (ec == 1 ) {
-                               newbc->ec_enable=1;
-                       } else if ( ec > 1 ) {
-                               newbc->ec_enable=1;
-                               newbc->ec_deftaps=ec;
-                       }
-
-                       if ( !ectr ) {
-                               newbc->ec_training=0;
-                       }
+                       
+               case NUMPLAN_SUBSCRIBER:
+                       /* dunno what to do here ? */
+                       break;
+                       
+               case NUMPLAN_UNKNOWN:
+                               break;
+               default:
+                       break;
                }
                
-       } 
-       
-       chan_misdn_log(3, port, " --> * adding2newbc ext %s\n",ast->exten);
-       if (ast->exten) {
-               int l = sizeof(newbc->dad);
-               strncpy(newbc->dad,ast->exten, l);
-               newbc->dad[l-1] = 0;
-       }
-       newbc->rad[0]=0;
-       chan_misdn_log(3, port, " --> * adding2newbc callerid %s\n",AST_CID_P(ast));
-       if (ast_strlen_zero(newbc->oad) && AST_CID_P(ast) ) {
-               if (AST_CID_P(ast)) {
-                       int l = sizeof(newbc->oad);
-                       strncpy(newbc->oad,AST_CID_P(ast), l);
-                       newbc->oad[l-1] = 0;
+               {
+                       int l = strlen(prefix) + strlen(bc->oad);
+                       char tmp[l+1];
+                       strcpy(tmp,prefix);
+                       strcat(tmp,bc->oad);
+                       strcpy(bc->oad,tmp);
                }
-       }
-       
-       {
-               struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast);
-               if (!ch) { ast_verbose("No chan_list in misdn_call"); return -1;}
-               ch->bc = newbc;
-               ch->orginator=ORG_AST;
-               ch->ast = ast;
-               
-               MISDN_ASTERISK_TECH_PVT(ast) = ch ;
                
                
-      
-               newbc->capability=ast->transfercapability;
-               pbx_builtin_setvar_helper(ast,"TRANSFERCAPABILITY",ast_transfercapability2str(newbc->capability));
-               if ( ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
-                       chan_misdn_log(2, port, " --> * Call with flag Digital\n");
+               if (!ast_strlen_zero(bc->dad)) {
+                       strncpy(bc->orig_dad,bc->dad, sizeof(bc->orig_dad));
+                       bc->orig_dad[sizeof(bc->orig_dad)-1] = 0;
                }
                
-
-               /* Finally The Options Override Everything */
-               if (opts)
-                       misdn_set_opt_exec(ast,opts);
-               else
-                       chan_misdn_log(1,0,"NO OPTS GIVEN\n");
-               
-               
-               cl_queue_chan(&cl_te, ch) ;
-               ch->state=MISDN_CALLING;
-
-               chan_misdn_trace_call(ast,1,"*->I: EVENT_CALL\n" );
-               
-               r=misdn_lib_send_event( newbc, EVENT_SETUP );
+               if ( ast_strlen_zero(bc->dad) && !ast_strlen_zero(bc->keypad)) {
+                       strncpy(bc->dad,bc->keypad, sizeof(bc->dad));
+                       bc->dad[sizeof(bc->dad)-1] = 0;
+               }
+               prefix[0] = 0;
                
-               /** we should have l3id after sending setup **/
-               ch->l3id=newbc->l3_id;
-       }
-       
-       if ( r == -ENOCHAN  ) {
+               switch( bc->dnumplan ) {
+               case NUMPLAN_INTERNATIONAL:
+                       misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
+                       break;
+                       
+                       case NUMPLAN_NATIONAL:
+                               misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
+                               break;
+                               
+                               
+               case NUMPLAN_SUBSCRIBER:
+                               /* dunno what to do here ? */
+                       break;
+                       
+               case NUMPLAN_UNKNOWN:
+                       break;
+               default:
+                       break;
+               }
+               
+               {
+                       int l = strlen(prefix) + strlen(bc->dad);
+                       char tmp[l+1];
+                       strcpy(tmp,prefix);
+                       strcat(tmp,bc->dad);
+                       strcpy(bc->dad,tmp);
+               }
+               
+               
+               
+               pbx_builtin_setvar_helper(ch->ast,"REDIRECTING_NUMBER",bc->rad);
+       }
+       
+       return 0;
+}
+
+
+/*****************************/
+/*** AST Indications Start ***/
+/*****************************/
+
+static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
+{
+       int port=0;
+       int r;
+       struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast);
+       struct misdn_bchannel *newbc;
+       char *opts=NULL, *ext,*tokb;
+       char dest_cp[256];
+       
+       {
+               strncpy(dest_cp,dest,sizeof(dest_cp)-1);
+               dest_cp[sizeof(dest_cp)]=0;
+               
+               ext=strtok_r(dest_cp,"/",&tokb);
+               
+               if (ext) {
+                       ext=strtok_r(NULL,"/",&tokb);
+                       if (ext) {
+                               opts=strtok_r(NULL,"/",&tokb);
+                       } else {
+                               chan_misdn_log(-1,0,"misdn_call: No Extension given!\n");
+                               return -1;
+                       }
+                       
+               }
+               
+               
+       }
+
+       if (!ast) {
+               ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
+               return -1;
+       }
+
+       if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest  ) {
+               ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
+               ast->hangupcause=41;
+               ast_setstate(ast, AST_STATE_DOWN);
+               return -1;
+       }
+
+
+       if (!ch) {
+               ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
+               ast->hangupcause=41;
+               ast_setstate(ast, AST_STATE_DOWN);
+               return -1;
+       }
+       
+       newbc=ch->bc;
+       
+       if (!newbc) {
+               ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
+               ast->hangupcause=41;
+               ast_setstate(ast, AST_STATE_DOWN);
+               return -1;
+       }
+       
+       port=newbc->port;
+       strncpy(newbc->dad,ext,sizeof( newbc->dad));
+       strncpy(ast->exten,ext,sizeof(ast->exten));
+       
+       
+       chan_misdn_log(1, 0, "* CALL: %s\n",dest);
+       
+       chan_misdn_log(1, port, " --> * dad:%s tech:%s ctx:%s\n",ast->exten,ast->name, ast->context);
+       
+       
+       
+       chan_misdn_log(3, port, " --> * adding2newbc ext %s\n",ast->exten);
+       if (ast->exten) {
+               int l = sizeof(newbc->dad);
+               strncpy(newbc->dad,ast->exten, l);
+               newbc->dad[l-1] = 0;
+       }
+       newbc->rad[0]=0;
+       chan_misdn_log(3, port, " --> * adding2newbc callerid %s\n",AST_CID_P(ast));
+       if (ast_strlen_zero(newbc->oad) && AST_CID_P(ast) ) {
+
+               if (AST_CID_P(ast)) {
+                       int l = sizeof(newbc->oad);
+                       strncpy(newbc->oad,AST_CID_P(ast), l);
+                       newbc->oad[l-1] = 0;
+               }
+       }
+       
+       {
+               struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast);
+               if (!ch) { ast_verbose("No chan_list in misdn_call"); return -1;}
+               
+               newbc->capability=ast->transfercapability;
+               pbx_builtin_setvar_helper(ast,"TRANSFERCAPABILITY",ast_transfercapability2str(newbc->capability));
+               if ( ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
+                       chan_misdn_log(2, port, " --> * Call with flag Digital\n");
+               }
+               
+               
+               /* Finally The Options Override Everything */
+               if (opts)
+                       misdn_set_opt_exec(ast,opts);
+               else
+                       chan_misdn_log(1,0,"NO OPTS GIVEN\n");
+               
+               
+               
+               ch->state=MISDN_CALLING;
+
+               
+               r=misdn_lib_send_event( newbc, EVENT_SETUP );
+               
+               /** we should have l3id after sending setup **/
+               ch->l3id=newbc->l3_id;
+       }
+       
+       if ( r == -ENOCHAN  ) {
                chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n");
                chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n",newbc?newbc->pid:-1);
                ast->hangupcause=34;
@@ -1286,7 +1512,7 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
 }
 
 
-int misdn_answer(struct ast_channel *ast)
+static int misdn_answer(struct ast_channel *ast)
 {
        struct chan_list *p;
 
@@ -1294,7 +1520,6 @@ int misdn_answer(struct ast_channel *ast)
        if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
        p = MISDN_ASTERISK_TECH_PVT(ast) ;
        
-       chan_misdn_trace_call(ast,1,"*->I: EVENT_ANSWER\n");
        
        chan_misdn_log(1, p? (p->bc? p->bc->port : 0) : 0, "* ANSWER:\n");
        
@@ -1310,7 +1535,7 @@ int misdn_answer(struct ast_channel *ast)
        }
 
        {
-               char *tmp_key = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY");
+               const char *tmp_key = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY");
                
                if (tmp_key ) {
                        chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
@@ -1326,7 +1551,7 @@ int misdn_answer(struct ast_channel *ast)
        }
 
        {
-               char *async=pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
+               const char *async=pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
                if (async) {
                        chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n");
                        p->bc->async=1;
@@ -1340,7 +1565,7 @@ int misdn_answer(struct ast_channel *ast)
        return 0;
 }
 
-int misdn_digit(struct ast_channel *ast, char digit )
+static int misdn_digit(struct ast_channel *ast, char digit )
 {
        struct chan_list *p;
        
@@ -1400,7 +1625,7 @@ int misdn_digit(struct ast_channel *ast, char digit )
 }
 
 
-int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
+static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
 {
        struct chan_list *p;
        
@@ -1416,20 +1641,8 @@ int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
 }
 
 
-int misdn_transfer (struct ast_channel *ast, char *dest)
-{
-       struct chan_list *p;
-       
-       if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
-       p = MISDN_ASTERISK_TECH_PVT(ast) ;
-
-       chan_misdn_log(1, p->bc?p->bc->port:0, "* IND : Got Transfer %s\n",dest);
-       return 0;
-}
-
-
 
-int misdn_indication(struct ast_channel *ast, int cond)
+static int misdn_indication(struct ast_channel *ast, int cond)
 {
        struct chan_list *p;
 
@@ -1459,8 +1672,8 @@ int misdn_indication(struct ast_channel *ast, int cond)
                        misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
                        manager_send_tone(p->bc, TONE_BUSY);
                } else {
-                       chan_misdn_log(0, p->bc->port, " --> !! Got Busy in Connected State !?! port:%d ast:%s\n",
-                                      p->bc->port, ast->name);
+
+                       chan_misdn_log(-1, p->bc->port, " --> !! Got Busy in Connected State !?! ast:%s\n", ast->name);
                }
                break;
        case AST_CONTROL_RING:
@@ -1498,16 +1711,17 @@ int misdn_indication(struct ast_channel *ast, int cond)
                chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n",p->bc?p->bc->pid:-1);
                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;
                if (p->state != MISDN_CONNECTED) {
                        start_bc_tones(p);
-                       //misdn_lib_send_event( p->bc, EVENT_RELEASE_COMPLETE);
                        misdn_lib_send_event( p->bc, EVENT_RELEASE);
                } else {
                        misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
                }
+
                if (p->bc->nt) {
                        manager_send_tone(p->bc, TONE_BUSY);
                }
@@ -1515,6 +1729,7 @@ int misdn_indication(struct ast_channel *ast, int cond)
        case -1 :
                chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! pid:%d\n",p->bc?p->bc->pid:-1);
                break;
+
        case AST_CONTROL_HOLD:
                chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n",p->bc?p->bc->pid:-1);
                break;
@@ -1528,17 +1743,15 @@ int misdn_indication(struct ast_channel *ast, int cond)
        return 0;
 }
 
-int misdn_hangup(struct ast_channel *ast)
+static int misdn_hangup(struct ast_channel *ast)
 {
        struct chan_list *p;
        struct misdn_bchannel *bc=NULL;
        
-       if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
-       p = MISDN_ASTERISK_TECH_PVT(ast) ;
-
+       if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast) ) ) return -1;
+       
        release_lock;
 
-       chan_misdn_trace_call(ast,1,"*->I: EVENT_HANGUP cause=%d\n",ast->hangupcause);
        
        ast_log(LOG_DEBUG, "misdn_hangup(%s)\n", ast->name);
        
@@ -1560,6 +1773,8 @@ int misdn_hangup(struct ast_channel *ast)
        MISDN_ASTERISK_TECH_PVT(ast)=NULL;
        p->ast=NULL;
 
+       bc=p->bc;
+       
        if (ast->_state == AST_STATE_RESERVED) {
                /* between request and call */
                MISDN_ASTERISK_TECH_PVT(ast)=NULL;
@@ -1567,8 +1782,9 @@ int misdn_hangup(struct ast_channel *ast)
                
                cl_dequeue_chan(&cl_te, p);
                free(p);
-
-               misdn_lib_release(bc);
+               
+               if (bc)
+                       misdn_lib_release(bc);
                
                return 0;
        }
@@ -1578,12 +1794,8 @@ int misdn_hangup(struct ast_channel *ast)
        release_unlock;
        
        
-       
-       
-       
-       
        {
-               char *varcause=NULL;
+               const char *varcause=NULL;
                bc->cause=ast->hangupcause?ast->hangupcause:16;
                
                if ( (varcause=pbx_builtin_getvar_helper(ast, "HANGUPCAUSE")) ||
@@ -1644,6 +1856,7 @@ int misdn_hangup(struct ast_channel *ast)
                        break;
                default:
                        /*  Alerting or Disconect */
+
                        if (bc->nt)
                                misdn_lib_send_event(bc, EVENT_RELEASE);
                        else
@@ -1658,7 +1871,7 @@ int misdn_hangup(struct ast_channel *ast)
        return 0;
 }
 
-struct ast_frame  *misdn_read(struct ast_channel *ast)
+static struct ast_frame  *misdn_read(struct ast_channel *ast)
 {
        struct chan_list *tmp;
        
@@ -1666,8 +1879,7 @@ struct ast_frame  *misdn_read(struct ast_channel *ast)
        int len =0 ;
        
        if (!ast) return NULL;
-       tmp = MISDN_ASTERISK_TECH_PVT(ast);
-       if (!tmp) return NULL;
+       if (! (tmp=MISDN_ASTERISK_TECH_PVT(ast)) ) return NULL;
        if (!tmp->bc) return NULL;
        
        
@@ -1690,39 +1902,41 @@ struct ast_frame  *misdn_read(struct ast_channel *ast)
        tmp->frame.src = NULL;
        tmp->frame.data = tmp->ast_rd_buf ;
 
-       chan_misdn_trace_call(tmp->ast,3,"*->I: EVENT_READ len=%d\n",len);
        return &tmp->frame;
 }
 
-int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
+
+
+
+static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
 {
-       struct chan_list *p;
+       struct chan_list *ch;
        int i  = 0;
        
-       if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
-       p = MISDN_ASTERISK_TECH_PVT(ast) ;
+       if (!ast || ! (ch=MISDN_ASTERISK_TECH_PVT(ast)) ) return -1;
        
-       if (!p->bc ) {
+       if (!ch->bc ) {
                ast_log(LOG_WARNING, "private but no bc\n");
                return -1;
        }
        
-       if (p->bc->tone != TONE_NONE)
-               manager_send_tone(p->bc,TONE_NONE);
+       if (ch->bc->tone != TONE_NONE)
+               manager_send_tone(ch->bc,TONE_NONE);
        
        
-       if (p->holded ) {
-               chan_misdn_log(5, p->bc->port, "misdn_write: Returning because holded\n");
+       if (ch->holded ) {
+               chan_misdn_log(5, ch->bc->port, "misdn_write: Returning because holded\n");
                return 0;
        }
        
-       if (p->notxtone) {
-               chan_misdn_log(5, p->bc->port, "misdn_write: Returning because notxone\n");
+       if (ch->notxtone) {
+               chan_misdn_log(5, ch->bc->port, "misdn_write: Returning because notxone\n");
                return 0;
        }
        
        if ( !(frame->subclass & prefformat)) {
-               chan_misdn_log(0, p->bc->port, "Got Unsupported Frame with Format:%d\n", frame->subclass);
+
+               chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%d\n", frame->subclass);
        }
        
        
@@ -1736,13 +1950,32 @@ int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
                printf ("\n");
        }
 #endif
-       chan_misdn_trace_call(ast,3,"*->I: EVENT_WRITE len=%d\n",frame->samples);
-       i= manager_tx2misdn_frm(p->bc, frame->data, frame->samples);
+
+       /*if speech flip bits*/
+       if ( misdn_cap_is_speech(ch->bc->capability) )
+               flip_buf_bits(frame->data,frame->samples);
+       
+       
+       if ( !ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability) ) {
+               /* Buffered Transmit (triggert by read from isdn side)*/
+               if (misdn_jb_fill(ch->jb,frame->data,frame->samples) < 0) {
+                       if (ch->bc->active)
+                               cb_log(0,ch->bc->port,"Misdn Jitterbuffer Overflow.\n");
+               }
+               
+       } else {
+               /*transmit without jitterbuffer*/
+               i=misdn_lib_tx2misdn_frm(ch->bc, frame->data, frame->samples);
+       }
+
+       
        
        return 0;
 }
 
 
+
+
 enum ast_bridge_result  misdn_bridge (struct ast_channel *c0,
                                      struct ast_channel *c1, int flags,
                                      struct ast_frame **fo,
@@ -1850,12 +2083,12 @@ static int stop_bc_tones(struct chan_list *cl)
 }
 
 
-struct chan_list *init_chan_list(void)
+static struct chan_list *init_chan_list(void)
 {
        struct chan_list *cl=malloc(sizeof(struct chan_list));
        
        if (!cl) {
-               chan_misdn_log(0, 0, "misdn_request: malloc failed!");
+               chan_misdn_log(-1, 0, "misdn_request: malloc failed!");
                return NULL;
        }
        
@@ -1878,7 +2111,7 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
        
        struct chan_list *cl=init_chan_list();
        
-       sprintf(buf,"%s/%s",type,(char*)data);
+       sprintf(buf,"%s/%s",misdn_type,(char*)data);
        strncpy(buf2,data, 128);
        buf2[127] = 0;
        port_str=strtok_r(buf2,"/", &tokb);
@@ -1906,7 +2139,7 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
                        *p = 0;
                        channel = atoi(++p);
                        port = atoi(port_str);
-                       chan_misdn_log(2, port, " --> Call on preselected Channel (%d) on Port %d\n", channel, port);
+                       chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel);
                }
                else {
                        port = atoi(port_str);
@@ -2012,16 +2245,25 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
                chan_misdn_log(1, 0, " --> SEND: State Down\n");
                return NULL;
        }
-       
+
+       /* create ast_channel and link all the objects together */
        cl->bc=newbc;
        
-       tmp = misdn_new(cl, AST_STATE_RESERVED, buf, "default", ext, ext, format, port, channel);
+       tmp = misdn_new(cl, AST_STATE_RESERVED, ext, NULL, format, port, channel);
+       cl->ast=tmp;
+       
+       /* register chan in local list */
+       cl_queue_chan(&cl_te, cl) ;
+       
+       /* fill in the config into the objects */
+       read_config(cl, ORG_AST);
+       
        
        return tmp;
 }
 
 
-struct ast_channel_tech misdn_tech = {
+static struct ast_channel_tech misdn_tech = {
        .type="mISDN",
        .description="Channel driver for mISDN Support (Bri/Pri)",
        .capabilities= AST_FORMAT_ALAW ,
@@ -2036,10 +2278,9 @@ struct ast_channel_tech misdn_tech = {
        .indicate=misdn_indication,
        .fixup=misdn_fixup,
        .properties=0
-       /* .transfer=misdn_transfer */
 };
 
-struct ast_channel_tech misdn_tech_wo_bridge = {
+static struct ast_channel_tech misdn_tech_wo_bridge = {
        .type="mISDN",
        .description="Channel driver for mISDN Support (Bri/Pri)",
        .capabilities=AST_FORMAT_ALAW ,
@@ -2053,61 +2294,55 @@ struct ast_channel_tech misdn_tech_wo_bridge = {
        .indicate=misdn_indication,
        .fixup=misdn_fixup,
        .properties=0
-       /* .transfer=misdn_transfer */
 };
 
 
-unsigned long glob_channel=0;
+static unsigned long glob_channel=0;
 
-struct ast_channel *misdn_new(struct chan_list *chlist, int state, char * name, char * context, char *exten, char *callerid, int format, int port, int c)
+static struct ast_channel *misdn_new(struct chan_list *chlist, int state,  char *exten, char *callerid, int format, int port, int c)
 {
        struct ast_channel *tmp;
        
        tmp = ast_channel_alloc(1);
        
        if (tmp) {
-               chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s ctx:%s\n",exten,callerid, context);
+               chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s\n",exten,callerid);
                
                
                if (c<=0) {
                        c=glob_channel++;
                        snprintf(tmp->name, sizeof(tmp->name), "%s/%d-u%d",
-                                type, port, c);
+                                misdn_type, port, c);
                } else {
                        snprintf(tmp->name, sizeof(tmp->name), "%s/%d-%d",
-                                type, port, c);
+                                misdn_type, port, c);
                }
                
-               tmp->type = type;
-    
+               tmp->type = misdn_type;
+               
                tmp->nativeformats = prefformat;
+
                tmp->readformat = format;
                tmp->writeformat = format;
     
                tmp->tech_pvt = chlist;
-
+               
                int bridging;
                misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int));
                if (bridging)
                        tmp->tech = &misdn_tech;
                else
                        tmp->tech = &misdn_tech_wo_bridge;
-    
-    
+               
                tmp->writeformat = format;
                tmp->readformat = format;
                tmp->priority=1;
-    
                
-               if (context)
-                       ast_copy_string(tmp->context, context, sizeof(tmp->context));
-               else
-                       chan_misdn_log(1,0,"misdn_new: no context given.\n");
                if (exten) 
-                       ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
+                       ast_copy_string(tmp->exten, exten,  sizeof(tmp->exten));
                else
                        chan_misdn_log(1,0,"misdn_new: no exten given.\n");
-
+               
                if (callerid) {
                        char *cid_name, *cid_num;
       
@@ -2126,37 +2361,15 @@ struct ast_channel *misdn_new(struct chan_list *chlist, int state, char * name,
                        
                }
                
-               if (chlist->bc) {
-                       int port=chlist->bc->port;
-                       misdn_cfg_get( port, MISDN_CFG_LANGUAGE, tmp->language, sizeof(tmp->language));
-                       
-                       {
-                               char buf[256];
-                               ast_group_t pg,cg;
-                               
-                               misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
-                               misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
-                               
-                               chan_misdn_log(2, port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg));
-                               tmp->pickupgroup=pg;
-                               tmp->callgroup=cg;
-                       }
-                       misdn_cfg_get(port, MISDN_CFG_TXGAIN, &chlist->bc->txgain, sizeof(int));
-                       misdn_cfg_get(port, MISDN_CFG_RXGAIN, &chlist->bc->rxgain, sizeof(int));
-                       chan_misdn_log(2, port, " --> rxgain:%d txgain:%d\n",chlist->bc->rxgain,chlist->bc->txgain);
-                       
-               } else {
-                       chan_misdn_log(3, 0, " --> Not Setting Pickupgroup, we have no bc yet\n");
-               }
-               
                ast_setstate(tmp, state);
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
                else
                        tmp->rings = 0;
+               
+               
        } else {
-               ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
-               chan_misdn_log(0,0,"Unable to allocate channel structure\n");
+               chan_misdn_log(-1,0,"Unable to allocate channel structure\n");
        }
        
        return tmp;
@@ -2164,7 +2377,7 @@ struct ast_channel *misdn_new(struct chan_list *chlist, int state, char * name,
 
 
 
-int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
+static int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
 {
        struct ast_frame frame;
        
@@ -2271,7 +2484,7 @@ int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
 
 /** Channel Queue ***/
 
-struct chan_list *find_chan_by_l3id(struct chan_list *list, unsigned long l3id)
+static struct chan_list *find_chan_by_l3id(struct chan_list *list, unsigned long l3id)
 {
        struct chan_list *help=list;
        for (;help; help=help->next) {
@@ -2283,7 +2496,7 @@ struct chan_list *find_chan_by_l3id(struct chan_list *list, unsigned long l3id)
        return NULL;
 }
 
-struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc)
+static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc)
 {
        struct chan_list *help=list;
        for (;help; help=help->next) {
@@ -2296,7 +2509,7 @@ struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel
 }
 
 
-struct chan_list *find_holded(struct chan_list *list, struct misdn_bchannel *bc)
+static struct chan_list *find_holded(struct chan_list *list, struct misdn_bchannel *bc)
 {
        struct chan_list *help=list;
        
@@ -2312,7 +2525,7 @@ struct chan_list *find_holded(struct chan_list *list, struct misdn_bchannel *bc)
        return NULL;
 }
 
-void cl_queue_chan(struct chan_list **list, struct chan_list *chan)
+static void cl_queue_chan(struct chan_list **list, struct chan_list *chan)
 {
        chan_misdn_log(4, chan->bc? chan->bc->port : 0, "* Queuing chan %p\n",chan);
   
@@ -2328,12 +2541,13 @@ void cl_queue_chan(struct chan_list **list, struct chan_list *chan)
        ast_mutex_unlock(&cl_te_lock);
 }
 
-void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan) 
+static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan) 
 {
        if (chan->dsp) 
                ast_dsp_free(chan->dsp);
        if (chan->trans)
                ast_translator_free_path(chan->trans);
+
        
 
        ast_mutex_lock(&cl_te_lock);
@@ -2381,9 +2595,17 @@ static void release_chan(struct misdn_bchannel *bc) {
                release_unlock;
                
                chan_misdn_log(1, bc->port, "Trying to Release bc with l3id: %x\n",bc->l3_id);
+
+               //releaseing jitterbuffer
+               if (ch->jb ) {
+                       misdn_jb_destroy(ch->jb);
+                       ch->jb=NULL;
+               } else {
+                       if (!bc->nojitter)
+                               chan_misdn_log(5,bc->port,"Jitterbuffer already destroyed.\n");
+               }
+               
                if (ch) {
-                       if (ast)
-                               chan_misdn_trace_call(ast,1,"I->*: EVENT_RELEASE\n");
                        
                        close(ch->pipe[0]);
                        close(ch->pipe[1]);
@@ -2463,7 +2685,7 @@ static void release_chan(struct misdn_bchannel *bc) {
 }
 /*** release end **/
 
-void misdn_transfer_bc(struct chan_list *tmp_ch, struct chan_list *holded_chan)
+static void misdn_transfer_bc(struct chan_list *tmp_ch, struct chan_list *holded_chan)
 {
        chan_misdn_log(4,0,"TRANSFERING %s to %s\n",holded_chan->ast->name, tmp_ch->ast->name);
        
@@ -2479,7 +2701,7 @@ void misdn_transfer_bc(struct chan_list *tmp_ch, struct chan_list *holded_chan)
 }
 
 
-void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct ast_channel *ast)
+static void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct ast_channel *ast)
 {
        char predial[256]="";
        char *p = predial;
@@ -2490,6 +2712,7 @@ void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct
   
        ch->state=MISDN_DIALING;
        
+
        if (bc->nt) {
                int ret; 
                ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
@@ -2511,6 +2734,7 @@ void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct
        if (ast_pbx_start(ast)<0) {
                ast=NULL;
                manager_send_tone(bc,TONE_BUSY);
+
                if (bc->nt)
                        misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
                else
@@ -2537,7 +2761,7 @@ void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct
 
 
 
-void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc) {
+static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc) {
        
        ast->hangupcause=bc->cause;
        
@@ -2567,7 +2791,7 @@ void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc) {
 /************************************************************/
 /*  Receive Events from isdn_lib  here                     */
 /************************************************************/
-enum event_response_e
+static enum event_response_e
 cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 {
        struct chan_list *ch=find_chan_by_bc(cl_te, bc);
@@ -2576,7 +2800,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                ch=find_chan_by_l3id(cl_te, bc->l3_id);
        
        if (event != EVENT_BCHAN_DATA) { /*  Debug Only Non-Bchan */
-               chan_misdn_log(1, bc->port, "I IND :%s oad:%s dad:%s port:%d\n", manager_isdn_get_info(event), bc->oad, bc->dad, bc->port);
+               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);
        }
        
@@ -2660,27 +2884,30 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        }
 /*                     chan_misdn_log(5, bc->port, "Can Match Extension: dad:%s oad:%s\n",bc->dad,bc->oad);*/
                        
-                       char bc_context[BUFFERSIZE];
-                       misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
-                       if(!ast_canmatch_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) {
-                               chan_misdn_log(0, bc->port, "Extension can never match, so disconnecting\n");
+                       
+                       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);
                                ch->state=MISDN_EXTCANTMATCH;
                                bc->out_cause=1;
+
                                if (bc->nt)
                                        misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
                                else
                                        misdn_lib_send_event(bc, EVENT_DISCONNECT );
                                break;
                        }
-                       if (ast_exists_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) {
+                       if (ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
                                ch->state=MISDN_DIALING;
          
                                manager_send_tone(bc,TONE_NONE);
-/*                             chan_misdn_log(1, bc->port, " --> * Starting Ast ctx:%s\n", ch->ast->context);*/
+/*                             chan_misdn_log(1, bc->port, " --> * Starting Ast ctx:%s\n", ch->context);*/
                                if (ast_pbx_start(ch->ast)<0) {
-                                       chan_misdn_log(0, bc->port, "ast_pbx_start returned < 0 in INFO\n");
+
+                                       chan_misdn_log(-1, bc->port, "ast_pbx_start returned < 0 in INFO\n");
                                        manager_send_tone(bc,TONE_BUSY);
+
                                        if (bc->nt)
                                                misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
                                        else
@@ -2730,6 +2957,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                }
        }
        
+
        int msn_valid = misdn_cfg_is_msn_valid(bc->port, bc->dad);
        if (!bc->nt && ! msn_valid) {
                chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n");
@@ -2741,108 +2969,22 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
        {
                struct chan_list *ch=init_chan_list();
                struct ast_channel *chan;
-               char name[128];
-               if (!ch) { chan_misdn_log(0, bc->port, "cb_events: malloc for chan_list failed!\n"); return 0;}
+
+               if (!ch) { chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n"); return 0;}
                
                ch->bc = bc;
                ch->l3id=bc->l3_id;
                ch->addr=bc->addr;
                ch->orginator = ORG_MISDN;
-
                
-               {
-                       char prefix[BUFFERSIZE]="";
-                       switch( bc->onumplan ) {
-                       case NUMPLAN_INTERNATIONAL:
-                               misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
-                               break;
-         
-                       case NUMPLAN_NATIONAL:
-                               misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
-                               break;
-         
-         
-                       case NUMPLAN_SUBSCRIBER:
-                               /* dunno what to do here ? */
-                               break;
-
-                       case NUMPLAN_UNKNOWN:
-                               break;
-                       default:
-                               break;
-                       }
+               chan=misdn_new(ch, AST_STATE_RESERVED,bc->dad, bc->oad, AST_FORMAT_ALAW, bc->port, bc->channel);
+       ch->ast = chan;
+               
+               read_config(ch, ORG_MISDN);
+               
+               ch->ast->rings=1;
+               ast_setstate(ch->ast, AST_STATE_RINGING);
 
-                       {
-                               int l = strlen(prefix) + strlen(bc->oad);
-                               char tmp[l+1];
-                               strcpy(tmp,prefix);
-                               strcat(tmp,bc->oad);
-                               strcpy(bc->oad,tmp);
-                       }
-                       
-                       if (!ast_strlen_zero(bc->oad))
-                               sprintf(name,"mISDN/%d/%s",bc->port,bc->oad);
-                       else
-                               sprintf(name,"mISDN/%d",bc->port);
-
-
-                       if (!ast_strlen_zero(bc->dad)) {
-                               strncpy(bc->orig_dad,bc->dad, sizeof(bc->orig_dad));
-                               bc->orig_dad[sizeof(bc->orig_dad)-1] = 0;
-                       }
-                       
-                       if ( ast_strlen_zero(bc->dad) && !ast_strlen_zero(bc->keypad)) {
-                               strncpy(bc->dad,bc->keypad, sizeof(bc->dad));
-                               bc->dad[sizeof(bc->dad)-1] = 0;
-                       }
-                       prefix[0] = 0;
-                       
-                       switch( bc->dnumplan ) {
-                       case NUMPLAN_INTERNATIONAL:
-                               misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
-                               break;
-                               
-                       case NUMPLAN_NATIONAL:
-                               misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
-                               break;
-                               
-                               
-                       case NUMPLAN_SUBSCRIBER:
-                               /* dunno what to do here ? */
-                               break;
-                               
-                       case NUMPLAN_UNKNOWN:
-                               break;
-                       default:
-                               break;
-                       }
-               
-                       {
-                               int l = strlen(prefix) + strlen(bc->dad);
-                               char tmp[l+1];
-                               strcpy(tmp,prefix);
-                               strcat(tmp,bc->dad);
-                               strcpy(bc->dad,tmp);
-                       }
-                       
-                       char bc_context[BUFFERSIZE];
-                       misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
-                       chan=misdn_new(ch, AST_STATE_RING,name ,bc_context, bc->dad, bc->oad, AST_FORMAT_ALAW, bc->port, bc->channel);
-                       
-                       if (!chan) {
-                               misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
-                               return 0;
-                       }
-                       
-                       ch->ast = chan;
-                       pbx_builtin_setvar_helper(ch->ast,"REDIRECTING_NUMBER",bc->rad);
-                       
-               }
-
-               
-
-               chan_misdn_trace_call(chan,1,"I->*: EVENT_SETUP\n");
-               
                if ( bc->pres ) {
                        chan->cid.cid_pres=AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
                }  else {
@@ -2851,7 +2993,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
       
                pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability));
                chan->transfercapability=bc->capability;
-      
+               
                switch (bc->capability) {
                case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
                        pbx_builtin_setvar_helper(chan,"CALLTYPE","DIGITAL");
@@ -2863,86 +3005,33 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                /** queue new chan **/
                cl_queue_chan(&cl_te, ch) ;
 
-
+               
                /*
                  added support for s extension hope it will help those poor cretains
                  which haven't overlap dial.
                */
                {
-                       
-                       misdn_cfg_get( bc->port, MISDN_CFG_LANGUAGE, chan->language, sizeof(chan->language));
                        int ai;
                        misdn_cfg_get( bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
                        if ( ai ) {
                                do_immediate_setup(bc, ch , chan);
                                break;
                        }
-
-                       int immediate;
-                       misdn_cfg_get( bc->port, MISDN_CFG_IMMEDIATE, &immediate, sizeof(int));
                        
-                       if (ast_strlen_zero(bc->orig_dad) && immediate ) {
-                               do_immediate_setup(bc, ch , chan);
-                               break;
-                       }
                        
-               }
-
-
-               {
-                       int ec, ectr;
-                       
-                       misdn_cfg_get( bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
-                       
-                       misdn_cfg_get( bc->port, MISDN_CFG_ECHOTRAINING, &ectr, sizeof(int));
-                       if (ec == 1 ) {
-                               bc->ec_enable=1;
-                       } else if ( ec > 1 ) {
-                               bc->ec_enable=1;
-                               bc->ec_deftaps=ec;
-                       }
                        
-                       if ( !ectr ) {
-                               bc->ec_training=0;
-                       }
                }
-               
 
-               if (bc->urate>0) {
-                       char buf[16];
-                       snprintf(buf,16,"%d",bc->urate);
-                       pbx_builtin_setvar_helper(chan,"MISDN_URATE",buf);
-               }
                
-               /* Check for Pickup Request first */
-               if (!strcmp(chan->exten, ast_pickup_ext())) {
-                       int ret;/** Sending SETUP_ACK**/
-                       ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
-       
-                       if (ast_pickup_call(chan)) {
-                               ast_hangup(chan);
-                       } else {
-                               ch->state = MISDN_CALLING_ACKNOWLEDGE;
-         
-                               ch->ast=NULL;
-         
-                               ast_setstate(chan, AST_STATE_DOWN);
-                               ast_hangup(chan); 
-         
-                               break;
-                       }
-               }
-               
-               /** Now after we've finished configuring our channel object
-                   we'll jump into the dialplan **/
-               
-               char bc_context[BUFFERSIZE];
-               misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
-               if(!ast_canmatch_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) {
-                       chan_misdn_log(0, bc->port, "Extension can never match, so disconnecting\n");
+                       chan_misdn_log(0,bc->port,"CONTEXT:%s\n",ch->context);
+                       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);
                        ch->state=MISDN_EXTCANTMATCH;
                        bc->out_cause=1;
+
                        if (bc->nt)
                                misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
                        else
@@ -2950,9 +3039,9 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        break;
                }
                
-               if (ast_exists_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) {
+               if (ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
                        ch->state=MISDN_DIALING;
-       
+                       
                        if (bc->nt) {
                                int ret; 
                                ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
@@ -2962,9 +3051,11 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        }
        
                        if (ast_pbx_start(chan)<0) {
-                               chan_misdn_log(0, bc->port, "ast_pbx_start returned <0 in SETUP\n");
+
+                               chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n");
                                chan=NULL;
                                manager_send_tone(bc,TONE_BUSY);
+
                                if (bc->nt)
                                        misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
                                else
@@ -2977,6 +3068,8 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                                misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
                        }
                        /*  send tone to phone :) */
+
+                       /** ADD IGNOREPAT **/
                        
                        int stop_tone;
                        misdn_cfg_get( 0, MISDN_GEN_STOP_TONE, &stop_tone, sizeof(int));
@@ -3066,7 +3159,6 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                
                start_bc_tones(ch);
                
-               chan_misdn_trace_call(ch->ast,1,"I->*: EVENT_CONNECT\n");
                
                ch->state = MISDN_CONNECTED;
                ast_queue_control(ch->ast, AST_CONTROL_ANSWER);
@@ -3101,6 +3193,12 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                
                stop_bc_tones(ch);
                bc->out_cause=16;
+               
+               //if (ch->state == MISDN_CONNECTED) 
+               //misdn_lib_send_event(bc,EVENT_RELEASE);
+               //else
+               //misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
+               
                misdn_lib_send_event(bc,EVENT_RELEASE);
                
        }
@@ -3138,7 +3236,6 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
        
        case EVENT_BCHAN_DATA:
        {
-               chan_misdn_trace_call(ch->ast,3,"I->*: EVENT_B_DATA len=%d\n",bc->bframe_len);
                
                if ( !misdn_cap_is_speech(ch->bc->capability) || bc->nojitter) {
                        misdn_tx2ast_frm(ch, bc->bframe, bc->bframe_len );
@@ -3181,11 +3278,14 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
        }
        break;
        case EVENT_TIMEOUT:
-               break; /* Ignore now .. */
+
+               misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
+               break;
                {
                        switch (ch->state) {
                        case MISDN_CALLING:
-                               chan_misdn_log(0, bc?bc->port:0, "GOT TIMOUT AT CALING pid:%d\n", bc?bc->pid:-1);
+
+                               chan_misdn_log(-1, bc?bc->port:0, "GOT TIMOUT AT CALING pid:%d\n", bc?bc->pid:-1);
                                        break;
                        case MISDN_DIALING:
                        case MISDN_PROGRESS:
@@ -3210,8 +3310,6 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                struct ast_channel *hold_ast=AST_BRIDGED_P(ch->ast);
                ch->state = MISDN_CONNECTED;
                
-               //ast_moh_stop(ch->ast);
-               //start_bc_tones(ch);
                if (hold_ast) {
                        ast_moh_stop(hold_ast);
                }
@@ -3229,7 +3327,8 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                misdn_cfg_get( bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(int));
                
                if (!hold_allowed) {
-                       chan_misdn_log(0, bc->port, "Hold not allowed on port:%d\n", bc->port);
+
+                       chan_misdn_log(-1, bc->port, "Hold not allowed this port.\n");
                        misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
                        break;
                }
@@ -3238,7 +3337,8 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        struct chan_list *holded_ch=find_holded(cl_te, bc);
                        if (holded_ch) {
                                misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
-                               chan_misdn_log(0, bc->port, "We can't use RETRIEVE at the moment due to mISDN bug!\n");
+
+                               chan_misdn_log(-1, bc->port, "We can't use RETRIEVE at the moment due to mISDN bug!\n");
                                break;
                        }
                }
@@ -3255,6 +3355,34 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                }
        } 
        break;
+       
+       case EVENT_FACILITY:
+               print_facility(bc);
+               
+               switch (bc->fac_type) {
+               case FACILITY_CALLDEFLECT:
+               {
+                       struct ast_channel *bridged=AST_BRIDGED_P(ch->ast);
+                       struct chan_list *ch;
+                       
+                       if (bridged && MISDN_ASTERISK_TECH_PVT(bridged)) {
+                               ch=MISDN_ASTERISK_TECH_PVT(bridged);
+                               //ch->state=MISDN_FACILITY_DEFLECTED;
+                               if (ch->bc) {
+                                       /* todo */
+                               }
+                               
+                       }
+                       
+               } 
+               
+               break;
+               default:
+                       chan_misdn_log(1,bc->port," --> not yet handled\n");
+               }
+               
+               break;
+                               
        default:
                ast_log(LOG_WARNING, "Got Unknown Event\n");
                break;
@@ -3273,13 +3401,13 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
  *******************************************/
 
 
-int clearl3_true ( void ) {
+static int clearl3_true ( void ) {
        int default_clearl3;
        misdn_cfg_get( 0, MISDN_GEN_CLEAR_L3, &default_clearl3, sizeof(int));
        return default_clearl3;
 }
 
-int g_config_initialized=0;
+static int g_config_initialized=0;
 
 int load_module(void)
 {
@@ -3323,6 +3451,7 @@ int load_module(void)
                struct misdn_lib_iface iface = {
                        .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))
@@ -3332,7 +3461,7 @@ int load_module(void)
 
        {
                if (ast_channel_register(&misdn_tech)) {
-                       ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
+                       ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type);
                        unload_module();
                        return -1;
                }
@@ -3373,7 +3502,10 @@ int load_module(void)
                                 "type=calldeflect args=Nr where to deflect\n"
                                 "\n"
                );
-  
+
+
+       misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
+       
        chan_misdn_log(0, 0, "-- mISDN Channel Driver Registred -- (BE AWARE THIS DRIVER IS EXPERIMENTAL!)\n");
 
        return 0;
@@ -3384,8 +3516,8 @@ int load_module(void)
 int unload_module(void)
 {
        /* First, take us out of the channel loop */
-       chan_misdn_log(0, 0, "-- Unregistering mISDN Channel Driver --\n");
-
+       ast_log(LOG_VERBOSE, "-- Unregistering mISDN Channel Driver --\n");
+       
        if (!g_config_initialized) return 0;
        
        ast_cli_unregister(&cli_send_display);
@@ -3412,6 +3544,7 @@ int unload_module(void)
   
        ast_channel_unregister(&misdn_tech);
 
+
        free_robin_list();
        misdn_cfg_destroy();
        misdn_lib_destroy();
@@ -3443,102 +3576,7 @@ char *key(void)
        return ASTERISK_GPL_KEY;
 }
 
-void chan_misdn_log(int level, int port, char *tmpl, ...)
-{
-       if (! ((0 <= port) && (port <= max_ports))) {
-               ast_log(LOG_WARNING, "cb_log called with out-of-range port number! (%d)\n", port);
-               return;
-       }
-               
-       va_list ap;
-       char buf[1024];
-  
-       va_start(ap, tmpl);
-       vsnprintf( buf, 1023, tmpl, ap );
-       va_end(ap);
-       
-       if (misdn_debug_only[port] ? (level==1 && misdn_debug[port]) || (level==misdn_debug[port]) : level <= misdn_debug[port]) {
-               ast_console_puts(buf);
-       }
-       
-       if (level <= misdn_debug[0] && tracing) {
-               time_t tm = time(NULL);
-               char *tmp=ctime(&tm),*p;
-               char file[BUFFERSIZE];
-               misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, file, BUFFERSIZE);
-               FILE *fp= fopen(file, "a+");
 
-               p=strchr(tmp,'\n');
-               if (p) *p=':';
-    
-               if (!fp) {
-                       ast_console_puts("Error opening Tracefile: ");
-                       ast_console_puts(strerror(errno));
-                       ast_console_puts("\n");
-                       return ;
-               }
-               
-               fputs(tmp,fp);
-               fputs(" ", fp);
-               fputs(buf, fp);
-
-               fclose(fp);
-       }
-}
-
-
-void chan_misdn_trace_call(struct ast_channel *chan, int debug, char *tmpl, ...)
-{
-       va_list ap;
-       char buf[1024];
-       char name[1024];
-
-       int trace;
-       misdn_cfg_get( 0, MISDN_GEN_TRACE_CALLS, &trace, sizeof(int));
-       if (!trace) return ;
-       
-       if (misdn_debug[0] < debug) return ; 
-       
-       char tracedir[BUFFERSIZE];
-       misdn_cfg_get( 0, MISDN_GEN_TRACE_DIR, tracedir, BUFFERSIZE);
-       sprintf(name,"%s/%s.%s",tracedir, chan->uniqueid, chan->cid.cid_num );
-       
-       va_start(ap, tmpl);
-       
-       vsprintf( buf, tmpl, ap );
-       
-       va_end(ap);
-       
-       time_t tm = time(NULL);
-       char *tmp=ctime(&tm),*p;
-       FILE *fp= fopen(name, "a");
-       int fd;
-       
-       if (!fp) {
-               ast_console_puts("Error opening Tracefile");
-               ast_console_puts(strerror(errno));
-               ast_console_puts("\n");
-               return ;
-       }
-       
-       fd=fileno(fp) ;
-       
-       flock(fd, LOCK_EX);
-       
-       p=strchr(tmp,'\n');
-       if (p) *p=':';
-       
-       
-       
-       fputs(tmp,fp);
-       fputs(" ", fp);
-       fputs(buf, fp);
-
-       flock(fd, LOCK_UN);
-       
-       fclose(fp);
-       
-}
 
 
 /*** SOME APPS ;)***/
@@ -3547,9 +3585,10 @@ static int misdn_facility_exec(struct ast_channel *chan, void *data)
 {
        struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
        char *tok, *tokb;
-       
 
-       if (strcasecmp(MISDN_ASTERISK_TYPE(chan),"mISDN")) {
+       chan_misdn_log(0,0,"TYPE: %s\n",chan->tech->type);
+       
+       if (strcasecmp(chan->tech->type,"mISDN")) {
                ast_log(LOG_WARNING, "misdn_facility makes only sense with chan_misdn channels!\n");
                return -1;
        }
@@ -3591,8 +3630,8 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
        int  keyidx=0;
        int rxgain=0;
        int txgain=0;
-
-       if (strcasecmp(MISDN_ASTERISK_TYPE(chan),"mISDN")) {
+       
+       if (strcasecmp(chan->tech->type,"mISDN")) {
                ast_log(LOG_WARNING, "misdn_set_opt makes only sense with chan_misdn channels!\n");
                return -1;
        }
@@ -3688,13 +3727,9 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
                                chan_misdn_log(1, ch->bc->port, "SETOPT: Digital TRANS_DIGITAL\n");
                                ch->bc->async=1;
                                ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
-                               /*ch->bc->state=STATE_CONNECTED;
-                                 misdn_lib_setup_bc(ch->bc);*/
                        } else {
                                ch->bc->async=0;
                                ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
-                               /*ch->bc->state=STATE_CONNECTED;
-                                 misdn_lib_setup_bc(ch->bc);*/
                        }
                        break;
             
@@ -3748,3 +3783,229 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
 }
 
 
+int chan_misdn_jb_empty ( struct misdn_bchannel *bc, char *buf, int len) 
+{
+       struct chan_list *ch=find_chan_by_bc(cl_te, bc);
+       
+       if (ch && ch->jb) {
+               return misdn_jb_empty(ch->jb, buf, len);
+       }
+       
+       return -1;
+}
+
+
+
+/*******************************************************/
+/***************** JITTERBUFFER ************************/
+/*******************************************************/
+
+
+/* allocates the jb-structure and initialise the elements*/
+struct misdn_jb *misdn_jb_init(int size, int upper_threshold)
+{
+    int i;
+    struct misdn_jb *jb = (struct misdn_jb*) malloc(sizeof(struct misdn_jb));
+    jb->size = size;
+    jb->upper_threshold = upper_threshold;
+    jb->wp = 0;
+    jb->rp = 0;
+    jb->state_full = 0;
+    jb->state_empty = 0;
+    jb->bytes_wrote = 0;
+    jb->samples = (char *)malloc(size*sizeof(char));
+    jb->ok = (char *)malloc(size*sizeof(char));
+    for(i=0; i<size; i++)
+       jb->ok[i]=0;
+
+    ast_mutex_init(&jb->mutexjb);
+
+    return jb;
+}
+
+/* frees the data and destroys the given jitterbuffer struct */
+void misdn_jb_destroy(struct misdn_jb *jb)
+{
+       ast_mutex_destroy(&jb->mutexjb);
+       
+       
+       free(jb->samples);
+       free(jb);
+}
+
+/* fills the jitterbuffer with len data returns < 0 if there was an
+   error (bufferoverflow). */
+int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
+{
+    int i, j, rp, wp;
+
+    ast_mutex_lock (&jb->mutexjb);
+    
+    wp=jb->wp;
+    rp=jb->rp;
+       
+    for(i=0; i<len; i++)
+    {
+       jb->samples[wp]=data[i];
+       jb->ok[wp]=1;
+       wp = (wp!=jb->size-1 ? wp+1 : 0);
+
+       if(wp==jb->rp)
+           jb->state_full=1;
+    }
+    
+    if(wp>=rp)
+      jb->state_buffer=wp-rp;
+    else
+      jb->state_buffer= jb->size-rp+wp;
+    chan_misdn_log(9,0,"misdn_jb_fill: written:%d | Bufferstatus:%d p:%x\n",len,jb->state_buffer,jb);
+    
+    if(jb->state_full)
+    {
+       jb->wp=wp;
+
+       rp=wp;
+       for(j=0; j<jb->upper_threshold; j++)
+           rp = (rp!=0 ? rp-1 : jb->size-1);
+       jb->rp=rp;
+       jb->state_full=0;
+       jb->state_empty=1;
+
+       ast_mutex_unlock (&jb->mutexjb);
+       
+       return -1;
+    }
+
+    if(!jb->state_empty)
+    {
+       jb->bytes_wrote+=len;
+       if(jb->bytes_wrote>=jb->upper_threshold)
+       {
+           jb->state_empty=1;
+           jb->bytes_wrote=0;
+       }
+    }
+    jb->wp=wp;
+
+    ast_mutex_unlock (&jb->mutexjb);
+    
+    return 0;
+}
+
+/* gets len bytes out of the jitterbuffer if available, else only the
+available data is returned and the return value indicates the number
+of data. */
+int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
+{
+    int i, wp, rp, read=0;
+
+    ast_mutex_lock (&jb->mutexjb);
+
+    rp=jb->rp;
+    wp=jb->wp;
+
+    if(jb->state_empty)
+    {  
+       for(i=0; i<len; i++)
+       {
+           if(wp==rp)
+           {
+               jb->rp=rp;
+               jb->state_empty=0;
+
+               ast_mutex_unlock (&jb->mutexjb);
+               
+               return read;
+           }
+           else
+           {
+               if(jb->ok[rp]==1)
+               {
+                   data[i]=jb->samples[rp];
+                   jb->ok[rp]=0;
+                   rp=(rp!=jb->size-1 ? rp+1 : 0);
+                   read+=1;
+               }
+           }
+       }
+
+       if(wp >= rp)
+               jb->state_buffer=wp-rp;
+       else
+               jb->state_buffer= jb->size-rp+wp;
+       chan_misdn_log(9,0,"misdn_jb_empty: read:%d | Bufferstatus:%d p:%x\n",len,jb->state_buffer,jb);
+       
+       jb->rp=rp;
+    }
+    else
+           chan_misdn_log(9,0,"misdn_jb_empty: Wait...requested:%d p:%x\n",len,jb);
+    
+    ast_mutex_unlock (&jb->mutexjb);
+
+    return read;
+}
+
+
+
+
+/*******************************************************/
+/*************** JITTERBUFFER  END *********************/
+/*******************************************************/
+
+
+
+
+void chan_misdn_log(int level, int port, char *tmpl, ...)
+{
+       if (! ((0 <= port) && (port <= max_ports))) {
+               ast_log(LOG_WARNING, "cb_log called with out-of-range port number! (%d)\n", port);
+               port=-1;
+       }
+               
+       va_list ap;
+       char buf[1024];
+       char port_buf[8];
+       sprintf(port_buf,"P[%2d] ",port);
+       
+       va_start(ap, tmpl);
+       vsnprintf( buf, 1023, tmpl, ap );
+       va_end(ap);
+
+       if (level == -1)
+               ast_log(LOG_WARNING, buf);
+       else if (misdn_debug_only[port] ? 
+                       (level==1 && misdn_debug[port]) || (level==misdn_debug[port]) 
+                : level <= misdn_debug[port]) {
+               
+               ast_console_puts(port_buf);
+               ast_console_puts(buf);
+       }
+       
+       if ((level <= misdn_debug[0]) && !ast_strlen_zero(global_tracefile) ) {
+               time_t tm = time(NULL);
+               char *tmp=ctime(&tm),*p;
+               
+               FILE *fp= fopen(global_tracefile, "a+");
+               
+               p=strchr(tmp,'\n');
+               if (p) *p=':';
+               
+               if (!fp) {
+                       ast_console_puts("Error opening Tracefile: [ ");
+                       ast_console_puts(global_tracefile);
+                       ast_console_puts(" ] ");
+                       
+                       ast_console_puts(strerror(errno));
+                       ast_console_puts("\n");
+                       return ;
+               }
+               
+               fputs(tmp,fp);
+               fputs(" ", fp);
+               fputs(port_buf,fp);
+               fputs(" ", fp);
+               fputs(buf, fp);
+
+               fclose(fp);
+       }
+}
index f8e4941..317515c 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "chan_misdn_config.h"
 
 #include <asterisk/channel.h>
 #include <asterisk/logger.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
@@ -57,20 +60,25 @@ struct port_config {
        int *te_choose_channel;
        char *context;
        char *language;
+       char *musicclass;
        char *callerid;
        char *method;
-       int *dialplan; 
+       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 */
@@ -148,13 +156,16 @@ static void free_port_cfg (void) {
                        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);
@@ -162,6 +173,8 @@ static void free_port_cfg (void) {
                        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);
@@ -263,20 +276,28 @@ void misdn_cfg_get(int port, enum misdn_cfg_elements elem, void *buf, int bufsiz
                case MISDN_CFG_RXGAIN:          GET_PORTCFG_MEMCPY(rxgain);
                                                break;
                case MISDN_CFG_TXGAIN:          GET_PORTCFG_MEMCPY(txgain);
-                                               break;
-               case MISDN_CFG_TE_CHOOSE_CHANNEL:
+                       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_CALLERID:        GET_PORTCFG_STRCPY(callerid);
+                       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:
@@ -284,9 +305,12 @@ void misdn_cfg_get(int port, enum misdn_cfg_elements elem, void *buf, int bufsiz
                                                break;
                case MISDN_CFG_PRES:            GET_PORTCFG_MEMCPY(pres);
                                                break;
-               case MISDN_CFG_ALWAYS_IMMEDIATE:
+       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:
@@ -361,7 +385,7 @@ int misdn_cfg_is_msn_valid (int port, char* msn) {
        else
                iter = port_cfg[0]->msn_list;
        for (; iter; iter = iter->next) 
-               if (*(iter->msn) == '*' || !strcasecmp(iter->msn, msn)) {
+               if (*(iter->msn) == '*' || ast_extension_match(iter->msn, msn)) {
                        misdn_cfg_unlock();
                        return 1;
                }
@@ -518,8 +542,12 @@ void misdn_cfg_get_config_string(int port, enum misdn_cfg_elements elem, char* b
                case MISDN_CFG_GROUPNAME:       GET_CFG_STRING(GROUPNAME, name);
                                                                        break;
                case MISDN_CFG_RXGAIN:          GET_CFG_INT(RXGAIN, rxgain);
+                       break;
+       case MISDN_CFG_TXGAIN:          GET_CFG_INT(TXGAIN, txgain);
+                                                                       break;
+       case MISDN_CFG_JITTERBUFFER:            GET_CFG_INT(JITTERBUFFER, jitterbuffer);
                                                                        break;
-               case MISDN_CFG_TXGAIN:          GET_CFG_INT(TXGAIN, txgain);
+                                                                               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);
@@ -528,12 +556,16 @@ void misdn_cfg_get_config_string(int port, enum misdn_cfg_elements elem, char* b
                                                                        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:
@@ -541,9 +573,12 @@ void misdn_cfg_get_config_string(int port, enum misdn_cfg_elements elem, char* b
                                                                        break;
                case MISDN_CFG_PRES:            GET_CFG_BOOL(PRESENTATION, pres, allowed, not_screened);
                                                                        break;
-               case MISDN_CFG_ALWAYS_IMMEDIATE:
+       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:
@@ -771,50 +806,40 @@ static void build_port_config(struct ast_variable *v, char *cat) {
        
        for (; v; v=v->next) {
                if (!strcasecmp(v->name, "ports")) {
-                       /* TODO check for value not beeing set, like PORTS= */
-                       char *iter;
-                       char *value = v->value;
-                       while ((iter = strchr(value, ',')) != NULL) {
-                               *iter = 0;
-                               /* strip spaces */
-                               while (*value && *value == ' ') {
-                                       value++;
-                               }
-                               /* TODO check for char not 0-9 */
-
-                               if (*value){
-                                       int p = atoi(value);
-                                       if (p <= max_ports && p > 0) {
-                                               cfg_for_ports[p] = 1;
-                                               if (strstr(value, "ptp"))
-                                                       ptp[p] = 1;
+                       char *value;
+                       char ptpbuf[BUFFERSIZE] = "";
+                       int start, end;
+                       for (value = strsep(&v->value, ","); value; value = strsep(&v->value, ","), *ptpbuf = 0) { 
+                               if (!*value)
+                                       continue;
+                               if (sscanf(value, "%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);
+                                       }
+                               } else {
+                                       if (sscanf(value, "%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);
                                        } 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);
-                                       value = ++iter;
+                                               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);
                                }
                        }
-                       /* the remaining or the only one */
-                       /* strip spaces */
-                       while (*value && *value == ' ') {
-                               value++;
-                       }
-                       /* TODO check for char not 0-9 */
-                       if (*value) {
-                               int p = atoi(value);
-                               if (p <= max_ports && p > 0) {
-                                       cfg_for_ports[p] = 1;
-                                       if (strstr(value, "ptp"))
-                                               ptp[p] = 1;
-                               } 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);
-                       }
                        continue;
                }
                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));
@@ -830,9 +855,12 @@ static void build_port_config(struct ast_variable *v, char *cat) {
                }
                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);
@@ -842,38 +870,10 @@ static void build_port_config(struct ast_variable *v, char *cat) {
                PARSE_CFG_STR(callerid);
                PARSE_CFG_STR(method);
                if (!strcasecmp(v->name, "msns")) {
-                       /* TODO check for value not beeing set, like msns= */
-                       char *iter;
-                       char *value = v->value;
-
-                       while ((iter = strchr(value, ',')) != NULL) {
-                               *iter = 0;
-                               /* strip spaces */
-                               while (*value && *value == ' ') {
-                                       value++;
-                               }
-                               /* TODO check for char not 0-9 */
-                               if (*value){
-                                       int l = strlen(value);
-                                       if (l) {
-                                               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;
-                                       }
-                                       value = ++iter;
-                               }
-                       }
-                       /* the remaining or the only one */
-                       /* strip spaces */
-                       while (*value && *value == ' ') {
-                               value++;
-                       }
-                       /* TODO check for char not 0-9 */
-                       if (*value) {
-                               int l = strlen(value);
-                               if (l) {
+                       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);
@@ -945,6 +945,10 @@ static void fill_defaults (void) {
                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) {
@@ -955,6 +959,10 @@ static void fill_defaults (void) {
                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) {
@@ -963,6 +971,8 @@ static void fill_defaults (void) {
        }
        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");
@@ -977,6 +987,10 @@ static void fill_defaults (void) {
        }
        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)
index 2288bc7..bacbf5b 100644 (file)
@@ -5,41 +5,30 @@
 #
 
 # Verify those options with main Makefile
-ifndef LINUX
-LINUX=/lib/modules/$(shell uname -r)/build
-endif
-
 CFLAGS         += -pipe -c
 SOURCES                = isdn_lib.c isdn_msg_parser.c 
 OBJDIR         = .
-OBJS           = isdn_lib.o isdn_msg_parser.o
-
-ifndef MISDNUSER
-MISDNUSER=/usr/src/install-misdn/mISDNuser
-endif
+OBJS           = isdn_lib.o isdn_msg_parser.o fac.o
 
-MISDNCFLAGS    += -I$(MISDNUSER)/include -I$(MISDNUSER)/i4lnet -I$(MISDNUSER)/lib
-MISDNCFLAGS    += -DMISDNUSER_JOLLY -I$(LINUX)/include
 
-
-all: chan_misdn_lib.a Makefile.ast
+all: chan_misdn_lib.a 
 
 
 %.o: %.c
-       $(CC) $(MISDNCFLAGS) $(CFLAGS) -o $@ $<
+       $(CC) $(CFLAGS) -o $@ $<
        
 
 chan_misdn_lib.a:      $(OBJS)
        ar crv $@ $(OBJS)
 
-Makefile.ast:  FORCE
-       @echo CFLAGS+=$(MISDNCFLAGS) -Imisdn/ -DCHAN_MISDN_VERSION=\\\"0.2.0\\\" >$@.tmp
-       @echo MISDNUSER = $(MISDNUSER) >>$@.tmp
-       @if [ -r $@ ] && cmp -s $@ $@.tmp; then rm -f $@.tmp; else mv -f $@.tmp $@; fi
-
-
+misdn:
+       @mkdir -p lib
+       cd lib ; cvs -d:pserver:anonymous:readonly@cvs.isdn4linux.de:/i4ldev co -r mqueue mISDN mISDNuser ; 
+       cd lib/mISDN ; make install
+       cd lib/mISDNuser ; make install
+       
 FORCE:
 
-
 clean: 
-       rm *.a *.o Makefile.ast
+       rm -rf *.a *.o *.so
+       rm -rf lib
index 328d888..b6e309f 100644 (file)
@@ -26,17 +26,22 @@ enum misdn_cfg_elements {
        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[] */
+       MISDN_CFG_MUSICCLASS,            /* char[] */
        MISDN_CFG_CALLERID,            /* char[] */
        MISDN_CFG_METHOD,              /* char[] */
        MISDN_CFG_DIALPLAN,            /* int */
+       MISDN_CFG_LOCALDIALPLAN,       /* int */
        MISDN_CFG_NATPREFIX,           /* char[] */
        MISDN_CFG_INTERNATPREFIX,      /* char[] */
        MISDN_CFG_PRES,                /* int (bool) */
        MISDN_CFG_ALWAYS_IMMEDIATE,    /* int (bool) */
        MISDN_CFG_IMMEDIATE,           /* int (bool) */
+       MISDN_CFG_SENDDTMF,           /* int (bool) */
        MISDN_CFG_HOLD_ALLOWED,        /* int (bool) */
        MISDN_CFG_EARLY_BCONNECT,      /* int (bool) */
        MISDN_CFG_USE_CALLINGPRES,     /* int (bool) */
diff --git a/channels/misdn/fac.c b/channels/misdn/fac.c
new file mode 100644 (file)
index 0000000..994d8cf
--- /dev/null
@@ -0,0 +1,313 @@
+
+#include "isdn_lib_intern.h"
+#include "isdn_lib.h"
+
+#include "string.h"
+
+
+
+
+#define CENTREX_ID      0xa1
+#define CALLDEFLECT_ID      0xa1
+
+/**
+   This file covers the encoding and decoding of facility messages and
+   facility information elements.
+
+   There will be 2 Functions as Interface:
+   
+   fac_enc( char **ntmsg, msg_t * msg, enum facility_type type,  union facility fac, struct misdn_bchannel *bc)
+   fac_dec( unsigned char *p, Q931_info_t *qi, enum facility_type *type,  union facility *fac, struct misdn_bchannel *bc);
+
+   Those will either read the union facility or fill it.
+
+   internally, we will have deconding and encoding functions for each facility
+   IE.
+   
+**/
+
+
+/* support stuff */
+static void strnncpy(unsigned char *dest, unsigned char *src, int len, int dst_len)
+{
+       if (len > dst_len-1)
+               len = dst_len-1;
+       strncpy((char *)dest, (char *)src, len);
+       dest[len] = '\0';
+}
+
+
+
+
+/**********************/
+/*** FACILITY STUFF ***/
+/**********************/
+
+
+/* IE_FACILITY */
+void enc_ie_facility(unsigned char **ntmode, msg_t *msg, unsigned char *facility, int facility_len, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char *p;
+       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
+       int l;
+
+
+       if (!facility || facility_len<=0)
+       {
+               return;
+       }
+
+       
+       l = facility_len;
+       p = msg_put(msg, l+2);
+       if (nt)
+               *ntmode = p+1;
+       else
+               qi->QI_ELEMENT(facility) = p - (unsigned char *)qi - sizeof(Q931_info_t);
+       p[0] = IE_FACILITY;
+       p[1] = l;
+       memcpy(p+2, facility, facility_len);
+}
+
+
+/* facility for siemens CENTEX (known parts implemented only) */
+void enc_ie_facility_centrex(unsigned char **ntmode, msg_t *msg, unsigned char *cnip, int setup, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char centrex[256];
+       int i = 0;
+
+       if (!cnip)
+               return;
+
+       /* centrex facility */
+       centrex[i++] = FACILITY_CENTREX;
+       centrex[i++] = CENTREX_ID;
+
+       /* cnip */
+       if (strlen((char *)cnip) > 15)
+       {
+/*             if (options.deb & DEBUG_PORT) */
+               cb_log(1,0,"%s: CNIP/CONP text too long (max 13 chars), cutting.\n", __FUNCTION__);
+               cnip[15] = '\0';
+       }
+       /*  dunno what the 8 bytes mean */
+       if (setup)
+       {
+               centrex[i++] = 0x17;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x44;
+               centrex[i++] = 0x18;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x01;
+               centrex[i++] = 0x09;
+       } else
+       {
+               centrex[i++] = 0x18;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x81;
+               centrex[i++] = 0x09;
+               centrex[i++] = 0x02;
+               centrex[i++] = 0x01;
+               centrex[i++] = 0x0a;
+       }
+
+       centrex[i++] = 0x80;
+       centrex[i++] = strlen((char *)cnip);
+       strcpy((char *)(&centrex[i]), (char *)cnip);
+       i += strlen((char *)cnip);
+       cb_log(4,0,"    cnip='%s'\n", cnip);
+
+       /* encode facility */
+       enc_ie_facility(ntmode, msg, centrex, i, nt , bc);
+}
+
+void dec_ie_facility_centrex(unsigned char *p, Q931_info_t *qi, unsigned char *centrex, int facility_len, unsigned char *cnip, int cnip_len, int nt, struct misdn_bchannel *bc)
+{
+
+       int i = 0;
+       *cnip = '\0';
+       
+       if (facility_len >= 2)
+       {
+               if (centrex[i++] != FACILITY_CENTREX)
+                       return;
+               if (centrex[i++] != CENTREX_ID)
+                       return;
+       }
+
+       /* loop sub IEs of facility */
+       while(facility_len > i+1)
+       {
+               if (centrex[i+1]+i+1 > facility_len)
+               {
+                       printf("%s: ERROR: short read of centrex facility.\n", __FUNCTION__);
+                       return;
+               }
+               switch(centrex[i])
+               {
+               case 0x80:
+                       strnncpy(cnip, &centrex[i+2], centrex[i+1], cnip_len);
+                       cb_log(4,0,"    CENTREX cnip='%s'\n", cnip);
+                       break;
+               }
+               i += 1+centrex[i+1];
+       }
+}
+
+
+
+
+/* facility for CALL Deflect (known parts implemented only) */
+void enc_ie_facility_calldeflect(unsigned char **ntmode, msg_t *msg, unsigned char *nr, int nt, struct misdn_bchannel *bc)
+{
+       unsigned char fac[256];
+       
+       if (!nr)
+               return;
+
+       int len = strlen(nr);
+       /* calldeflect facility */
+       
+       /* cnip */
+       if (strlen((char *)nr) > 15)
+       {
+/*             if (options.deb & DEBUG_PORT) */
+               cb_log(1,0,"%s: NR text too long (max 13 chars), cutting.\n", __FUNCTION__);
+               nr[15] = '\0';
+       }
+       
+       fac[0]=FACILITY_CALLDEFLECT;    // ..
+       fac[1]=CALLDEFLECT_ID;
+       fac[2]=0x0f + len;      // strlen destination + 15 = 26
+       fac[3]=0x02;
+       fac[4]=0x01;
+       //fac[5]=0x70;
+       fac[5]=0x09;
+       fac[6]=0x02;
+       fac[7]=0x01;
+       fac[8]=0x0d;
+       fac[9]=0x30;
+       fac[10]=0x07 + len;     // strlen destination + 7 = 18
+       fac[11]=0x30;   // ...hm 0x30
+       fac[12]=0x02+ len;      // strlen destination + 2       
+       fac[13]=0x80;   // CLIP
+       fac[14]= len;   //  strlen destination 
+       
+       memcpy((unsigned char *)fac+15,nr,len);
+       fac[15+len]=0x01; //sending complete
+       fac[16+len]=0x01;
+       fac[17+len]=0x80;
+       
+       enc_ie_facility(ntmode, msg, fac, 17+len +1 , nt , bc);
+}
+
+
+void dec_ie_facility_calldeflect(unsigned char *p, Q931_info_t *qi, unsigned char *fac, int fac_len, unsigned char *cd_nr,  int nt, struct misdn_bchannel *bc)
+{
+       *cd_nr = '\0';
+       
+       if (fac_len >= 15)
+       {
+               if (fac[0] != FACILITY_CALLDEFLECT)
+                       return;
+               if (fac[1] != CALLDEFLECT_ID)
+                       return;
+       } else {
+               cb_log(1,bc->port, "IE too short: FAC_CALLDEFLECT\n");
+               return ;
+       }
+       
+       
+       
+       {
+               int dest_len=fac[2]-0x0f;
+               
+               if (dest_len <0 || dest_len > 15) {
+                       cb_log(1,bc->port, "IE is garbage: FAC_CALLDEFLECT\n");
+                       return ;
+               }
+               
+               if (fac_len < 15+dest_len) {
+                       cb_log(1,bc->port, "IE too short: FAC_CALLDEFLECT\n");
+                       return ;
+               }
+               
+               memcpy(cd_nr, &fac[15],dest_len);
+               cd_nr[dest_len]=0;
+               
+               cb_log(5,bc->port, "--> IE CALLDEFLECT NR: %s\n",cd_nr);
+       }
+}
+
+
+
+void fac_enc( unsigned char **ntmsg, msg_t * msg, enum facility_type type,  union facility fac, struct misdn_bchannel *bc)
+{
+       switch (type) {
+       case FACILITY_CENTREX:
+       {
+               int setup=0;
+               enc_ie_facility_centrex(ntmsg, msg, fac.cnip, setup, bc->nt, bc);
+       }
+               break;
+       case FACILITY_CALLDEFLECT:
+               enc_ie_facility_calldeflect(ntmsg, msg, fac.calldeflect_nr, bc->nt, bc);
+               break;
+       default:
+               cb_log(1,0,"Don't know how handle this facility: %d\n", type);
+       }
+}
+
+void fac_dec( unsigned char *p, Q931_info_t *qi, enum facility_type *type,  union facility *fac, struct misdn_bchannel *bc)
+{
+       int i, fac_len=0;
+       unsigned char facility[256];
+       
+       if (! (bc->nt) )
+       {
+               p = NULL;
+               if (qi->QI_ELEMENT(facility))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
+       }
+       if (!p)
+               return;
+       
+       fac_len = p[0];
+       memcpy(facility, p+1, fac_len);
+       
+       
+       switch(facility[0]) {
+       case FACILITY_CENTREX:
+       {
+               int cnip_len=15;
+               
+               dec_ie_facility_centrex(p, qi,facility, fac_len, fac->cnip, cnip_len, bc->nt, bc);
+               
+               *type=FACILITY_CENTREX;
+       }
+       break;
+       case FACILITY_CALLDEFLECT:
+               dec_ie_facility_calldeflect(p, qi,facility, fac_len, fac->calldeflect_nr,  bc->nt, bc);
+               
+               *type=FACILITY_CALLDEFLECT;
+               break;
+       default:
+               cb_log(3, bc->port, "Unknown Facility received: ");
+               i = 0;
+               while(i < fac_len)
+               {
+                       cb_log(3, bc->port, " %02x", facility[i]);
+                       i++;
+               }
+               cb_log(3, bc->port, "    facility\n");
+               
+               *type=FACILITY_NONE;
+       }
+       
+       
+}
+
+/*** FACILITY END **/
+
diff --git a/channels/misdn/fac.h b/channels/misdn/fac.h
new file mode 100644 (file)
index 0000000..acc41f1
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef FAC_H
+#define FAC_H
+
+void fac_enc( unsigned char **ntmsg, msg_t * msg, enum facility_type type,  union facility fac, struct misdn_bchannel *bc);
+
+void fac_dec( unsigned char *p, Q931_info_t *qi, enum facility_type *type,  union facility *fac, struct misdn_bchannel *bc);
+
+#endif
index 911ebaf..6f29cb2 100644 (file)
 
 #include <string.h>
 
+#include <mISDNuser/mISDNlib.h>
+#include <mISDNuser/isdn_net.h>
+#include <mISDNuser/l3dss1.h>
+#include <mISDNuser/net_l3.h>
 
-#include "isdn_lib_intern.h"
 
-#include <mISDNlib.h>
-#include <isdn_net.h>
-#include <l3dss1.h>
-#include <net_l3.h>
-
-
-#define CENTREX_FAC     0x88
-#define CENTREX_ID      0xa1
 
 #define MISDN_IE_DEBG 0
 
@@ -69,7 +64,8 @@ void enc_ie_complete(unsigned char **ntmode, msg_t *msg, int complete, int nt, s
                {
                        *ntmode = p;
                } else
-                       qi->sending_complete = p - (unsigned char *)qi - sizeof(Q931_info_t);
+                       qi->QI_ELEMENT(sending_complete) = p - (unsigned char *)qi - sizeof(Q931_info_t);
+
                p[0] = IE_COMPLETE;
        }
 }
@@ -79,7 +75,7 @@ void dec_ie_complete(unsigned char *p, Q931_info_t *qi, int *complete, int nt, s
        *complete = 0;
        if (!nt)
        {
-               if (qi->sending_complete)
+               if (qi->QI_ELEMENT(sending_complete))
                        *complete = 1;
        } else
                if (p)
@@ -140,7 +136,7 @@ void enc_ie_bearer(unsigned char **ntmode, msg_t *msg, int coding, int capabilit
        if (nt)
                *ntmode = p+1;
        else
-               qi->bearer_capability = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(bearer_capability) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_BEARER;
        p[1] = l;
        p[2] = 0x80 + (coding<<5) + capability;
@@ -170,10 +166,10 @@ void dec_ie_bearer(unsigned char *p, Q931_info_t *qi, int *coding, int *capabili
        if (!nt)
        {
                p = NULL;
-               if (qi->llc)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->llc + 1;
-               else if (qi->bearer_capability)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->bearer_capability + 1;
+               if (qi->QI_ELEMENT(llc))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(llc) + 1;
+               else if (qi->QI_ELEMENT(bearer_capability))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(bearer_capability) + 1;
        }
        if (!p)
                return;
@@ -292,7 +288,7 @@ void enc_ie_call_id(unsigned char **ntmode, msg_t *msg, unsigned char *callid, i
        if (nt)
                *ntmode = p+1;
        else
-               qi->call_id = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(call_id) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_CALL_ID;
        p[1] = l;
        memcpy(p+2, callid, callid_len);
@@ -308,8 +304,8 @@ void dec_ie_call_id(unsigned char *p, Q931_info_t *qi, unsigned char *callid, in
        if (!nt)
        {
                p = NULL;
-               if (qi->call_id)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->call_id + 1;
+               if (qi->QI_ELEMENT(call_id))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(call_id) + 1;
        }
        if (!p)
                return;
@@ -363,7 +359,7 @@ void enc_ie_called_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, un
        if (nt)
                *ntmode = p+1;
        else
-               qi->called_nr = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(called_nr) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_CALLED_PN;
        p[1] = l;
        p[2] = 0x80 + (type<<4) + plan;
@@ -379,8 +375,8 @@ void dec_ie_called_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, u
        if (!nt)
        {
                p = NULL;
-               if (qi->called_nr)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->called_nr + 1;
+               if (qi->QI_ELEMENT(called_nr))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(called_nr) + 1;
        }
        if (!p)
                return;
@@ -437,7 +433,7 @@ void enc_ie_calling_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, i
        if (nt)
                *ntmode = p+1;
        else
-               qi->calling_nr = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(calling_nr) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_CALLING_PN;
        p[1] = l;
        if (present >= 0)
@@ -465,8 +461,8 @@ void dec_ie_calling_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan,
        if (!nt)
        {
                p = NULL;
-               if (qi->calling_nr)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->calling_nr + 1;
+               if (qi->QI_ELEMENT(calling_nr))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(calling_nr) + 1;
        }
        if (!p)
                return;
@@ -539,7 +535,7 @@ void enc_ie_connected_pn(unsigned char **ntmode, msg_t *msg, int type, int plan,
        if (nt)
                *ntmode = p+1;
        else
-               qi->connected_nr = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(connected_nr) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_CONNECT_PN;
        p[1] = l;
        if (present >= 0)
@@ -567,8 +563,8 @@ void dec_ie_connected_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan
        if (!nt)
        {
                p = NULL;
-               if (qi->connected_nr)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->connected_nr + 1;
+               if (qi->QI_ELEMENT(connected_nr))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(connected_nr) + 1;
        }
        if (!p)
                return;
@@ -624,7 +620,7 @@ void enc_ie_cause(unsigned char **ntmode, msg_t *msg, int location, int cause, i
        if (nt)
                *ntmode = p+1;
        else
-               qi->cause = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(cause) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_CAUSE;
        p[1] = l;
        p[2] = 0x80 + location;
@@ -637,7 +633,7 @@ void enc_ie_cause_standalone(unsigned char **ntmode, msg_t *msg, int location, i
        if (ntmode)
                *ntmode = p+1;
        else
-               qi->cause = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(cause) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_CAUSE;
        p[1] = 2;
        p[2] = 0x80 + location;
@@ -653,8 +649,8 @@ void dec_ie_cause(unsigned char *p, Q931_info_t *qi, int *location, int *cause,
        if (!nt)
        {
                p = NULL;
-               if (qi->cause)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->cause + 1;
+               if (qi->QI_ELEMENT(cause))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(cause) + 1;
        }
        if (!p)
                return;
@@ -705,7 +701,7 @@ void enc_ie_channel_id(unsigned char **ntmode, msg_t *msg, int exclusive, int ch
                if (nt)
                        *ntmode = p+1;
                else
-                       qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t);
+                       qi->QI_ELEMENT(channel_id) = p - (unsigned char *)qi - sizeof(Q931_info_t);
                p[0] = IE_CHANNEL_ID;
                p[1] = l;
                if (channel == 0xff)
@@ -725,7 +721,7 @@ void enc_ie_channel_id(unsigned char **ntmode, msg_t *msg, int exclusive, int ch
                        if (nt)
                                *ntmode = p+1;
                        else
-                               qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t);
+                               qi->QI_ELEMENT(channel_id) = p - (unsigned char *)qi - sizeof(Q931_info_t);
                        p[0] = IE_CHANNEL_ID;
                        p[1] = l;
                        p[2] = 0x80 + 0x20 + 0x03;
@@ -737,7 +733,7 @@ void enc_ie_channel_id(unsigned char **ntmode, msg_t *msg, int exclusive, int ch
                if (nt)
                        *ntmode = p+1;
                else
-                       qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t);
+                       qi->QI_ELEMENT(channel_id) = p - (unsigned char *)qi - sizeof(Q931_info_t);
                p[0] = IE_CHANNEL_ID;
                p[1] = l;
                p[2] = 0x80 + 0x20 + (exclusive<<3) + 0x01;
@@ -758,8 +754,8 @@ void dec_ie_channel_id(unsigned char *p, Q931_info_t *qi, int *exclusive, int *c
        if (!nt)
        {
                p = NULL;
-               if (qi->channel_id)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->channel_id + 1;
+               if (qi->QI_ELEMENT(channel_id))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(channel_id) + 1;
        }
        if (!p)
                return;
@@ -863,7 +859,7 @@ void enc_ie_date(unsigned char **ntmode, msg_t *msg, time_t ti, int nt, struct m
        if (nt)
                *ntmode = p+1;
        else
-               qi->date = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(date) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_DATE;
        p[1] = l;
        p[2] = tm->tm_year % 100;
@@ -900,7 +896,7 @@ void enc_ie_display(unsigned char **ntmode, msg_t *msg, unsigned char *display,
        if (nt)
                *ntmode = p+1;
        else
-               qi->display = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(display) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_DISPLAY;
        p[1] = l;
        strncpy((char *)p+2, (char *)display, strlen((char *)display));
@@ -913,8 +909,8 @@ void dec_ie_display(unsigned char *p, Q931_info_t *qi, unsigned char *display, i
        if (!nt)
        {
                p = NULL;
-               if (qi->display)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->display + 1;
+               if (qi->QI_ELEMENT(display))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(display) + 1;
        }
        if (!p)
                return;
@@ -950,7 +946,7 @@ void enc_ie_keypad(unsigned char **ntmode, msg_t *msg, unsigned char *keypad, in
        if (nt)
                *ntmode = p+1;
        else
-               qi->keypad = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(keypad) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_KEYPAD;
        p[1] = l;
        strncpy((char *)p+2, (char *)keypad, strlen((char *)keypad));
@@ -963,8 +959,8 @@ void dec_ie_keypad(unsigned char *p, Q931_info_t *qi, unsigned char *keypad, int
        if (!nt)
        {
                p = NULL;
-               if (qi->keypad)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->keypad + 1;
+               if (qi->QI_ELEMENT(keypad))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(keypad) + 1;
        }
        if (!p)
                return;
@@ -1000,7 +996,7 @@ void enc_ie_notify(unsigned char **ntmode, msg_t *msg, int notify, int nt, struc
        if (nt)
                *ntmode = p+1;
        else
-               qi->notify = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(notify) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_NOTIFY;
        p[1] = l;
        p[2] = 0x80 + notify;
@@ -1013,8 +1009,8 @@ void dec_ie_notify(unsigned char *p, Q931_info_t *qi, int *notify, int nt, struc
        if (!nt)
        {
                p = NULL;
-               if (qi->notify)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->notify + 1;
+               if (qi->QI_ELEMENT(notify))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(notify) + 1;
        }
        if (!p)
                return;
@@ -1060,7 +1056,7 @@ void enc_ie_progress(unsigned char **ntmode, msg_t *msg, int coding, int locatio
        if (nt)
                *ntmode = p+1;
        else
-               qi->progress = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(progress) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_PROGRESS;
        p[1] = l;
        p[2] = 0x80 + (coding<<5) + location;
@@ -1077,8 +1073,8 @@ void dec_ie_progress(unsigned char *p, Q931_info_t *qi, int *coding, int *locati
        if (!nt)
        {
                p = NULL;
-               if (qi->progress)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->progress + 1;
+               if (qi->QI_ELEMENT(progress))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(progress) + 1;
        }
        if (!p)
                return;
@@ -1092,7 +1088,8 @@ void dec_ie_progress(unsigned char *p, Q931_info_t *qi, int *coding, int *locati
        *location = p[1] & 0x0f;
        *progress = p[2] & 0x7f;
 
-       if (MISDN_IE_DEBG) printf("    coding=%d location=%d progress=%d\n", *coding, *location, *progress);
+       //if (MISDN_IE_DEBG) printf("    coding=%d location=%d progress=%d\n", *coding, *location, *progress);
+       if (1) printf("    coding=%d location=%d progress=%d\n", *coding, *location, *progress);
 }
 
 
@@ -1144,7 +1141,7 @@ void enc_ie_redir_nr(unsigned char **ntmode, msg_t *msg, int type, int plan, int
        if (nt)
                *ntmode = p+1;
        else
-               qi->redirect_nr = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(redirect_nr) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_REDIR_NR;
        p[1] = l;
        if (present >= 0)
@@ -1183,8 +1180,8 @@ void dec_ie_redir_nr(unsigned char *p, Q931_info_t *qi, int *type, int *plan, in
        if (!nt)
        {
                p = NULL;
-               if (qi->redirect_nr)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->redirect_nr + 1;
+               if (qi->QI_ELEMENT(redirect_nr))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(redirect_nr) + 1;
        }
        if (!p)
                return;
@@ -1252,7 +1249,7 @@ void enc_ie_redir_dn(unsigned char **ntmode, msg_t *msg, int type, int plan, int
                *ntmode = p+1;
        else
 /* #warning REINSERT redir_dn, when included in te-mode */
-               /*qi->redir_dn = p - (unsigned char *)qi - sizeof(Q931_info_t)*/;
+               /*qi->QI_ELEMENT(redir_dn) = p - (unsigned char *)qi - sizeof(Q931_info_t)*/;
        p[0] = IE_REDIR_DN;
        p[1] = l;
        if (present >= 0)
@@ -1280,8 +1277,8 @@ void dec_ie_redir_dn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, in
        {
                p = NULL;
 /* #warning REINSERT redir_dn, when included in te-mode */
-/*             if (qi->redir_dn) */
-/*                     p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->redir_dn + 1; */
+/*             if (qi->QI_ELEMENT(redir_dn)) */
+/*                     p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(redir_dn) + 1; */
        }
        if (!p)
                return;
@@ -1306,242 +1303,6 @@ void dec_ie_redir_dn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, in
 }
 
 
-/* IE_FACILITY */
-void enc_ie_facility(unsigned char **ntmode, msg_t *msg, unsigned char *facility, int facility_len, int nt, struct misdn_bchannel *bc)
-{
-       unsigned char *p;
-       Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
-       int l;
-
-       char debug[768];
-       int i;
-
-       if (!facility || facility_len<=0)
-       {
-               return;
-       }
-
-       i = 0;
-       while(i < facility_len)
-       {
-               if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", facility[i]);
-               i++;
-       }
-               
-       if (MISDN_IE_DEBG) printf("    facility%s\n", debug);
-
-       l = facility_len;
-       p = msg_put(msg, l+2);
-       if (nt)
-               *ntmode = p+1;
-       else
-               qi->facility = p - (unsigned char *)qi - sizeof(Q931_info_t);
-       p[0] = IE_FACILITY;
-       p[1] = l;
-       memcpy(p+2, facility, facility_len);
-}
-
-void dec_ie_facility(unsigned char *p, Q931_info_t *qi, unsigned char *facility, int *facility_len, int nt, struct misdn_bchannel *bc)
-{
-       int i;
-       struct misdn_stack *stack=get_stack_by_bc(bc);
-       
-       *facility_len = 0;
-
-       if (!nt)
-       {
-               p = NULL;
-               if (qi->facility)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->facility + 1;
-       }
-       if (!p)
-               return;
-
-       *facility_len = p[0];
-       memcpy(facility, p+1, *facility_len);
-       
-       i = 0;
-       while(i < *facility_len)
-       {
-               cb_log(3, stack->port, " %02x", facility[i]);
-               i++;
-       }
-       cb_log(3, stack->port, "    facility\n");
-}
-
-
-/* facility for siemens CENTEX (known parts implemented only) */
-void enc_facility_centrex(unsigned char **ntmode, msg_t *msg, unsigned char *cnip, int setup, int nt, struct misdn_bchannel *bc)
-{
-       unsigned char centrex[256];
-       int i = 0;
-
-       if (!cnip)
-               return;
-
-       /* centrex facility */
-       centrex[i++] = CENTREX_FAC;
-       centrex[i++] = CENTREX_ID;
-
-       /* cnip */
-       if (strlen((char *)cnip) > 15)
-       {
-/*             if (options.deb & DEBUG_PORT) */
-               if (MISDN_IE_DEBG) printf("%s: CNIP/CONP text too long (max 13 chars), cutting.\n", __FUNCTION__);
-               cnip[15] = '\0';
-       }
-       /*  dunno what the 8 bytes mean */
-       if (setup)
-       {
-               centrex[i++] = 0x17;
-               centrex[i++] = 0x02;
-               centrex[i++] = 0x02;
-               centrex[i++] = 0x44;
-               centrex[i++] = 0x18;
-               centrex[i++] = 0x02;
-               centrex[i++] = 0x01;
-               centrex[i++] = 0x09;
-       } else
-       {
-               centrex[i++] = 0x18;
-               centrex[i++] = 0x02;
-               centrex[i++] = 0x02;
-               centrex[i++] = 0x81;
-               centrex[i++] = 0x09;
-               centrex[i++] = 0x02;
-               centrex[i++] = 0x01;
-               centrex[i++] = 0x0a;
-       }
-
-       centrex[i++] = 0x80;
-       centrex[i++] = strlen((char *)cnip);
-       strcpy((char *)(&centrex[i]), (char *)cnip);
-       i += strlen((char *)cnip);
-       if (MISDN_IE_DEBG) printf("    cnip='%s'\n", cnip);
-
-       /* encode facility */
-       enc_ie_facility(ntmode, msg, centrex, i, nt , bc);
-}
-
-void dec_facility_centrex(unsigned char *p, Q931_info_t *qi, unsigned char *cnip, int cnip_len, int nt, struct misdn_bchannel *bc)
-{
-       unsigned char centrex[256];
-       char debug[768];
-       int facility_len = 0;
-       int i = 0, j;
-       *cnip = '\0';
-
-       dec_ie_facility(p, qi, centrex, &facility_len,  nt, bc);
-       if (facility_len >= 2)
-       {
-               if (centrex[i++] != CENTREX_FAC)
-                       return;
-               if (centrex[i++] != CENTREX_ID)
-                       return;
-       }
-
-       /* loop sub IEs of facility */
-       while(facility_len > i+1)
-       {
-               if (centrex[i+1]+i+1 > facility_len)
-               {
-                       printf("%s: ERROR: short read of centrex facility.\n", __FUNCTION__);
-                       return;
-               }
-               switch(centrex[i])
-               {
-               case 0x80:
-                       strnncpy(cnip, &centrex[i+2], centrex[i+1], cnip_len);
-                       if (MISDN_IE_DEBG) printf("    CENTREX cnip='%s'\n", cnip);
-                       break;
-
-               default:
-                       j = 0;
-                       while(j < centrex[i+1])
-                       {
-                               if (MISDN_IE_DEBG) printf(debug+(j*3), " %02x", centrex[i+1+j]);
-                               i++;
-                       }
-                       if (MISDN_IE_DEBG) printf("    CENTREX unknown=0x%2x len=%d%s\n", centrex[i], centrex[i+1], debug);
-               }
-               i += 1+centrex[i+1];
-       }
-}
-
-
-
-
-/* facility for siemens CENTEX (known parts implemented only) */
-void enc_facility_calldeflect(unsigned char **ntmode, msg_t *msg, unsigned char *nr, int nt, struct misdn_bchannel *bc)
-{
-       unsigned char fac[256];
-       
-       if (!nr)
-               return;
-       
-       /* calldeflect facility */
-       
-       /* cnip */
-       if (strlen((char *)nr) > 15)
-       {
-/*             if (options.deb & DEBUG_PORT) */
-               if (MISDN_IE_DEBG) printf("%s: NR text too long (max 13 chars), cutting.\n", __FUNCTION__);
-               nr[15] = '\0';
-       }
-       
-       fac[0]=0; // len 
-       fac[1]=0; //len 
-       fac[2]=0x01; // Use D-Chan
-       fac[3]=0; // Keypad len
-       fac[4]=31;      // user user data? len = 31 = 29 + 2
-       fac[5]=0x1c;    // magic?
-       fac[6]=0x1d;    // strlen destination + 18 = 29
-       fac[7]=0x91;    // ..
-       fac[8]=0xA1;
-       fac[9]=0x1A;    // strlen destination + 15 = 26
-       fac[10]=0x02;
-       fac[11]=0x01;
-       fac[12]=0x70;
-       fac[13]=0x02;
-       fac[14]=0x01;
-       fac[15]=0x0d;
-       fac[16]=0x30;
-       fac[17]=0x12;   // strlen destination + 7 = 18
-       fac[18]=0x30;   // ...hm 0x30
-       fac[19]=0x0d;   // strlen destination + 2       
-       fac[20]=0x80;   // CLIP
-       fac[21]=0x0b;   //  strlen destination 
-       fac[22]=0x01;   //  destination start
-       fac[23]=0x01;   //  
-       fac[24]=0x01;   //  
-       fac[25]=0x01;   //  
-       fac[26]=0x01;   //  
-       fac[27]=0x01;   //  
-       fac[28]=0x01;   //  
-       fac[29]=0x01;   //  
-       fac[30]=0x01;   //  
-       fac[31]=0x01;   //  
-       fac[32]=0x01;   //  
-       fac[33]=0x01;   // 0x1 = sending complete
-       fac[34]=0x01;
-       fac[35]=0x01;
-                                  
-       memcpy((unsigned char *)fac+22,nr,strlen(nr));
-       fac[22+strlen( nr)]=0x01;       // fill with 0x01 if number is only 6 numbers (local call)
-       fac[23+strlen(nr)]=0x01;
-       fac[24+strlen(nr)]=0x01;
-       fac[25+strlen(nr)]=0x01;
-       fac[26+strlen(nr)]=0x01;
-       
-       fac[6]=18+strlen(nr);
-       fac[9]=15+strlen(nr);
-       fac[17]=7+strlen(nr);
-       fac[19]=2+strlen(nr);
-       fac[21]=strlen(nr);
-       
-       enc_ie_facility(ntmode, msg, &fac[4], 36-4, nt , bc);
-}
-
 
 /* IE_USERUSER */
 void enc_ie_useruser(unsigned char **ntmode, msg_t *msg, int protocol, unsigned char *user, int user_len, int nt, struct misdn_bchannel *bc)
@@ -1577,7 +1338,7 @@ void enc_ie_useruser(unsigned char **ntmode, msg_t *msg, int protocol, unsigned
        if (nt)
                *ntmode = p+1;
        else
-               qi->useruser = p - (unsigned char *)qi - sizeof(Q931_info_t);
+               qi->QI_ELEMENT(useruser) = p - (unsigned char *)qi - sizeof(Q931_info_t);
        p[0] = IE_USER_USER;
        p[1] = l;
        p[2] = 0x80 + protocol;
@@ -1595,8 +1356,8 @@ void dec_ie_useruser(unsigned char *p, Q931_info_t *qi, int *protocol, unsigned
        if (!nt)
        {
                p = NULL;
-               if (qi->useruser)
-                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->useruser + 1;
+               if (qi->QI_ELEMENT(useruser))
+                       p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(useruser) + 1;
        }
        if (!p)
                return;
@@ -1619,3 +1380,5 @@ void dec_ie_useruser(unsigned char *p, Q931_info_t *qi, int *protocol, unsigned
 }
 
 
+
+
index d912795..8529812 100644 (file)
 #include "isdn_lib_intern.h"
 
 
+void misdn_free_ibuffer(void *ibuf)
+{
+       free_ibuffer((ibuffer_t*)ibuf);
+}
+
+
+void misdn_clear_ibuffer(void *ibuf)
+{
+       clear_ibuffer( (ibuffer_t*)ibuf);
+}
+
+void *misdn_init_ibuffer(int len)
+{
+       return init_ibuffer(len);
+}
+
 int misdn_ibuf_freecount(void *buf)
 {
        return ibuf_freecount( (ibuffer_t*)buf);
@@ -73,7 +89,8 @@ void get_show_stack_details(int port, char *buf)
        }
        
        if (stack) {
-               sprintf(buf, "* Stack Addr: Port %d Type %s Prot. %s L2Link %s L1Link:%s", stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN");
+               sprintf(buf, "* Stack Addr:%x Port %d Type %s Prot. %s L2Link %s L1Link:%s", stack->upper_id, stack->port, stack->nt?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN");
+               
        } else {
                buf[0]=0;
        }
@@ -91,8 +108,8 @@ enum global_states {
 static enum global_states  global_state=MISDN_INITIALIZING;
 
 
-#include <../i4lnet/net_l2.h>
-#include <tone.h>
+#include <mISDNuser/net_l2.h>
+#include <mISDNuser/tone.h>
 #include <unistd.h>
 #include <semaphore.h>
 #include <pthread.h>
@@ -165,7 +182,7 @@ struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned lon
 
 /* from isdn_lib.h */
 int init_bc(struct misdn_stack * stack,  struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime);
-struct misdn_stack* stack_te_init(int midev,  int port, int ptp);
+struct misdn_stack* stack_init(int midev,  int port, int ptp);
 void stack_te_destroy(struct misdn_stack* stack);
        /* user iface */
 int te_lib_init( void ) ; /* returns midev */
@@ -234,7 +251,7 @@ void init_flip_bits(void)
        }
 }
 
-static unsigned char * flip_buf_bits ( unsigned char * buf , int len)
+unsigned char * flip_buf_bits ( unsigned char * buf , int len)
 {
        int i;
        char * start = buf;
@@ -315,12 +332,20 @@ int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
        iframe_t *frm;
        frm = (iframe_t *)dmsg->data;
        struct misdn_stack *stack=get_stack_by_bc(bc);
+
+
+       if (!stack) {
+               cb_log(0,0,"send_msg: IEK!! no stack\n ");
+               return -1;
+       }
+       
+       frm->addr = (stack->upper_id | FLG_MSG_DOWN);
+
        
-       frm->addr = (stack->upper_id &  IF_ADDRMASK) | IF_DOWN ;
        frm->dinfo = bc->l3_id;
   
        frm->len = (dmsg->len) - mISDN_HEADER_LEN;
-  
+       
        mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
   
        free_msg(dmsg);
@@ -331,6 +356,7 @@ int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
 
 static int mypid=0;
 
+
 int misdn_cap_is_speech(int cap)
 /** Poor mans version **/
 {
@@ -341,6 +367,10 @@ int misdn_cap_is_speech(int cap)
 
 int misdn_inband_avail(struct misdn_bchannel *bc)
 {
+
+       /*if ! early_bconnect we have never inband available*/
+       if ( ! bc->early_bconnect ) return 0;
+       
        switch (bc->progress_indicator) {
        case INFO_PI_INBAND_AVAILABLE:
        case INFO_PI_CALL_NOT_E2E_ISDN:
@@ -370,7 +400,7 @@ static int find_free_chan_in_stack(struct misdn_stack *stack, int channel)
        int i;
 
        if (channel < 0 || channel > MAX_BCHANS) {
-               cb_log(4, stack->port, " !! out of bound call to find_free_chan_in_stack! (port:%d ch:%d)\n", stack->port, channel);
+               cb_log(4, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
                return 0;
        }
        
@@ -411,7 +441,7 @@ void empty_bc(struct misdn_bchannel *bc)
        bc->send_dtmf=0;
        bc->nodsp=0;
        bc->nojitter=0;
-       
+
        bc->time_usec=0;
        
        bc->rxgain=0;
@@ -421,7 +451,7 @@ void empty_bc(struct misdn_bchannel *bc)
        bc->curptx=0; bc->curprx=0;
        
        bc->crypt_key[0] = 0;
-  
+       
        bc->tone=TONE_NONE;
        bc->tone_cnt2 = bc->tone_cnt=0;
   
@@ -429,12 +459,15 @@ void empty_bc(struct misdn_bchannel *bc)
        bc->onumplan=NUMPLAN_UNKNOWN;
        bc->rnumplan=NUMPLAN_UNKNOWN;
        
+
        bc->active = 0;
-  
+
+       bc->early_bconnect = 1;
+       
        bc->ec_enable = 0;
        bc->ec_deftaps = 128;
        bc->ec_whenbridged = 0;
-       bc->ec_training = 400;
+       bc->ec_training = 1;
        
        
        bc->orig=0;
@@ -467,9 +500,8 @@ void empty_bc(struct misdn_bchannel *bc)
        bc->dad[0] = 0;
        bc->orig_dad[0] = 0;
        
-       bc->facility=FACILITY_NONE;
-       bc->facility_calldeflect_nr[0]=0;
-
+       bc->fac_type=FACILITY_NONE;
+       
        bc->te_choose_channel = 0;
 }
 
@@ -488,21 +520,27 @@ int clean_up_bc(struct misdn_bchannel *bc)
                cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
                return -1;
        }
-  
+
+       if (bc->active) {
+               cb_log(2, stack->port, "$$$ bc still active, deactivatiing .. stid :%x\n", bc->b_stid);
+               manager_bchannel_deactivate(bc);
+       }
+       
        cb_log(5, stack->port, "$$$ Cleaning up bc with stid :%x\n", bc->b_stid);
        
        
        if ( misdn_cap_is_speech(bc->capability) && bc->ec_enable) {
                manager_ec_disable(bc);
        }
-       
-       mISDN_write_frame(stack->midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+
+       mISDN_write_frame(stack->midev, buff, bc->addr|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
        
        
        bc->b_stid = 0;
        
        bc->upset=0;
        
+       
        return ret;
 }
 
@@ -554,7 +592,9 @@ int misdn_lib_get_l1_up(struct misdn_stack *stack)
        /* Pull Up L1 if we have JOLLY */ 
        iframe_t act;
        act.prim = PH_ACTIVATE | REQUEST; 
-       act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+       act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
+
+       
        act.dinfo = 0;
        act.len = 0;
 
@@ -565,7 +605,7 @@ int misdn_lib_get_l1_up(struct misdn_stack *stack)
 int misdn_lib_get_l2_up(struct misdn_stack *stack)
 {
        
-       if (stack->ptp && (stack->mode == NT_MODE) ) {
+       if (stack->ptp && (stack->nt) ) {
                msg_t *dmsg;
                /* L2 */
                dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
@@ -577,8 +617,8 @@ int misdn_lib_get_l2_up(struct misdn_stack *stack)
                iframe_t act;
                
                act.prim = DL_ESTABLISH | REQUEST;
-
-               act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN;
+               act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
+               
                act.dinfo = 0;
                act.len = 0;
                return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
@@ -592,17 +632,31 @@ int misdn_lib_get_l2_status(struct misdn_stack *stack)
 {
        iframe_t act;
        
-#ifdef DL_STATUS
-       act.prim = DL_STATUS | REQUEST; 
-#else
        act.prim = DL_ESTABLISH | REQUEST;
-#endif
-       act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN;
+
+       act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
+
        act.dinfo = 0;
        act.len = 0;
        return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
 }
 
+int misdn_lib_get_short_status(struct misdn_stack *stack)
+{
+       iframe_t act;
+       
+       
+       act.prim = MGR_SHORTSTATUS | REQUEST; 
+       
+       act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
+
+       act.dinfo = SSTATUS_L1;
+       
+       act.len = 0;
+       return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+}
+
+
 
 static int create_process (int midev, struct misdn_bchannel *bc) {
        iframe_t ncr;
@@ -611,7 +665,7 @@ static int create_process (int midev, struct misdn_bchannel *bc) {
        struct misdn_stack *stack=get_stack_by_bc(bc);
        int free_chan;
   
-       if (stack->mode == NT_MODE) {
+       if (stack->nt) {
                free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0);
                if (!free_chan) return -1;
                bc->channel=free_chan;
@@ -620,7 +674,7 @@ static int create_process (int midev, struct misdn_bchannel *bc) {
                        if (stack->procids[i]==0) break;
     
                if (i== MAXPROCS) {
-                       cb_log(0, stack->port, "Couldnt Create New ProcId Port:%d\n",stack->port);
+                       cb_log(-1, stack->port, "Couldnt Create New ProcId.\n");
                        return -1;
                }
                stack->procids[i]=1;
@@ -632,7 +686,9 @@ static int create_process (int midev, struct misdn_bchannel *bc) {
 #endif
     
                ncr.prim = CC_NEW_CR | REQUEST; 
-               ncr.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+
+               ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
+
                ncr.dinfo = l3_id;
                ncr.len = 0;
 
@@ -661,7 +717,9 @@ static int create_process (int midev, struct misdn_bchannel *bc) {
                l3_id = (entity<<16) | newteid;
                /* preparing message */
                ncr.prim = CC_NEW_CR | REQUEST; 
-               ncr.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+
+               ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
+
                ncr.dinfo =l3_id;
                ncr.len = 0;
                /* send message */
@@ -691,16 +749,14 @@ int setup_bc(struct misdn_bchannel *bc)
   
        mISDN_pid_t pid;
        int ret;
+       
 
        struct misdn_stack *stack=get_stack_by_bc(bc);
        
        int midev=stack->midev;
        int channel=bc->channel-1-(bc->channel>16);
        int b_stid=stack->b_stids[channel>=0?channel:0];
-
        
-       if (bc->nodsp ) 
-               clean_up_bc(bc);
        
        if ( !misdn_cap_is_speech(bc->capability))
                clean_up_bc(bc);
@@ -714,11 +770,12 @@ int setup_bc(struct misdn_bchannel *bc)
        cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
        
        if (b_stid <= 0) {
-               cb_log(0, stack->port," -- Stid <=0 at the moment on port:%d channel:%d\n",stack->port,channel);
+               cb_log(-1, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
+
                return 1;
        }
        
-  
+       
        bc->b_stid = b_stid;
        
        {
@@ -731,7 +788,11 @@ int setup_bc(struct misdn_bchannel *bc)
                li.st = bc->b_stid; /*  given idx */
 
 
-               if ( misdn_cap_is_speech(bc->capability) && !bc->nodsp && bc->async != 1) {
+#define MISDN_DSP
+#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");
                        { 
                                int l = sizeof(li.name);
@@ -740,6 +801,8 @@ int setup_bc(struct misdn_bchannel *bc)
                        }
                        li.pid.layermask = ISDN_LAYER((4));
                        li.pid.protocol[4] = ISDN_PID_L4_B_USER;
+
+                       bc->layer=4;
                        
                } else {
                        cb_log(4, stack->port,"setup_bc: without dsp\n");
@@ -750,21 +813,24 @@ int setup_bc(struct misdn_bchannel *bc)
                        }
                        li.pid.layermask = ISDN_LAYER((3));
                        li.pid.protocol[3] = ISDN_PID_L3_B_USER;
+                       
+                       bc->layer=3;
                }
                
                ret = mISDN_new_layer(midev, &li);
-               if (ret <= 0) {
-                       cb_log(0, stack->port,"New Layer Err: %d %s port:%d\n",ret,strerror(errno), stack->port);
+               if (ret ) {
+                       cb_log(-1, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
+                       
                        return(-EINVAL);
                }
-      
-               bc->layer_id = ret;
+               
+               bc->layer_id = li.id;
        }
        
        memset(&pid, 0, sizeof(pid));
        
-       bc->addr = ( bc->layer_id & IF_ADDRMASK) | IF_DOWN;
-       cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
+       
+       
        cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
        
        
@@ -793,16 +859,31 @@ int setup_bc(struct misdn_bchannel *bc)
        
        ret = mISDN_set_stack(midev, bc->b_stid, &pid);
        
-       
        if (ret){
                cb_log(5, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
-    
-               mISDN_write_frame(midev, buff, bc->addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+               
+               mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
                return(-EINVAL);
        }
-  
+
+#if 0
+       ret = mISDN_get_setstack_ind(midev, bc->layer_id );
+       
+       if (ret){
+               cb_log(5, stack->port,"$$$ Get Set Stack Err: %d %s\n",ret,strerror(errno));
+               
+               mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+               return(-EINVAL);
+       }
+
+#endif
+
+       
        bc->upset=1;
-  
+       
+       
+       manager_bchannel_deactivate(bc);
+       
        return 0;
 }
 
@@ -817,7 +898,7 @@ int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, in
   
        if (!bc) return -1;
   
-       cb_log(4, port, "Init.BC %d on port:%d\n",bidx, port);
+       cb_log(4, port, "Init.BC %d.\n",bidx);
        
        memset(bc, 0,sizeof(struct misdn_bchannel));
        
@@ -831,28 +912,21 @@ int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, in
        empty_bc(bc);
        bc->upset=0;
        bc->port=stack->port;
-       bc->nt=stack->mode==NT_MODE?1:0;
+       bc->nt=stack->nt?1:0;
        
        {
                ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
-               ibuffer_t* mbuf= init_ibuffer(MISDN_IBUF_SIZE);
 
                if (!ibuf) return -1;
-               if (!mbuf) return -1;
                
                clear_ibuffer( ibuf);
-               clear_ibuffer( mbuf);
                
                ibuf->rsem=malloc(sizeof(sem_t));
-               mbuf->rsem=malloc(sizeof(sem_t));
                
                bc->astbuf=ibuf;
-               bc->misdnbuf=mbuf;
 
                if (sem_init(ibuf->rsem,1,0)<0)
                        sem_init(ibuf->rsem,0,0);
-               if (sem_init(mbuf->rsem,1,0)< 0)
-                       sem_init(mbuf->rsem,0,0);
                
        }
        
@@ -863,7 +937,7 @@ int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, in
                stack_info_t *stinf;
                ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
                if (ret < 0) {
-                       cb_log(0, port, "%s: Cannot get stack info for port:%d (ret=%d)\n", __FUNCTION__, port, ret);
+                       cb_log(-1, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
                        return -1;
                }
     
@@ -877,98 +951,7 @@ int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, in
 
 
 
-struct misdn_stack * stack_nt_init(struct misdn_stack *stack, int midev, int port)
-{
-       int ret;
-       layer_info_t li;
-       interface_info_t ii;
-  
-  
-       cb_log(4, port, "Init. Stack on port:%d\n",port);
-       stack->mode = NT_MODE;
-  
-       stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, 1); 
-       if (stack->lower_id <= 0) {
-               cb_log(0, port, "%s: Cannot get layer(%d) id of port:%d\n", __FUNCTION__, 1, port);
-               return(NULL);
-       }
-  
-  
-       memset(&li, 0, sizeof(li));
-       {
-               int l = sizeof(li.name);
-               strncpy(li.name,"net l2", l);
-               li.name[l-1] = 0;
-       }
-       li.object_id = -1;
-       li.extentions = 0;
-       li.pid.protocol[2] = ISDN_PID_L2_LAPD_NET;
-       li.pid.layermask = ISDN_LAYER((2));
-       li.st = stack->d_stid;
-  
-  
-       stack->upper_id = mISDN_new_layer(midev, &li);
-       if (stack->upper_id <= 0) {
-               cb_log(0, port, "%s: Cannot add layer %d of port:%d\n", __FUNCTION__, 2, port);
-               return(NULL);
-       }
-
-       cb_log(4, port, "NT Stacks upper_id %x\n",stack->upper_id);
-   
-       memset(&ii, 0, sizeof(ii));
-       ii.extentions = EXT_IF_EXCLUSIV;
-       ii.owner = stack->upper_id;
-       ii.peer = stack->lower_id;
-       ii.stat = IF_DOWN;
-       ret = mISDN_connect(midev, &ii);
-       if (ret) {
-               cb_log(0, port, "%s: Cannot connect layer %d of port:%d exclusively.\n", __FUNCTION__, 2, port);
-               return(NULL);
-       }
-
-       /* create nst (nt-mode only) */
-       {
-               memset(&stack->nst, 0, sizeof(net_stack_t));
-               memset(&stack->mgr, 0, sizeof(manager_t));
-    
-               stack->mgr.nst = &stack->nst;
-               stack->nst.manager = &stack->mgr;
-    
-               stack->nst.l3_manager = handle_event_nt;
-               stack->nst.device = midev;
-               stack->nst.cardnr = port;
-               stack->nst.d_stid = stack->d_stid;
-    
-#ifdef MISDNUSER_JOLLY
-               stack->nst.feature = FEATURE_NET_HOLD;
-               if (stack->ptp)
-                       stack->nst.feature |= FEATURE_NET_PTP;
-               if (stack->pri)
-                       stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
-#endif
-    
-               stack->nst.l1_id = stack->lower_id;
-               stack->nst.l2_id = stack->upper_id;
-    
-               msg_queue_init(&stack->nst.down_queue);
-    
-               Isdnl2Init(&stack->nst);
-               Isdnl3Init(&stack->nst);
-       }
-
-       misdn_lib_get_l1_up(stack);
-
-       if (stack->ptp) {
-               misdn_lib_get_l2_up(stack);
-               stack->l2link=0;
-       }
-       
-       
-       return stack;
-}
-
-
-struct misdn_stack* stack_te_init( int midev, int port, int ptp )
+struct misdn_stack* stack_init( int midev, int port, int ptp )
 {
        int ret;
        unsigned char buff[1025];
@@ -976,18 +959,18 @@ struct misdn_stack* stack_te_init( int midev, int port, int ptp )
        stack_info_t *stinf;
        int i; 
        layer_info_t li;
-       interface_info_t ii;
+
        struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
        if (!stack ) return NULL;
 
 
        //cb_log(2, "Init. Stack on port:%d\n",port);
-       cb_log(4, port, "Init. Stack on port:%d\n",port);
+       cb_log(4, port, "Init. Stack.\n");
   
        memset(stack,0,sizeof(struct misdn_stack));
   
        for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
-  
+       
        stack->port=port;
        stack->midev=midev;
        stack->ptp=ptp;
@@ -1000,7 +983,7 @@ struct misdn_stack* stack_te_init( int midev, int port, int ptp )
        /* query port's requirements */
        ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
        if (ret < 0) {
-               cb_log(0, port, "%s: Cannot get stack info for port:%d (ret=%d)\n", __FUNCTION__, port, ret);
+               cb_log(-1, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
                return(NULL);
        }
   
@@ -1015,12 +998,12 @@ struct misdn_stack* stack_te_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->mode = TE_MODE;
+               stack->nt=0;
                break;
        case ISDN_PID_L0_NT_S0:
                cb_log(4, port, "NT Stack\n");
 
-               return stack_nt_init(stack,midev,port); 
+               stack->nt=1;
                break;
 
        case ISDN_PID_L0_TE_U:
@@ -1033,77 +1016,142 @@ struct misdn_stack* stack_te_init( int midev, int port, int ptp )
                break;
        case ISDN_PID_L0_TE_E1:
                cb_log(4, port, "TE S2M Stack\n");
-               stack->mode = TE_MODE;
+               stack->nt=0;
                stack->pri=1;
                break;
        case ISDN_PID_L0_NT_E1:
                cb_log(4, port, "TE S2M Stack\n");
-               stack->mode = NT_MODE;
+               stack->nt=1;
                stack->pri=1;
-    
-               return stack_nt_init(stack,midev,port); 
+               
                break;
        default:
-               cb_log(0, port, "unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]);
+               cb_log(-1, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
 
        }
-  
-       if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { /* || (nt&&ptp) || pri */
-               stack->ptp = 1;
-       } else {
-               stack->ptp = 0;
-       }
 
-       stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, 3); 
-       if (stack->lower_id <= 0) {
-               cb_log(0, stack->port, "No lower Id port:%d\n", stack->port);
-               return(NULL);
+       if (!stack->nt) {
+               if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { 
+                       stack->ptp = 1;
+               } else {
+                       stack->ptp = 0;
+               }
        }
-  
-       memset(&li, 0, sizeof(li));
+       
+       
+
        {
-               int l = sizeof(li.name);
-               strncpy(li.name, "user L4", l);
-               li.name[l-1] = 0;
-       }
-       li.object_id = -1;
-       li.extentions = 0;
-  
-       li.pid.protocol[4] = ISDN_PID_L4_CAPI20;
-  
-       li.pid.layermask = ISDN_LAYER((4));
-       li.st = stack->d_stid;
-       stack->upper_id = mISDN_new_layer(midev, &li);
-  
-       if (stack->upper_id <= 0)       {
-               cb_log(0, stack->port, "No Upper ID port:%d\n",stack->port);
-               return(NULL);
-       } 
-  
-       memset(&ii, 0, sizeof(ii));
-       ii.extentions = EXT_IF_EXCLUSIV | EXT_IF_CREATE;
-       ii.owner = stack->upper_id;
-       ii.peer = stack->lower_id;
-       ii.stat = IF_DOWN;
-       ret = mISDN_connect(midev, &ii);
-       if (ret) {
-               cb_log(0, stack->port, "No Connect port:%d\n", stack->port);
-               return NULL;
+               int ret;
+               int nt=stack->nt;
+
+               cb_log(4, port, "Init. Stack.\n");
+               
+               
+               
+               memset(&li, 0, sizeof(li));
+               {
+                       int l = sizeof(li.name);
+                       strncpy(li.name,nt?"net l2":"user l4", l);
+                       li.name[l-1] = 0;
+               }
+               li.object_id = -1;
+               li.extentions = 0;
+               li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
+               li.pid.layermask = ISDN_LAYER((nt?2:4));
+               li.st = stack->d_stid;
+               
+               
+               ret = mISDN_new_layer(midev, &li);
+               if (ret) {
+                       cb_log(-1, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
+                       return(NULL);
+               }
+               
+               
+               stack->upper_id = li.id;
+               ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
+               if (ret)
+               {
+                       cb_log(-1,port,"Cannot register layer %d of this port.\n", nt?2:4);
+                       return(NULL);
+               }
+               
+               
+               stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3); 
+               if (stack->lower_id < 0) {
+                       cb_log(-1, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
+                       return(NULL);
+               }
+               
+               stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
+               if (stack->upper_id < 0) {
+                       cb_log(-1, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
+                       return(NULL);
+               }
+               
+               cb_log(4, port, "NT Stacks upper_id %x\n",stack->upper_id);
+               
+               
+               /* create nst (nt-mode only) */
+               if (nt) {
+                       
+                       memset(&stack->nst, 0, sizeof(net_stack_t));
+                       memset(&stack->mgr, 0, sizeof(manager_t));
+    
+                       stack->mgr.nst = &stack->nst;
+                       stack->nst.manager = &stack->mgr;
+    
+                       stack->nst.l3_manager = handle_event_nt;
+                       stack->nst.device = midev;
+                       stack->nst.cardnr = port;
+                       stack->nst.d_stid = stack->d_stid;
+    
+                       stack->nst.feature = FEATURE_NET_HOLD;
+                       if (stack->ptp)
+                               stack->nst.feature |= FEATURE_NET_PTP;
+                       if (stack->pri)
+                               stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
+                       
+                       stack->nst.l1_id = stack->lower_id;
+                       stack->nst.l2_id = stack->upper_id;
+                       
+                       msg_queue_init(&stack->nst.down_queue);
+                       
+                       Isdnl2Init(&stack->nst);
+                       Isdnl3Init(&stack->nst);
+                       
+                       //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);
+               
        }
-  
-       misdn_lib_get_l1_up(stack);
-       misdn_lib_get_l2_status(stack);
+
+       cb_log(1,0,"stack_init: port:%d lowerId:%x  upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
        
-       /*  initially, we assume that the link is NOT up */
-       stack->l2link = 0;
-       stack->l1link = 0;
-  
-       stack->next=NULL;
-  
        return stack;
-  
 }
 
+
 void stack_te_destroy(struct misdn_stack* stack)
 {
        char buf[1024];
@@ -1120,11 +1168,14 @@ void stack_te_destroy(struct misdn_stack* stack)
 struct misdn_stack * find_stack_by_addr(int  addr)
 {
        struct misdn_stack *stack;
-  
+       
        for (stack=glob_mgr->stack_list;
             stack;
             stack=stack->next) {
-               if ( stack->upper_id == addr) return stack;
+               //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;
+
        }
   
        return NULL;
@@ -1186,34 +1237,37 @@ struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
 
 struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
 {
-       int port = addr & IF_CONTRMASK;
        struct misdn_stack* stack;
        int i;
 
-
+       
        for (stack=glob_mgr->stack_list;
             stack;
             stack=stack->next) {
-    
-               if (stack->port == port) {
-                       for (i=0; i< stack->b_num; i++) {
-                               if (stack->bc[i].addr==addr) {
-                                       return &stack->bc[i];
-                               }
+               
+               for (i=0; i< stack->b_num; i++) {
+
+                       if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) ||  stack->bc[i].layer_id== addr ) {
+                               return &stack->bc[i];
                        }
                }
+               
        }
-  
+
+       
        return NULL;
 }
 
 
 
 
+
 int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
 {
        struct misdn_stack *stack=get_stack_by_bc(bc);
-       if (stack->mode == TE_MODE) {
+       
+       if (!stack->nt) {
+               
                switch (event) {
 
                case EVENT_CONNECT_ACKNOWLEDGE:
@@ -1222,7 +1276,7 @@ int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
                case EVENT_CONNECT:
                        
                        if ( *bc->crypt_key ) {
-                               cb_log(4, stack->port, "ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s\n", stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+                               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) );
                        }
@@ -1236,19 +1290,22 @@ int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
                case EVENT_SETUP_ACKNOWLEDGE:
                        
                {
-                       struct misdn_stack *stack=find_stack_by_port(frm->addr&IF_CONTRMASK);
-                       if (!stack) return -1;
-                       
                        if (bc->channel == 0xff) {
                                bc->channel=find_free_chan_in_stack(stack, 0);
                                if (!bc->channel) {
-                                       cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
+                                       cb_log(-1, stack->port, "Any Channel Requested, but we have no more!!\n");
+
                                        break;
                                }
                        } 
                        
-                       if (stack->mode == TE_MODE) {
-                               setup_bc(bc);
+                       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);
+                               }
                        }
                }
                
@@ -1264,10 +1321,11 @@ int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
 int handle_new_process(struct misdn_stack *stack, iframe_t *frm)
 {
   
-       struct misdn_bchannel* bc=misdn_lib_get_free_bc(frm->addr&IF_CONTRMASK, 0);
+       struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0);
+       
        
        if (!bc) {
-               cb_log(0, 0, " --> !! lib: No free channel!\n");
+               cb_log(-1, 0, " --> !! lib: No free channel!\n");
                return -1;
        }
   
@@ -1279,15 +1337,14 @@ int handle_new_process(struct misdn_stack *stack, iframe_t *frm)
        return 0;
 }
 
-int handle_cr ( iframe_t *frm)
+int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
 {
-       struct misdn_stack *stack=find_stack_by_port(frm->addr&IF_CONTRMASK);
-
+       
        if (!stack) return -1;
   
        switch (frm->prim) {
        case CC_NEW_CR|INDICATION:
-               cb_log(4, stack->port, " --> lib: NEW_CR Ind with l3id:%x port:%d\n",frm->dinfo, stack->port);
+               cb_log(4, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
                handle_new_process(stack, frm); 
                return 1;
        case CC_NEW_CR|CONFIRM:
@@ -1305,7 +1362,7 @@ int handle_cr ( iframe_t *frm)
                        struct misdn_bchannel dummybc;
       
                        if (!bc) {
-                               cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", frm->dinfo, stack->port);
+                               cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
                                memset (&dummybc,0,sizeof(dummybc));
                                dummybc.port=stack->port;
                                dummybc.l3_id=frm->dinfo;
@@ -1328,7 +1385,7 @@ int handle_cr ( iframe_t *frm)
                                }
                        }
                        else {
-                               if (stack->mode == NT_MODE) 
+                               if (stack->nt) 
                                        cb_log(4, stack->port, "BC with dinfo: %x  not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
                        }
       
@@ -1385,8 +1442,16 @@ int misdn_lib_get_port_up (int port)
 
 int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data)
 {
-       bc->facility=fac;
-       strcpy(bc->facility_calldeflect_nr,(char*)data);
+       switch (fac) {
+       case FACILITY_CALLDEFLECT:
+               strcpy(bc->out_fac.calldeflect_nr,(char*)data);
+               break;
+       default:
+               cb_log(1,bc?bc->port:0,"We don't handle this facility yet: %d\n",fac);
+               return 0;
+       }
+       
+       bc->out_fac_type=fac;
        
        misdn_lib_send_event(bc,EVENT_FACILITY);
        return 0;
@@ -1402,7 +1467,7 @@ int misdn_lib_port_up(int port)
             stack=stack->next) {
                
                if (stack->port == port) {
-                       if (stack->mode == NT_MODE) {
+                       if (stack->nt) {
                                if (stack->l1link)
                                        return 1;
                                else
@@ -1426,26 +1491,21 @@ handle_event_nt(void *dat, void *arg)
 {
        manager_t *mgr = (manager_t *)dat;
        msg_t *msg = (msg_t *)arg;
-#ifdef MISDNUSER_JOLLY
        mISDNuser_head_t *hh;
-#else
-       mISDN_head_t *hh;
-#endif
+
        struct misdn_stack *stack=find_stack_by_mgr(mgr);
        int port;
 
        if (!msg || !mgr)
                return(-EINVAL);
 
-#ifdef MISDNUSER_JOLLY
        hh=(mISDNuser_head_t*)msg->data;
-#else
-       hh=(mISDN_head_t*)msg->data;
-#endif
-  
-       port=hh->dinfo & IF_CONTRMASK;
-  
-       cb_log(4, stack->port, " --> lib: prim %x dinfo %x port: %d\n",hh->prim, hh->dinfo ,stack->port);
+
+
+       port=stack->port;
+       
+       
+       cb_log(4, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
   
        {
                switch(hh->prim){
@@ -1454,10 +1514,12 @@ handle_event_nt(void *dat, void *arg)
                {
                        iframe_t frm; /* fake te frm to add callref to global callreflist */
                        frm.dinfo = hh->dinfo;
-                       frm.addr=stack->upper_id;
+
+                       frm.addr=stack->upper_id | FLG_MSG_DOWN;
+
                        frm.prim = CC_NEW_CR|INDICATION;
                        
-                       if (handle_cr(&frm)< 0) {
+                       if (handle_cr( stack, &frm)< 0) {
                                msg_t *dmsg;
                                cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
                                dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
@@ -1488,7 +1550,7 @@ handle_event_nt(void *dat, void *arg)
                        cb_log(4, bc?stack->port:0, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID  is %x\n",l3id );
        
                        if (!bc) { cb_log(4, 0, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
-       
+                       cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
                        bc->l3_id=l3id;
                        cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
                }
@@ -1502,7 +1564,7 @@ handle_event_nt(void *dat, void *arg)
                        frm.addr=stack->upper_id;
                        frm.prim = CC_NEW_CR|INDICATION;
                        
-                       if (handle_cr(&frm)< 0) {
+                       if (handle_cr(stack, &frm)< 0) {
                                msg_t *dmsg;
                                cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
                                dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
@@ -1523,20 +1585,37 @@ handle_event_nt(void *dat, void *arg)
                        
                        if (!bc) {
                                msg_t *dmsg;
-                               cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x port:%d\n",hh->dinfo, stack->port);
+                               cb_log(-1, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
                                
-                               cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
+                               cb_log(-1, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
                                dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
                                stack->nst.manager_l3(&stack->nst, dmsg);
                                free_msg(msg);
                                return 0;
                                
                        }
-                       
-                       setup_bc(bc);
+                       int ret=setup_bc(bc);
+                       if (ret == -EINVAL){
+                               cb_log(-1,bc->port,"handle_event_nt: setup_bc failed\n");
+                               misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
+                       }
+               }
+               break;
+               case CC_DISCONNECT|INDICATION:
+               {
+                       struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
+                       if (!bc) {
+                               bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
+                               if (bc) { //repair reject bug
+                                       int myprocid=bc->l3_id&0x0000ffff;
+                                       hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
+                                       cb_log(4,stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
+                               }
+                       }
                }
                break;
-               case CC_DISCONNECT|INDICATION:
+               
+               case CC_FACILITY|INDICATION:
                {
                        struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
                        if (!bc) {
@@ -1544,11 +1623,12 @@ handle_event_nt(void *dat, void *arg)
                                if (bc) { //repair reject bug
                                        int myprocid=bc->l3_id&0x0000ffff;
                                        hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
-                                       cb_log(4,stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
+                                       cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
                                }
                        }
                }
                break;
+               
 
                case CC_RELEASE_COMPLETE|INDICATION:
                        break;
@@ -1583,13 +1663,16 @@ handle_event_nt(void *dat, void *arg)
                        struct misdn_bchannel dummybc;
                        iframe_t frm; /* fake te frm to remove callref from global callreflist */
                        frm.dinfo = hh->dinfo;
-                       frm.addr=stack->upper_id;
+
+                       frm.addr=stack->upper_id | FLG_MSG_DOWN;
+
+
                        frm.prim = CC_RELEASE_CR|INDICATION;
                        cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
                        /** removing procid **/
 
                        if (!bc) {
-                               cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", hh->dinfo, stack->port);
+                               cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
                                memset (&dummybc,0,sizeof(dummybc));
                                dummybc.port=stack->port;
                                dummybc.l3_id=hh->dinfo;
@@ -1599,12 +1682,12 @@ handle_event_nt(void *dat, void *arg)
                        if (bc) {
 #ifdef MISDNUSER_JOLLY
                                if ( (bc->l3_id & 0xff00) == 0xff00) {
-                                       cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", bc->l3_id&0xff, stack->port);
+                                       cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
                                        stack->procids[bc->l3_id&0xff] = 0 ;
                                }
 #else
                                if ( (bc->l3_id & 0xfff0) == 0xfff0) {
-                                       cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", bc->l3_id&0xf, stack->port);
+                                       cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xf);
                                        stack->procids[bc->l3_id&0xf] = 0 ;
            
                                }
@@ -1612,9 +1695,9 @@ handle_event_nt(void *dat, void *arg)
 #endif
 
                        }
-                       else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is bad Port:%d\n", stack->port );
+                       else cb_log(-1, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
        
-                       handle_cr(&frm);
+                       handle_cr(stack, &frm);
                        free_msg(msg);
                        return 0 ;
                }
@@ -1627,15 +1710,15 @@ handle_event_nt(void *dat, void *arg)
                {
                        struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
                        int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
-                       if (!bc) { cb_log(0, 0, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
+                       if (!bc) { cb_log(-1, 0, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
 #ifdef MISDNUSER_JOLLY
                        if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
-                               cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", 0xff&bc->l3_id, stack->port);
+                               cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
                                stack->procids[bc->l3_id&0xff] = 0 ;
                        }
 #else
                        if (((l3id&0xfff0)!=0xfff0) && ((bc->l3_id&0xfff0)==0xfff0)) {
-                               cb_log(4, stack->port, "Removing Process Id:%x on port:%d\n", 0xf&bc->l3_id, stack->port);
+                               cb_log(4, stack->port, "Removing Process Id:%x on this port.\n", 0xf&bc->l3_id);
                                stack->procids[bc->l3_id&0xf] = 0 ;
                        }
 
@@ -1653,9 +1736,9 @@ handle_event_nt(void *dat, void *arg)
                case DL_ESTABLISH | INDICATION:
                case DL_ESTABLISH | CONFIRM:
                {
-                       cb_log(4, stack->port, "%% GOT L2 Activate Info port:%d\n",stack->port);
+                       cb_log(4, stack->port, "%% GOT L2 Activate Info.\n");
                        stack->l2link = 1;
-       
+                       
                        free_msg(msg);
                        return 0;
                }
@@ -1664,7 +1747,7 @@ handle_event_nt(void *dat, void *arg)
                case DL_RELEASE | INDICATION:
                case DL_RELEASE | CONFIRM:
                {
-                       cb_log(4, stack->port, "%% GOT L2 DeActivate Info port:%d\n",stack->port);
+                       cb_log(4, stack->port, "%% GOT L2 DeActivate Info.\n");
                        stack->l2link = 0;
                        
                        /** Clean the L3 here **/
@@ -1691,7 +1774,7 @@ handle_event_nt(void *dat, void *arg)
     
                if (!bc) {
       
-                       cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", hh->dinfo, stack->port);
+                       cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
                        memset (&dummybc,0,sizeof(dummybc));
                        dummybc.port=stack->port;
                        dummybc.l3_id=hh->dinfo;
@@ -1742,7 +1825,7 @@ int handle_timers(msg_t* msg)
                     stack = stack->next) {
                        itimer_t *it;
       
-                       if (stack->mode != NT_MODE) continue;
+                       if (!stack->nt) continue;
       
                        it = stack->nst.tlist;
                        /* find timer */
@@ -1763,7 +1846,7 @@ int handle_timers(msg_t* msg)
                        }
                }
     
-               cb_log(0, 0, "Timer Msg without Timer ??\n");
+               cb_log(-1, 0, "Timer Msg without Timer ??\n");
                free_msg(msg);
                return 1;
        }
@@ -1785,12 +1868,15 @@ static int do_tone(struct misdn_bchannel *bc, int len)
 
        frm->prim = DL_DATA|REQUEST;
        frm->dinfo = 0;
-       frm->addr = bc->addr | IF_DOWN;
+
+       frm->addr = bc->addr | FLG_MSG_DOWN ;
+
+       
   
        bc->tone_cnt+=len;
 
        if (bc->tone_cnt < TONE_425_SIZE) return 1;
-
+       
        switch(bc->tone) {
        case TONE_DIAL:
        {
@@ -1856,16 +1942,47 @@ 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) return 0 ;
-
+       if (!bc) {
+               cb_log(0,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
+               return 0 ;
+       }
+       
        struct misdn_stack *stack=get_stack_by_bc(bc);
-
-       if (!stack) return 0;
+       
+       if (!stack) {
+               cb_log(0,0,"handle_bchan: STACK not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
+               return 0;
+       }
        
        switch (frm->prim) {
+       case MGR_SETSTACK| INDICATION:
+               cb_log(2, stack->port, "BCHAN: MGR_SETSTACK|IND \n");
+               
+               
+               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);
+               }
+               cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
+               
+               bc->upset=2;
+               
+               free_msg(msg);
+               
+               manager_bchannel_activate(bc);
+               return 1;
+       case MGR_DELLAYER| CONFIRM:
+               cb_log(2, stack->port, "BCHAN: MGR_DELLAYER|CNF \n");
+               
+               free_msg(msg);
+               return 1;
+               
        case PH_ACTIVATE | INDICATION:
        case DL_ESTABLISH | INDICATION:
                cb_log(4, stack->port, "BCHAN: ACT Ind\n");
@@ -1895,7 +2012,7 @@ int handle_bchan(msg_t *msg)
        {
                unsigned long cont = *((unsigned long *)&frm->data.p);
                
-               cb_log(4, stack->port, "PH_CONTROL: port:%d channel:%d oad%d:%s dad%d:%s \n", stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+               cb_log(4, stack->port, "PH_CONTROL: channel:%d oad%d:%s dad%d:%s \n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
 
                if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
                        int dtmf = cont & DTMF_TONE_MASK;
@@ -1918,7 +2035,15 @@ int handle_bchan(msg_t *msg)
                }
        }
        break;
-    
+
+       case PH_DATA|REQUEST:
+       case DL_DATA|REQUEST:
+               cb_log(0, stack->port, "DL_DATA REQUEST \n");
+               do_tone(bc, 64);
+               
+               free_msg(msg);
+               return 1;
+               
        case PH_DATA|INDICATION:
        case DL_DATA|INDICATION:
        {
@@ -1931,7 +2056,8 @@ int handle_bchan(msg_t *msg)
                
                
 #if MISDN_DEBUG
-               cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
+               cb_log(-1, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
+
 #endif
                
                if (bc->active && frm->len > 0) {
@@ -1943,25 +2069,27 @@ int handle_bchan(msg_t *msg)
                                                iframe_t *txfrm= (iframe_t*)buf;
                                                int len, r;
                                                
-                                               len = ibuf_usedcount(bc->misdnbuf);
-                                               
-                                               if (len < frm->len) {
-                                                       /** send nothing 
-                                                        * till we are synced
-                                                        **/
-                                               } else {
+                                               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; /*  | IF_DOWN; */
-                                                       txfrm->len = frm->len;
-                                                       ibuf_memcpy_r(&buf[mISDN_HEADER_LEN], bc->misdnbuf,frm->len);
+                                                       
+                                                       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
                                                }
-                                               
                                        }
                                }
                                
@@ -1976,19 +2104,19 @@ int handle_bchan(msg_t *msg)
        case PH_DATA | CONFIRM:
        case DL_DATA|CONFIRM:
 #if MISDN_DEBUG
-               cb_log(0, stack->port, "Data confirmed\n");
+
+               cb_log(-1, stack->port, "Data confirmed\n");
+
 #endif
                free_msg(msg);
                return 1;
                break;
        case DL_DATA|RESPONSE:
 #if MISDN_DEBUG
-               cb_log(0, stack->port, "Data response\n");
+               cb_log(-1, stack->port, "Data response\n");
+
 #endif
                break;
-    
-       case DL_DATA | REQUEST:
-               break;
        }
   
        return 0;
@@ -2001,30 +2129,31 @@ int handle_frm_nt(msg_t *msg)
        iframe_t *frm= (iframe_t*)msg->data;
        struct misdn_stack *stack;
        int err=0;
+
+       stack=find_stack_by_addr( frm->addr );
+
+       
   
-       stack=find_stack_by_addr((frm->addr & IF_ADDRMASK ) );
-  
-       if (!stack || stack->mode != NT_MODE) {
+       if (!stack || !stack->nt) {
                return 0;
        }
   
-
        if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
     
                if (nt_err_cnt > 0 ) {
                        if (nt_err_cnt < 100) {
                                nt_err_cnt++; 
-                               cb_log(0, stack->port, "NT Stack sends us error: %d port:%d\n", err,stack->port);
+                               cb_log(-1, stack->port, "NT Stack sends us error: %d \n", err);
                        } else if (nt_err_cnt < 105){
-                               cb_log(0, stack->port, "NT Stack sends us error: %d port:%d over 100 times, so I'll stop this message\n", err,stack->port);
+                               cb_log(-1, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
                                nt_err_cnt = - 1; 
                        }
                }
                free_msg(msg);
                return 1;
-    
+               
        }
-  
+       
        return 1;
 }
 
@@ -2032,16 +2161,21 @@ int handle_frm_nt(msg_t *msg)
 int handle_frm(msg_t *msg)
 {
        iframe_t *frm = (iframe_t*) msg->data;
-       struct misdn_stack *stack=find_stack_by_addr(frm->addr & IF_ADDRMASK);
+       
+       struct misdn_stack *stack=find_stack_by_addr(frm->addr);
+
+       
+       cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim);
+       
   
-       if (!stack || stack->mode != TE_MODE) {
+       if (!stack || stack->nt) {
                return 0;
        }
 
        {
                struct misdn_bchannel *bc;
 
-               if(handle_cr(frm)) {
+               if(handle_cr(stack, frm)) {
                        free_msg(msg);
                        return 1;
                }
@@ -2053,48 +2187,60 @@ int handle_frm(msg_t *msg)
                        enum event_response_e response=RESPONSE_OK;
       
                        isdn_msg_parse_event(msgs_g,msg,bc, 0);
-
+                       
                        /** Preprocess some Events **/
                        handle_event(bc, event, frm);
                        /*  shoot up event to App: */
                        cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
       
                        if(!isdn_get_info(msgs_g,event,0)) 
-                               cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
+                               cb_log(-1, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
                        else 
                                response=cb_event(event, bc, glob_mgr->user_data);
 #if 1
                        if (event == EVENT_SETUP) {
                                switch (response) {
                                case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
-                                       cb_log(0, stack->port, "TOTALY IGNORING SETUP: port:%d\n", frm->addr&IF_CONTRMASK);
+
+                                       cb_log(-1, stack->port, "TOTALY IGNORING SETUP \n");                                    
+                                       
                                        break;
                                case RESPONSE_IGNORE_SETUP:
                                        /* I think we should send CC_RELEASE_CR, but am not sure*/
                                        empty_chan_in_stack(stack, bc->channel);
                                        empty_bc(bc);
-         
-                                       cb_log(0, stack->port, "GOT IGNORE SETUP: port:%d\n", frm->addr&IF_CONTRMASK);
+                                       cb_log(-1, stack->port, "GOT IGNORE SETUP\n");
+
+                                       
                                        break;
                                case RESPONSE_OK:
-                                       cb_log(4, stack->port, "GOT SETUP OK: port:%d\n", frm->addr&IF_CONTRMASK);
+                                       cb_log(4, stack->port, "GOT SETUP OK\n");
+
+                                       
                                        break;
                                default:
                                        break;
                                }
                        }
-      
-                       cb_log(5, stack->port, "Freeing Msg on prim:%x port:%d\n",frm->prim, frm->addr&IF_CONTRMASK);
+
+                       cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
+
+                       
                        free_msg(msg);
                        return 1;
 #endif
       
                } else {
-                       cb_log(0, stack->port, "NO BC FOR STACK: port:%d\n", frm->addr&IF_CONTRMASK);
+       
+                       cb_log(-1, stack->port, "NO BC FOR STACK\n");           
+                       
                }
 
        }
-       cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x port:%d\n",frm->prim, frm->addr&IF_CONTRMASK);
+
+       cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
+
+       
   
        return 0;
 }
@@ -2103,7 +2249,10 @@ int handle_frm(msg_t *msg)
 int handle_l1(msg_t *msg)
 {
        iframe_t *frm = (iframe_t*) msg->data;
-       struct misdn_stack *stack = find_stack_by_port(frm->addr & IF_CONTRMASK);
+
+       struct misdn_stack *stack = find_stack_by_addr(frm->addr);
+
+       
        int i ;
        
        if (!stack) return 0 ;
@@ -2111,10 +2260,10 @@ int handle_l1(msg_t *msg)
        switch (frm->prim) {
        case PH_ACTIVATE | CONFIRM:
        case PH_ACTIVATE | INDICATION:
-               cb_log (1, stack->port, "L1: PH L1Link Up! port:%d\n",stack->port);
+               cb_log (1, stack->port, "L1: PH L1Link Up!\n");
                stack->l1link=1;
                
-               if (stack->mode==NT_MODE) {
+               if (stack->nt) {
                        
                        if (stack->nst.l1_l2(&stack->nst, msg))
                                free_msg(msg);
@@ -2132,9 +2281,22 @@ int handle_l1(msg_t *msg)
                }
                
                return 1;
+
+       case PH_ACTIVATE | REQUEST:
+               free_msg(msg);
+               cb_log(1,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
+               
+               return 1;
+               
+       case PH_DEACTIVATE | REQUEST:
+               free_msg(msg);
+               cb_log(1,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
+               
+               return 1;
+               
        case PH_DEACTIVATE | CONFIRM:
        case PH_DEACTIVATE | INDICATION:
-               cb_log (1, stack->port, "L1: PH L1Link Down! port:%d\n",stack->port);
+               cb_log (1, stack->port, "L1: PH L1Link Down! \n");
                
                for (i=0; i<stack->b_num; i++) {
                        if (global_state == MISDN_INITIALIZED)  {
@@ -2143,7 +2305,7 @@ int handle_l1(msg_t *msg)
                        
                }
                
-               if (stack->mode==NT_MODE) {
+               if (stack->nt) {
                        if (stack->nst.l1_l2(&stack->nst, msg))
                                free_msg(msg);
                        
@@ -2163,39 +2325,28 @@ int handle_l1(msg_t *msg)
 int handle_l2(msg_t *msg)
 {
        iframe_t *frm = (iframe_t*) msg->data;
-       struct misdn_stack *stack = find_stack_by_addr(frm->addr & IF_ADDRMASK);
 
-       if (!stack) return 0 ;
-  
+       struct misdn_stack *stack = find_stack_by_addr(frm->addr);
+
+       
+       
+       if (!stack) {
+               return 0 ;
+       }
+       
        switch(frm->prim) {
 
-#ifdef DL_STATUS
-       case DL_STATUS | INDICATION:
-       case DL_STATUS | CONFIRM:
-               cb_log (3, stack->port, "L2: DL_STATUS! port:%d\n", stack->port);
+       case DL_ESTABLISH | REQUEST:
+               cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
+               return 1;
+       case DL_RELEASE | REQUEST:
+               cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
+               return 1;
                
-               switch (frm->dinfo) {
-               case SDL_ESTAB:
-                       cb_log (4, stack->port, " --> SDL_ESTAB port:%d\n", stack->port);
-                       stack->l1link=1;
-                       goto dl_estab;
-               case SDL_REL:
-                       cb_log (4, stack->port, " --> SDL_REL port:%d\n", stack->port);
-                       stack->l1link=0;
-                       misdn_lib_get_l2_up(stack);
-                       goto dl_rel;
-               }
-               break;
-#endif
-    
-    
        case DL_ESTABLISH | INDICATION:
        case DL_ESTABLISH | CONFIRM:
-#ifdef DL_STATUS
-       dl_estab:
-#endif
        {
-               cb_log (3, stack->port, "L2: L2Link Up! port:%d\n", stack->port);
+               cb_log (3, stack->port, "L2: L2Link Up! \n");
                stack->l2link=1;
                free_msg(msg);
                return 1;
@@ -2204,11 +2355,8 @@ int handle_l2(msg_t *msg)
     
        case DL_RELEASE | INDICATION:
        case DL_RELEASE | CONFIRM:
-#ifdef DL_STATUS
-       dl_rel:
-#endif
        {
-               cb_log (3, stack->port, "L2: L2Link Down! port:%d\n", stack->port);
+               cb_log (3, stack->port, "L2: L2Link Down! \n");
                stack->l2link=0;
                
                free_msg(msg);
@@ -2223,12 +2371,78 @@ int handle_l2(msg_t *msg)
 int handle_mgmt(msg_t *msg)
 {
        iframe_t *frm = (iframe_t*) msg->data;
-  
-       if ( (frm->prim & 0x0f0000) ==  0x0f0000) {
-               cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
+       
+
+
+       if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
+               cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
+               free(msg);
+               return 1;
+       }
+       
+       struct misdn_stack * stack=find_stack_by_addr(frm->addr);
+       
+       if (!stack) {
+               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);
+               
+               switch (frm->dinfo) {
+               case SSTATUS_L1_ACTIVATED:
+                       cb_log(1, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
+                       stack->l1link=1;
+                       break;
+               case SSTATUS_L1_DEACTIVATED:
+                       cb_log(1, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
+
+                       /*reopen L1 if down*/
+                       if (stack->l1link==2)
+                               stack->l1link--;
+                       else                    
+                               stack->l1link=0;
+                       
+                       break;
+               case SSTATUS_L2_ESTABLISHED:
+                       cb_log(1, 0, "MGMT: SSTATUS: L2_ESTABLISH \n");
+                       stack->l2link=1;
+                       
+                       break;
+                       
+               case SSTATUS_L2_RELEASED:
+                       cb_log(1, 0, "MGMT: SSTATUS: L2_RELEASED \n");
+                       stack->l2link=0;
+                       stack->l1link=2;
+                       
+                       break;
+                       
+               }
+               
+               
+               free_msg(msg);
+               return 1;
+               
+       case MGR_SETSTACK | INDICATION:
+               cb_log(2, 0, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
                free_msg(msg);
                return 1;
+       case MGR_DELLAYER | CONFIRM:
+               cb_log(2, 0, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
+               free_msg(msg);
+               return 1;
+               
        }
+       
+       /*
+       if ( (frm->prim & 0x0f0000) ==  0x0f0000) {
+       cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
+       free_msg(msg);
+       return 1;
+       } */
     
        return 0;
 }
@@ -2241,7 +2455,7 @@ msg_t *fetch_msg(int midev)
        fd_set rdfs;
 
        if (!msg) {
-               cb_log(0, 0, "fetch_msg: alloc msg failed !!");
+               cb_log(-1, 0, "fetch_msg: alloc msg failed !!");
                return NULL;
        }
        
@@ -2275,7 +2489,7 @@ static void misdn_lib_isdn_event_catcher(void *arg)
        struct misdn_lib *mgr = arg;
        int zero_frm=0 , fff_frm=0 ;
        int midev= mgr->midev;
-       int port;
+       int port=0;
        
        //cb_log(5, 0, "In event_catcher thread\n");
        
@@ -2287,7 +2501,9 @@ static void misdn_lib_isdn_event_catcher(void *arg)
                if (!msg) continue;
                
                frm = (iframe_t*) msg->data;
-               port = frm->addr&IF_CONTRMASK;
+               
+               //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 ) {
@@ -2296,7 +2512,7 @@ static void misdn_lib_isdn_event_catcher(void *arg)
                        continue;
                } else {
                        if (zero_frm) {
-                               cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
+                               cb_log(-1, port, "*** Alert: %d zero_frms caught\n", zero_frm);
                                zero_frm = 0 ;
                        }
                }
@@ -2308,7 +2524,7 @@ static void misdn_lib_isdn_event_catcher(void *arg)
                        continue;
                } else {
                        if (fff_frm) {
-                               cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
+                               cb_log(-1, port, "*** Alert: %d fff_frms caught\n", fff_frm);
                                fff_frm = 0 ;
                        }
                }
@@ -2433,7 +2649,8 @@ void misdn_lib_log_ies(struct misdn_bchannel *bc)
 
        if (!stack) return;
        
-       cb_log(2, stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", stack->mode==NT_MODE?"NT":"TE", bc->cause, bc->out_cause, bc->rad);
+
+       cb_log(2, stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", stack->nt?"NT":"TE", bc->cause, bc->out_cause, bc->rad);
        
        cb_log(2, stack->port,
               " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c\n",
@@ -2448,7 +2665,8 @@ void misdn_lib_log_ies(struct misdn_bchannel *bc)
        cb_log(3, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
        
        cb_log(3, stack->port, " --> pid:%d addr:%x l3id:%x\n", bc->pid, bc->addr, bc->l3_id);
-
+       cb_log(3, stack->port, " --> b_stid:%x layer_id:%x\n", bc->b_stid, bc->layer_id);
+       
        cb_log(4, stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
 }
 
@@ -2458,62 +2676,66 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
        int err = -1 ;
   
        if (!bc) goto ERR; 
+
        
        struct misdn_stack *stack=get_stack_by_bc(bc);
-       
-       if ( stack->mode == NT_MODE && !stack->l1link) {
+
+
+       cb_log(0,0,"SENDEVENT: stack->nt:%d stack->uperid:%x\n",stack->nt, stack->upper_id);
+
+       if ( stack->nt && !stack->l1link) {
                /** Queue Event **/
                bc->evq=event;
                cb_log(1, stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
-               { /* Pull Up L1 */ 
-                       iframe_t act;
-                       act.prim = PH_ACTIVATE | REQUEST; 
-                       act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
-                       act.dinfo = 0;
-                       act.len = 0;
-                       mISDN_write(glob_mgr->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
-               }
-               
+               misdn_lib_get_l1_up(stack);
                return 0;
        }
        
-       cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s port:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, stack->port);
+       cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s \n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad);
        misdn_lib_log_ies(bc);
        
        switch (event) {
        case EVENT_SETUP:
                if (create_process(glob_mgr->midev, bc)<0) {
-                       cb_log(0,  stack->port, " No free channel at the moment @ send_event\n");
+                       cb_log(-1,  stack->port, " No free channel at the moment @ send_event\n");
+
                        err=-ENOCHAN;
                        goto ERR;
                }
                break;
 
+               
+
        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->mode == NT_MODE) {
+               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(0, stack->port, " No free channel at the moment\n");
+                                       cb_log(-1, stack->port, " No free channel at the moment\n");
+
                                        err=-ENOCHAN;
                                        goto ERR;
                                }
                        }
                        /* Its that i generate channels */
                }
-               
-               setup_bc(bc);
+
+               int ret=setup_bc(bc);
+               if (ret == -EINVAL){
+                       cb_log(-1,bc->port,"send_event: setup_bc failed\n");
+                       
+               }
                
                
                if ( event == EVENT_CONNECT ) {
                        if ( *bc->crypt_key ) {
-                               cb_log(4, stack->port,  " --> ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s \n", stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+                               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) );
                        }
@@ -2550,7 +2772,7 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
                holded_bc->holded=1;
                stack_holder_add(stack,holded_bc);
                
-               if (stack->mode == NT_MODE) {
+               if (stack->nt) {
                        empty_chan_in_stack(stack,bc->channel);
                        empty_bc(bc);
                        clean_up_bc(bc);
@@ -2594,8 +2816,11 @@ 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->mode==NT_MODE?1:0);
+       
+       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;
@@ -2613,30 +2838,36 @@ int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
                printf("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
        }
 
+       if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
+               cb_log(9,0,"BCHANNEL_BIT\n");
+               if (handle_bchan(msg)) 
+                       return 0 ;
+       } else {
+               cb_log(9,0,"NO BCHANNEL_BIT !\n");
+       }
+       
        if (handle_timers(msg)) 
                return 0 ;
-    
+       
        if (handle_mgmt(msg)) 
-               return 0 ;
-
+               return 0 ; 
+       
        if (handle_l2(msg)) 
                return 0 ;
 
        /* Its important to handle l1 AFTER l2  */
        if (handle_l1(msg)) 
                return 0 ;
-
-       if (handle_bchan(msg)) 
-               return 0 ;
-
+       
+       
        /** Handle L2/3 Signalling after bchans **/ 
        if (handle_frm_nt(msg)) 
                return 0 ;
-    
+       
        if (handle_frm(msg)) 
                return 0 ;
-
-       cb_log(0, frm->addr&IF_CONTRMASK, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on port: %d\n",frm->prim, frm->len, frm->addr, frm->dinfo, frm->addr&IF_CONTRMASK);
+       
+       cb_log(-1, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);           
    
        free_msg(msg);
 
@@ -2652,17 +2883,19 @@ int misdn_lib_get_port_info(int port)
        iframe_t *frm;
        struct misdn_stack *stack=find_stack_by_port(port);
        if (!msg) {
-               cb_log(0, port, "misgn_lib_get_port: alloc_msg failed!\n");
+               cb_log(-1, port, "misgn_lib_get_port: alloc_msg failed!\n");
                return -1;
        }
        frm=(iframe_t*)msg->data;
        if (!stack ) {
-               cb_log(0, port, "There is no Stack on Port:%d\n",port);
+               cb_log(-1, port, "There is no Stack for this port.\n");
                return -1;
        }
        /* activate bchannel */
        frm->prim = CC_STATUS_ENQUIRY | REQUEST;
-       frm->addr = stack->upper_id;
+
+       frm->addr = stack->upper_id| FLG_MSG_DOWN;
+
        frm->dinfo = 0;
        frm->len = 0;
   
@@ -2677,7 +2910,7 @@ int misdn_lib_port_restart(int port)
 {
        struct misdn_stack *stack=find_stack_by_port(port);
  
-       cb_log(0, port, "Restarting Port:%d\n",port);
+       cb_log(0, port, "Restarting this port.\n");
        if (stack) {
                cb_log(0, port, "Stack:%p\n",stack);
 
@@ -2689,7 +2922,7 @@ int misdn_lib_port_restart(int port)
                        iframe_t *frm;
 
                        if (!msg) {
-                               cb_log(0, port, "port_restart: alloc_msg fialed\n");
+                               cb_log(-1, port, "port_restart: alloc_msg failed\n");
                                return -1;
                        }
                        
@@ -2698,8 +2931,10 @@ int misdn_lib_port_restart(int port)
                        /* activate bchannel */
        
                        frm->prim = DL_RELEASE | REQUEST;
+
+                       frm->addr = stack->upper_id | FLG_MSG_DOWN;
+
        
-                       frm->addr = stack->upper_id ;
                        frm->dinfo = 0;
                        frm->len = 0;
                        msg_queue_tail(&glob_mgr->activatequeue, msg);
@@ -2711,7 +2946,7 @@ int misdn_lib_port_restart(int port)
       
                {
                        struct misdn_stack *tmpstack;
-                       struct misdn_stack *newstack=stack_te_init(stack->midev ,port, stack->ptp);
+                       struct misdn_stack *newstack=stack_init(stack->midev ,port, stack->ptp);
       
       
                        if (stack == glob_mgr->stack_list) {
@@ -2725,7 +2960,7 @@ int misdn_lib_port_restart(int port)
                                        if (tmpstack->next == stack) break;
 
                                if (!tmpstack->next) {
-                                       cb_log(0, port, "Stack to restart not found\n");
+                                       cb_log(-1, port, "Stack to restart not found\n");
                                        return 0;
                                }  else {
                                        struct misdn_stack *n=tmpstack->next->next;
@@ -2739,7 +2974,7 @@ int misdn_lib_port_restart(int port)
                                for(i=0;i<newstack->b_num; i++) {
                                        int r;
                                        if ((r=init_bc(newstack, &newstack->bc[i], newstack->midev,port,i, "", 1))<0) {
-                                               cb_log(0, port, "Got Err @ init_bc :%d\n",r);
+                                               cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
                                                return 0;
                                        }
                                }
@@ -2787,11 +3022,10 @@ void manager_event_handler(void *arg)
                     stack;
                     stack=stack->next ) { 
                        while ( (msg=msg_dequeue(&stack->downqueue)) ) {
-       
-                               if (stack->mode == NT_MODE ){
-         
+                               
+                               if (stack->nt ){
                                        if (stack->nst.manager_l3(&stack->nst, msg))
-                                               cb_log(0, stack->port, "Error@ Sending Message in NT-Stack.\n");
+                                               cb_log(-1, stack->port, "Error@ Sending Message in NT-Stack.\n");
          
                                } else {
                                        if (msg) {
@@ -2832,6 +3066,7 @@ 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;
@@ -2876,10 +3111,10 @@ int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_dat
                        ptp=1;
 
                if (port > port_count) {
-                       cb_log(0, port, "Couldn't Initialize Port:%d since we have only %d ports\n",port, port_count);
+                       cb_log(-1, port, "Couldn't Initialize this port since we have only %d ports\n", port_count);
                        exit(1);
                }
-               stack=stack_te_init(midev, port, ptp);
+               stack=stack_init(midev, port, ptp);
     
     
     
@@ -2896,7 +3131,7 @@ int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_dat
                                for(i=0;i<stack->b_num; i++) {
                                        int r;
                                        if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "", 1))<0) {
-                                               cb_log(0, port, "Got Err @ init_bc :%d\n",r);
+                                               cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
                                                exit(1);
                                        }
                                }
@@ -2918,7 +3153,7 @@ int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_dat
                                for(i=0;i<stack->b_num; i++) {
                                        int r;
                                        if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "",1 ))<0) {
-                                               cb_log(0, port, "Got Err @ init_bc :%d\n",r);
+                                               cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
                                                exit(1);
                                        } 
                                }
@@ -2944,8 +3179,6 @@ int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_dat
        return (mgr == NULL);
 }
 
-
-
 void misdn_lib_destroy()
 {
        struct misdn_stack *help;
@@ -2954,13 +3187,10 @@ void misdn_lib_destroy()
        for ( help=glob_mgr->stack_list; help; help=help->next ) {
                for(i=0;i<help->b_num; i++) {
                        char buf[1024];
-      
                        mISDN_write_frame(help->midev, buf, help->bc[i].addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
                        help->bc[i].addr = 0;
                }
-
-
-               cb_log (1, help->port, "Destroying port:%d\n", help->port);
+               cb_log (1, help->port, "Destroying this port.\n");
                stack_te_destroy(help);
        }
        
@@ -2971,7 +3201,6 @@ void misdn_lib_destroy()
                        pthread_join(glob_mgr->event_handler_thread, NULL);
                }
          
-         
                cb_log(4, 0, "Killing Main Thread\n");
                if ( pthread_cancel(glob_mgr->event_thread) == 0 ) {
                        cb_log(4, 0, "Joining Main Thread\n");
@@ -2983,15 +3212,11 @@ void misdn_lib_destroy()
        te_lib_destroy(glob_mgr->midev);
 }
 
-
 char *manager_isdn_get_info(enum event_e event)
 {
        return isdn_get_info(msgs_g , event, 0);
 }
 
-
-
-
 void manager_bchannel_activate(struct misdn_bchannel *bc)
 {
        msg_t *msg=alloc_msg(MAX_MSG_SIZE);
@@ -3000,23 +3225,28 @@ void manager_bchannel_activate(struct misdn_bchannel *bc)
        struct misdn_stack *stack=get_stack_by_bc(bc);
        
        if (!msg) {
-               cb_log(0, stack->port, "bchannel_activate: alloc_msg failed !");
+               cb_log(-1, stack->port, "bchannel_activate: alloc_msg failed !");
+
                return ;
        }
+
+       if (bc->upset != 2 ) {
+               cb_log(1, stack->port, "bchannel_activate: BC Not properly upsetted addr:%x\n", bc->addr);
+               return;
+       }
+       
        
        frm=(iframe_t*)msg->data;
        /* we must activate if we are deactivated */
-       clear_ibuffer(bc->misdnbuf);
        clear_ibuffer(bc->astbuf);
        
-       
        if (bc->active) return;
   
        cb_log(5, stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
 
        /* activate bchannel */
        frm->prim = DL_ESTABLISH | REQUEST;
-       frm->addr = bc->addr ;
+       frm->addr = bc->addr | FLG_MSG_DOWN ;
        frm->dinfo = 0;
        frm->len = 0;
        
@@ -3042,12 +3272,11 @@ void manager_bchannel_deactivate(struct misdn_bchannel * bc)
        bc->tone=TONE_NONE;
 
        dact.prim = DL_RELEASE | REQUEST;
-       dact.addr = bc->addr;
+       dact.addr = bc->addr | FLG_MSG_DOWN;
        dact.dinfo = 0;
        dact.len = 0;
   
        mISDN_write(stack->midev, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
-       clear_ibuffer(bc->misdnbuf);
        clear_ibuffer(bc->astbuf);
        bc->active=0;
   
@@ -3055,40 +3284,28 @@ void manager_bchannel_deactivate(struct misdn_bchannel * bc)
 }
 
 
-int manager_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
+int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
 {
-
        struct misdn_stack *stack=get_stack_by_bc(bc);
        if (!bc->active) return -1;   
-
-       if ( misdn_cap_is_speech(bc->capability) )
-               flip_buf_bits(data,len);
        
-       if ( !bc->nojitter && misdn_cap_is_speech(bc->capability) ) {
-               if (len > ibuf_freecount(bc->misdnbuf)) {
-                       len=ibuf_freecount(bc->misdnbuf);
-               }
-               ibuf_memcpy_w(bc->misdnbuf, (unsigned char*)data, len);
-       } else {
-               char buf[4096 + mISDN_HEADER_LEN];
-               iframe_t *frm= (iframe_t*)buf;
-               int  r;
-               
-               frm->prim = DL_DATA|REQUEST;
-               frm->dinfo = 0;
-               frm->addr = bc->addr | IF_DOWN;
-               frm->len = len;
-               memcpy(&buf[mISDN_HEADER_LEN], data,len);
-
-               
-               
-               if ( misdn_cap_is_speech(bc->capability))
-                       cb_log(4, stack->port, "Writing %d bytes\n",len);
-               
-               cb_log(9, stack->port, "Wrinting %d bytes 2 mISDN\n",len);
-               r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
-       }
-
+       char buf[4096 + mISDN_HEADER_LEN];
+       iframe_t *frm= (iframe_t*)buf;
+       int  r;
+       
+       frm->prim = DL_DATA|REQUEST;
+       frm->dinfo = 0;
+       frm->addr = bc->addr | FLG_MSG_DOWN ;
+       
+       frm->len = len;
+       memcpy(&buf[mISDN_HEADER_LEN], data,len);
+       
+       if ( ! misdn_cap_is_speech(bc->capability))
+               cb_log(6, stack->port, "Writing %d data bytes\n",len);
+       
+       cb_log(9, stack->port, "Writing %d bytes 2 mISDN\n",len);
+       r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
+       
        return 0;
 }
 
@@ -3116,7 +3333,7 @@ void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2)
        struct misdn_stack *stack=get_stack_by_bc(bc);
        
        ctrl->prim = PH_CONTROL | REQUEST;
-       ctrl->addr = bc->addr;
+       ctrl->addr = bc->addr | FLG_MSG_DOWN;
        ctrl->dinfo = 0;
        ctrl->len = sizeof(unsigned long)*2;
        *d++ = c1;
@@ -3135,7 +3352,7 @@ void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c
        struct misdn_stack *stack=get_stack_by_bc(bc);
        
        ctrl->prim = PH_CONTROL | REQUEST;
-       ctrl->addr = bc->addr;
+       ctrl->addr = bc->addr | FLG_MSG_DOWN;
        ctrl->dinfo = 0;
        ctrl->len = sizeof(unsigned long) + c2_len;
        *d++ = c1;
@@ -3240,7 +3457,7 @@ void manager_ec_enable(struct misdn_bchannel *bc)
 
        struct misdn_stack *stack=get_stack_by_bc(bc);
        
-       cb_log(1, stack?stack->port:0,"Sending Control ECHOCAN_ON enblock\n");
+       cb_log(1, stack?stack->port:0,"Sending Control ECHOCAN_ON taps:%d training:%d\n",bc->ec_deftaps, bc->ec_training);
        
        switch (bc->ec_deftaps) {
        case 4:
@@ -3255,7 +3472,7 @@ void manager_ec_enable(struct misdn_bchannel *bc)
                cb_log(4, stack->port, "Taps is %d\n",bc->ec_deftaps);
                break;
        default:
-               cb_log(0, stack->port, "Taps should be power of 2\n");
+               cb_log(-1, stack->port, "Taps should be power of 2\n");
                bc->ec_deftaps=128;
        }
 
@@ -3266,6 +3483,7 @@ void manager_ec_enable(struct misdn_bchannel *bc)
 }
 
 
+
 void manager_ec_disable(struct misdn_bchannel *bc)
 {
        struct misdn_stack *stack=get_stack_by_bc(bc);
index 92ebdf4..a27e7c5 100644 (file)
 #ifndef TE_LIB
 #define TE_LIB
 
+
 /** For initialization usage **/
 /* typedef int ie_nothing_t ;*/
 /** end of init usage **/
 
 
+
 #define MAX_BCHANS 30
 
 enum bc_state_e {
@@ -144,9 +146,19 @@ enum layer_e {
        UNKNOWN
 }; 
 
+
+
+/** FACILITY STUFF **/
+
 enum facility_type {
        FACILITY_NONE,
-       FACILITY_CALLDEFLECT
+       FACILITY_CALLDEFLECT=0x91,
+       FACILITY_CENTREX=0x88
+};
+
+union facility {
+       char calldeflect_nr[15];
+       char cnip[256];
 };
 
 
@@ -161,7 +173,8 @@ struct misdn_bchannel {
        /* int b_addr; */
        int layer_id;
 
-
+       int layer;
+       
        
        /** var stuff**/
        int l3_id;
@@ -180,10 +193,11 @@ struct misdn_bchannel {
        
        
        void *astbuf;
+
        void *misdnbuf;
-       
 
        int te_choose_channel;
+       int early_bconnect;
        
        /* dtmf digit */
        int dtmf;
@@ -203,8 +217,11 @@ struct misdn_bchannel {
        int progress_location;
        int progress_indicator;
        
-       enum facility_type facility;
-       char facility_calldeflect_nr[15];
+       enum facility_type fac_type;
+       union facility fac;
+       
+       enum facility_type out_fac_type;
+       union facility out_fac;
        
        enum event_e evq;
        
@@ -255,14 +272,14 @@ struct misdn_bchannel {
        unsigned char dad[32];
        unsigned char orig_dad[32];
        unsigned char keypad[32];
-  
+
        unsigned char info_dad[64];
        unsigned char infos_pending[64];
-       unsigned char info_keypad[32];
-       unsigned char clisub[24];
-       unsigned char cldsub[24];
-       unsigned char fac[132];
-       unsigned char uu[256];
+
+/*     unsigned char info_keypad[32]; */
+/*     unsigned char clisub[24]; */
+/*     unsigned char cldsub[24]; */
+/*     unsigned char uu[256]; */
   
        int cause;
        int out_cause;
@@ -288,11 +305,13 @@ 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 {
        
        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);
 };
 
@@ -312,7 +331,9 @@ struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel);
 
 void manager_bchannel_activate(struct misdn_bchannel *bc);
 void manager_bchannel_deactivate(struct misdn_bchannel * bc);
-int manager_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len);
+
+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);
@@ -338,6 +359,7 @@ int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, v
 void manager_ec_enable(struct misdn_bchannel *bc);
 void manager_ec_disable(struct misdn_bchannel *bc);
 
+
 void get_show_stack_details(int port, char *buf);
 
 
@@ -347,6 +369,10 @@ int misdn_ibuf_freecount(void *buf);
 void misdn_ibuf_memcpy_r(char *to, void *from, int len);
 void misdn_ibuf_memcpy_w(void *buf, char *from, int len);
 
+void misdn_free_ibuffer(void *ibuf);
+void misdn_clear_ibuffer(void *ibuf);
+void *misdn_init_ibuffer(int len);
+
 /** Ibuf interface End **/
 
 void misdn_lib_setup_bc(struct misdn_bchannel *bc);
@@ -354,6 +380,7 @@ void misdn_lib_setup_bc(struct misdn_bchannel *bc);
 void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2);
 void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2);
 
+unsigned char * flip_buf_bits ( unsigned char * buf , int len);
 
 int misdn_lib_is_ptp(int port);
 
index 471648a..618d6d9 100644 (file)
@@ -1,18 +1,17 @@
 #ifndef ISDN_LIB_INTERN
-#define ISDN_LIB_INTER
+#define ISDN_LIB_INTERN
 
 
-#include <mISDNlib.h>
-#include <isdn_net.h>
-#include <l3dss1.h>
-#include <net_l3.h>
+#include <mISDNuser/mISDNlib.h>
+#include <mISDNuser/isdn_net.h>
+#include <mISDNuser/l3dss1.h>
+#include <mISDNuser/net_l3.h>
 
 #include <pthread.h>
 
 #include "isdn_lib.h"
 
-
-
+#define QI_ELEMENT(a) a.off
 
 
 #ifndef mISDNUSER_HEAD_SIZE
@@ -37,8 +36,6 @@ struct isdn_msg {
   
        void (*msg_parser)(struct isdn_msg *msgs, msg_t *msg, struct misdn_bchannel *bc, int nt);
        msg_t *(*msg_builder)(struct isdn_msg *msgs, struct misdn_bchannel *bc, int nt);
-       void (*msg_printer)(struct isdn_msg *msgs);
-  
        char *info;
   
 } ; 
@@ -60,6 +57,8 @@ struct misdn_stack {
        int b_stids[MAX_BCHANS + 1];
   
        int ptp;
+
+       int l2_id;
        int lower_id;
        int upper_id;
   
@@ -70,7 +69,7 @@ struct misdn_stack {
        int l1link;
        int midev;
   
-       enum mode_e {NT_MODE, TE_MODE} mode;
+       int nt;
        
        int pri;
   
index 751d36f..5cf921a 100644 (file)
  */
 
 
+#include "isdn_lib_intern.h"
+
+
 #include "isdn_lib.h"
+
 #include "ie.c"
 
+#include "fac.h"
 
 void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -30,12 +35,14 @@ void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel
                        channel=-1;
     
                /*  ALERT: is that everytime true ?  */
-               if (channel > 0 && stack->mode == NT_MODE) 
+
+               if (channel > 0 && stack->nt) 
                        bc->channel = channel;
        }
        
        dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)proceeding, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
        
+       
 #if DEBUG 
        printf("Parsing PROCEEDING Msg\n"); 
 #endif
@@ -59,9 +66,6 @@ msg_t *build_proceeding (struct isdn_msg msgs[], struct misdn_bchannel *bc, int
 #endif
        return msg; 
 }
-void print_proceeding (struct isdn_msg msgs[]) 
-{
-}
 
 void parse_alerting (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -94,9 +98,6 @@ msg_t *build_alerting (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt
 #endif
        return msg; 
 }
-void print_alerting (struct isdn_msg msgs[]) 
-{
-}
 
 
 void parse_progress (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
@@ -125,10 +126,6 @@ msg_t *build_progress (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt
 #endif
        return msg; 
 }
-void print_progress (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_setup (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 { 
@@ -319,10 +316,6 @@ msg_t *build_setup (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
        return msg; 
 }
 
-void print_setup (struct isdn_msg msgs[]) 
-{
-}
-
 void parse_connect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
        int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
@@ -342,7 +335,9 @@ msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
        int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
        CONNECT_t *connect;
        msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_t) ,nt); 
-  
+       
+       cb_log(0,0,"BUILD_CONNECT: bc:%p bc->l3id:%d, nt:%d\n",bc,bc->l3_id,nt);
+
        connect=(CONNECT_t*)((msg->data+HEADER_LEN)); 
 
        if (nt) {
@@ -361,10 +356,6 @@ msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
 #endif
        return msg; 
 }
-void print_connect (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -408,10 +399,6 @@ msg_t *build_setup_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *b
        return msg; 
 }
 
-void print_setup_acknowledge (struct isdn_msg msgs[]) 
-{
-}
-
 void parse_connect_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
 #if DEBUG 
@@ -435,10 +422,6 @@ msg_t *build_connect_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel
 #endif
        return msg; 
 }
-void print_connect_acknowledge (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_user_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -461,10 +444,6 @@ msg_t *build_user_information (struct isdn_msg msgs[], struct misdn_bchannel *bc
 #endif
        return msg; 
 }
-void print_user_information (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -487,10 +466,6 @@ msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc,
 #endif
        return msg; 
 }
-void print_suspend_reject (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -513,10 +488,6 @@ msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, i
 #endif
        return msg; 
 }
-void print_resume_reject (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -539,10 +510,6 @@ msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
 #endif
        return msg; 
 }
-void print_hold (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -565,10 +532,6 @@ msg_t *build_suspend (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
 #endif
        return msg; 
 }
-void print_suspend (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -591,10 +554,6 @@ msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
 #endif
        return msg; 
 }
-void print_resume (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -617,10 +576,6 @@ msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc
 #endif
        return msg; 
 }
-void print_hold_acknowledge (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -643,10 +598,6 @@ msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel
 #endif
        return msg; 
 }
-void print_suspend_acknowledge (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -669,10 +620,6 @@ msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *
 #endif
        return msg; 
 }
-void print_resume_acknowledge (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -695,10 +642,6 @@ msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int
 #endif
        return msg; 
 }
-void print_hold_reject (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -721,10 +664,6 @@ msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt
 #endif
        return msg; 
 }
-void print_retrieve (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -748,10 +687,6 @@ msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel
 #endif
        return msg; 
 }
-void print_retrieve_acknowledge (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {
@@ -774,10 +709,6 @@ msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc,
 #endif
        return msg; 
 }
-void print_retrieve_reject (struct isdn_msg msgs[]) 
-{
-}
-
 
 void parse_disconnect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
 {