Merged revisions 136062 via svnmerge from
[asterisk/asterisk.git] / channels / chan_unistim.c
index 898026e..2757671 100644 (file)
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <sys/socket.h>
-#include <sys/ioctl.h>
 #include <sys/stat.h>
-#include <net/if.h>
-#include <fcntl.h>
-#include <netdb.h>
-#include <sys/signal.h>
 #include <signal.h>
-#include <netinet/in.h>
-#include <netinet/in_systm.h>
-#include <netinet/ip.h>
-#include <arpa/inet.h>
-#include <ctype.h>
+
+#if defined(__CYGWIN__)
+/*
+ * cygwin headers are partly inconsistent. struct iovec is defined in sys/uio.h
+ * which is not included by default by sys/socket.h - in_pktinfo is defined in
+ * w32api/ws2tcpip.h but this probably has compatibility problems with sys/socket.h
+ * So for the time being we simply disable HAVE_PKTINFO when building under cygwin.
+ *    This should be done in some common header, but for now this is the only file
+ * using iovec and in_pktinfo so it suffices to apply the fix here.
+ */
+#ifdef HAVE_PKTINFO
+#undef HAVE_PKTINFO
+#endif
+#endif /* __CYGWIN__ */
 
 #include "asterisk/paths.h"    /* ast_config_AST_LOG_DIR used in (too ?) many places */
-#include "asterisk/lock.h"
+#include "asterisk/network.h"
 #include "asterisk/channel.h"
 #include "asterisk/config.h"
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
-#include "asterisk/options.h"
-#include "asterisk/sched.h"
-#include "asterisk/io.h"
+#include "asterisk/event.h"
 #include "asterisk/rtp.h"
+#include "asterisk/netsock.h"
 #include "asterisk/acl.h"
 #include "asterisk/callerid.h"
 #include "asterisk/cli.h"
-#include "asterisk/say.h"
-#include "asterisk/cdr.h"
-#include "asterisk/astdb.h"
-#include "asterisk/features.h"
 #include "asterisk/app.h"
 #include "asterisk/musiconhold.h"
-#include "asterisk/utils.h"
 #include "asterisk/causes.h"
 #include "asterisk/indications.h"
-#include "asterisk/dsp.h"
-#include "asterisk/devicestate.h"
-#include "asterisk/stringfields.h"
-#include "asterisk/abstract_jb.h"
-#include "asterisk/event.h"
 
 /*! Beware, G729 and G723 are not supported by asterisk, except with the proper licence */
 #define CAPABILITY AST_FORMAT_ALAW | AST_FORMAT_ULAW    /* | AST_FORMAT_G729A | AST_FORMAT_G723_1 */
@@ -214,6 +206,10 @@ static int unistim_port;
 static enum autoprovision autoprovisioning = AUTOPROVISIONING_NO;
 static int unistim_keepalive;
 static int unistimsock = -1;
+static unsigned int tos = 0;
+static unsigned int tos_audio = 0;
+static unsigned int cos = 0;
+static unsigned int cos_audio = 0;
 static struct io_context *io;
 static struct sched_context *sched;
 static struct sockaddr_in public_ip = { 0, };
@@ -223,8 +219,6 @@ static struct sockaddr_in addr_from;
 static unsigned int size_addr_from = sizeof(addr_from);
 /*! Receive buffer address */
 static unsigned char *buff;
-/*! Used for test */
-static unsigned char flag = 0;
 static int unistim_reloading = 0;
 AST_MUTEX_DEFINE_STATIC(unistim_reload_lock);
 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
@@ -410,6 +404,8 @@ struct unistim_line {
        int amaflags;
        /*! Codec supported */
        int capability;
+       /*! Parkinglot */
+       char parkinglot[AST_MAX_CONTEXT];
        struct unistim_line *next;
        struct unistim_device *parent;
 };
@@ -500,48 +496,48 @@ static struct unistimsession {
  * byte 5 : direction, 1 = server to phone, 2 = phone to server arguments
  */
 
-const static unsigned char packet_rcv_discovery[] =
+static const unsigned char packet_rcv_discovery[] =
        { 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
 static unsigned char packet_send_discovery_ack[] =
        { 0x00, 0x00, /*Initial Seq (2 bytes) */ 0x00, 0x00, 0x00, 0x01 };
 
-const static unsigned char packet_recv_firm_version[] =
+static const unsigned char packet_recv_firm_version[] =
        { 0x00, 0x00, 0x00, 0x13, 0x9a, 0x0a, 0x02 };
-const static unsigned char packet_recv_pressed_key[] =
+static const unsigned char packet_recv_pressed_key[] =
        { 0x00, 0x00, 0x00, 0x13, 0x99, 0x04, 0x00 };
-const static unsigned char packet_recv_pick_up[] =
+static const unsigned char packet_recv_pick_up[] =
        { 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x04 };
-const static unsigned char packet_recv_hangup[] =
+static const unsigned char packet_recv_hangup[] =
        { 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x03 };
-const static unsigned char packet_recv_r2[] = { 0x00, 0x00, 0x00, 0x13, 0x96, 0x03, 0x03 };
+static const unsigned char packet_recv_r2[] = { 0x00, 0x00, 0x00, 0x13, 0x96, 0x03, 0x03 };
 
 /*! TransportAdapter */
-const static unsigned char packet_recv_resume_connection_with_server[] =
+static const unsigned char packet_recv_resume_connection_with_server[] =
        { 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
-const static unsigned char packet_recv_mac_addr[] =
-       { 0xff, 0xff, 0xff, 0xff, 0x9a, 0x0d, 0x07, 0x31, 0x38 /*MacAddr */  };
+static const unsigned char packet_recv_mac_addr[] =
+       { 0xff, 0xff, 0xff, 0xff, 0x9a, 0x0d, 0x07 /*MacAddr */  };
 
-const static unsigned char packet_send_date_time3[] =
+static const unsigned char packet_send_date_time3[] =
        { 0x11, 0x09, 0x02, 0x02, /*Month */ 0x05, /*Day */ 0x06, /*Hour */ 0x07,
 /*Minutes */ 0x08, 0x32
 };
-const static unsigned char packet_send_date_time[] =
+static const unsigned char packet_send_date_time[] =
        { 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05, /*Day */ 0x06, /*Hour */ 0x07, /*Minutes */
 0x08, 0x32, 0x17, 0x04, 0x24, 0x07, 0x19,
        0x04, 0x07, 0x00, 0x19, 0x05, 0x09, 0x3e, 0x0f, 0x16, 0x05, 0x00, 0x80, 0x00, 0x1e,
                0x05, 0x12, 0x00, 0x78
 };
 
-const static unsigned char packet_send_no_ring[] =
+static const unsigned char packet_send_no_ring[] =
        { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00 };
-const static unsigned char packet_send_s4[] =
+static const unsigned char packet_send_s4[] =
        { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00, 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff,
 0x16, 0x05, 0x1c, 0x00, 0x00, 0x17, 0x05,
        0x0b, 0x00, 0x00, 0x19, 0x04, 0x00, 0x00, 0x19, 0x04, 0x00, 0x08, 0x19, 0x04, 0x00,
                0x10, 0x19, 0x04, 0x00, 0x18, 0x16, 0x05,
        0x31, 0x00, 0x00, 0x16, 0x05, 0x04, 0x00, 0x00
 };
-const static unsigned char packet_send_call[] =
+static const unsigned char packet_send_call[] =
        { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00, 0x16, 0x06, 0x32, 0xdf,
        0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16, 0x0a, 0x38, 0x00, 0x12, 0xca, 0x03,
                0xc0, 0xc3, 0xc5, 0x16, 0x16, 0x30, 0x00,
@@ -550,36 +546,36 @@ const static unsigned char packet_send_call[] =
        /*port RTP */ 0x0f, 0xa0, /* port RTCP */ 0x9c, 0x41, /* IP Address */ 0x0a, 0x01,
                0x16, 0x66
 };
-const static unsigned char packet_send_stream_based_tone_off[] =
+static const unsigned char packet_send_stream_based_tone_off[] =
        { 0x16, 0x05, 0x1c, 0x00, 0x00 };
 
-/* const static unsigned char packet_send_Mute[] = { 0x16, 0x05, 0x04, 0x00, 0x00 };
-const static unsigned char packet_send_CloseAudioStreamRX[] = { 0x16, 0x05, 0x31, 0x00, 0xff };
-const static unsigned char packet_send_CloseAudioStreamTX[] = { 0x16, 0x05, 0x31, 0xff, 0x00 };*/
-const static unsigned char packet_send_stream_based_tone_on[] =
+/* static const unsigned char packet_send_Mute[] = { 0x16, 0x05, 0x04, 0x00, 0x00 };
+static const unsigned char packet_send_CloseAudioStreamRX[] = { 0x16, 0x05, 0x31, 0x00, 0xff };
+static const unsigned char packet_send_CloseAudioStreamTX[] = { 0x16, 0x05, 0x31, 0xff, 0x00 };*/
+static const unsigned char packet_send_stream_based_tone_on[] =
        { 0x16, 0x06, 0x1b, 0x00, 0x00, 0x05 };
-const static unsigned char packet_send_stream_based_tone_single_freq[] =
+static const unsigned char packet_send_stream_based_tone_single_freq[] =
        { 0x16, 0x06, 0x1d, 0x00, 0x01, 0xb8 };
-const static unsigned char packet_send_stream_based_tone_dial_freq[] =
+static const unsigned char packet_send_stream_based_tone_dial_freq[] =
        { 0x16, 0x08, 0x1d, 0x00, 0x01, 0xb8, 0x01, 0x5e };
-const static unsigned char packet_send_select_output[] =
+static const unsigned char packet_send_select_output[] =
        { 0x16, 0x06, 0x32, 0xc0, 0x01, 0x00 };
-const static unsigned char packet_send_ring[] =
+static const unsigned char packet_send_ring[] =
        { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16,
        0x04, 0x1a, 0x01, 0x16, 0x05, 0x12, 0x13 /* Ring type 10 to 17 */ , 0x18, 0x16, 0x04, 0x18,     /* volume 00, 10, 20... */
        0x20, 0x16, 0x04, 0x10, 0x00
 };
-const static unsigned char packet_send_end_call[] =
+static const unsigned char packet_send_end_call[] =
        { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x31, 0x00, 0x00, 0x19, 0x04, 0x00,
 0x10, 0x19, 0x04, 0x00, 0x18, 0x16, 0x05,
        0x04, 0x00, 0x00, 0x16, 0x04, 0x37, 0x10
 };
-const static unsigned char packet_send_s9[] =
+static const unsigned char packet_send_s9[] =
        { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x19, 0x04, 0x00, 0x10, 0x16, 0x05, 0x1c, 0x00,
 0x00 };
-const static unsigned char packet_send_rtp_packet_size[] =
+static const unsigned char packet_send_rtp_packet_size[] =
        { 0x16, 0x08, 0x38, 0x00, 0x00, 0xe0, 0x00, 0xa0 };
-const static unsigned char packet_send_jitter_buffer_conf[] =
+static const unsigned char packet_send_jitter_buffer_conf[] =
        { 0x16, 0x0e, 0x3a, 0x00, /* jitter */ 0x02, /* high water mark */ 0x04, 0x00, 0x00,
 /* early packet resync 2 bytes */ 0x3e, 0x80,
        0x00, 0x00, /* late packet resync 2 bytes */ 0x3e, 0x80
@@ -588,86 +584,86 @@ const static unsigned char packet_send_jitter_buffer_conf[] =
 /* Duration in ms div 2 (0x20 = 64ms, 0x08 = 16ms) 
 static unsigned char packet_send_StreamBasedToneCad[] =
   { 0x16, 0x0a, 0x1e, 0x00, duration on  0x0a, duration off  0x0d, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x2b }; */
-const static unsigned char packet_send_open_audio_stream_rx[] =
+static const unsigned char packet_send_open_audio_stream_rx[] =
        { 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
 0x0e, 0x01, /* Port */ 0x14, 0x50, 0x00,
        0x00, /* Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
 };
-const static unsigned char packet_send_open_audio_stream_tx[] =
+static const unsigned char packet_send_open_audio_stream_tx[] =
        { 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
 0x0e, 0x01, /* Local port */ 0x14, 0x50,
        0x00, 0x00, /* Rmt Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
 };
 
-const static unsigned char packet_send_open_audio_stream_rx3[] =
+static const unsigned char packet_send_open_audio_stream_rx3[] =
        { 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
 0x06, 0x81, /* RTP Port */ 0x14, 0x50,
 /* RTCP Port */ 0x14,
        0x51, /* RTP Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00, /* Dest IP */ 0x0a, 0x93,
                0x69, 0x05
 };
-const static unsigned char packet_send_open_audio_stream_tx3[] =
+static const unsigned char packet_send_open_audio_stream_tx3[] =
        { 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
 0x06, 0x81, /* RTP Local port */ 0x14, 0x50,
        /* RTCP Port */ 0x00, 0x00, /* RTP Rmt Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00,
                /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
 };
 
-const static unsigned char packet_send_arrow[] = { 0x17, 0x04, 0x04, 0x00 };
-const static unsigned char packet_send_blink_cursor[] = { 0x17, 0x04, 0x10, 0x86 };
-const static unsigned char packet_send_date_time2[] = { 0x17, 0x04, 0x17, 0x3d, 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05,   /*Day */
+static const unsigned char packet_send_arrow[] = { 0x17, 0x04, 0x04, 0x00 };
+static const unsigned char packet_send_blink_cursor[] = { 0x17, 0x04, 0x10, 0x86 };
+static const unsigned char packet_send_date_time2[] = { 0x17, 0x04, 0x17, 0x3d, 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05,   /*Day */
        0x06, /*Hour */ 0x07, /*Minutes */ 0x08, 0x32
 };
-const static unsigned char packet_send_Contrast[] =
+static const unsigned char packet_send_Contrast[] =
        { 0x17, 0x04, 0x24, /*Contrast */ 0x08 };
-const static unsigned char packet_send_StartTimer[] =
+static const unsigned char packet_send_StartTimer[] =
        { 0x17, 0x05, 0x0b, 0x05, 0x00, 0x17, 0x08, 0x16, /* Text */ 0x44, 0x75, 0x72, 0xe9,
 0x65 };
-const static unsigned char packet_send_stop_timer[] = { 0x17, 0x05, 0x0b, 0x02, 0x00 };
-const static unsigned char packet_send_icon[] = { 0x17, 0x05, 0x14, /*pos */ 0x00, /*icon */ 0x25 };      /* display an icon in front of the text zone */
-const static unsigned char packet_send_S7[] = { 0x17, 0x06, 0x0f, 0x30, 0x07, 0x07 };
-const static unsigned char packet_send_set_pos_cursor[] =
+static const unsigned char packet_send_stop_timer[] = { 0x17, 0x05, 0x0b, 0x02, 0x00 };
+static const unsigned char packet_send_icon[] = { 0x17, 0x05, 0x14, /*pos */ 0x00, /*icon */ 0x25 };      /* display an icon in front of the text zone */
+static const unsigned char packet_send_S7[] = { 0x17, 0x06, 0x0f, 0x30, 0x07, 0x07 };
+static const unsigned char packet_send_set_pos_cursor[] =
        { 0x17, 0x06, 0x10, 0x81, 0x04, /*pos */ 0x20 };
 
 /*static unsigned char packet_send_MonthLabelsDownload[] =
   { 0x17, 0x0a, 0x15,  Month (3 char)  0x46, 0x65, 0x62, 0x4d, 0xe4, 0x72, 0x20 }; */
-const static unsigned char packet_send_favorite[] =
+static const unsigned char packet_send_favorite[] =
        { 0x17, 0x0f, 0x19, 0x10, /*pos */ 0x01, /*name */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
 0x20, 0x20, 0x20, 0x20, /*end_name */ 0x19,
        0x05, 0x0f, /*pos */ 0x01, /*icone */ 0x00
 };
-const static unsigned char packet_send_title[] =
+static const unsigned char packet_send_title[] =
        { 0x17, 0x10, 0x19, 0x02, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
 0x20, 0x20, 0x20, 0x20 /*end_text */  };
-const static unsigned char packet_send_text[] =
+static const unsigned char packet_send_text[] =
        { 0x17, 0x1e, 0x1b, 0x04, /*pos */ 0x00, /*inverse */ 0x25, /*text */ 0x20, 0x20,
 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
        0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
                /*end_text */ 0x17, 0x04, 0x10, 0x87
 };
-const static unsigned char packet_send_status[] =
+static const unsigned char packet_send_status[] =
        { 0x17, 0x20, 0x19, 0x08, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
        0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20    /*end_text */
 };
-const static unsigned char packet_send_status2[] =
+static const unsigned char packet_send_status2[] =
        { 0x17, 0x0b, 0x19, /* pos [08|28|48|68] */ 0x00, /* text */ 0x20, 0x20, 0x20, 0x20,
 0x20, 0x20, 0x20 /* end_text */  };
 
-const static unsigned char packet_send_led_update[] = { 0x19, 0x04, 0x00, 0x00 };
+static const unsigned char packet_send_led_update[] = { 0x19, 0x04, 0x00, 0x00 };
 
-const static unsigned char packet_send_query_basic_manager_04[] = { 0x1a, 0x04, 0x01, 0x04 };
-const static unsigned char packet_send_query_mac_address[] = { 0x1a, 0x04, 0x01, 0x08 };
-const static unsigned char packet_send_query_basic_manager_10[] = { 0x1a, 0x04, 0x01, 0x10 };
-const static unsigned char packet_send_S1[] = { 0x1a, 0x07, 0x07, 0x00, 0x00, 0x00, 0x13 };
+static const unsigned char packet_send_query_basic_manager_04[] = { 0x1a, 0x04, 0x01, 0x04 };
+static const unsigned char packet_send_query_mac_address[] = { 0x1a, 0x04, 0x01, 0x08 };
+static const unsigned char packet_send_query_basic_manager_10[] = { 0x1a, 0x04, 0x01, 0x10 };
+static const unsigned char packet_send_S1[] = { 0x1a, 0x07, 0x07, 0x00, 0x00, 0x00, 0x13 };
 
 static unsigned char packet_send_ping[] =
        { 0x1e, 0x05, 0x12, 0x00, /*Watchdog timer */ 0x78 };
 
 #define BUFFSEND unsigned char buffsend[64] = { 0x00, 0x00, 0xaa, 0xbb, 0x02, 0x01 }
 
-const static char tdesc[] = "UNISTIM Channel Driver";
-const static char type[] = "USTM";
+static const char tdesc[] = "UNISTIM Channel Driver";
+static const char type[] = "USTM";
 
 /*! Protos */
 static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state);
@@ -725,31 +721,9 @@ static void display_last_error(const char *sz_msg)
                        strerror(errno));
 }
 
-static void get_localtime(struct systemtime * systime)
-{
-       struct tm *stm;
-       time_t cur_time;
-       
-       time(&cur_time);
-
-       if ((stm = localtime(&cur_time)) == 0) {
-               display_last_error("Error in localtime()");
-               return;
-       }
-
-       systime->w_year = stm->tm_year;
-       systime->w_month = stm->tm_mon + 1;
-       systime->w_day_of_week = stm->tm_wday;
-       systime->w_day = stm->tm_mday;
-       systime->w_hour = stm->tm_hour;
-       systime->w_minute = stm->tm_min;
-       systime->w_second = stm->tm_sec;
-       systime->w_milliseconds = 0;
-}
-
 static unsigned int get_tick_count(void)
 {
-       struct timeval tv = ast_tvnow();;
+       struct timeval tv = ast_tvnow();
 
        return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
 }
@@ -777,7 +751,7 @@ static void send_raw_client(int size, unsigned char *data, struct sockaddr_in *a
        msg.msg_flags = 0;                    /* flags on received message */
 
        ip_msg->cmsg_len = CMSG_LEN(sizeof(*pki));
-       ip_msg->cmsg_level = SOL_IP;
+       ip_msg->cmsg_level = IPPROTO_IP;
        ip_msg->cmsg_type = IP_PKTINFO;
        pki->ipi_ifindex = 0;      /* Interface index, 0 = use interface specified in routing table */
        pki->ipi_spec_dst.s_addr = addr_ourip->sin_addr.s_addr; /* Local address */
@@ -788,12 +762,12 @@ static void send_raw_client(int size, unsigned char *data, struct sockaddr_in *a
                int tmp;
                char iabuf[INET_ADDRSTRLEN];
                char iabuf2[INET_ADDRSTRLEN];
-               ast_verbose("\n**> From %s sending %d bytes to %s ***\n",
+               ast_verb(0, "\n**> From %s sending %d bytes to %s ***\n",
                                        ast_inet_ntoa(addr_ourip->sin_addr), (int) size,
                                        ast_inet_ntoa(addr_to->sin_addr));
                for (tmp = 0; tmp < size; tmp++)
-                       ast_verbose("%.2x ", (unsigned char) data[tmp]);
-               ast_verbose("\n******************************************\n");
+                       ast_verb(0, "%.2x ", (unsigned char) data[tmp]);
+               ast_verb(0, "\n******************************************\n");
 
        }
 #endif
@@ -829,10 +803,8 @@ static void send_client(int size, const unsigned char *data, struct unistimsessi
        pte->timeout = tick + RETRANSMIT_TIMER;
 
 /*#ifdef DUMP_PACKET */
-       if ((unistimdebug) && (option_verbose > 5)) {
-               ast_verbose("Sending datas with seq #0x%.4x Using slot #%d :\n", pte->seq_server,
-                                       buf_pos);
-       }
+       if (unistimdebug)
+               ast_verb(6, "Sending datas with seq #0x%.4x Using slot #%d :\n", pte->seq_server, buf_pos);
 /*#endif */
        send_raw_client(pte->wsabufsend[buf_pos].len, pte->wsabufsend[buf_pos].buf, &(pte->sin),
                                  &(pte->sout));
@@ -843,8 +815,8 @@ static void send_client(int size, const unsigned char *data, struct unistimsessi
 static void send_ping(struct unistimsession *pte)
 {
        BUFFSEND;
-       if ((unistimdebug) && (option_verbose > 5))
-               ast_verbose("Sending ping\n");
+       if (unistimdebug)
+               ast_verb(6, "Sending ping\n");
        pte->tick_next_ping = get_tick_count() + unistim_keepalive;
        memcpy(buffsend + SIZE_HEADER, packet_send_ping, sizeof(packet_send_ping));
        send_client(SIZE_HEADER + sizeof(packet_send_ping), buffsend, pte);
@@ -894,8 +866,7 @@ static struct unistimsession *create_client(const struct sockaddr_in *addr_from)
        memcpy(&s->sin, addr_from, sizeof(struct sockaddr_in));
        get_to_address(unistimsock, &s->sout);
        if (unistimdebug) {
-               ast_verbose
-                       ("Creating a new entry for the phone from %s received via server ip %s\n",
+               ast_verb(0, "Creating a new entry for the phone from %s received via server ip %s\n",
                         ast_inet_ntoa(addr_from->sin_addr), ast_inet_ntoa(s->sout.sin_addr));
        }
        ast_mutex_init(&s->lock);
@@ -923,7 +894,7 @@ static void send_end_call(struct unistimsession *pte)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending end call\n");
+               ast_verb(0, "Sending end call\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_end_call, sizeof(packet_send_end_call));
        send_client(SIZE_HEADER + sizeof(packet_send_end_call), buffsend, pte);
 }
@@ -943,22 +914,22 @@ static void check_send_queue(struct unistimsession *pte)
 {
        /* Check if our send queue contained only one element */
        if (pte->last_buf_available == 1) {
-               if ((unistimdebug) && (option_verbose > 5))
-                       ast_verbose("Our single packet was ACKed.\n");
+               if (unistimdebug)
+                       ast_verb(6, "Our single packet was ACKed.\n");
                pte->last_buf_available--;
                set_ping_timer(pte);
                return;
        }
        /* Check if this ACK catch up our latest packet */
        else if (pte->last_seq_ack + 1 == pte->seq_server + 1) {
-               if ((unistimdebug) && (option_verbose > 5))
-                       ast_verbose("Our send queue is completely ACKed.\n");
+               if (unistimdebug)
+                       ast_verb(6, "Our send queue is completely ACKed.\n");
                pte->last_buf_available = 0;    /* Purge the send queue */
                set_ping_timer(pte);
                return;
        }
-       if ((unistimdebug) && (option_verbose > 5))
-               ast_verbose("We still have packets in our send queue\n");
+       if (unistimdebug)
+               ast_verb(6, "We still have packets in our send queue\n");
        return;
 }
 
@@ -966,7 +937,7 @@ static void send_start_timer(struct unistimsession *pte)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending start timer\n");
+               ast_verb(0, "Sending start timer\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_StartTimer, sizeof(packet_send_StartTimer));
        send_client(SIZE_HEADER + sizeof(packet_send_StartTimer), buffsend, pte);
 }
@@ -975,7 +946,7 @@ static void send_stop_timer(struct unistimsession *pte)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending stop timer\n");
+               ast_verb(0, "Sending stop timer\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_stop_timer, sizeof(packet_send_stop_timer));
        send_client(SIZE_HEADER + sizeof(packet_send_stop_timer), buffsend, pte);
 }
@@ -984,7 +955,7 @@ static void Sendicon(unsigned char pos, unsigned char status, struct unistimsess
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending icon pos %d with status 0x%.2x\n", pos, status);
+               ast_verb(0, "Sending icon pos %d with status 0x%.2x\n", pos, status);
        memcpy(buffsend + SIZE_HEADER, packet_send_icon, sizeof(packet_send_icon));
        buffsend[9] = pos;
        buffsend[10] = status;
@@ -996,7 +967,7 @@ static void send_tone(struct unistimsession *pte, uint16_t tone1, uint16_t tone2
        BUFFSEND;
        if (!tone1) {
                if (unistimdebug)
-                       ast_verbose("Sending Stream Based Tone Off\n");
+                       ast_verb(0, "Sending Stream Based Tone Off\n");
                memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_off,
                           sizeof(packet_send_stream_based_tone_off));
                send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_off), buffsend, pte);
@@ -1004,12 +975,11 @@ static void send_tone(struct unistimsession *pte, uint16_t tone1, uint16_t tone2
        }
        /* Since most of the world use a continuous tone, it's useless
           if (unistimdebug)
-          ast_verbose ("Sending Stream Based Tone Cadence Download\n");
+          ast_verb(0, "Sending Stream Based Tone Cadence Download\n");
           memcpy (buffsend + SIZE_HEADER, packet_send_StreamBasedToneCad, sizeof (packet_send_StreamBasedToneCad));
           send_client (SIZE_HEADER + sizeof (packet_send_StreamBasedToneCad), buffsend, pte); */
        if (unistimdebug)
-               ast_verbose("Sending Stream Based Tone Frequency Component List Download %d %d\n",
-                                       tone1, tone2);
+               ast_verb(0, "Sending Stream Based Tone Frequency Component List Download %d %d\n", tone1, tone2);
        tone1 *= 8;
        if (!tone2) {
                memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_single_freq,
@@ -1031,7 +1001,7 @@ static void send_tone(struct unistimsession *pte, uint16_t tone1, uint16_t tone2
        }
 
        if (unistimdebug)
-               ast_verbose("Sending Stream Based Tone On\n");
+               ast_verb(0, "Sending Stream Based Tone On\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_on,
                   sizeof(packet_send_stream_based_tone_on));
        send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_on), buffsend, pte);
@@ -1053,7 +1023,7 @@ send_favorite(unsigned char pos, unsigned char status, struct unistimsession *pt
        int i;
 
        if (unistimdebug)
-               ast_verbose("Sending favorite pos %d with status 0x%.2x\n", pos, status);
+               ast_verb(0, "Sending favorite pos %d with status 0x%.2x\n", pos, status);
        memcpy(buffsend + SIZE_HEADER, packet_send_favorite, sizeof(packet_send_favorite));
        buffsend[10] = pos;
        buffsend[24] = pos;
@@ -1070,7 +1040,7 @@ static void refresh_all_favorite(struct unistimsession *pte)
        int i = 0;
 
        if (unistimdebug)
-               ast_verbose("Refreshing all favorite\n");
+               ast_verb(0, "Refreshing all favorite\n");
        for (i = 0; i < 6; i++) {
                if ((pte->device->softkeyicon[i] <= FAV_ICON_HEADPHONES_ONHOLD) &&
                        (pte->device->softkeylinepos != i))
@@ -1111,7 +1081,7 @@ static void change_favorite_icon(struct unistimsession *pte, unsigned char statu
 static int RegisterExtension(const struct unistimsession *pte)
 {
        if (unistimdebug)
-               ast_verbose("Trying to register extension '%s' into context '%s' to %s\n",
+               ast_verb(0, "Trying to register extension '%s' into context '%s' to %s\n",
                                        pte->device->extension_number, pte->device->lines->context,
                                        pte->device->lines->fullname);
        return ast_add_extension(pte->device->lines->context, 0,
@@ -1122,7 +1092,7 @@ static int RegisterExtension(const struct unistimsession *pte)
 static int UnregisterExtension(const struct unistimsession *pte)
 {
        if (unistimdebug)
-               ast_verbose("Trying to unregister extension '%s' context '%s'\n",
+               ast_verb(0, "Trying to unregister extension '%s' context '%s'\n",
                                        pte->device->extension_number, pte->device->lines->context);
        return ast_context_remove_extension(pte->device->lines->context,
                                                                                pte->device->extension_number, 1, "Unistim");
@@ -1146,7 +1116,7 @@ static void close_client(struct unistimsession *s)
                if (cur->device) {            /* This session was registred ? */
                        s->state = STATE_CLEANING;
                        if (unistimdebug)
-                               ast_verbose("close_client session %p device %p lines %p sub %p\n",
+                               ast_verb(0, "close_client session %p device %p lines %p sub %p\n",
                                                        s, s->device, s->device->lines,
                                                        s->device->lines->subs[SUB_REAL]);
                        change_favorite_icon(s, FAV_ICON_NONE);
@@ -1154,8 +1124,8 @@ static void close_client(struct unistimsession *s)
                        if (sub) {
                                if (sub->owner) {       /* Call in progress ? */
                                        if (unistimdebug)
-                                               ast_verbose("Aborting call\n");
-                                       ast_queue_hangup(sub->owner);
+                                               ast_verb(0, "Aborting call\n");
+                                       ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NETWORK_OUT_OF_ORDER);
                                }
                        } else
                                ast_log(LOG_WARNING, "Freeing a client with no subchannel !\n");
@@ -1164,7 +1134,7 @@ static void close_client(struct unistimsession *s)
                        cur->device->session = NULL;
                } else {
                        if (unistimdebug)
-                               ast_verbose("Freeing an unregistered client\n");
+                               ast_verb(0, "Freeing an unregistered client\n");
                }
                if (prev)
                        prev->next = cur->next;
@@ -1173,7 +1143,7 @@ static void close_client(struct unistimsession *s)
                ast_mutex_destroy(&s->lock);
                ast_free(s);
        } else
-               ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
+               ast_log(LOG_WARNING, "Trying to delete non-existent session %p?\n", s);
        ast_mutex_unlock(&sessionlock);
        return;
 }
@@ -1186,7 +1156,7 @@ static int send_retransmit(struct unistimsession *pte)
        ast_mutex_lock(&pte->lock);
        if (++pte->nb_retransmit >= NB_MAX_RETRANSMIT) {
                if (unistimdebug)
-                       ast_verbose("Too many retransmit - freeing client\n");
+                       ast_verb(0, "Too many retransmit - freeing client\n");
                ast_mutex_unlock(&pte->lock);
                close_client(pte);
                return 1;
@@ -1207,7 +1177,7 @@ static int send_retransmit(struct unistimsession *pte)
                        unsigned short seq;
 
                        seq = ntohs(sbuf[1]);
-                       ast_verbose("Retransmit slot #%d (seq=#0x%.4x), last ack was #0x%.4x\n", i,
+                       ast_verb(0, "Retransmit slot #%d (seq=#0x%.4x), last ack was #0x%.4x\n", i,
                                                seq, pte->last_seq_ack);
                }
                send_raw_client(pte->wsabufsend[i].len, pte->wsabufsend[i].buf, &pte->sin,
@@ -1225,7 +1195,7 @@ send_text(unsigned char pos, unsigned char inverse, struct unistimsession *pte,
        int i;
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending text at pos %d, inverse flag %d\n", pos, inverse);
+               ast_verb(0, "Sending text at pos %d, inverse flag %d\n", pos, inverse);
        memcpy(buffsend + SIZE_HEADER, packet_send_text, sizeof(packet_send_text));
        buffsend[10] = pos;
        buffsend[11] = inverse;
@@ -1241,7 +1211,7 @@ static void send_text_status(struct unistimsession *pte, const char *text)
        BUFFSEND;
        int i;
        if (unistimdebug)
-               ast_verbose("Sending status text\n");
+               ast_verb(0, "Sending status text\n");
        if (pte->device) {
                if (pte->device->status_method == 1) {  /* For new firmware and i2050 soft phone */
                        int n = strlen(text);
@@ -1278,7 +1248,7 @@ static void send_led_update(struct unistimsession *pte, unsigned char led)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending led_update (%x)\n", led);
+               ast_verb(0, "Sending led_update (%x)\n", led);
        memcpy(buffsend + SIZE_HEADER, packet_send_led_update, sizeof(packet_send_led_update));
        buffsend[9] = led;
        send_client(SIZE_HEADER + sizeof(packet_send_led_update), buffsend, pte);
@@ -1293,7 +1263,7 @@ send_select_output(struct unistimsession *pte, unsigned char output, unsigned ch
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending select output packet output=%x volume=%x mute=%x\n", output,
+               ast_verb(0, "Sending select output packet output=%x volume=%x mute=%x\n", output,
                                        volume, mute);
        memcpy(buffsend + SIZE_HEADER, packet_send_select_output,
                   sizeof(packet_send_select_output));
@@ -1352,7 +1322,7 @@ static void send_ring(struct unistimsession *pte, char volume, char style)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending ring packet\n");
+               ast_verb(0, "Sending ring packet\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_ring, sizeof(packet_send_ring));
        buffsend[24] = style + 0x10;
        buffsend[29] = volume * 0x10;
@@ -1363,7 +1333,7 @@ static void send_no_ring(struct unistimsession *pte)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending no ring packet\n");
+               ast_verb(0, "Sending no ring packet\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_no_ring, sizeof(packet_send_no_ring));
        send_client(SIZE_HEADER + sizeof(packet_send_no_ring), buffsend, pte);
 }
@@ -1373,7 +1343,7 @@ static void send_texttitle(struct unistimsession *pte, const char *text)
        BUFFSEND;
        int i;
        if (unistimdebug)
-               ast_verbose("Sending title text\n");
+               ast_verb(0, "Sending title text\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_title, sizeof(packet_send_title));
        i = strlen(text);
        if (i > 12)
@@ -1383,80 +1353,58 @@ static void send_texttitle(struct unistimsession *pte, const char *text)
 
 }
 
-static void SendTest(struct unistimsession *pte)
-{
-       char text[TEXT_LENGTH_MAX];
-       int i;
-       if (unistimdebug)
-               ast_verbose("Sending test packet %x\n", flag);
-
-       sprintf(text, "first/last char0x%x/0x%x", flag, flag + TEXT_LENGTH_MAX - 1);
-       send_text(TEXT_LINE1, TEXT_NORMAL, pte, text);
-
-       for (i = 0; i < TEXT_LENGTH_MAX; i++) {
-               text[i] = flag++;
-       }
-       text[i] = '\0';
-       send_text(TEXT_LINE0, TEXT_NORMAL, pte, text);
-}
 static void send_date_time(struct unistimsession *pte)
 {
        BUFFSEND;
-       struct systemtime systime;
-       systime.w_year = systime.w_month = systime.w_day_of_week = 0;
-       systime.w_day = systime.w_hour = systime.w_minute = 0;
-       systime.w_second = systime.w_milliseconds = 0;
+       struct timeval tv = ast_tvnow();
+       struct ast_tm atm = { 0, };
 
        if (unistimdebug)
-               ast_verbose("Sending Time & Date\n");
+               ast_verb(0, "Sending Time & Date\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_date_time, sizeof(packet_send_date_time));
-       get_localtime(&systime);
-       buffsend[10] = (unsigned char) systime.w_month;
-       buffsend[11] = (unsigned char) systime.w_day;
-       buffsend[12] = (unsigned char) systime.w_hour;
-       buffsend[13] = (unsigned char) systime.w_minute;
+       ast_localtime(&tv, &atm, NULL);
+       buffsend[10] = (unsigned char) atm.tm_mon + 1;
+       buffsend[11] = (unsigned char) atm.tm_mday;
+       buffsend[12] = (unsigned char) atm.tm_hour;
+       buffsend[13] = (unsigned char) atm.tm_min;
        send_client(SIZE_HEADER + sizeof(packet_send_date_time), buffsend, pte);
 }
 
 static void send_date_time2(struct unistimsession *pte)
 {
        BUFFSEND;
-       struct systemtime systime;
-       systime.w_year = systime.w_month = systime.w_day_of_week = 0;
-       systime.w_day = systime.w_hour = systime.w_minute = 0;
-       systime.w_second = systime.w_milliseconds = 0;
+       struct timeval tv = ast_tvnow();
+       struct ast_tm atm = { 0, };
 
        if (unistimdebug)
-               ast_verbose("Sending Time & Date #2\n");
+               ast_verb(0, "Sending Time & Date #2\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_date_time2, sizeof(packet_send_date_time2));
-       get_localtime(&systime);
+       ast_localtime(&tv, &atm, NULL);
        if (pte->device)
                buffsend[9] = pte->device->datetimeformat;
        else
                buffsend[9] = 61;
-       buffsend[14] = (unsigned char) systime.w_month;
-       buffsend[15] = (unsigned char) systime.w_day;
-       buffsend[16] = (unsigned char) systime.w_hour;
-       buffsend[17] = (unsigned char) systime.w_minute;
+       buffsend[14] = (unsigned char) atm.tm_mon + 1;
+       buffsend[15] = (unsigned char) atm.tm_mday;
+       buffsend[16] = (unsigned char) atm.tm_hour;
+       buffsend[17] = (unsigned char) atm.tm_min;
        send_client(SIZE_HEADER + sizeof(packet_send_date_time2), buffsend, pte);
 }
 
 static void send_date_time3(struct unistimsession *pte)
 {
        BUFFSEND;
-       struct systemtime systime;
-       systime.w_year = systime.w_month = systime.w_day_of_week = 0;
-       systime.w_day = systime.w_hour = systime.w_minute = 0;
-       systime.w_second = systime.w_milliseconds = 0;
+       struct timeval tv = ast_tvnow();
+       struct ast_tm atm = { 0, };
 
        if (unistimdebug)
-               ast_verbose("Sending Time & Date #3\n");
+               ast_verb(0, "Sending Time & Date #3\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_date_time3, sizeof(packet_send_date_time3));
-       get_localtime(&systime);
-       buffsend[10] = (unsigned char) systime.w_month;
-       buffsend[11] = (unsigned char) systime.w_day;
-       buffsend[12] = (unsigned char) systime.w_hour;
-       buffsend[13] = (unsigned char) systime.w_minute;
+       ast_localtime(&tv, &atm, NULL);
+       buffsend[10] = (unsigned char) atm.tm_mon + 1;
+       buffsend[11] = (unsigned char) atm.tm_mday;
+       buffsend[12] = (unsigned char) atm.tm_hour;
+       buffsend[13] = (unsigned char) atm.tm_min;
        send_client(SIZE_HEADER + sizeof(packet_send_date_time3), buffsend, pte);
 }
 
@@ -1464,7 +1412,7 @@ static void send_blink_cursor(struct unistimsession *pte)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending set blink\n");
+               ast_verb(0, "Sending set blink\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_blink_cursor, sizeof(packet_send_blink_cursor));
        send_client(SIZE_HEADER + sizeof(packet_send_blink_cursor), buffsend, pte);
        return;
@@ -1475,7 +1423,7 @@ static void send_cursor_pos(struct unistimsession *pte, unsigned char pos)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending set cursor position\n");
+               ast_verb(0, "Sending set cursor position\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_set_pos_cursor,
                   sizeof(packet_send_set_pos_cursor));
        buffsend[11] = pos;
@@ -1486,10 +1434,10 @@ static void send_cursor_pos(struct unistimsession *pte, unsigned char pos)
 static void rcv_resume_connection_with_server(struct unistimsession *pte)
 {
        BUFFSEND;
-       if (unistimdebug)
-               ast_verbose("ResumeConnectionWithServer received\n");
-       if (unistimdebug)
-               ast_verbose("Sending packet_send_query_mac_address\n");
+       if (unistimdebug) {
+               ast_verb(0, "ResumeConnectionWithServer received\n");
+               ast_verb(0, "Sending packet_send_query_mac_address\n");
+       }
        memcpy(buffsend + SIZE_HEADER, packet_send_query_mac_address,
                   sizeof(packet_send_query_mac_address));
        send_client(SIZE_HEADER + sizeof(packet_send_query_mac_address), buffsend, pte);
@@ -1530,9 +1478,7 @@ static int alloc_sub(struct unistim_line *l, int x)
                return 0;
 
        if (unistimdebug)
-               ast_verbose(VERBOSE_PREFIX_3
-                                       "Allocating UNISTIM subchannel #%d on %s@%s ptr=%p\n", x, l->name,
-                                       l->parent->name, sub);
+               ast_verb(3, "Allocating UNISTIM subchannel #%d on %s@%s ptr=%p\n", x, l->name, l->parent->name, sub);
        sub->parent = l;
        sub->subtype = x;
        l->subs[x] = sub;
@@ -1563,13 +1509,13 @@ static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
        char addrmac[19];
        int res = 0;
        if (unistimdebug)
-               ast_verbose("Mac Address received : ");
+               ast_verb(0, "Mac Address received : ");
        for (tmp = 15; tmp < 15 + SIZE_HEADER; tmp++) {
                sprintf(&addrmac[i], "%.2x", (unsigned char) buf[tmp]);
                i += 2;
        }
        if (unistimdebug)
-               ast_verbose("%s\n", addrmac);
+               ast_verb(0, "%s\n", addrmac);
        strcpy(pte->macaddr, addrmac);
        res = unistim_register(pte);
        if (!res) {
@@ -1583,7 +1529,7 @@ static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
                                struct unistim_device *d, *newd;
                                struct unistim_line *newl;
                                if (unistimdebug)
-                                       ast_verbose("New phone, autoprovisioning on\n");
+                                       ast_verb(0, "New phone, autoprovisioning on\n");
                                /* First : locate the [template] section */
                                ast_mutex_lock(&devicelock);
                                d = devices;
@@ -1656,9 +1602,7 @@ static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
                }
        }
        if (pte->state != STATE_AUTHDENY) {
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfuly registered\n",
-                                               pte->device->name);
+               ast_verb(3, "Device '%s' successfuly registered\n", pte->device->name);
                switch (pte->device->extension) {
                case EXTENSION_NONE:
                        pte->state = STATE_MAINPAGE;
@@ -1700,18 +1644,18 @@ static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
                pte->device->extension_number[0] = '\0';
        }
        if (unistimdebug)
-               ast_verbose("\nSending S1\n");
+               ast_verb(0, "\nSending S1\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_S1, sizeof(packet_send_S1));
        send_client(SIZE_HEADER + sizeof(packet_send_S1), buffsend, pte);
 
        if (unistimdebug)
-               ast_verbose("Sending query_basic_manager_04\n");
+               ast_verb(0, "Sending query_basic_manager_04\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_04,
                   sizeof(packet_send_query_basic_manager_04));
        send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_04), buffsend, pte);
 
        if (unistimdebug)
-               ast_verbose("Sending query_basic_manager_10\n");
+               ast_verb(0, "Sending query_basic_manager_10\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_10,
                   sizeof(packet_send_query_basic_manager_10));
        send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_10), buffsend, pte);
@@ -1748,10 +1692,9 @@ static int write_history(struct unistimsession *pte, char way, char ismissed)
        char count = 0, *histbuf;
        int size;
        FILE *f, *f2;
-       struct systemtime systime;
-       systime.w_year = systime.w_month = systime.w_day_of_week = 0;
-       systime.w_day = systime.w_hour = systime.w_minute = 0;
-       systime.w_second = systime.w_milliseconds = 0;
+       struct timeval tv = ast_tvnow();
+       struct ast_tm atm = { 0, };
+
        if (!pte->device)
                return -1;
        if (!pte->device->callhistory)
@@ -1761,14 +1704,16 @@ static int write_history(struct unistimsession *pte, char way, char ismissed)
                                pte->device->name);
                return -1;
        }
-       snprintf(tmp, sizeof(tmp), "%s/%s", (char *) ast_config_AST_LOG_DIR, USTM_LOG_DIR);
-       if (mkdir(tmp, 0770)) {
+
+       snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, USTM_LOG_DIR);
+       if (ast_mkdir(tmp, 0770)) {
                if (errno != EEXIST) {
-                       display_last_error("Unable to create directory for history.");
+                       display_last_error("Unable to create directory for history");
                        return -1;
                }
        }
-       get_localtime(&systime);
+
+       ast_localtime(&tv, &atm, NULL);
        if (ismissed) {
                if (way == 'i')
                        strcpy(tmp2, "Miss");
@@ -1777,12 +1722,12 @@ static int write_history(struct unistimsession *pte, char way, char ismissed)
        } else
                strcpy(tmp2, "Answ");
        snprintf(line1, sizeof(line1), "%04d/%02d/%02d %02d:%02d:%02d %s",
-                        systime.w_year + 1900, systime.w_month, systime.w_day, systime.w_hour,
-                        systime.w_minute, systime.w_second, tmp2);
-       snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", (char *) ast_config_AST_LOG_DIR,
+                        atm.tm_year + 1900, atm.tm_mon + 1, atm.tm_mday, atm.tm_hour,
+                        atm.tm_min, atm.tm_sec, tmp2);
+
+       snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
                         USTM_LOG_DIR, pte->device->name, way);
-       f = fopen(tmp, "r");
-       if (f) {
+       if ((f = fopen(tmp, "r"))) {
                struct stat bufstat;
 
                if (stat(tmp, &bufstat)) {
@@ -1793,12 +1738,14 @@ static int write_history(struct unistimsession *pte, char way, char ismissed)
                size = 1 + (MAX_ENTRY_LOG * TEXT_LENGTH_MAX * 3);
                if (bufstat.st_size != size) {
                        ast_log(LOG_WARNING,
-                                       "History file %s have an incorrect size (%d instead of %d). It will be replaced by a new one.",
+                                       "History file %s has an incorrect size (%d instead of %d). It will be replaced by a new one.",
                                        tmp, (int) bufstat.st_size, size);
-                       f = 0;
+                       fclose(f);
+                       f = NULL;
                        count = 1;
                }
        }
+
        /* If we can't open the log file, we create a brand new one */
        if (!f) {
                char c = 1;
@@ -1841,28 +1788,30 @@ static int write_history(struct unistimsession *pte, char way, char ismissed)
                fclose(f);
                return -1;
        }
-       snprintf(tmp2, sizeof(tmp2), "%s/%s/%s-%c.csv.tmp", (char *) ast_config_AST_LOG_DIR,
+       snprintf(tmp2, sizeof(tmp2), "%s/%s/%s-%c.csv.tmp", ast_config_AST_LOG_DIR,
                         USTM_LOG_DIR, pte->device->name, way);
-       f2 = fopen(tmp2, "w");
-       if (!f2) {
+       if (!(f2 = fopen(tmp2, "w"))) {
                display_last_error("Unable to create temporary history log.");
                fclose(f);
                return -1;
        }
-       count++;
-       if (count > MAX_ENTRY_LOG)
+
+       if (++count > MAX_ENTRY_LOG)
                count = MAX_ENTRY_LOG;
+
        if (write_entry_history(pte, f2, count, line1)) {
                fclose(f);
                fclose(f2);
                return -1;
        }
+
        size = (MAX_ENTRY_LOG - 1) * TEXT_LENGTH_MAX * 3;
        if (!(histbuf = ast_malloc(size))) {
                fclose(f);
                fclose(f2);
                return -1;
        }
+
        if (fread(histbuf, size, 1, f) != 1) {
                ast_free(histbuf);
                fclose(f);
@@ -1905,7 +1854,7 @@ static void swap_subs(struct unistim_line *p, int a, int b)
        int fds;
 
        if (unistimdebug)
-               ast_verbose("Swapping %d and %d\n", a, b);
+               ast_verb(0, "Swapping %d and %d\n", a, b);
 
        if ((!p->subs[a]->owner) || (!p->subs[b]->owner)) {
                ast_log(LOG_WARNING,
@@ -2002,6 +1951,8 @@ void change_callerid(struct unistimsession *pte, int type, char *callerid)
        else
                data = pte->device->lst_cid;
 
+       /* This is very nearly strncpy(), except that the remaining buffer
+        * is padded with ' ', instead of '\0' */
        memset(data, ' ', TEXT_LENGTH_MAX);
        size = strlen(callerid);
        if (size > TEXT_LENGTH_MAX)
@@ -2021,17 +1972,17 @@ static void close_call(struct unistimsession *pte)
                if (l->subs[SUB_THREEWAY]) {
                        l->subs[SUB_THREEWAY]->alreadygone = 1;
                        if (attempt_transfer(sub, l->subs[SUB_THREEWAY]) < 0)
-                               ast_verbose("attempt_transfer failed.\n");
+                               ast_verb(0, "attempt_transfer failed.\n");
                } else
                        ast_queue_hangup(sub->owner);
        } else {
                if (l->subs[SUB_THREEWAY]) {
                        if (l->subs[SUB_THREEWAY]->owner)
-                               ast_queue_hangup(l->subs[SUB_THREEWAY]->owner);
+                               ast_queue_hangup_with_cause(l->subs[SUB_THREEWAY]->owner, AST_CAUSE_NORMAL_CLEARING);
                        else
                                ast_log(LOG_WARNING, "threeway sub without owner\n");
                } else
-                       ast_verbose("USTM(%s@%s-%d) channel already destroyed\n", sub->parent->name,
+                       ast_verb(0, "USTM(%s@%s-%d) channel already destroyed\n", sub->parent->name,
                                                sub->parent->parent->name, sub->subtype);
        }
        change_callerid(pte, 0, pte->device->redial_number);
@@ -2056,9 +2007,7 @@ static void *unistim_ss(void *data)
        struct unistimsession *s = l->parent->session;
        int res;
 
-       if (option_verbose > 2)
-               ast_verbose(VERBOSE_PREFIX_3 "Starting switch on '%s@%s-%d' to %s\n",
-                                       l->name, l->parent->name, sub->subtype, s->device->phone_number);
+       ast_verb(3, "Starting switch on '%s@%s-%d' to %s\n", l->name, l->parent->name, sub->subtype, s->device->phone_number);
        ast_copy_string(chan->exten, s->device->phone_number, sizeof(chan->exten));
        ast_copy_string(s->device->redial_number, s->device->phone_number,
                                        sizeof(s->device->redial_number));
@@ -2102,7 +2051,7 @@ static void start_rtp(struct unistim_subchannel *sub)
        ast_mutex_lock(&sub->lock);
        /* Allocate the RTP */
        if (unistimdebug)
-               ast_verbose("Starting RTP. Bind on %s\n", ast_inet_ntoa(sout.sin_addr));
+               ast_verb(0, "Starting RTP. Bind on %s\n", ast_inet_ntoa(sout.sin_addr));
        sub->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, sout.sin_addr);
        if (!sub->rtp) {
                ast_log(LOG_WARNING, "Unable to create RTP session: %s binaddr=%s\n",
@@ -2114,8 +2063,10 @@ static void start_rtp(struct unistim_subchannel *sub)
                sub->owner->fds[0] = ast_rtp_fd(sub->rtp);
                sub->owner->fds[1] = ast_rtcp_fd(sub->rtp);
        }
-       if (sub->rtp)
+       if (sub->rtp) {
+               ast_rtp_setqos(sub->rtp, tos_audio, cos_audio, "UNISTIM RTP");
                ast_rtp_setnat(sub->rtp, sub->parent->parent->nat);
+       }
 
        /* Create the RTP connection */
        ast_rtp_get_us(sub->rtp, &us);
@@ -2143,18 +2094,17 @@ static void start_rtp(struct unistim_subchannel *sub)
        else
                memcpy(&public, &public_ip, sizeof(public));    /* override  */
        if (unistimdebug) {
-               ast_verbose
-                       ("RTP started : Our IP/port is : %s:%hd with codec %s (%d)\n",
+               ast_verb(0, "RTP started : Our IP/port is : %s:%hd with codec %s (%d)\n",
                         ast_inet_ntoa(us.sin_addr),
                         htons(us.sin_port), ast_getformatname(sub->owner->readformat),
                         sub->owner->readformat);
-               ast_verbose("Starting phone RTP stack. Our public IP is %s\n",
+               ast_verb(0, "Starting phone RTP stack. Our public IP is %s\n",
                                        ast_inet_ntoa(public.sin_addr));
        }
        if ((sub->owner->readformat == AST_FORMAT_ULAW) ||
                (sub->owner->readformat == AST_FORMAT_ALAW)) {
                if (unistimdebug)
-                       ast_verbose("Sending packet_send_rtp_packet_size for codec %d\n", codec);
+                       ast_verb(0, "Sending packet_send_rtp_packet_size for codec %d\n", codec);
                memcpy(buffsend + SIZE_HEADER, packet_send_rtp_packet_size,
                           sizeof(packet_send_rtp_packet_size));
                buffsend[10] = codec;
@@ -2162,7 +2112,7 @@ static void start_rtp(struct unistim_subchannel *sub)
                                   sub->parent->parent->session);
        }
        if (unistimdebug)
-               ast_verbose("Sending Jitter Buffer Parameters Configuration\n");
+               ast_verb(0, "Sending Jitter Buffer Parameters Configuration\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_jitter_buffer_conf,
                   sizeof(packet_send_jitter_buffer_conf));
        send_client(SIZE_HEADER + sizeof(packet_send_jitter_buffer_conf), buffsend,
@@ -2171,7 +2121,7 @@ static void start_rtp(struct unistim_subchannel *sub)
                uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
 
                if (unistimdebug)
-                       ast_verbose("Sending OpenAudioStreamTX using method #%d\n",
+                       ast_verb(0, "Sending OpenAudioStreamTX using method #%d\n",
                                                sub->parent->parent->rtp_method);
                if (sub->parent->parent->rtp_method == 3)
                        memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx3,
@@ -2202,7 +2152,7 @@ static void start_rtp(struct unistim_subchannel *sub)
                                   sub->parent->parent->session);
 
                if (unistimdebug)
-                       ast_verbose("Sending OpenAudioStreamRX\n");
+                       ast_verb(0, "Sending OpenAudioStreamRX\n");
                if (sub->parent->parent->rtp_method == 3)
                        memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx3,
                                   sizeof(packet_send_open_audio_stream_rx3));
@@ -2234,7 +2184,7 @@ static void start_rtp(struct unistim_subchannel *sub)
                uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
 
                if (unistimdebug)
-                       ast_verbose("Sending packet_send_call default method\n");
+                       ast_verb(0, "Sending packet_send_call default method\n");
 
                memcpy(buffsend + SIZE_HEADER, packet_send_call, sizeof(packet_send_call));
                memcpy(buffsend + 53, &public.sin_addr, sizeof(public.sin_addr));
@@ -2275,13 +2225,13 @@ static void SendDialTone(struct unistimsession *pte)
        /* No country defined ? Using US tone */
        if (ast_strlen_zero(pte->device->country)) {
                if (unistimdebug)
-                       ast_verbose("No country defined, using US tone\n");
+                       ast_verb(0, "No country defined, using US tone\n");
                send_tone(pte, 350, 440);
                return;
        }
        if (strlen(pte->device->country) != 2) {
                if (unistimdebug)
-                       ast_verbose("Country code != 2 char, using US tone\n");
+                       ast_verb(0, "Country code != 2 char, using US tone\n");
                send_tone(pte, 350, 440);
                return;
        }
@@ -2290,7 +2240,7 @@ static void SendDialTone(struct unistimsession *pte)
                if ((frequency[i].country[0] == pte->device->country[0]) &&
                        (frequency[i].country[1] == pte->device->country[1])) {
                        if (unistimdebug)
-                               ast_verbose("Country code found (%s), freq1=%d freq2=%d\n",
+                               ast_verb(0, "Country code found (%s), freq1=%d freq2=%d\n",
                                                        frequency[i].country, frequency[i].freq1, frequency[i].freq2);
                        send_tone(pte, frequency[i].freq1, frequency[i].freq2);
                }
@@ -2360,9 +2310,9 @@ static void TransferCallStep1(struct unistimsession *pte)
        }
        if (p->subs[SUB_THREEWAY]) {
                if (unistimdebug)
-                       ast_verbose("Transfer canceled, hangup our threeway channel\n");
+                       ast_verb(0, "Transfer canceled, hangup our threeway channel\n");
                if (p->subs[SUB_THREEWAY]->owner)
-                       ast_queue_hangup(p->subs[SUB_THREEWAY]->owner);
+                       ast_queue_hangup_with_cause(p->subs[SUB_THREEWAY]->owner, AST_CAUSE_NORMAL_CLEARING);
                else
                        ast_log(LOG_WARNING, "Canceling a threeway channel without owner\n");
                return;
@@ -2387,7 +2337,7 @@ static void TransferCallStep1(struct unistimsession *pte)
                if (pte->device->silence_generator == NULL)
                        ast_log(LOG_WARNING, "Unable to start a silence generator.\n");
                else if (unistimdebug)
-                       ast_verbose("Starting silence generator\n");
+                       ast_verb(0, "Starting silence generator\n");
        }
        handle_dial_page(pte);
 }
@@ -2418,7 +2368,7 @@ static void HandleCallOutgoing(struct unistimsession *s)
                        /* start switch */
                        if (ast_pthread_create(&t, NULL, unistim_ss, c)) {
                                display_last_error("Unable to create switch thread");
-                               ast_queue_hangup(c);
+                               ast_queue_hangup_with_cause(c, AST_CAUSE_SWITCH_CONGESTION);
                        }
                } else
                        ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n",
@@ -2446,7 +2396,7 @@ static void HandleCallOutgoing(struct unistimsession *s)
                        /* Stop the silence generator */
                        if (s->device->silence_generator) {
                                if (unistimdebug)
-                                       ast_verbose("Stopping silence generator\n");
+                                       ast_verb(0, "Stopping silence generator\n");
                                ast_channel_stop_silence_generator(sub->owner,
                                                                                                   s->device->silence_generator);
                                s->device->silence_generator = NULL;
@@ -2472,8 +2422,7 @@ static void HandleCallOutgoing(struct unistimsession *s)
                                return;
                        }
                        if (unistimdebug)
-                               ast_verbose
-                                       ("Started three way call on channel %p (%s) subchan %d\n",
+                               ast_verb(0, "Started three way call on channel %p (%s) subchan %d\n",
                                         p->subs[SUB_THREEWAY]->owner, p->subs[SUB_THREEWAY]->owner->name,
                                         p->subs[SUB_THREEWAY]->subtype);
                } else
@@ -2494,7 +2443,7 @@ static void HandleCallIncoming(struct unistimsession *s)
                ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
                return;
        } else if (unistimdebug)
-               ast_verbose("Handle Call Incoming for %s@%s\n", sub->parent->name,
+               ast_verb(0, "Handle Call Incoming for %s@%s\n", sub->parent->name,
                                        s->device->name);
        start_rtp(sub);
        if (!sub->rtp)
@@ -2526,7 +2475,7 @@ static int unistim_do_senddigit(struct unistimsession *pte, char digit)
                return -1;
        }
        if (unistimdebug)
-               ast_verbose("Send Digit %c\n", digit);
+               ast_verb(0, "Send Digit %c\n", digit);
        switch (digit) {
        case '0':
                send_tone(pte, 941, 1336);
@@ -2767,7 +2716,7 @@ static void key_dial_page(struct unistimsession *pte, char keycode)
                        /* Stop the silence generator */
                        if (pte->device->silence_generator) {
                                if (unistimdebug)
-                                       ast_verbose("Stopping silence generator\n");
+                                       ast_verb(0, "Stopping silence generator\n");
                                ast_channel_stop_silence_generator(pte->device->lines->subs[SUB_REAL]->
                                                                                                   owner, pte->device->silence_generator);
                                pte->device->silence_generator = NULL;
@@ -3033,14 +2982,14 @@ static void show_entry_history(struct unistimsession *pte, FILE ** f)
                fclose(*f);
                return;
        }
-       line[TEXT_LENGTH_MAX + 1] = 0;
+       line[sizeof(line) - 1] = '\0';
        send_text(TEXT_LINE0, TEXT_NORMAL, pte, line);
        if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
                display_last_error("Can't read callerid entry");
                fclose(*f);
                return;
        }
-       line[TEXT_LENGTH_MAX + 1] = 0;
+       line[sizeof(line) - 1] = '\0';
        ast_copy_string(pte->device->lst_cid, line, sizeof(pte->device->lst_cid));
        send_text(TEXT_LINE1, TEXT_NORMAL, pte, line);
        if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
@@ -3048,7 +2997,7 @@ static void show_entry_history(struct unistimsession *pte, FILE ** f)
                fclose(*f);
                return;
        }
-       line[TEXT_LENGTH_MAX + 1] = 0;
+       line[sizeof(line) - 1] = '\0';
        send_text(TEXT_LINE2, TEXT_NORMAL, pte, line);
        fclose(*f);
 
@@ -3077,7 +3026,7 @@ static char OpenHistory(struct unistimsession *pte, char way, FILE ** f)
        char tmp[AST_CONFIG_MAX_PATH];
        char count;
 
-       snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", (char *) ast_config_AST_LOG_DIR,
+       snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
                         USTM_LOG_DIR, pte->device->name, way);
        *f = fopen(tmp, "r");
        if (!*f) {
@@ -3332,30 +3281,30 @@ static void init_phone_step2(struct unistimsession *pte)
 {
        BUFFSEND;
        if (unistimdebug)
-               ast_verbose("Sending S4\n");
+               ast_verb(0, "Sending S4\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_s4, sizeof(packet_send_s4));
        send_client(SIZE_HEADER + sizeof(packet_send_s4), buffsend, pte);
        send_date_time2(pte);
        send_date_time3(pte);
        if (unistimdebug)
-               ast_verbose("Sending S7\n");
+               ast_verb(0, "Sending S7\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_S7, sizeof(packet_send_S7));
        send_client(SIZE_HEADER + sizeof(packet_send_S7), buffsend, pte);
        if (unistimdebug)
-               ast_verbose("Sending Contrast\n");
+               ast_verb(0, "Sending Contrast\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_Contrast, sizeof(packet_send_Contrast));
        if (pte->device != NULL)
                buffsend[9] = pte->device->contrast;
        send_client(SIZE_HEADER + sizeof(packet_send_Contrast), buffsend, pte);
 
        if (unistimdebug)
-               ast_verbose("Sending S9\n");
+               ast_verb(0, "Sending S9\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_s9, sizeof(packet_send_s9));
        send_client(SIZE_HEADER + sizeof(packet_send_s9), buffsend, pte);
        send_no_ring(pte);
 
        if (unistimdebug)
-               ast_verbose("Sending S7\n");
+               ast_verb(0, "Sending S7\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_S7, sizeof(packet_send_S7));
        send_client(SIZE_HEADER + sizeof(packet_send_S7), buffsend, pte);
        send_led_update(pte, 0);
@@ -3383,7 +3332,7 @@ static void init_phone_step2(struct unistimsession *pte)
        show_main_page(pte);
        refresh_all_favorite(pte);
        if (unistimdebug)
-               ast_verbose("Sending arrow\n");
+               ast_verb(0, "Sending arrow\n");
        memcpy(buffsend + SIZE_HEADER, packet_send_arrow, sizeof(packet_send_arrow));
        send_client(SIZE_HEADER + sizeof(packet_send_arrow), buffsend, pte);
        return;
@@ -3402,7 +3351,7 @@ static void process_request(int size, unsigned char *buf, struct unistimsession
                0) {
                buf[size] = 0;
                if (unistimdebug)
-                       ast_verbose("Got the firmware version : '%s'\n", buf + 13);
+                       ast_verb(0, "Got the firmware version : '%s'\n", buf + 13);
                init_phone_step2(pte);
                return;
        }
@@ -3412,35 +3361,30 @@ static void process_request(int size, unsigned char *buf, struct unistimsession
        }
        if (memcmp(buf + SIZE_HEADER, packet_recv_r2, sizeof(packet_recv_r2)) == 0) {
                if (unistimdebug)
-                       ast_verbose("R2 received\n");
+                       ast_verb(0, "R2 received\n");
                return;
        }
 
        if (pte->state < STATE_MAINPAGE) {
                if (unistimdebug)
-                       ast_verbose("Request not authorized in this state\n");
+                       ast_verb(0, "Request not authorized in this state\n");
                return;
        }
        if (!memcmp(buf + SIZE_HEADER, packet_recv_pressed_key, sizeof(packet_recv_pressed_key))) {
                char keycode = buf[13];
 
                if (unistimdebug)
-                       ast_verbose("Key pressed : keycode = 0x%.2x - current state : %d\n", keycode,
+                       ast_verb(0, "Key pressed : keycode = 0x%.2x - current state : %d\n", keycode,
                                                pte->state);
-               /* test key */
-               if (keycode == KEY_COMPUTR) {
-                       SendTest(pte);
-                       return;
-               }
-               /* End of the test section */
+
                switch (pte->state) {
                case STATE_INIT:
                        if (unistimdebug)
-                               ast_verbose("No keys allowed in the init state\n");
+                               ast_verb(0, "No keys allowed in the init state\n");
                        break;
                case STATE_AUTHDENY:
                        if (unistimdebug)
-                               ast_verbose("No keys allowed in authdeny state\n");
+                               ast_verb(0, "No keys allowed in authdeny state\n");
                        break;
                case STATE_MAINPAGE:
                        key_main_page(pte, keycode);
@@ -3470,7 +3414,7 @@ static void process_request(int size, unsigned char *buf, struct unistimsession
        }
        if (memcmp(buf + SIZE_HEADER, packet_recv_pick_up, sizeof(packet_recv_pick_up)) == 0) {
                if (unistimdebug)
-                       ast_verbose("Handset off hook\n");
+                       ast_verb(0, "Handset off hook\n");
                if (!pte->device)              /* We are not yet registred (asking for a TN in AUTOPROVISIONING_TN) */
                        return;
                pte->device->receiver_state = STATE_OFFHOOK;
@@ -3492,7 +3436,7 @@ static void process_request(int size, unsigned char *buf, struct unistimsession
        }
        if (memcmp(buf + SIZE_HEADER, packet_recv_hangup, sizeof(packet_recv_hangup)) == 0) {
                if (unistimdebug)
-                       ast_verbose("Handset on hook\n");
+                       ast_verb(0, "Handset on hook\n");
                if (!pte->device)
                        return;
                pte->device->receiver_state = STATE_ONHOOK;
@@ -3536,11 +3480,11 @@ static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
                } else {
                        if (memcmp(buf, packet_rcv_discovery, sizeof(packet_rcv_discovery)) == 0) {
                                if (unistimdebug)
-                                       ast_verbose("Discovery packet received - Sending Discovery ACK\n");
+                                       ast_verb(0, "Discovery packet received - Sending Discovery ACK\n");
                                if (pte) {            /* A session was already active for this IP ? */
                                        if (pte->state == STATE_INIT) { /* Yes, but it's a dupe */
                                                if (unistimdebug)
-                                                       ast_verbose("Duplicated Discovery packet\n");
+                                                       ast_verb(1, "Duplicated Discovery packet\n");
                                                send_raw_client(sizeof(packet_send_discovery_ack),
                                                                          packet_send_discovery_ack, addr_from, &pte->sout);
                                                pte->seq_phone = (short) 0x0000;        /* reset sequence number */
@@ -3564,7 +3508,7 @@ static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
        }
        if (!pte) {
                if (unistimdebug)
-                       ast_verbose("%s Not a discovery packet from an unknown source : ignoring\n",
+                       ast_verb(0, "%s Not a discovery packet from an unknown source : ignoring\n",
                                                tmpbuf);
                return;
        }
@@ -3581,8 +3525,8 @@ static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
        seq = ntohs(sbuf[1]);
        if (buf[4] == 1) {
                ast_mutex_lock(&pte->lock);
-               if ((unistimdebug) && (option_verbose > 5))
-                       ast_verbose("ACK received for packet #0x%.4x\n", seq);
+               if (unistimdebug)
+                       ast_verb(6, "ACK received for packet #0x%.4x\n", seq);
                pte->nb_retransmit = 0;
 
                if ((pte->last_seq_ack) + 1 == seq) {
@@ -3593,7 +3537,7 @@ static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
                }
                if (pte->last_seq_ack > seq) {
                        if (pte->last_seq_ack == 0xffff) {
-                               ast_verbose("ACK at 0xffff, restarting counter.\n");
+                               ast_verb(0, "ACK at 0xffff, restarting counter.\n");
                                pte->last_seq_ack = 0;
                        } else
                                ast_log(LOG_NOTICE,
@@ -3604,13 +3548,13 @@ static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
                }
                if (pte->seq_server < seq) {
                        ast_log(LOG_NOTICE,
-                                       "%s Error : ACK received for a non-existant packet : #0x%.4x\n",
+                                       "%s Error : ACK received for a non-existent packet : #0x%.4x\n",
                                        tmpbuf, pte->seq_server);
                        ast_mutex_unlock(&pte->lock);
                        return;
                }
                if (unistimdebug)
-                       ast_verbose("%s ACK gap : Received ACK #0x%.4x, previous was #0x%.4x\n",
+                       ast_verb(0, "%s ACK gap : Received ACK #0x%.4x, previous was #0x%.4x\n",
                                                tmpbuf, seq, pte->last_seq_ack);
                pte->last_seq_ack = seq;
                check_send_queue(pte);
@@ -3619,7 +3563,7 @@ static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
        }
        if (buf[4] == 2) {
                if (unistimdebug)
-                       ast_verbose("Request received\n");
+                       ast_verb(0, "Request received\n");
                if (pte->seq_phone == seq) {
                        /* Send ACK */
                        buf[4] = 1;
@@ -3656,7 +3600,7 @@ static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
                }
                if (pte->seq_server < seq) {
                        ast_log(LOG_NOTICE,
-                                       "%s Error : received a request for a non-existant packet : #0x%.4x\n",
+                                       "%s Error : received a request for a non-existent packet : #0x%.4x\n",
                                        tmpbuf, pte->seq_server);
                        return;
                }
@@ -3718,7 +3662,7 @@ static int unistim_call(struct ast_channel *ast, char *dest, int timeout)
        }
 
        if (unistimdebug)
-               ast_verbose(VERBOSE_PREFIX_3 "unistim_call(%s)\n", ast->name);
+               ast_verb(3, "unistim_call(%s)\n", ast->name);
 
        session->state = STATE_RINGING;
        Sendicon(TEXT_LINE0, FAV_ICON_NONE, session);
@@ -3775,7 +3719,7 @@ static int unistim_hangup(struct ast_channel *ast)
                ast_mutex_unlock(&sub->lock);
                if (sub->rtp) {
                        if (unistimdebug)
-                               ast_verbose("Destroying RTP session\n");
+                               ast_verb(0, "Destroying RTP session\n");
                        ast_rtp_destroy(sub->rtp);
                        sub->rtp = NULL;
                }
@@ -3783,11 +3727,11 @@ static int unistim_hangup(struct ast_channel *ast)
        }
        l = sub->parent;
        if (unistimdebug)
-               ast_verbose("unistim_hangup(%s) on %s@%s\n", ast->name, l->name, l->parent->name);
+               ast_verb(0, "unistim_hangup(%s) on %s@%s\n", ast->name, l->name, l->parent->name);
 
        if ((l->subs[SUB_THREEWAY]) && (sub->subtype == SUB_REAL)) {
                if (unistimdebug)
-                       ast_verbose("Real call disconnected while talking to threeway\n");
+                       ast_verb(0, "Real call disconnected while talking to threeway\n");
                sub->owner = NULL;
                ast->tech_pvt = NULL;
                return 0;
@@ -3795,7 +3739,7 @@ static int unistim_hangup(struct ast_channel *ast)
        if ((l->subs[SUB_REAL]->owner) && (sub->subtype == SUB_THREEWAY) &&
                (sub->alreadygone == 0)) {
                if (unistimdebug)
-                       ast_verbose("threeway call disconnected, switching to real call\n");
+                       ast_verb(0, "threeway call disconnected, switching to real call\n");
                send_text(TEXT_LINE0, TEXT_NORMAL, s, "Three way call canceled,");
                send_text(TEXT_LINE1, TEXT_NORMAL, s, "switching back to");
                send_text(TEXT_LINE2, TEXT_NORMAL, s, "previous call.");
@@ -3817,10 +3761,10 @@ static int unistim_hangup(struct ast_channel *ast)
        ast_mutex_unlock(&sub->lock);
        if (!s) {
                if (unistimdebug)
-                       ast_verbose("Asked to hangup channel not connected (no session)\n");
+                       ast_verb(0, "Asked to hangup channel not connected (no session)\n");
                if (sub->rtp) {
                        if (unistimdebug)
-                               ast_verbose("Destroying RTP session\n");
+                               ast_verb(0, "Destroying RTP session\n");
                        ast_rtp_destroy(sub->rtp);
                        sub->rtp = NULL;
                }
@@ -3830,7 +3774,7 @@ static int unistim_hangup(struct ast_channel *ast)
                /* Stop the silence generator */
                if (s->device->silence_generator) {
                        if (unistimdebug)
-                               ast_verbose("Stopping silence generator\n");
+                               ast_verb(0, "Stopping silence generator\n");
                        if (sub->owner)
                                ast_channel_stop_silence_generator(sub->owner,
                                                                                                   s->device->silence_generator);
@@ -3845,14 +3789,14 @@ static int unistim_hangup(struct ast_channel *ast)
        send_end_call(s);
        if (sub->rtp) {
                if (unistimdebug)
-                       ast_verbose("Destroying RTP session\n");
+                       ast_verb(0, "Destroying RTP session\n");
                ast_rtp_destroy(sub->rtp);
                sub->rtp = NULL;
        } else if (unistimdebug)
-               ast_verbose("No RTP session to destroy\n");
+               ast_verb(0, "No RTP session to destroy\n");
        if (l->subs[SUB_THREEWAY]) {
                if (unistimdebug)
-                       ast_verbose("Cleaning other subchannels\n");
+                       ast_verb(0, "Cleaning other subchannels\n");
                unalloc_sub(l, SUB_THREEWAY);
        }
        if (s->state == STATE_RINGING)
@@ -3882,7 +3826,7 @@ static int unistim_answer(struct ast_channel *ast)
        if ((!sub->rtp) && (!l->subs[SUB_THREEWAY]))
                start_rtp(sub);
        if (unistimdebug)
-               ast_verbose("unistim_answer(%s) on %s@%s-%d\n", ast->name, l->name,
+               ast_verb(0, "unistim_answer(%s) on %s@%s-%d\n", ast->name, l->name,
                                        l->parent->name, sub->subtype);
        send_text(TEXT_LINE2, TEXT_NORMAL, l->parent->session, "is now on-line");
        if (l->subs[SUB_THREEWAY])
@@ -3935,17 +3879,17 @@ static int unistimsock_read(int *id, int fd, short events, void *ignore)
 
 #ifdef DUMP_PACKET
        if (unistimdebug)
-               ast_verbose("\n*** Dump %d bytes from %s - phone_table[%d] ***\n",
+               ast_verb(0, "\n*** Dump %d bytes from %s - phone_table[%d] ***\n",
                                        dw_num_bytes_rcvd, ast_inet_ntoa(addr_from.sin_addr), tmp);
        for (dw_num_bytes_rcvdd = 0; dw_num_bytes_rcvdd < dw_num_bytes_rcvd;
                 dw_num_bytes_rcvdd++)
-               ast_verbose("%.2x ", (unsigned char) buff[dw_num_bytes_rcvdd]);
-       ast_verbose("\n******************************************\n");
+               ast_verb(0, "%.2x ", (unsigned char) buff[dw_num_bytes_rcvdd]);
+       ast_verb(0, "\n******************************************\n");
 #endif
 
        if (!found) {
                if (unistimdebug)
-                       ast_verbose("Received a packet from an unknown source\n");
+                       ast_verb(0, "Received a packet from an unknown source\n");
                parsing(dw_num_bytes_rcvd, buff, NULL, (struct sockaddr_in *) &addr_from);
 
        } else
@@ -4130,7 +4074,7 @@ static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
        struct unistimsession *s;
 
        if (unistimdebug) {
-               ast_verbose(VERBOSE_PREFIX_3 "Asked to indicate '%s' condition on channel %s\n",
+               ast_verb(3, "Asked to indicate '%s' condition on channel %s\n",
                                        control2str(ind), ast->name);
        }
 
@@ -4175,6 +4119,7 @@ static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
                ast_moh_stop(ast);
                break;
        case AST_CONTROL_PROGRESS:
+       case AST_CONTROL_SRCUPDATE:
                break;
        case -1:
                ast_playtones_stop(ast);
@@ -4215,7 +4160,7 @@ static struct unistim_subchannel *find_subchannel_by_name(const char *dest)
        while (d) {
                if (!strcasecmp(d->name, device)) {
                        if (unistimdebug)
-                               ast_verbose("Found device: %s\n", d->name);
+                               ast_verb(0, "Found device: %s\n", d->name);
                        /* Found the device */
                        l = d->lines;
                        while (l) {
@@ -4236,8 +4181,7 @@ static struct unistim_subchannel *find_subchannel_by_name(const char *dest)
                                                                if ((*at >= '0') && (*at <= '3'))       /* ring volume */
                                                                        ring_volume = *at - '0';
                                                                if (unistimdebug)
-                                                                       ast_verbose
-                                                                               ("Distinctive ring : style #%d volume %d\n",
+                                                                       ast_verb(0, "Distinctive ring : style #%d volume %d\n",
                                                                                 ring_style, ring_volume);
                                                                l->subs[SUB_REAL]->ringvolume = ring_volume;
                                                                l->subs[SUB_REAL]->ringstyle = ring_style;
@@ -4282,7 +4226,7 @@ static int unistim_senddigit_end(struct ast_channel *ast, char digit, unsigned i
        }
 
        if (unistimdebug)
-               ast_verbose("Send Digit off %c\n", digit);
+               ast_verb(0, "Send Digit off %c\n", digit);
 
        if (!pte)
                return -1;
@@ -4305,7 +4249,7 @@ static int unistim_sendtext(struct ast_channel *ast, const char *text)
        char tmp[TEXT_LENGTH_MAX + 1];
 
        if (unistimdebug)
-               ast_verbose("unistim_sendtext called\n");
+               ast_verb(0, "unistim_sendtext called\n");
 
        if (!text) {
                ast_log(LOG_WARNING, "unistim_sendtext called with a null text\n");
@@ -4413,16 +4357,16 @@ static int unistim_sendtext(struct ast_channel *ast, const char *text)
                        return 0;
                }
                memcpy(tmp, text + TEXT_LENGTH_MAX, TEXT_LENGTH_MAX);
-               tmp[TEXT_LENGTH_MAX + 1] = '\0';
+               tmp[sizeof(tmp) - 1] = '\0';
                send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
                return 0;
        }
        send_text(TEXT_LINE0, TEXT_NORMAL, pte, text);
        memcpy(tmp, text + TEXT_LENGTH_MAX, TEXT_LENGTH_MAX);
-       tmp[TEXT_LENGTH_MAX + 1] = '\0';
+       tmp[sizeof(tmp) - 1] = '\0';
        send_text(TEXT_LINE1, TEXT_NORMAL, pte, tmp);
        memcpy(tmp, text + TEXT_LENGTH_MAX * 2, TEXT_LENGTH_MAX);
-       tmp[TEXT_LENGTH_MAX + 1] = '\0';
+       tmp[sizeof(tmp) - 1] = '\0';
        send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
        return 0;
 }
@@ -4431,7 +4375,7 @@ static int unistim_sendtext(struct ast_channel *ast, const char *text)
 static int unistim_send_mwi_to_peer(struct unistimsession *s, unsigned int tick)
 {
        struct ast_event *event;
-       int new, old;
+       int new;
        char *mailbox, *context;
        struct unistim_line *peer = s->device->lines;
 
@@ -4444,27 +4388,28 @@ static int unistim_send_mwi_to_peer(struct unistimsession *s, unsigned int tick)
                AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
                AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
                AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
-               AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
                AST_EVENT_IE_END);
 
        if (event) {
                new = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
-               old = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
                ast_event_destroy(event);
-       } else /* Fall back on checking the mailbox directly */
-               ast_app_inboxcount(peer->mailbox, &new, &old);
+       } else { /* Fall back on checking the mailbox directly */
+               new = ast_app_has_voicemail(peer->mailbox, "INBOX");
+       }
 
        peer->nextmsgcheck = tick + TIMER_MWI;
 
        /* Return now if it's the same thing we told them last time */
-       if (((new << 8) | (old)) == peer->lastmsgssent)
+       if (new == peer->lastmsgssent) {
                return 0;
+       }
 
-       peer->lastmsgssent = ((new << 8) | (old));
-       if (new == 0)
+       peer->lastmsgssent = new;
+       if (new == 0) {
                send_led_update(s, 0);
-       else
+       } else {
                send_led_update(s, 1);
+       }
 
        return 0;
 }
@@ -4489,7 +4434,7 @@ static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state
        tmp = ast_channel_alloc(1, state, l->cid_num, NULL, l->accountcode, l->exten, 
                l->context, l->amaflags, "%s-%08x", l->fullname, (int) (long) sub);
        if (unistimdebug)
-               ast_verbose("unistim_new sub=%d (%p) chan=%p\n", sub->subtype, sub, tmp);
+               ast_verb(0, "unistim_new sub=%d (%p) chan=%p\n", sub->subtype, sub, tmp);
        if (!tmp) {
                ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
                return NULL;
@@ -4500,14 +4445,13 @@ static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state
                tmp->nativeformats = CAPABILITY;
        fmt = ast_best_codec(tmp->nativeformats);
        if (unistimdebug)
-               ast_verbose
-                       ("Best codec = %d from nativeformats %d (line cap=%d global=%d)\n", fmt,
+               ast_verb(0, "Best codec = %d from nativeformats %d (line cap=%d global=%d)\n", fmt,
                         tmp->nativeformats, l->capability, CAPABILITY);
        ast_string_field_build(tmp, name, "USTM/%s@%s-%d", l->name, l->parent->name,
                                                   sub->subtype);
        if ((sub->rtp) && (sub->subtype == 0)) {
                if (unistimdebug)
-                       ast_verbose("New unistim channel with a previous rtp handle ?\n");
+                       ast_verb(0, "New unistim channel with a previous rtp handle ?\n");
                tmp->fds[0] = ast_rtp_fd(sub->rtp);
                tmp->fds[1] = ast_rtcp_fd(sub->rtp);
        }
@@ -4518,6 +4462,7 @@ static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state
        ast_setstate(tmp, state);
        if (state == AST_STATE_RING)
                tmp->rings = 1;
+       tmp->adsicpe = AST_ADSI_UNAVAILABLE;
        tmp->writeformat = fmt;
        tmp->rawwriteformat = fmt;
        tmp->readformat = fmt;
@@ -4547,7 +4492,7 @@ static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state
        tmp->priority = 1;
        if (state != AST_STATE_DOWN) {
                if (unistimdebug)
-                       ast_verbose("Starting pbx in unistim_new\n");
+                       ast_verb(0, "Starting pbx in unistim_new\n");
                if (ast_pbx_start(tmp)) {
                        ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
                        ast_hangup(tmp);
@@ -4624,10 +4569,10 @@ static void *do_monitor(void *data)
                unistim_reloading = 0;
                ast_mutex_unlock(&unistim_reload_lock);
                if (reloading) {
-                       if (option_verbose > 0)
-                               ast_verbose(VERBOSE_PREFIX_1 "Reloading unistim.conf...\n");
+                       ast_verb(1, "Reloading unistim.conf...\n");
                        reload_config();
                }
+               pthread_testcancel();
        }
        /* Never reached */
        return NULL;
@@ -4654,7 +4599,7 @@ static int restart_monitor(void)
                pthread_kill(monitor_thread, SIGURG);
        } else {
                pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
                /* Start a new monitor */
                if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
                        ast_mutex_unlock(&monlock);
@@ -4702,12 +4647,11 @@ static struct ast_channel *unistim_request(const char *type, int format, void *d
                return NULL;
        }
 
-       if (option_verbose > 2)
-               ast_verbose(VERBOSE_PREFIX_3 "unistim_request(%s)\n", tmp);
+       ast_verb(3, "unistim_request(%s)\n", tmp);
        /* Busy ? */
        if (sub->owner) {
                if (unistimdebug)
-                       ast_verbose("Can't create channel : Busy !\n");
+                       ast_verb(0, "Can't create channel : Busy !\n");
                *cause = AST_CAUSE_BUSY;
                return NULL;
        }
@@ -4716,7 +4660,7 @@ static struct ast_channel *unistim_request(const char *type, int format, void *d
        if (!tmpc)
                ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
        if (unistimdebug)
-               ast_verbose("unistim_request owner = %p\n", sub->owner);
+               ast_verb(0, "unistim_request owner = %p\n", sub->owner);
        restart_monitor();
 
        /* and finish */
@@ -4904,7 +4848,7 @@ static char *unistim_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
                return CLI_SHOWUSAGE;
 
        if (unistimdebug)
-               ast_verbose("reload unistim\n");
+               ast_verb(0, "reload unistim\n");
 
        ast_mutex_lock(&unistim_reload_lock);
        if (!unistim_reloading)
@@ -5017,7 +4961,7 @@ static int ParseBookmark(const char *text, struct unistim_device *d)
        ast_copy_string(d->softkeylabel[p], line, sizeof(d->softkeylabel[p]));
        ast_copy_string(d->softkeynumber[p], number, sizeof(d->softkeynumber[p]));
        if (unistimdebug)
-               ast_verbose("New bookmark at pos %d label='%s' number='%s' icon=%x\n",
+               ast_verb(0, "New bookmark at pos %d label='%s' number='%s' icon=%x\n",
                                        p, d->softkeylabel[p], d->softkeynumber[p], d->softkeyicon[p]);
        return 1;
 }
@@ -5170,6 +5114,8 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
                        l->pickupgroup = ast_get_group(v->value);
                else if (!strcasecmp(v->name, "mailbox"))
                        ast_copy_string(l->mailbox, v->value, sizeof(l->mailbox));
+               else if (!strcasecmp(v->name, "parkinglot"))
+                       ast_copy_string(l->parkinglot, v->value, sizeof(l->parkinglot));
                else if (!strcasecmp(v->name, "linelabel"))
                        unquote(linelabel, v->value, sizeof(linelabel) - 1);
                else if (!strcasecmp(v->name, "extension")) {
@@ -5239,8 +5185,7 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
                        ast_copy_string(l->context, context, sizeof(l->context));
                        if (!ast_strlen_zero(l->mailbox)) {
                                if (unistimdebug)
-                                       ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n",
-                                                               l->mailbox, d->name, l->name);
+                                       ast_verb(3, "Setting mailbox '%s' on %s@%s\n", l->mailbox, d->name, l->name);
                        }
 
                        l->capability = CAPABILITY;
@@ -5305,21 +5250,21 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
        if (ast_strlen_zero(d->maintext1))
                strcpy(d->maintext1, d->name);
        if (ast_strlen_zero(d->titledefault)) {
-               struct tm *stm;
-               time_t cur_time = time(0);
+               struct ast_tm tm = { 0, };
+               struct timeval cur_time = ast_tvnow();
 
-               if ((stm = localtime(&cur_time)) == 0) {
-                       display_last_error("Error in localtime()");
+               if ((ast_localtime(&cur_time, &tm, 0)) == 0 || ast_strlen_zero(tm.tm_zone)) {
+                       display_last_error("Error in ast_localtime()");
                        ast_copy_string(d->titledefault, "UNISTIM for*", 12);
                } else {
-                       if (strlen(stm->tm_zone) < 4) {
+                       if (strlen(tm.tm_zone) < 4) {
                                strcpy(d->titledefault, "TimeZone ");
-                               strcat(d->titledefault, stm->tm_zone);
-                       } else if (strlen(stm->tm_zone) < 9) {
+                               strcat(d->titledefault, tm.tm_zone);
+                       } else if (strlen(tm.tm_zone) < 9) {
                                strcpy(d->titledefault, "TZ ");
-                               strcat(d->titledefault, stm->tm_zone);
+                               strcat(d->titledefault, tm.tm_zone);
                        } else
-                               ast_copy_string(d->titledefault, stm->tm_zone, 12);
+                               ast_copy_string(d->titledefault, tm.tm_zone, 12);
                }
        }
        /* Update the chained link if it's a new device */
@@ -5328,11 +5273,9 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
                d->next = devices;
                devices = d;
                ast_mutex_unlock(&devicelock);
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Added device '%s'\n", d->name);
+               ast_verb(3, "Added device '%s'\n", d->name);
        } else {
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Device '%s' reloaded\n", d->name);
+               ast_verb(3, "Device '%s' reloaded\n", d->name);
        }
        return d;
 }
@@ -5374,7 +5317,19 @@ static int reload_config(void)
                        unistim_keepalive = atoi(v->value);
                else if (!strcasecmp(v->name, "port"))
                        unistim_port = atoi(v->value);
-               else if (!strcasecmp(v->name, "autoprovisioning")) {
+                else if (!strcasecmp(v->name, "tos")) {
+                        if (ast_str2tos(v->value, &tos))
+                            ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
+                } else if (!strcasecmp(v->name, "tos_audio")) {
+                        if (ast_str2tos(v->value, &tos_audio))
+                            ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
+                } else if (!strcasecmp(v->name, "cos")) {
+                        if (ast_str2cos(v->value, &cos))
+                            ast_log(LOG_WARNING, "Invalid cos value at line %d, refer to QoS documentation\n", v->lineno);
+                } else if (!strcasecmp(v->name, "cos_audio")) {
+                        if (ast_str2cos(v->value, &cos_audio))
+                            ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
+               } else if (!strcasecmp(v->name, "autoprovisioning")) {
                        if (!strcasecmp(v->value, "no"))
                                autoprovisioning = AUTOPROVISIONING_NO;
                        else if (!strcasecmp(v->value, "yes"))
@@ -5436,7 +5391,7 @@ static int reload_config(void)
                        int i;
 
                        if (unistimdebug)
-                               ast_verbose("Removing device '%s'\n", d->name);
+                               ast_verb(0, "Removing device '%s'\n", d->name);
                        if (!d->lines) {
                                ast_log(LOG_ERROR, "Device '%s' without a line !, aborting\n", d->name);
                                ast_config_destroy(cfg);
@@ -5550,11 +5505,8 @@ static int reload_config(void)
                close(unistimsock);
                unistimsock = -1;
        } else {
-               if (option_verbose > 1) {
-                       ast_verbose(VERBOSE_PREFIX_2
-                                               "UNISTIM Listening on %s:%d\n",
-                                               ast_inet_ntoa(bindaddr.sin_addr), htons(bindaddr.sin_port));
-               }
+               ast_verb(2, "UNISTIM Listening on %s:%d\n", ast_inet_ntoa(bindaddr.sin_addr), htons(bindaddr.sin_port));
+               ast_netsock_set_qos(unistimsock, tos, cos, "UNISTIM");
        }
        return 0;
 }
@@ -5572,7 +5524,7 @@ static enum ast_rtp_get_result unistim_get_rtp_peer(struct ast_channel *chan,
        enum ast_rtp_get_result res = AST_RTP_GET_FAILED;
 
        if (unistimdebug)
-               ast_verbose("unistim_get_rtp_peer called\n");
+               ast_verb(0, "unistim_get_rtp_peer called\n");
                
        sub = chan->tech_pvt;
        if (sub && sub->rtp) {
@@ -5589,7 +5541,7 @@ static int unistim_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp,
        struct unistim_subchannel *sub;
 
        if (unistimdebug)
-               ast_verbose("unistim_set_rtp_peer called\n");
+               ast_verb(0, "unistim_set_rtp_peer called\n");
 
        sub = chan->tech_pvt;