Merge the cli_cleanup branch.
[asterisk/asterisk.git] / channels / chan_misdn.c
index 2e9eb6a..3d3da0c 100644 (file)
@@ -90,14 +90,14 @@ struct misdn_jb{
 
 
 
-/*! \brief allocates the jb-structure and initialise the elements*/
+/*! \brief allocates the jb-structure and initialize the elements */
 struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
 
 /*! \brief frees the data and destroys the given jitterbuffer struct */
 void misdn_jb_destroy(struct misdn_jb *jb);
 
 /*! \brief fills the jitterbuffer with len data returns < 0 if there was an
-error (bufferoverun). */
+error (buffer overrun). */
 int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
 
 /*! \brief gets len bytes out of the jitterbuffer if available, else only the
@@ -116,7 +116,7 @@ ast_mutex_t release_lock;
 enum misdn_chan_state {
        MISDN_NOTHING=0,        /*!< at beginning */
        MISDN_WAITING4DIGS, /*!<  when waiting for infos */
-       MISDN_EXTCANTMATCH, /*!<  when asterisk couldnt match our ext */
+       MISDN_EXTCANTMATCH, /*!<  when asterisk couldn't match our ext */
        MISDN_INCOMING_SETUP, /*!<  for incoming setups*/
        MISDN_DIALING, /*!<  when pbx_start */
        MISDN_PROGRESS, /*!<  we got a progress */
@@ -131,9 +131,8 @@ enum misdn_chan_state {
        MISDN_RELEASED, /*!<  when connected */
        MISDN_BRIDGED, /*!<  when bridged */
        MISDN_CLEANING, /*!< when hangup from * but we were connected before */
-       MISDN_HUNGUP_FROM_MISDN, /*!< when DISCONNECT/RELEASE/REL_COMP  cam from misdn */
-       MISDN_HUNGUP_FROM_AST, /*!< when DISCONNECT/RELEASE/REL_COMP came out of */
-       /* misdn_hangup */
+       MISDN_HUNGUP_FROM_MISDN, /*!< when DISCONNECT/RELEASE/REL_COMP  came from misdn */
+       MISDN_HUNGUP_FROM_AST, /*!< when DISCONNECT/RELEASE/REL_COMP came out of misdn_hangup */
        MISDN_HOLDED, /*!< if this chan is holded */
        MISDN_HOLD_DISCONNECT, /*!< if this chan is holded */
   
@@ -285,7 +284,8 @@ static pthread_t misdn_tasks_thread;
 
 static int *misdn_ports;
 
-static void chan_misdn_log(int level, int port, char *tmpl, ...);
+static void chan_misdn_log(int level, int port, char *tmpl, ...)
+       __attribute__ ((format (printf, 3, 4)));
 
 static struct ast_channel *misdn_new(struct chan_list *cl, int state,  char *exten, char *callerid, int format, int port, int c);
 static void send_digit_to_chan(struct chan_list *cl, char digit );
@@ -362,11 +362,6 @@ static int update_ec_config(struct misdn_bchannel *bc);
 
 
 
-
-/*protos*/ 
-
-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)
@@ -394,49 +389,35 @@ static struct chan_list * get_chan_by_ast_name(char *name)
 
 
 struct allowed_bearers {
-       int cap;
-       int val;
-       char *name;
+       char *name;                     /*!< Bearer capability name string used in /etc/misdn.conf allowed_bearers */
+       char *display;          /*!< Bearer capability displayable name */
+       int cap;                        /*!< SETUP message bearer capability field code value */
+       int deprecated;         /*!< TRUE if this entry is deprecated. (Misspelled or bad name to use) */
 };
 
-struct allowed_bearers allowed_bearers_array[]={
-       {INFO_CAPABILITY_SPEECH,1,"speech"},
-       {INFO_CAPABILITY_AUDIO_3_1K,2,"3_1khz"},
-       {INFO_CAPABILITY_DIGITAL_UNRESTRICTED,4,"digital_unrestricted"},
-       {INFO_CAPABILITY_DIGITAL_RESTRICTED,8,"digital_restriced"},
-       {INFO_CAPABILITY_VIDEO,16,"video"}
+/* *INDENT-OFF* */
+static const struct allowed_bearers allowed_bearers_array[]= {
+       /* Name,                      Displayable Name       Bearer Capability,                    Deprecated */
+       { "speech",                  "Speech",               INFO_CAPABILITY_SPEECH,               0 },
+       { "3_1khz",                  "3.1KHz Audio",         INFO_CAPABILITY_AUDIO_3_1K,           0 },
+       { "digital_unrestricted",    "Unrestricted Digital", INFO_CAPABILITY_DIGITAL_UNRESTRICTED, 0 },
+       { "digital_restricted",      "Restricted Digital",   INFO_CAPABILITY_DIGITAL_RESTRICTED,   0 },
+       { "digital_restriced",       "Restricted Digital",   INFO_CAPABILITY_DIGITAL_RESTRICTED,   1 }, /* Allow misspelling for backwards compatibility */
+       { "video",                   "Video",                INFO_CAPABILITY_VIDEO,                0 }
 };
+/* *INDENT-ON* */
 
-static char *bearer2str(int cap) {
-       static char *bearers[]={
-               "Speech",
-               "Audio 3.1k",
-               "Unres Digital",
-               "Res Digital",
-               "Video",
-               "Unknown Bearer"
-       };
-       
-       switch (cap) {
-       case INFO_CAPABILITY_SPEECH:
-               return bearers[0];
-               break;
-       case INFO_CAPABILITY_AUDIO_3_1K:
-               return bearers[1];
-               break;
-       case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
-               return bearers[2];
-               break;
-       case INFO_CAPABILITY_DIGITAL_RESTRICTED:
-               return bearers[3];
-               break;
-       case INFO_CAPABILITY_VIDEO:
-               return bearers[4];
-               break;
-       default:
-               return bearers[5];
-               break;
-       }
+static const char *bearer2str(int cap)
+{
+       unsigned index;
+
+       for (index = 0; index < ARRAY_LEN(allowed_bearers_array); ++index) {
+               if (allowed_bearers_array[index].cap == cap) {
+                       return allowed_bearers_array[index].display;
+               }
+       }       /* end for */
+
+       return "Unknown Bearer";
 }
 
 
@@ -464,11 +445,11 @@ static void print_facility(struct FacParm *fac, struct misdn_bchannel *bc)
                        else if (fac->u.AOCDcur.freeOfCharge)
                                chan_misdn_log(1,bc->port," --> AOCD currency: free of charge\n");
                        else if (fac->u.AOCDchu.billingId >= 0)
-                               chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%d billingId:%d\n",
+                               chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s billingId:%d\n",
                                                fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
                                                (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId);
                        else
-                               chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%d\n",
+                               chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s\n",
                                                fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
                                                (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total");
                        break;
@@ -652,7 +633,7 @@ static int misdn_tasks_add_variable (int timeout, ast_sched_cb callback, const v
 
 static void misdn_tasks_remove (int task_id)
 {
-       ast_sched_del(misdn_tasks, task_id);
+       AST_SCHED_DEL(misdn_tasks, task_id);
 }
 
 static int misdn_l1_task (const void *data)
@@ -685,9 +666,16 @@ static int misdn_overlap_dial_task (const void *data)
        diff = ast_tvdiff_ms(tv_end, tv_now);
 
        if (diff <= 100) {
+               char *dad=ch->bc->dad, sexten[]="s";
                /* if we are 100ms near the timeout, we are satisfied.. */
                stop_indicate(ch);
-               if (ast_exists_extension(ch->ast, ch->context, ch->bc->dad, 1, ch->bc->oad)) {
+               
+               if (ast_strlen_zero(ch->bc->dad)) {
+                       dad=sexten;
+                       strcpy(ch->ast->exten, sexten);
+               }
+
+               if (ast_exists_extension(ch->ast, ch->context, dad, 1, ch->bc->oad)) {
                        ch->state=MISDN_DIALING;
                        if (pbx_start_chan(ch) < 0) {
                                chan_misdn_log(-1, ch->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
@@ -696,7 +684,7 @@ static int misdn_overlap_dial_task (const void *data)
                } else {
 misdn_overlap_dial_task_disconnect:
                        hanguptone_indicate(ch);
-                       ch->bc->out_cause=1;
+                       ch->bc->out_cause = AST_CAUSE_UNALLOCATED;
                        ch->state=MISDN_CLEANING;
                        misdn_lib_send_event(ch->bc, EVENT_DISCONNECT);
                }
@@ -748,9 +736,9 @@ static char *handle_cli_misdn_set_debug(struct ast_cli_entry *e, int cmd, struct
 
        switch (cmd) {
        case CLI_INIT:
-               e->command = "misdn set debug";
+               e->command = "misdn set debug {on|off|<level>}";
                e->usage =
-                       "Usage: misdn set debug <level> [only] | [port <port> [only]]\n"
+                       "Usage: misdn set debug {on|off|<level>} [only] | [port <port> [only]]\n"
                        "       Set the debug level of the mISDN channel.\n";
                return NULL;
        case CLI_GENERATE:
@@ -759,8 +747,14 @@ static char *handle_cli_misdn_set_debug(struct ast_cli_entry *e, int cmd, struct
 
        if (a->argc < 4 || a->argc > 7)
                return CLI_SHOWUSAGE;
-
-       level = atoi(a->argv[3]);
+       
+       if (!strcasecmp(a->argv[3], "on")) {
+               level = 1;
+       } else if (!strcasecmp(a->argv[3], "off")) {
+               level = 0;
+       } else {
+               level = atoi(a->argv[3]);
+       }
 
        switch (a->argc) {
        case 4: 
@@ -794,7 +788,7 @@ static char *handle_cli_misdn_set_debug(struct ast_cli_entry *e, int cmd, struct
                                        ast_cli(a->fd, "port number not valid! no ports available so you won't get lucky with any number here...\n");
                                        break;
                                case 1:
-                                       ast_cli(a->fd, "port number not valid! only port 1 is availble.\n");
+                                       ast_cli(a->fd, "port number not valid! only port 1 is available.\n");
                                        break;
                                default:
                                        ast_cli(a->fd, "port number not valid! only ports 1 to %d are available.\n", max_ports);
@@ -950,7 +944,7 @@ static char *handle_cli_misdn_port_down(struct ast_cli_entry *e, int cmd, struct
                e->command = "misdn port down";
                e->usage =
                        "Usage: misdn port down <port>\n"
-                       "       Try to deacivate the L1 on the given port.\n";
+                       "       Try to deactivate the L1 on the given port.\n";
                return NULL;
        case CLI_GENERATE:
                return NULL;
@@ -1084,7 +1078,7 @@ struct state_struct {
 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 */
+       {MISDN_EXTCANTMATCH,"EXTCANTMATCH"}, /*  when asterisk couldn't match our ext */
        {MISDN_INCOMING_SETUP,"INCOMING SETUP"}, /*  when pbx_start */
        {MISDN_DIALING,"DIALING"}, /*  when pbx_start */
        {MISDN_PROGRESS,"PROGRESS"}, /*  when pbx_start */
@@ -1099,11 +1093,10 @@ static struct state_struct state_array[] = {
        {MISDN_RELEASED,"RELEASED"}, /*  when connected */
        {MISDN_BRIDGED,"BRIDGED"}, /*  when bridged */
        {MISDN_CLEANING,"CLEANING"}, /* when hangup from * but we were connected before */
-       {MISDN_HUNGUP_FROM_MISDN,"HUNGUP_FROM_MISDN"}, /* when DISCONNECT/RELEASE/REL_COMP  cam from misdn */
-       {MISDN_HOLDED,"HOLDED"}, /* when DISCONNECT/RELEASE/REL_COMP  cam from misdn */
-       {MISDN_HOLD_DISCONNECT,"HOLD_DISCONNECT"}, /* when DISCONNECT/RELEASE/REL_COMP  cam from misdn */
-       {MISDN_HUNGUP_FROM_AST,"HUNGUP_FROM_AST"} /* when DISCONNECT/RELEASE/REL_COMP came out of */
-       /* misdn_hangup */
+       {MISDN_HUNGUP_FROM_MISDN,"HUNGUP_FROM_MISDN"}, /* when DISCONNECT/RELEASE/REL_COMP  came from misdn */
+       {MISDN_HOLDED,"HOLDED"}, /* when DISCONNECT/RELEASE/REL_COMP  came from misdn */
+       {MISDN_HOLD_DISCONNECT,"HOLD_DISCONNECT"}, /* when DISCONNECT/RELEASE/REL_COMP  came from misdn */
+       {MISDN_HUNGUP_FROM_AST,"HUNGUP_FROM_AST"} /* when DISCONNECT/RELEASE/REL_COMP came out of misdn_hangup */
 };
 
 static const char *misdn_get_ch_state(struct chan_list *p) 
@@ -1244,10 +1237,19 @@ static char *handle_cli_misdn_show_channels(struct ast_cli_entry *e, int cmd, st
        help = cl_te;
   
        ast_cli(a->fd, "Channel List: %p\n", cl_te); 
-  
+
        for (; help; help = help->next) {
                struct misdn_bchannel *bc = help->bc;   
                struct ast_channel *ast = help->ast;
+               if (!ast) {
+                       if (!bc) {
+                               ast_cli(a->fd, "chan_list obj. with l3id:%x has no bc and no ast Leg\n", help->l3id);
+                               continue;
+                       }
+                       ast_cli(a->fd, "bc with pid:%d has no Ast Leg\n", bc->pid);
+                       continue;
+               }
+
                if (misdn_debug[0] > 2)
                        ast_cli(a->fd, "Bc:%p Ast:%p\n", bc, ast);
                if (bc) {
@@ -1737,7 +1739,7 @@ static char *complete_show_config(struct ast_cli_args *a)
 
 static struct ast_cli_entry chan_misdn_clis[] = {
        AST_CLI_DEFINE(handle_cli_misdn_port_block,        "Block the given port"),
-       AST_CLI_DEFINE(handle_cli_misdn_port_down,         "Try to deacivate the L1 on the given port"),
+       AST_CLI_DEFINE(handle_cli_misdn_port_down,         "Try to deactivate the L1 on the given port"),
        AST_CLI_DEFINE(handle_cli_misdn_port_unblock,      "Unblock the given port"),
        AST_CLI_DEFINE(handle_cli_misdn_port_up,           "Try to establish L1 on the given port"),
        AST_CLI_DEFINE(handle_cli_misdn_reload,            "Reload internal mISDN config, read from the config file"),
@@ -1799,46 +1801,42 @@ static int update_config(struct chan_list *ch, int orig)
        misdn_cfg_get(port, MISDN_CFG_SCREEN, &screen, sizeof(screen));
        chan_misdn_log(2, port, " --> pres: %d screen: %d\n", pres, screen);
                
-       if ( (pres + screen) < 0 ) {
-
+       if (pres < 0 || screen < 0) {
                chan_misdn_log(2, port, " --> pres: %x\n", ast->cid.cid_pres);
                        
                switch (ast->cid.cid_pres & 0x60) {
-                               
                case AST_PRES_RESTRICTED:
                        bc->pres = 1;
-                       chan_misdn_log(2, port, " --> PRES: Restricted (0x1)\n");
+                       chan_misdn_log(2, port, " --> PRES: Restricted (1)\n");
                        break;
                case AST_PRES_UNAVAILABLE:
                        bc->pres = 2;
-                       chan_misdn_log(2, port, " --> PRES: Unavailable (0x2)\n");
+                       chan_misdn_log(2, port, " --> PRES: Unavailable (2)\n");
                        break;
                default:
                        bc->pres = 0;
-                       chan_misdn_log(2, port, " --> PRES: Allowed (0x0)\n");
+                       chan_misdn_log(2, port, " --> PRES: Allowed (0)\n");
+                       break;
                }
-                       
-               switch (ast->cid.cid_pres & 0x3) {
 
+               switch (ast->cid.cid_pres & 0x3) {
+               default:
                case AST_PRES_USER_NUMBER_UNSCREENED:
                        bc->screen = 0;
-                       chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
+                       chan_misdn_log(2, port, " --> SCREEN: Unscreened (0)\n");
                        break;
                case AST_PRES_USER_NUMBER_PASSED_SCREEN:
                        bc->screen = 1;
-                       chan_misdn_log(2, port, " --> SCREEN: Passed Screen (0x1)\n");
+                       chan_misdn_log(2, port, " --> SCREEN: Passed Screen (1)\n");
                        break;
                case AST_PRES_USER_NUMBER_FAILED_SCREEN:
                        bc->screen = 2;
-                       chan_misdn_log(2, port, " --> SCREEN: Failed Screen (0x2)\n");
+                       chan_misdn_log(2, port, " --> SCREEN: Failed Screen (2)\n");
                        break;
                case AST_PRES_NETWORK_NUMBER:
                        bc->screen = 3;
-                       chan_misdn_log(2, port, " --> SCREEN: Network Nr. (0x3)\n");
+                       chan_misdn_log(2, port, " --> SCREEN: Network Nr. (3)\n");
                        break;
-               default:
-                       bc->screen = 0;
-                       chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
                }
        } else {
                bc->screen = screen;
@@ -2137,9 +2135,9 @@ static int read_config(struct chan_list *ch, int orig)
                        ch->dsp = ast_dsp_new();
                if (ch->dsp) {
                        if (ch->faxdetect) 
-                               ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT | DSP_FEATURE_FAX_DETECT);
+                               ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_FAX_DETECT);
                        else 
-                               ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT );
+                               ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT );
                }
                if (!ch->trans)
                        ch->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW);
@@ -2303,13 +2301,13 @@ static int misdn_answer(struct ast_channel *ast)
        
        if (!p) {
                ast_log(LOG_WARNING, " --> Channel not connected ??\n");
-               ast_queue_hangup(ast);
+               ast_queue_hangup_with_cause(ast, AST_CAUSE_NETWORK_OUT_OF_ORDER);
        }
 
        if (!p->bc) {
                chan_misdn_log(1, 0, " --> Got Answer, but theres no bc obj ??\n");
 
-               ast_queue_hangup(ast);
+               ast_queue_hangup_with_cause(ast, AST_CAUSE_PROTOCOL_ERROR);
        }
 
        tmp = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY");
@@ -2361,19 +2359,19 @@ static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int dur
        chan_misdn_log(1, bc ? bc->port : 0, "* IND : Digit %c\n", digit);
        
        if (!bc) {
-               ast_log(LOG_WARNING, " --> !! Got Digit Event withut having bchannel Object\n");
+               ast_log(LOG_WARNING, " --> !! Got Digit Event without having bchannel Object\n");
                return -1;
        }
        
        switch (p->state ) {
        case MISDN_CALLING:
                if (strlen(bc->infos_pending) < sizeof(bc->infos_pending) - 1)
-                       strncat(bc->infos_pending, buf, sizeof(bc->infos_pending) - 1);
+                       strncat(bc->infos_pending, buf, sizeof(bc->infos_pending) - strlen(bc->infos_pending) - 1);
                break;
        case MISDN_CALLING_ACKNOWLEDGE:
                ast_copy_string(bc->info_dad, buf, sizeof(bc->info_dad));
                if (strlen(bc->dad) < sizeof(bc->dad) - 1)
-                       strncat(bc->dad, buf, sizeof(bc->dad) - 1);
+                       strncat(bc->dad, buf, sizeof(bc->dad) - strlen(bc->dad) - 1);
                ast_copy_string(p->ast->exten, bc->dad, sizeof(p->ast->exten));
                misdn_lib_send_event( bc, EVENT_INFORMATION);
                break;
@@ -2444,7 +2442,7 @@ static int misdn_indication(struct ast_channel *ast, int cond, const void *data,
                chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n", p->bc ? p->bc->pid : -1);
                switch (p->state) {
                case MISDN_ALERTING:
-                       chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoreing it\n", p->bc ? p->bc->pid : -1);
+                       chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n", p->bc ? p->bc->pid : -1);
                        break;
                case MISDN_CONNECTED:
                        chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n", p->bc ? p->bc->pid : -1);
@@ -2516,9 +2514,11 @@ static int misdn_indication(struct ast_channel *ast, int cond, const void *data,
                        start_bc_tones(p);
                break;
        case AST_CONTROL_HOLD:
+               ast_moh_start(ast,data,ast->musicclass); 
                chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n", p->bc ? p->bc->pid : -1);
                break;
        case AST_CONTROL_UNHOLD:
+               ast_moh_stop(ast);
                chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n", p->bc ? p->bc->pid : -1);
                break;
        default:
@@ -2648,11 +2648,11 @@ static int misdn_hangup(struct ast_channel *ast)
                break;
        case MISDN_CONNECTED:
        case MISDN_PRECONNECTED:
-               /*  Alerting or Disconect */
+               /*  Alerting or Disconnect */
                if (p->bc->nt) {
                        start_bc_tones(p);
                        hanguptone_indicate(p);
-                       p->bc->progress_indicator = 8;
+                       p->bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
                }
                if (bc->need_disconnect)
                        misdn_lib_send_event( bc, EVENT_DISCONNECT);
@@ -2833,7 +2833,7 @@ static struct ast_frame *misdn_read(struct ast_channel *ast)
        tmp->frame.offset = 0;
        tmp->frame.delivery = ast_tv(0,0);
        tmp->frame.src = NULL;
-       tmp->frame.data = tmp->ast_rd_buf;
+       tmp->frame.data.ptr = tmp->ast_rd_buf;
 
        if (tmp->faxdetect && !tmp->faxhandled) {
                if (tmp->faxdetect_timeout) {
@@ -2884,7 +2884,7 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
        }
        
        if (ch->notxtone) {
-               chan_misdn_log(7, ch->bc->port, "misdn_write: Returning because notxone\n");
+               chan_misdn_log(7, ch->bc->port, "misdn_write: Returning because notxtone\n");
                return 0;
        }
 
@@ -2929,7 +2929,7 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
                ast_debug(1, "write2mISDN %p %d bytes: ", p, frame->samples);
 
                for (i = 0; i < max ; i++)
-                       ast_debug(1, "%2.2x ", ((char*) frame->data)[i]);
+                       ast_debug(1, "%2.2x ", ((char*) frame->data.ptr)[i]);
        }
 #endif
 
@@ -2939,29 +2939,28 @@ static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
                break;
        default:
                if (!ch->dropped_frame_cnt)
-                       chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) droping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n", frame->samples, ch->bc->addr, ast->exten, ast->cid.cid_num, misdn_get_ch_state( ch), ch->bc->bc_state, ch->bc->l3_id);
+                       chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n", frame->samples, ch->bc->addr, ast->exten, ast->cid.cid_num, misdn_get_ch_state( ch), ch->bc->bc_state, ch->bc->l3_id);
                
                ch->dropped_frame_cnt++;
                if (ch->dropped_frame_cnt > 100) {
                        ch->dropped_frame_cnt = 0;
-                       chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) droping: %d frames addr:%x  dropped > 100 frames!\n", frame->samples, ch->bc->addr);
-
+                       chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x  dropped > 100 frames!\n", frame->samples, ch->bc->addr);
                }
 
                return 0;
        }
 
-       chan_misdn_log(9, ch->bc->port, "Sending :%d bytes 2 MISDN\n", frame->samples);
+       chan_misdn_log(9, ch->bc->port, "Sending :%d bytes to MISDN\n", frame->samples);
        if ( !ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability) ) {
-               /* Buffered Transmit (triggert by read from isdn side)*/
-               if (misdn_jb_fill(ch->jb, frame->data, frame->samples) < 0) {
+               /* Buffered Transmit (triggered by read from isdn side)*/
+               if (misdn_jb_fill(ch->jb, frame->data.ptr, 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);
+               i=misdn_lib_tx2misdn_frm(ch->bc, frame->data.ptr, frame->samples);
        }
 
        return 0;
@@ -3034,11 +3033,10 @@ static enum ast_bridge_result  misdn_bridge (struct ast_channel *c0,
                        if (!f) 
                                chan_misdn_log(4, ch1->bc->port, "Read Null Frame\n");
                        else
-                               chan_misdn_log(4, ch1->bc->port, "Read Frame Controll class:%d\n", f->subclass);
+                               chan_misdn_log(4, ch1->bc->port, "Read Frame Control class:%d\n", f->subclass);
 
                        *fo = f;
                        *rc = who;
-
                        break;
                }
                
@@ -3212,7 +3210,7 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
        }
 
        if (misdn_cfg_is_group_method(group, METHOD_STANDARD_DEC)) {
-               chan_misdn_log(4, port, " --> STARTING STANDARDDEC...\n");
+               chan_misdn_log(4, port, " --> STARTING STANDARD DEC...\n");
                dec = 1;
        }
 
@@ -3407,6 +3405,7 @@ static void update_name(struct ast_channel *tmp, int port, int c)
 {
        int chan_offset = 0;
        int tmp_port = misdn_cfg_get_next_port(0);
+       char newname[255];
        for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
                if (tmp_port == port)
                        break;
@@ -3415,10 +3414,12 @@ static void update_name(struct ast_channel *tmp, int port, int c)
        if (c < 0)
                c = 0;
 
-       ast_string_field_build(tmp, name, "%s/%d-u%d",
-                                misdn_type, chan_offset+c, glob_channel++);
-
-       chan_misdn_log(3 , port, " --> updating channel name to [%s]\n", tmp->name);
+       snprintf(newname, sizeof(newname), "%s/%d-", misdn_type, chan_offset + c);
+       if (strncmp(tmp->name, newname, strlen(newname))) {
+               snprintf(newname, sizeof(newname), "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++);
+               ast_change_name(tmp, newname);
+               chan_misdn_log(3, port, " --> updating channel name to [%s]\n", tmp->name);
+       }
 }
 
 static struct ast_channel *misdn_new(struct chan_list *chlist, int state,  char *exten, char *callerid, int format, int port, int c)
@@ -3432,15 +3433,16 @@ static struct ast_channel *misdn_new(struct chan_list *chlist, int state,  char
        for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
                if (tmp_port == port)
                        break;
-               chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;        
+               chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
        }
        if (c < 0)
                c = 0;
 
-       if (callerid) 
+       if (callerid) {
                ast_callerid_parse(callerid, &cid_name, &cid_num);
+       }
 
-       tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", 0, "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++);
+       tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", 0, "%s/%s%d-u%d", misdn_type, c ? "" : "tmp", chan_offset + c, glob_channel++);
 
        if (tmp) {
                chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s\n", exten, callerid);
@@ -3485,7 +3487,7 @@ static struct ast_channel *misdn_new(struct chan_list *chlist, int state,  char
                else
                        tmp->rings = 0;
                
-               ast_jb_configure(tmp, misdn_get_global_jbconf());
+               ast_jb_configure(tmp, misdn_get_global_jbconf());
        } else {
                chan_misdn_log(-1, 0, "Unable to allocate channel structure\n");
        }
@@ -3601,7 +3603,7 @@ static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan)
 /** Channel Queue End **/
 
 
-int pbx_start_chan(struct chan_list *ch)
+static int pbx_start_chan(struct chan_list *ch)
 {
        int ret = ast_pbx_start(ch->ast);       
 
@@ -3642,7 +3644,7 @@ static void hangup_chan(struct chan_list *ch)
                send_cause2ast(ch->ast, ch->bc, ch);
 
                if (ch->ast)
-                       ast_queue_hangup(ch->ast);
+                       ast_queue_hangup_with_cause(ch->ast, ch->bc->cause);
                cb_log(2, port, " --> queue_hangup\n");
        } else {
                cb_log(1, port, "Cannot hangup chan, no ast\n");
@@ -3668,7 +3670,7 @@ static void release_chan(struct misdn_bchannel *bc) {
 
                chan_misdn_log(5, bc->port, "release_chan: bc with l3id: %x\n", bc->l3_id);
 
-               /*releaseing jitterbuffer*/
+               /*releasing jitterbuffer*/
                if (ch->jb ) {
                        misdn_jb_destroy(ch->jb);
                        ch->jb = NULL;
@@ -3721,7 +3723,7 @@ static void release_chan(struct misdn_bchannel *bc) {
 
 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);
+       chan_misdn_log(4, 0, "TRANSFERRING %s to %s\n", holded_chan->ast->name, tmp_ch->ast->name);
 
        tmp_ch->state = MISDN_HOLD_DISCONNECT;
 
@@ -3778,7 +3780,7 @@ static void do_immediate_setup(struct misdn_bchannel *bc, struct chan_list *ch,
                fr.frametype = AST_FRAME_DTMF;
                fr.subclass = *predial;
                fr.src = NULL;
-               fr.data = NULL;
+               fr.data.ptr = NULL;
                fr.datalen = 0;
                fr.samples = 0;
                fr.mallocd = 0;
@@ -3812,12 +3814,13 @@ static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, s
 
        switch (bc->cause) {
 
-       case 1: /** Congestion Cases **/
-       case 2:
-       case 3:
-       case 4:
-       case 22:
-       case 27:
+       case AST_CAUSE_UNALLOCATED:
+       case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
+       case AST_CAUSE_NO_ROUTE_DESTINATION:
+       case 4: /* Send special information tone */
+       case AST_CAUSE_NUMBER_CHANGED:
+       case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
+               /* Congestion Cases */
                /*
                 * Not Queueing the Congestion anymore, since we want to hear
                 * the inband message
@@ -3829,9 +3832,8 @@ static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, s
                */
                break;
 
-       case 21:
-       case 17: /* user busy */
-
+       case AST_CAUSE_CALL_REJECTED:
+       case AST_CAUSE_USER_BUSY:
                ch->state = MISDN_BUSY;
 
                if (!ch->need_busy) {
@@ -4063,7 +4065,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                fr.frametype = AST_FRAME_DTMF;
                fr.subclass = bc->dtmf ;
                fr.src = NULL;
-               fr.data = NULL;
+               fr.data.ptr = NULL;
                fr.datalen = 0;
                fr.samples = 0;
                fr.mallocd = 0;
@@ -4074,7 +4076,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf);
                        ast_queue_frame(ch->ast, &fr);
                } else {
-                       chan_misdn_log(2, bc->port, " --> Ingoring DTMF:%c due to bridge flags\n", bc->dtmf);
+                       chan_misdn_log(2, bc->port, " --> Ignoring DTMF:%c due to bridge flags\n", bc->dtmf);
                }
        }
                break;
@@ -4096,7 +4098,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                                ast_copy_string(bc->info_dad, bc->keypad, sizeof(bc->info_dad));
                        }
 
-                       strncat(bc->dad,bc->info_dad, sizeof(bc->dad) - 1);
+                       strncat(bc->dad,bc->info_dad, sizeof(bc->dad) - strlen(bc->dad) - 1);
                        ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten));
 
                        /* Check for Pickup Request first */
@@ -4123,14 +4125,14 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                                        break;
                                }
 
-                               ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d)."
-                                               "maybe you want to add an 'i' extension to catch this case.\n",
+                               ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d).\n"
+                                               "\tMaybe you want to add an 'i' extension to catch this case.\n",
                                                bc->port);
 
                                if (bc->nt)
                                        hanguptone_indicate(ch);
                                ch->state = MISDN_EXTCANTMATCH;
-                               bc->out_cause = 1;
+                               bc->out_cause = AST_CAUSE_UNALLOCATED;
 
                                misdn_lib_send_event(bc, EVENT_DISCONNECT);
                                break;
@@ -4156,10 +4158,11 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        /*  sending INFOS as DTMF-Frames :) */
                        struct ast_frame fr;
                        int digits;
+                       memset(&fr, 0, sizeof(fr));
                        fr.frametype = AST_FRAME_DTMF;
                        fr.subclass = bc->info_dad[0] ;
                        fr.src = NULL;
-                       fr.data = NULL;
+                       fr.data.ptr = NULL;
                        fr.datalen = 0;
                        fr.samples = 0;
                        fr.mallocd = 0;
@@ -4169,7 +4172,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        misdn_cfg_get(0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(digits));
                        if (ch->state != MISDN_CONNECTED ) {
                                if (digits) {
-                                       strncat(bc->dad, bc->info_dad, sizeof(bc->dad) - 1);
+                                       strncat(bc->dad, bc->info_dad, sizeof(bc->dad) - strlen(bc->dad) - 1);
                                        ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten));
                                        ast_cdr_update(ch->ast);
                                }
@@ -4209,7 +4212,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        int cause;
                        chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n");
                        misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause));
-                       bc->out_cause = cause ? cause : 16;
+                       bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING;
                        return RESPONSE_RELEASE_SETUP;
                }
 
@@ -4224,7 +4227,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        int cause;
                        chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n");
                        misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause));
-                       bc->out_cause = cause ? cause : 16;
+                       bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING;
                        return RESPONSE_RELEASE_SETUP;
                }
 
@@ -4272,17 +4275,19 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        break;
                case 2:
                        pres = AST_PRES_UNAVAILABLE;
-                       chan_misdn_log(2, bc->port, " --> PRES: Restricted (2)\n");
+                       chan_misdn_log(2, bc->port, " --> PRES: Unavailable (2)\n");
                        break;
                default:
                        pres = AST_PRES_ALLOWED;
-                       chan_misdn_log(2, bc->port, " --> PRES: Restricted (%d)\n", bc->pres);
+                       chan_misdn_log(2, bc->port, " --> PRES: Allowed (%d)\n", bc->pres);
+                       break;
                }
 
                switch (bc->screen) {
+               default:
                case 0:
                        screen = AST_PRES_USER_NUMBER_UNSCREENED;
-                       chan_misdn_log(2, bc->port, " --> SCREEN: Unscreened (0)\n");
+                       chan_misdn_log(2, bc->port, " --> SCREEN: Unscreened (%d)\n", bc->screen);
                        break;
                case 1:
                        screen = AST_PRES_USER_NUMBER_PASSED_SCREEN;
@@ -4296,12 +4301,9 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        screen = AST_PRES_NETWORK_NUMBER;
                        chan_misdn_log(2, bc->port, " --> SCREEN: Network Number (3)\n");
                        break;
-               default:
-                       screen = AST_PRES_USER_NUMBER_UNSCREENED;
-                       chan_misdn_log(2, bc->port, " --> SCREEN: Unscreened (%d)\n", bc->screen);
                }
 
-               chan->cid.cid_pres = pres + screen;
+               chan->cid.cid_pres = pres | screen;
 
                pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability));
                chan->transfercapability = bc->capability;
@@ -4319,18 +4321,28 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 
                if (!strstr(ch->allowed_bearers, "all")) {
                        int i;
-                       for (i = 0; i < sizeof(allowed_bearers_array) / sizeof(struct allowed_bearers); i++) {
-                               if (allowed_bearers_array[i].cap == bc->capability) {
-                                       if (!strstr(ch->allowed_bearers, allowed_bearers_array[i].name)) {
-                                               chan_misdn_log(0, bc->port, "Bearer Not allowed\b");
-                                               bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
 
-                                               ch->state = MISDN_EXTCANTMATCH;
-                                               misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
-                                               return RESPONSE_OK;
+                       for (i = 0; i < ARRAY_LEN(allowed_bearers_array); ++i) {
+                               if (allowed_bearers_array[i].cap == bc->capability) {
+                                       if (strstr(ch->allowed_bearers, allowed_bearers_array[i].name)) {
+                                               /* The bearer capability is allowed */
+                                               if (allowed_bearers_array[i].deprecated) {
+                                                       chan_misdn_log(0, bc->port, "%s in allowed_bearers list is deprecated\n",
+                                                               allowed_bearers_array[i].name);
+                                               }
+                                               break;
                                        }
                                }
+                       }       /* end for */
+                       if (i == ARRAY_LEN(allowed_bearers_array)) {
+                               /* We did not find the bearer capability */
+                               chan_misdn_log(0, bc->port, "Bearer capability not allowed: %s(%d)\n",
+                                       bearer2str(bc->capability), bc->capability);
+                               bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
 
+                               ch->state = MISDN_EXTCANTMATCH;
+                               misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
+                               return RESPONSE_OK;
                        }
                }
 
@@ -4381,8 +4393,8 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                                break;
                        }
 
-                       ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d)."
-                                       "maybe you want to add an 'i' extension to catch this case.\n",
+                       ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d).\n"
+                                       "\tMaybe you want to add an 'i' extension to catch this case.\n",
                                        bc->port);
                        if (bc->nt)
                                hanguptone_indicate(ch);
@@ -4440,7 +4452,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        break;
                }
 
-               /* If the extension does not exist and we're not TE_PTMP we wait for more digis 
+               /* If the extension does not exist and we're not TE_PTMP we wait for more digits 
                 * without interdigit timeout.
                 * */
                if (!ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad))  {
@@ -4688,7 +4700,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                int res;
                int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
 
-               chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n");
+               chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n", tone_len);
 
                if (!ast)
                        break;
@@ -4732,7 +4744,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        frame.offset = 0;
                        frame.delivery = ast_tv(0,0);
                        frame.src = NULL;
-                       frame.data = bc->bframe;
+                       frame.data.ptr = bc->bframe;
 
                        if (ch->ast) 
                                ast_queue_frame(ch->ast, &frame);
@@ -4757,7 +4769,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        }
                        
                        if (FD_ISSET(ch->pipe[1], &wrfs)) {
-                               chan_misdn_log(9, bc->port, "writing %d bytes 2 asterisk\n", bc->bframe_len);
+                               chan_misdn_log(9, bc->port, "writing %d bytes to asterisk\n", bc->bframe_len);
                                if (write(ch->pipe[1], bc->bframe, bc->bframe_len) <= 0) {
                                        chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n", strerror(errno));
 
@@ -4787,7 +4799,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                case MISDN_PROCEEDING:
                case MISDN_CALLING_ACKNOWLEDGE:
                        if (bc->nt) {
-                               bc->progress_indicator = 8;
+                               bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
                                hanguptone_indicate(ch);
                        }
                                
@@ -4797,7 +4809,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 
                case MISDN_WAITING4DIGS:
                        if (bc->nt) {
-                               bc->progress_indicator = 8;
+                               bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
                                bc->out_cause = AST_CAUSE_UNALLOCATED;
                                hanguptone_indicate(ch);
                                misdn_lib_send_event(bc, EVENT_DISCONNECT);
@@ -4809,7 +4821,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        break;
 
                case MISDN_CLEANING: 
-                       chan_misdn_log(1,bc->port," --> in state cleaning .. so ingoring, the stack should clean it for us\n");
+                       chan_misdn_log(1,bc->port," --> in state cleaning .. so ignoring, the stack should clean it for us\n");
                        break;
 
                default:
@@ -4819,9 +4831,9 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
        break;
 
     
-       /***************************/
-       /** Suplementary Services **/
-       /***************************/
+       /****************************/
+       /** Supplementary Services **/
+       /****************************/
        case EVENT_RETRIEVE:
        {
                struct ast_channel *hold_ast;
@@ -4943,7 +4955,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 #endif
                break;
                default:
-                       chan_misdn_log(0, bc->port," --> not yet handled: facility type:%p\n", bc->fac_in.Function);
+                       chan_misdn_log(0, bc->port," --> not yet handled: facility type:%d\n", bc->fac_in.Function);
                }
                
                break;
@@ -5090,20 +5102,30 @@ static int load_module(void)
        ast_cli_register_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
 
        ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
-                                "misdn_set_opt(:<opt><optarg>:<opt><optarg>..):\n"
-                                "Sets mISDN opts. and optargs\n"
-                                "\n"
-                                "The available options are:\n"
-                                "    d - Send display text on called phone, text is the optparam\n"
-                                "    n - don't detect dtmf tones on called channel\n"
-                                "    h - make digital outgoing call\n" 
-                                "    c - make crypted outgoing call, param is keyindex\n"
-                                "    e - perform echo cancelation on this channel,\n"
-                                "        takes taps as arguments (32,64,128,256)\n"
-                                "    s - send Non Inband DTMF as inband\n"
-                                "   vr - rxgain control\n"
-                                "   vt - txgain control\n"
-                                "    i - Ignore detected dtmf tones, don't signal them to asterisk, they will be transported inband.\n"
+               "misdn_set_opt(:<opt><optarg>:<opt><optarg>...):\n"
+               "Sets mISDN opts. and optargs\n"
+               "\n"
+               "The available options are:\n"
+               "    a - Have Asterisk detect DTMF tones on called channel\n"
+               "    c - Make crypted outgoing call, optarg is keyindex\n"
+               "    d - Send display text to called phone, text is the optarg\n"
+               "    e - Perform echo cancelation on this channel,\n"
+               "        takes taps as optarg (32,64,128,256)\n"
+               "   e! - Disable echo cancelation on this channel\n"
+               "    f - Enable fax detection\n"
+               "    h - Make digital outgoing call\n" 
+               "   h1 - Make HDLC mode digital outgoing call\n" 
+               "    i - Ignore detected DTMF tones, don't signal them to Asterisk,\n"
+               "        they will be transported inband.\n"
+               "   jb - Set jitter buffer length, optarg is length\n"
+               "   jt - Set jitter buffer upper threshold, optarg is threshold\n"
+               "   jn - Disable jitter buffer\n"
+               "    n - disable DSP on channel, disables: Echocancel, DTMF Detection and Volume Control.\n"
+               "    p - Caller ID presentation,\n"
+               "        optarg is either 'allowed' or 'restricted'\n"
+               "    s - Send Non-inband DTMF as inband\n"
+               "   vr - Rx gain control, optarg is gain\n"
+               "   vt - Tx gain control, optarg is gain\n"
                );
 
        
@@ -5454,7 +5476,10 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
                        /* CRICH: callingpres!!! */
                        if (strstr(tok,"allowed")) {
                                ch->bc->pres = 0;
+                       } else if (strstr(tok, "restricted")) {
+                               ch->bc->pres = 1;
                        } else if (strstr(tok, "not_screened")) {
+                               chan_misdn_log(0, ch->bc->port, "SETOPT: callerpres: not_screened is deprecated\n");
                                ch->bc->pres = 1;
                        }
                        break;
@@ -5474,7 +5499,7 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
                if (!ch->dsp)
                        ch->dsp = ast_dsp_new();
                if (ch->dsp)
-                       ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT | DSP_FEATURE_FAX_DETECT);
+                       ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_FAX_DETECT);
                if (!ch->trans)
                        ch->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW);
        }
@@ -5507,7 +5532,7 @@ int chan_misdn_jb_empty ( struct misdn_bchannel *bc, char *buf, int len)
 /*******************************************************/
 
 
-/* allocates the jb-structure and initialise the elements*/
+/* allocates the jb-structure and initialize the elements*/
 struct misdn_jb *misdn_jb_init(int size, int upper_threshold)
 {
        int i;
@@ -5560,7 +5585,7 @@ void misdn_jb_destroy(struct misdn_jb *jb)
 }
 
 /* fills the jitterbuffer with len data returns < 0 if there was an
-   error (bufferoverflow). */
+   error (buffer overflow). */
 int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
 {
        int i, j, rp, wp;
@@ -5586,7 +5611,7 @@ int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
                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);
+       chan_misdn_log(9, 0, "misdn_jb_fill: written:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
 
        if (jb->state_full) {
                jb->wp = wp;
@@ -5652,11 +5677,11 @@ int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
                        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);
+               chan_misdn_log(9, 0, "misdn_jb_empty: read:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
 
                jb->rp = rp;
        } else
-               chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%x\n", len, jb);
+               chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%p\n", len, jb);
 
        ast_mutex_unlock(&jb->mutexjb);
 
@@ -5673,7 +5698,7 @@ int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
 
 
 
-void chan_misdn_log(int level, int port, char *tmpl, ...)
+static void chan_misdn_log(int level, int port, char *tmpl, ...)
 {
        va_list ap;
        char buf[1024];
@@ -5692,7 +5717,7 @@ void chan_misdn_log(int level, int port, char *tmpl, ...)
        va_end(ap);
 
        if (level == -1)
-               ast_log(LOG_WARNING, buf);
+               ast_log(LOG_WARNING, "%s", buf);
 
        else if (misdn_debug_only[port] ? 
                        (level == 1 && misdn_debug[port]) || (level == misdn_debug[port])