Finally merge chan_skinny fixes into trunk.
[asterisk/asterisk.git] / channels / chan_skinny.c
index de72c79..bcdf536 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * chan_skinny was developed by Jeremy McNamara & Florian Overkamp
+ * chan_skinny was heavily modified/fixed by North Antara
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -20,7 +21,7 @@
  *
  * \brief Implementation of the Skinny protocol
  *
- * \author Jeremy McNamara & Florian Overkamp
+ * \author Jeremy McNamara & Florian Overkamp & North Antara
  * \ingroup channel_drivers
  */
 
@@ -69,6 +70,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/utils.h"
 #include "asterisk/dsp.h"
 #include "asterisk/stringfields.h"
+#include "asterisk/astobj.h"
 #include "asterisk/abstract_jb.h"
 
 /*************************************
@@ -88,11 +90,6 @@ static int keep_alive = 120;
 static char date_format[6] = "D-M-Y";
 static char version_id[16] = "P002F202";
 
-/* these should be in an include file, but dunno what to include */
-typedef unsigned char  UINT8;
-typedef unsigned short UINT16;
-typedef unsigned int   UINT32;
-
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define letohl(x) (x)
 #define letohs(x) (x)
@@ -137,127 +134,164 @@ static struct ast_jb_conf global_jbconf;
 #define REGISTER_MESSAGE 0x0001
 typedef struct register_message {
        char name[16];
-       int userId;
-       int instance;
-       char ip[4];
-       int type;
-       int maxStreams;
+       uint32_t userId;
+       uint32_t instance;
+       uint32_t ip;
+       uint32_t type;
+       uint32_t maxStreams;
 } register_message;
 
 #define IP_PORT_MESSAGE        0x0002
 
 #define KEYPAD_BUTTON_MESSAGE 0x0003
 typedef struct keypad_button_message {
-       int button;
+       uint32_t button;
+       uint32_t lineInstance;
+       uint32_t callReference;
 } keypad_button_message;
 
 #define STIMULUS_MESSAGE 0x0005
 typedef struct stimulus_message {
-       int stimulus;
-       int stimulusInstance;
+       uint32_t stimulus;
+       uint32_t stimulusInstance;
+       uint32_t unknown1;
 } stimulus_message;
 
 #define OFFHOOK_MESSAGE 0x0006
+typedef struct offhook_message {
+       uint32_t unknown1;
+       uint32_t unknown2;
+} offhook_message;
+
 #define ONHOOK_MESSAGE 0x0007
+typedef struct onhook_message {
+       uint32_t unknown1;
+       uint32_t unknown2;
+} onhook_message;
 
 #define CAPABILITIES_RES_MESSAGE 0x0010
 typedef struct station_capabilities {
-       int codec;
-       int frames;
+       uint32_t codec;
+       uint32_t frames;
        union {
                char res[8];
-               long rate;
+               uint64_t rate;
        } payloads;
 } station_capabilities;
 
 typedef struct capabilities_res_message {
-       int count;
+       uint32_t count;
        struct station_capabilities caps[18];
 } capabilities_res_message;
 
 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
 typedef struct speed_dial_stat_req_message {
-       int speedDialNumber;
+       uint32_t speedDialNumber;
 } speed_dial_stat_req_message;
 
 #define LINE_STATE_REQ_MESSAGE 0x000B
 typedef struct line_state_req_message {
-       int lineNumber;
+       uint32_t lineNumber;
 } line_state_req_message;
 
 #define TIME_DATE_REQ_MESSAGE 0x000D
 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
 #define VERSION_REQ_MESSAGE 0x000F
 #define SERVER_REQUEST_MESSAGE 0x0012
+
 #define ALARM_MESSAGE 0x0020
+typedef struct alarm_message {
+       uint32_t alarmSeverity;
+       char displayMessage[80];
+       uint32_t alarmParam1;
+       uint32_t alarmParam2;
+} alarm_message;
 
 #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
 typedef struct open_receive_channel_ack_message {
-       int status;
-       char ipAddr[4];
-       int port;
-       int passThruId;
+       uint32_t status;
+       uint32_t ipAddr;
+       uint32_t port;
+       uint32_t passThruId;
 } open_receive_channel_ack_message;
 
 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
+
+#define SOFT_KEY_EVENT_MESSAGE 0x0026
+typedef struct soft_key_event_message {
+       uint32_t softKeyEvent;
+       uint32_t instance;
+       uint32_t reference;
+} soft_key_event_message;
+
 #define UNREGISTER_MESSAGE 0x0027
 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
+#define HEADSET_STATUS_MESSAGE 0x002B
+#define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
 
 #define REGISTER_ACK_MESSAGE 0x0081
 typedef struct register_ack_message {
-       int keepAlive;
+       uint32_t keepAlive;
        char dateTemplate[6];
        char res[2];
-       int secondaryKeepAlive;
+       uint32_t secondaryKeepAlive;
        char res2[4];
 } register_ack_message;
 
 #define START_TONE_MESSAGE 0x0082
 typedef struct start_tone_message {
-       int tone;
+       uint32_t tone;
 } start_tone_message;
 
 #define STOP_TONE_MESSAGE 0x0083
 
 #define SET_RINGER_MESSAGE 0x0085
 typedef struct set_ringer_message {
-       int ringerMode;
+       uint32_t ringerMode;
+       uint32_t unknown1; /* See notes in transmit_ringer_mode */
+       uint32_t unknown2;
 } set_ringer_message;
 
 #define SET_LAMP_MESSAGE 0x0086
 typedef struct set_lamp_message {
-       int stimulus;
-       int stimulusInstance;
-       int deviceStimulus;
+       uint32_t stimulus;
+       uint32_t stimulusInstance;
+       uint32_t deviceStimulus;
 } set_lamp_message;
 
 #define SET_SPEAKER_MESSAGE 0x0088
 typedef struct set_speaker_message {
-       int mode;
+       uint32_t mode;
 } set_speaker_message;
 
+/* XXX When do we need to use this? */
+#define SET_MICROPHONE_MESSAGE 0x0089
+typedef struct set_microphone_message {
+       uint32_t mode;
+} set_microphone_message;
+
 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
 typedef struct media_qualifier {
-       int precedence;
-       int vad;
-       int packets;
-       int bitRate;
+       uint32_t precedence;
+       uint32_t vad;
+       uint32_t packets;
+       uint32_t bitRate;
 } media_qualifier;
 
 typedef struct start_media_transmission_message {
-       int conferenceId;
-       int passThruPartyId;
-       char remoteIp[4];
-       int remotePort;
-       int packetSize;
-       int payloadType;
+       uint32_t conferenceId;
+       uint32_t passThruPartyId;
+       uint32_t remoteIp;
+       uint32_t remotePort;
+       uint32_t packetSize;
+       uint32_t payloadType;
        media_qualifier qualifier;
 } start_media_transmission_message;
 
 #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
 typedef struct stop_media_transmission_message {
-       int conferenceId;
-       int passThruPartyId;
+       uint32_t conferenceId;
+       uint32_t passThruPartyId;
 } stop_media_transmission_message;
 
 #define CALL_INFO_MESSAGE 0x008F
@@ -266,199 +300,96 @@ typedef struct call_info_message {
        char callingParty[24];
        char calledPartyName[40];
        char calledParty[24];
-       int instance;
-       int reference;
-       int type;
+       uint32_t instance;
+       uint32_t reference;
+       uint32_t type;
        char originalCalledPartyName[40];
        char originalCalledParty[24];
 } call_info_message;
 
 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
 typedef struct speed_dial_stat_res_message {
-       int speedDialNumber;
+       uint32_t speedDialNumber;
        char speedDialDirNumber[24];
        char speedDialDisplayName[40];
 } speed_dial_stat_res_message;
 
 #define LINE_STAT_RES_MESSAGE 0x0092
 typedef struct line_stat_res_message {
-       int linenumber;
+       uint32_t lineNumber;
        char lineDirNumber[24];
        char lineDisplayName[42];
-       int space;
+       uint32_t space;
 } line_stat_res_message;
 
 #define DEFINETIMEDATE_MESSAGE 0x0094
 typedef struct definetimedate_message {
-       int year;       /* since 1900 */
-       int month;
-       int dayofweek;  /* monday = 1 */
-       int day;
-       int hour;
-       int minute;
-       int seconds;
-       int milliseconds;
-       int timestamp;
+       uint32_t year;  /* since 1900 */
+       uint32_t month;
+       uint32_t dayofweek;     /* monday = 1 */
+       uint32_t day;
+       uint32_t hour;
+       uint32_t minute;
+       uint32_t seconds;
+       uint32_t milliseconds;
+       uint32_t timestamp;
 } definetimedate_message;
 
 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
-
 typedef struct buttondefinition {
-       UINT8 instanceNumber;
-       UINT8 buttonDefinition;
+       uint8_t instanceNumber;
+       uint8_t buttonDefinition;
 } button_definition;
 
-#define STIMULUS_REDIAL        0x01
-#define STIMULUS_SPEEDDIAL     0x02
-#define STIMULUS_HOLD          0x03
-#define STIMULUS_TRANSFER      0x04
-#define STIMULUS_FORWARDALL    0x05
-#define STIMULUS_FORWARDBUSY   0x06
-#define STIMULUS_FORWARDNOANSWER 0x07
-#define STIMULUS_DISPLAY       0x08
-#define STIMULUS_LINE          0x09
-#define STIMULUS_VOICEMAIL     0x0F
-#define STIMULUS_AUTOANSWER    0x11
-#define STIMULUS_CONFERENCE    0x7D
-#define STIMULUS_CALLPARK      0x7E
-#define STIMULUS_CALLPICKUP    0x7F
-#define STIMULUS_NONE          0xFF
-
-button_definition button_def_30vip[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 2, STIMULUS_LINE },           /* Line 2 */
-       { 3, STIMULUS_LINE },           /* Line 3 */
-       { 4, STIMULUS_LINE },           /* Line 4 */
-       { 1, STIMULUS_CALLPARK },       /* Call Park */
-       { 0, STIMULUS_NONE },
-       { 1, STIMULUS_SPEEDDIAL },      /* Speeddial 1 */
-       { 2, STIMULUS_SPEEDDIAL },      /* Speeddial 2 */
-       { 3, STIMULUS_SPEEDDIAL },      /* Speeddial 3 */
-       { 4, STIMULUS_SPEEDDIAL },      /* Speeddial 4 */
-       { 5, STIMULUS_SPEEDDIAL },      /* Speeddial 5 */
-       { 6, STIMULUS_SPEEDDIAL },      /* Speeddial 6 */
-       { 1, STIMULUS_VOICEMAIL },      /* Voicemail */
-       { 1, STIMULUS_FORWARDALL },     /* Forward All */
-       { 1, STIMULUS_CONFERENCE },     /* Conference */
-       { 0, STIMULUS_NONE },
-       { 0, STIMULUS_NONE },
-       { 0, STIMULUS_NONE },
-       { 0, STIMULUS_NONE },
-       { 0, STIMULUS_NONE },
-       { 7, STIMULUS_SPEEDDIAL },      /* Speeddial 7 */
-       { 8, STIMULUS_SPEEDDIAL },      /* Speeddial 8 */
-       { 9, STIMULUS_SPEEDDIAL },      /* Speeddial 9 */
-       { 10, STIMULUS_SPEEDDIAL }      /* Speeddial 10 */
-};
-
-button_definition button_def_12sp[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 1, STIMULUS_SPEEDDIAL },      /* Speeddial 1 */
-       { 2, STIMULUS_SPEEDDIAL },      /* Speeddial 2 */
-       { 3, STIMULUS_SPEEDDIAL },      /* Speeddial 3 */
-       { 4, STIMULUS_SPEEDDIAL },      /* Speeddial 4 */
-       { 1, STIMULUS_VOICEMAIL },      /* Voicemail */
-       { 5, STIMULUS_SPEEDDIAL },      /* Speeddial 5 */
-       { 6, STIMULUS_SPEEDDIAL },      /* Speeddial 6 */
-       { 7, STIMULUS_SPEEDDIAL },      /* Speeddial 7 */
-       { 8, STIMULUS_SPEEDDIAL },      /* Speeddial 8 */
-       { 9, STIMULUS_SPEEDDIAL }       /* Speeddial 9 */
-};
-
-button_definition button_def_7902[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 1, STIMULUS_HOLD },           /* Hold */
-       { 1, STIMULUS_TRANSFER },
-       { 1, STIMULUS_DISPLAY },
-       { 1, STIMULUS_VOICEMAIL },
-       { 1, STIMULUS_CONFERENCE },
-       { 1, STIMULUS_FORWARDALL },
-       { 1, STIMULUS_SPEEDDIAL },      /* Speeddial 1 */
-       { 2, STIMULUS_SPEEDDIAL },      /* Speeddial 2 */
-       { 3, STIMULUS_SPEEDDIAL },      /* Speeddial 3 */
-       { 4, STIMULUS_SPEEDDIAL },      /* Speeddial 4 */
-       { 1, STIMULUS_REDIAL }
-};
-
-button_definition button_def_7910[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 1, STIMULUS_HOLD },           /* Hold */
-       { 1, STIMULUS_TRANSFER },
-       { 1, STIMULUS_DISPLAY },
-       { 1, STIMULUS_VOICEMAIL },
-       { 1, STIMULUS_CONFERENCE },
-       { 1, STIMULUS_FORWARDALL },
-       { 1, STIMULUS_SPEEDDIAL },      /* Speeddial 1 */
-       { 2, STIMULUS_SPEEDDIAL },      /* Speeddial 2 */
-       { 1, STIMULUS_REDIAL }
-};
-
-button_definition button_def_7920[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 2, STIMULUS_LINE },           /* Line 2 */
-       { 1, STIMULUS_SPEEDDIAL },      /* Speeddial 1 */
-       { 2, STIMULUS_SPEEDDIAL },      /* Speeddial 2 */
-       { 3, STIMULUS_SPEEDDIAL },      /* Speeddial 3 */
-       { 4, STIMULUS_SPEEDDIAL }       /* Speeddial 4 */
-};
-
-button_definition button_def_7935[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 2, STIMULUS_LINE }            /* Line 2 */
-};
-
-button_definition button_def_7940[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 2, STIMULUS_LINE }            /* Line 2 */
-};
-
-button_definition button_def_7960[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 2, STIMULUS_LINE },           /* Line 2 */
-       { 3, STIMULUS_LINE },           /* Line 3 */
-       { 1, STIMULUS_SPEEDDIAL },      /* Speeddial 1 */
-       { 2, STIMULUS_SPEEDDIAL },      /* Speeddial 2 */
-       { 3, STIMULUS_SPEEDDIAL }       /* Speeddial 3 */
-};
-
-button_definition button_def_7970[] = {
-       { 1, STIMULUS_LINE },           /* Line 1 */
-       { 2, STIMULUS_LINE },           /* Line 2 */
-       { 3, STIMULUS_LINE },           /* Line 3 */
-       { 1, STIMULUS_SPEEDDIAL },      /* Speeddial 1 */
-       { 2, STIMULUS_SPEEDDIAL },      /* Speeddial 2 */
-       { 3, STIMULUS_SPEEDDIAL },      /* Speeddial 3 */
-       { 4, STIMULUS_SPEEDDIAL },      /* Speeddial 4 */
-       { 5, STIMULUS_SPEEDDIAL }       /* Speeddial 5 */
-};
-
-button_definition button_def_none = { 0, STIMULUS_NONE };
-
-typedef struct button_defs {
-       char *type;
-       int num_buttons;
-       button_definition *button_def;
-} button_defs_t;
-
-button_defs_t button_defs[] = {
-       { "12SP",       12,     button_def_12sp }, /* First one is used if
-                                                     there's no match */
-       { "30VIP",      26,     button_def_30vip },
-       { "7902",       12,     button_def_7902 },
-       { "7910",       10,     button_def_7910 },
-       { "7920",       6,      button_def_7920 },
-       { "7935",       2,      button_def_7935 },
-       { "7940",       2,      button_def_7940 },
-       { "7960",       6,      button_def_7960 },
-       { "7970",       8,      button_def_7970 },
-       { NULL,         0,      NULL }
-};
+typedef struct buttondefinitiontemplate {
+       uint8_t buttonDefinition;
+       /* for now, anything between 0xB0 and 0xCF is custom */
+       /*int custom;*/
+} button_definition_template;
+
+#define STIMULUS_REDIAL                        0x01
+#define STIMULUS_SPEEDDIAL             0x02
+#define STIMULUS_HOLD                  0x03
+#define STIMULUS_TRANSFER              0x04
+#define STIMULUS_FORWARDALL            0x05
+#define STIMULUS_FORWARDBUSY           0x06
+#define STIMULUS_FORWARDNOANSWER       0x07
+#define STIMULUS_DISPLAY               0x08
+#define STIMULUS_LINE                  0x09
+#define STIMULUS_VOICEMAIL             0x0F
+#define STIMULUS_AUTOANSWER            0x11
+#define STIMULUS_CONFERENCE            0x7D
+#define STIMULUS_CALLPARK              0x7E
+#define STIMULUS_CALLPICKUP            0x7F
+#define STIMULUS_NONE                  0xFF
+
+/* Button types */
+#define BT_REDIAL                      STIMULUS_REDIAL
+#define BT_SPEEDDIAL                   STIMULUS_SPEEDDIAL
+#define BT_HOLD                                STIMULUS_HOLD
+#define BT_TRANSFER                    STIMULUS_TRANSFER
+#define BT_FORWARDALL                  STIMULUS_FORWARDALL
+#define BT_FORWARDBUSY                 STIMULUS_FORWARDBUSY
+#define BT_FORWARDNOANSWER             STIMULUS_FORWARDNOANSWER
+#define BT_DISPLAY                     STIMULUS_DISPLAY
+#define BT_LINE                                STIMULUS_LINE
+#define BT_VOICEMAIL                   STIMULUS_VOICEMAIL
+#define BT_AUTOANSWER                  STIMULUS_AUTOANSWER
+#define BT_CONFERENCE                  STIMULUS_CONFERENCE
+#define BT_CALLPARK                    STIMULUS_CALLPARK
+#define BT_CALLPICKUP                  STIMULUS_CALLPICKUP
+#define BT_NONE                                0x00
+
+/* Custom button types - add our own between 0xB0 and 0xCF.
+   This may need to be revised in the future,
+   if stimuluses are ever added in this range. */
+#define BT_CUST_LINESPEEDDIAL          0xB0    /* line or speeddial */
+#define BT_CUST_HINT                   0xB1    /* pipe dream */
 
 typedef struct button_template_res_message {
-       UINT32 buttonOffset;
-       UINT32 buttonCount;
-       UINT32 totalButtonCount;
+       uint32_t buttonOffset;
+       uint32_t buttonCount;
+       uint32_t totalButtonCount;
        button_definition definition[42];
 } button_template_res_message;
 
@@ -487,196 +418,303 @@ typedef struct server_identifier {
 
 typedef struct server_res_message {
        server_identifier server[5];
-       int serverListenPort[5];
-       int serverIpAddr[5];
+       uint32_t serverListenPort[5];
+       uint32_t serverIpAddr[5];
 } server_res_message;
 
+#define RESET_MESSAGE 0x009F
+typedef struct reset_message {
+       uint32_t resetType;
+} reset_message;
+
 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
 
 #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
 typedef struct open_receive_channel_message {
-       int conferenceId;
-       int partyId;
-       int packets;
-       int capability;
-       int echo;
-       int bitrate;
+       uint32_t conferenceId;
+       uint32_t partyId;
+       uint32_t packets;
+       uint32_t capability;
+       uint32_t echo;
+       uint32_t bitrate;
 } open_receive_channel_message;
 
 #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
 typedef struct close_receive_channel_message {
-       int conferenceId;
-       int partyId;
+       uint32_t conferenceId;
+       uint32_t partyId;
 } close_receive_channel_message;
 
 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
 
 typedef struct soft_key_template_definition {
        char softKeyLabel[16];
-       int softKeyEvent;
+       uint32_t softKeyEvent;
 } soft_key_template_definition;
 
+#define KEYDEF_ONHOOK                  0
+#define KEYDEF_CONNECTED               1
+#define KEYDEF_ONHOLD                  2
+#define KEYDEF_RINGIN                  3
+#define KEYDEF_OFFHOOK                 4
+#define KEYDEF_CONNWITHTRANS           5
+#define KEYDEF_DADFD                   6 /* Digits After Dialing First Digit */
+#define KEYDEF_CONNWITHCONF            7
+#define KEYDEF_RINGOUT                 8
+#define KEYDEF_OFFHOOKWITHFEAT         9
+#define KEYDEF_UNKNOWN                 10
+
+#define SOFTKEY_NONE                   0x00
+#define SOFTKEY_REDIAL                 0x01
+#define SOFTKEY_NEWCALL                        0x02
+#define SOFTKEY_HOLD                   0x03
+#define SOFTKEY_TRNSFER                        0x04
+#define SOFTKEY_CFWDALL                        0x05
+#define SOFTKEY_CFWDBUSY               0x06
+#define SOFTKEY_CFWDNOANSWER           0x07
+#define SOFTKEY_BKSPC                  0x08
+#define SOFTKEY_ENDCALL                        0x09
+#define SOFTKEY_RESUME                 0x0A
+#define SOFTKEY_ANSWER                 0x0B
+#define SOFTKEY_INFO                   0x0C
+#define SOFTKEY_CONFRN                 0x0D
+#define SOFTKEY_PARK                   0x0E
+#define SOFTKEY_JOIN                   0x0F
+#define SOFTKEY_MEETME                 0x10
+#define SOFTKEY_PICKUP                 0x11
+#define SOFTKEY_GPICKUP                        0x12
+
 soft_key_template_definition soft_key_template_default[] = {
-       { "Redial",             1 },
-       { "NewCall",            2 },
-       { "Hold",               3 },
-       { "Trnsfer",            4 },
-       { "CFwdAll",            5 },
-       { "CFwdBusy",           6 },
-       { "CFwdNoAnswer",       7 },
-       { "<<",                 8 },
-       { "EndCall",            9 },
-       { "Resume",             10 },
-       { "Answer",             11 },
-       { "Info",               12 },
-       { "Confrn",             13 },
-       { "Park",               14 },
-       { "Join",               15 },
-       { "MeetMe",             16 },
-       { "PickUp",             17 },
-       { "GPickUp",            18 },
+       { "Redial",             0x01 },
+       { "NewCall",            0x02 },
+       { "Hold",               0x03 },
+       { "Trnsfer",            0x04 },
+       { "CFwdAll",            0x05 },
+       { "CFwdBusy",           0x06 },
+       { "CFwdNoAnswer",       0x07 },
+       { "<<",                 0x08 },
+       { "EndCall",            0x09 },
+       { "Resume",             0x0A },
+       { "Answer",             0x0B },
+       { "Info",               0x0C },
+       { "Confrn",             0x0D },
+       { "Park",               0x0E },
+       { "Join",               0x0F },
+       { "MeetMe",             0x10 },
+       { "PickUp",             0x11 },
+       { "GPickUp",            0x12 },
+};
+
+typedef struct soft_key_definitions {
+       const uint8_t mode;
+       const uint8_t *defaults;
+       const int count;
+} soft_key_definitions;
+
+static const uint8_t soft_key_default_onhook[] = {
+       SOFTKEY_REDIAL,
+       SOFTKEY_CFWDALL,
+       SOFTKEY_CFWDBUSY,
+       SOFTKEY_GPICKUP,
+       SOFTKEY_CONFRN,
+};
+
+static const uint8_t soft_key_default_connected[] = {
+       SOFTKEY_HOLD,
+       SOFTKEY_ENDCALL,
+       SOFTKEY_TRNSFER,
+       SOFTKEY_PARK,
+       SOFTKEY_CFWDALL,
+       SOFTKEY_CFWDBUSY,
+};
+
+static const uint8_t soft_key_default_onhold[] = {
+       SOFTKEY_RESUME,
+       SOFTKEY_NEWCALL,
+       SOFTKEY_ENDCALL,
+       SOFTKEY_TRNSFER,
+};
+
+static const uint8_t soft_key_default_ringin[] = {
+       SOFTKEY_ANSWER,
+       SOFTKEY_ENDCALL,
+       SOFTKEY_TRNSFER,
+};
+
+static const uint8_t soft_key_default_offhook[] = {
+       SOFTKEY_REDIAL,
+       SOFTKEY_ENDCALL,
+       SOFTKEY_CFWDALL,
+       SOFTKEY_CFWDBUSY,
+       SOFTKEY_GPICKUP,
+};
+
+static const uint8_t soft_key_default_connwithtrans[] = {
+       SOFTKEY_HOLD,
+       SOFTKEY_ENDCALL,
+       SOFTKEY_TRNSFER,
+       SOFTKEY_PARK,
+       SOFTKEY_CFWDALL,
+       SOFTKEY_CFWDBUSY,
 };
 
-typedef struct soft_key_template {
-       int softKeyOffset;
-       int softKeyCount;
-       int totalSoftKeyCount;
+static const uint8_t soft_key_default_dadfd[] = {
+       SOFTKEY_BKSPC,
+       SOFTKEY_ENDCALL,
+};
+
+static const uint8_t soft_key_default_connwithconf[] = {
+       SOFTKEY_NONE,
+};
+
+static const uint8_t soft_key_default_ringout[] = {
+       SOFTKEY_ENDCALL,
+       SOFTKEY_TRNSFER,
+       SOFTKEY_CFWDALL,
+       SOFTKEY_CFWDBUSY,
+};
+
+static const uint8_t soft_key_default_offhookwithfeat[] = {
+       SOFTKEY_REDIAL,
+       SOFTKEY_ENDCALL,
+};
+
+static const uint8_t soft_key_default_unknown[] = {
+       SOFTKEY_NONE,
+};
+
+static const soft_key_definitions soft_key_default_definitions[] = {
+       {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
+       {KEYDEF_CONNECTED, soft_key_default_connected, sizeof(soft_key_default_connected) / sizeof(uint8_t)},
+       {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
+       {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
+       {KEYDEF_OFFHOOK, soft_key_default_offhook, sizeof(soft_key_default_offhook) / sizeof(uint8_t)},
+       {KEYDEF_CONNWITHTRANS, soft_key_default_connwithtrans, sizeof(soft_key_default_connwithtrans) / sizeof(uint8_t)},
+       {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
+       {KEYDEF_CONNWITHCONF, soft_key_default_connwithconf, sizeof(soft_key_default_connwithconf) / sizeof(uint8_t)},
+       {KEYDEF_RINGOUT, soft_key_default_ringout, sizeof(soft_key_default_ringout) / sizeof(uint8_t)},
+       {KEYDEF_OFFHOOKWITHFEAT, soft_key_default_offhookwithfeat, sizeof(soft_key_default_offhookwithfeat) / sizeof(uint8_t)},
+       {KEYDEF_UNKNOWN, soft_key_default_unknown, sizeof(soft_key_default_unknown) / sizeof(uint8_t)}
+};
+
+typedef struct soft_key_template_res_message {
+       uint32_t softKeyOffset;
+       uint32_t softKeyCount;
+       uint32_t totalSoftKeyCount;
        soft_key_template_definition softKeyTemplateDefinition[32];
-} soft_key_template;
+} soft_key_template_res_message;
 
 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
-static const char *soft_key_set_hack = {
-       "\x01\x02\x05\x03\x09\x0a\x0b\x10\x11\x12\x04\x0e\x0d\x00\x00\x00"
-       "\x2d\x01\x2e\x01\x31\x01\x2f\x01\x35\x01\x36\x01\x37\x01\x3c\x01"
-       "\x3d\x01\x3e\x01\x30\x01\x3a\x01\x39\x01\x00\x00\x00\x00\x00\x00"
-       "\x03\x09\x04\x0e\x0d\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x2f\x01\x35\x01\x30\x01\x3a\x01\x39\x01\x3f\x01\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x0a\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x36\x01\x2e\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x37\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x01\x09\x05\x10\x11\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x2d\x01\x35\x01\x31\x01\x3c\x01\x3d\x01\x3e\x01\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x09\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x35\x01\x30\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x08\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x34\x01\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x09\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x35\x01\x39\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x01\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x2d\x01\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x41\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-};
 
 typedef struct soft_key_set_definition {
-       UINT8 softKeyTemplateIndex[16];
-       UINT16 softKeyInfoIndex[16];
+       uint8_t softKeyTemplateIndex[16];
+       uint16_t softKeyInfoIndex[16];
 } soft_key_set_definition;
 
-typedef struct soft_key_sets {
-       UINT32 softKeySetOffset;
-       UINT32 softKeySetCount;
-       UINT32 totalSoftKeySetCount;
+typedef struct soft_key_set_res_message {
+       uint32_t softKeySetOffset;
+       uint32_t softKeySetCount;
+       uint32_t totalSoftKeySetCount;
        soft_key_set_definition softKeySetDefinition[16];
-       UINT32 res;
-} soft_key_sets;
+       uint32_t res;
+} soft_key_set_res_message;
 
 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
 typedef struct select_soft_keys_message {
-       int instance;
-       int reference;
-       int softKeySetIndex;
-       int validKeyMask;
+       uint32_t instance;
+       uint32_t reference;
+       uint32_t softKeySetIndex;
+       uint32_t validKeyMask;
 } select_soft_keys_message;
 
 #define CALL_STATE_MESSAGE 0x0111
 typedef struct call_state_message {
-       int callState;
-       int lineInstance;
-       int callReference;
+       uint32_t callState;
+       uint32_t lineInstance;
+       uint32_t callReference;
 } call_state_message;
 
 #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
 typedef struct display_prompt_status_message {
-       int messageTimeout;
+       uint32_t messageTimeout;
        char promptMessage[32];
-       int lineInstance;
-       int callReference;
+       uint32_t lineInstance;
+       uint32_t callReference;
 } display_prompt_status_message;
 
 #define DISPLAY_NOTIFY_MESSAGE 0x0114
 typedef struct display_notify_message {
-       int displayTimeout;
+       uint32_t displayTimeout;
        char displayMessage[100];
 } display_notify_message;
 
 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
 typedef struct activate_call_plane_message {
-       int lineInstance;
+       uint32_t lineInstance;
 } activate_call_plane_message;
 
 #define DIALED_NUMBER_MESSAGE 0x011D
 typedef struct dialed_number_message {
        char dialedNumber[24];
-       int lineInstance;
-       int callReference;
+       uint32_t lineInstance;
+       uint32_t callReference;
 } dialed_number_message;
 
+typedef union {
+       alarm_message alarm;
+       speed_dial_stat_req_message speeddialreq;
+       register_message reg;
+       register_ack_message regack;
+       register_rej_message regrej;
+       capabilities_res_message caps;
+       version_res_message version;
+       button_template_res_message buttontemplate;
+       displaytext_message displaytext;
+       display_prompt_status_message displaypromptstatus;
+       definetimedate_message definetimedate;
+       start_tone_message starttone;
+       speed_dial_stat_res_message speeddial;
+       line_state_req_message line;
+       line_stat_res_message linestat;
+       soft_key_set_res_message softkeysets;
+       soft_key_template_res_message softkeytemplate;
+       server_res_message serverres;
+       reset_message reset;
+       set_lamp_message setlamp;
+       set_ringer_message setringer;
+       call_state_message callstate;
+       keypad_button_message keypad;
+       select_soft_keys_message selectsoftkey;
+       activate_call_plane_message activatecallplane;
+       stimulus_message stimulus;
+       offhook_message offhook;
+       onhook_message onhook;
+       set_speaker_message setspeaker;
+       set_microphone_message setmicrophone;
+       call_info_message callinfo;
+       start_media_transmission_message startmedia;
+       stop_media_transmission_message stopmedia;
+       open_receive_channel_message openreceivechannel;
+       open_receive_channel_ack_message openreceivechannelack;
+       close_receive_channel_message closereceivechannel;
+       display_notify_message displaynotify;
+       dialed_number_message dialednumber;
+       soft_key_event_message softkeyeventmessage;
+} skinny_data;
+
 /* packet composition */
 typedef struct {
        int len;
        int res;
        int e;
-       union {
-               speed_dial_stat_req_message speeddialreq;
-               register_message reg;
-               register_ack_message regack;
-               register_rej_message regrej;
-               capabilities_res_message caps;
-               version_res_message version;
-               button_template_res_message buttontemplate;
-               displaytext_message displaytext;
-               display_prompt_status_message displaypromptstatus;
-               definetimedate_message definetimedate;
-               start_tone_message starttone;
-               speed_dial_stat_res_message speeddial;
-               line_state_req_message line;
-               line_stat_res_message linestat;
-               soft_key_sets softkeysets;
-               soft_key_template softkeytemplate;
-               server_res_message serverres;
-               set_lamp_message setlamp;
-               set_ringer_message setringer;
-               call_state_message callstate;
-               keypad_button_message keypad;
-               select_soft_keys_message selectsoftkey;
-               activate_call_plane_message activatecallplane;
-               stimulus_message stimulus;
-               set_speaker_message setspeaker;
-               call_info_message callinfo;
-               start_media_transmission_message startmedia;
-               stop_media_transmission_message stopmedia;
-               open_receive_channel_message openreceivechannel;
-               open_receive_channel_ack_message openreceivechannelack;
-               close_receive_channel_message closereceivechannel;
-               display_notify_message displaynotify;
-               dialed_number_message dialednumber;
-       } data;
+       skinny_data data;
 } skinny_req;
 
+/* XXX This is the combined size of the variables above.  (len, res, e)
+   If more are added, this MUST change.
+   (sizeof(skinny_req) - sizeof(skinny_data)) DOES NOT WORK on all systems (amd64?). */
+int skinny_header_size = 12;
+
 /*****************************
  * Asterisk specific globals *
  *****************************/
@@ -688,7 +726,8 @@ static struct sockaddr_in bindaddr;
 static char ourhost[256];
 static int ourport;
 static struct in_addr __ourip;
-struct ast_hostent ahp; struct hostent *hp;
+struct ast_hostent ahp;
+struct hostent *hp;
 static int skinnysock = -1;
 static pthread_t tcp_thread;
 static pthread_t accept_t;
@@ -715,13 +754,41 @@ static char mailbox[AST_MAX_EXTENSION];
 static int amaflags = 0;
 static int callnums = 1;
 
-#define SUB_REAL 0
-#define SUB_ALT 1
-#define MAX_SUBS 2
+#define SKINNY_DEVICE_UNKNOWN          -1
+#define SKINNY_DEVICE_NONE             0
+#define SKINNY_DEVICE_30SPPLUS         1
+#define SKINNY_DEVICE_12SPPLUS         2
+#define SKINNY_DEVICE_12SP             3
+#define SKINNY_DEVICE_12               4
+#define SKINNY_DEVICE_30VIP            5
+#define SKINNY_DEVICE_7910             6
+#define SKINNY_DEVICE_7960             7
+#define SKINNY_DEVICE_7940             8
+#define SKINNY_DEVICE_7935             9
+#define SKINNY_DEVICE_ATA186           12      /* Cisco ATA-186 */
+#define SKINNY_DEVICE_7941             115
+#define SKINNY_DEVICE_7971             119
+#define SKINNY_DEVICE_7985             302
+#define SKINNY_DEVICE_7911             307
+#define SKINNY_DEVICE_7961GE           308
+#define SKINNY_DEVICE_7941GE           309
+#define SKINNY_DEVICE_7905             20000
+#define SKINNY_DEVICE_7920             30002
+#define SKINNY_DEVICE_7970             30006
+#define SKINNY_DEVICE_7912             30007
+#define SKINNY_DEVICE_7902             30008
+#define SKINNY_DEVICE_CIPC             30016   /* Cisco IP Communicator */
+#define SKINNY_DEVICE_7961             30018
+#define SKINNY_DEVICE_7936             30019
+#define SKINNY_DEVICE_SCCPGATEWAY_AN   30027   /* ??? */
+#define SKINNY_DEVICE_SCCPGATEWAY_BRI  30028   /* ??? */
 
 #define SKINNY_SPEAKERON 1
 #define SKINNY_SPEAKEROFF 2
 
+#define SKINNY_MICON 1
+#define SKINNY_MICOFF 2
+
 #define SKINNY_OFFHOOK 1
 #define SKINNY_ONHOOK 2
 #define SKINNY_RINGOUT 3
@@ -814,26 +881,27 @@ static int matchdigittimeout = 3000;
 
 struct skinny_subchannel {
        ast_mutex_t lock;
-       unsigned int callid;
        struct ast_channel *owner;
-       struct skinny_line *parent;
        struct ast_rtp *rtp;
-       time_t lastouttime;
+       struct ast_rtp *vrtp;
+       unsigned int callid;
+       /* time_t lastouttime; */                       /* Unused */
        int progress;
        int ringing;
-       int lastout;
+       /* int lastout; */                              /* Unused */
        int cxmode;
        int nat;
        int outgoing;
        int alreadygone;
+
        struct skinny_subchannel *next;
+       struct skinny_line *parent;
 };
 
 struct skinny_line {
        ast_mutex_t lock;
        char name[80];
        char label[42];                                 /* Label that shows next to the line buttons */
-       struct skinny_subchannel *sub;                  /* pointer to our current connection, channel and stuff */
        char accountcode[AST_MAX_ACCOUNT_CODE];
        char exten[AST_MAX_EXTENSION];                  /* Extension where to start */
        char context[AST_MAX_CONTEXT];
@@ -844,6 +912,7 @@ struct skinny_line {
        char call_forward[AST_MAX_EXTENSION];
        char mailbox[AST_MAX_EXTENSION];
        char musicclass[MAX_MUSICCLASS];
+       char lastnumberdialed[AST_MAX_EXTENSION];       /* Last number that was dialed - used for redial */
        int curtone;                                    /* Current tone being played */
        ast_group_t callgroup;
        ast_group_t pickupgroup;
@@ -864,14 +933,34 @@ struct skinny_line {
        int capability;
        int nonCodecCapability;
        int onhooktime;
-       int msgstate;           /* voicemail message state */
+       int msgstate;                                   /* voicemail message state */
        int immediate;
        int hookstate;
-       int progress;
+       int nat;
+
+       struct skinny_subchannel *sub;
        struct skinny_line *next;
        struct skinny_device *parent;
 };
 
+struct skinny_speeddial {
+       ast_mutex_t lock;
+       char label[42];
+       char exten[AST_MAX_EXTENSION];
+       int instance;
+
+       struct skinny_speeddial *next;
+       struct skinny_device *parent;
+};
+
+struct skinny_addon {
+       ast_mutex_t lock;
+       char type[10];
+
+       struct skinny_addon *next;
+       struct skinny_device *parent;
+};
+
 static struct skinny_device {
        /* A device containing one or more lines */
        char name[80];
@@ -879,10 +968,13 @@ static struct skinny_device {
        char version_id[16];
        int type;
        int registered;
-       char model[6];
+       int lastlineinstance;
+       int lastcallreference;
        struct sockaddr_in addr;
        struct in_addr ourip;
        struct skinny_line *lines;
+       struct skinny_speeddial *speeddials;
+       struct skinny_addon *addons;
        struct ast_ha *ha;
        struct skinnysession *session;
        struct skinny_device *next;
@@ -901,6 +993,7 @@ static struct skinnysession {
        struct sockaddr_in sin;
        int fd;
        char inbuf[SKINNY_MAX_PACKET];
+       char outbuf[SKINNY_MAX_PACKET];
        struct skinny_device *device;
        struct skinnysession *next;
 } *sessions = NULL;
@@ -932,25 +1025,151 @@ static const struct ast_channel_tech skinny_tech = {
 /*     .bridge = ast_rtp_bridge, */
 };
 
-static skinny_req *req_alloc(size_t size)
+static void *get_button_template(struct skinnysession *s, button_definition_template *btn)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_addon *a = d->addons;
+       int i;
+
+       switch (d->type) {
+               case SKINNY_DEVICE_30SPPLUS:
+               case SKINNY_DEVICE_30VIP:
+                       /* 13 rows, 2 columns */
+                       for (i = 0; i < 4; i++)
+                               (btn++)->buttonDefinition = BT_LINE;
+                       (btn++)->buttonDefinition = BT_REDIAL;
+                       (btn++)->buttonDefinition = BT_VOICEMAIL;
+                       (btn++)->buttonDefinition = BT_CALLPARK;
+                       (btn++)->buttonDefinition = BT_FORWARDALL;
+                       (btn++)->buttonDefinition = BT_CONFERENCE;
+                       for (i = 0; i < 4; i++)
+                               (btn++)->buttonDefinition = BT_NONE;
+                       for (i = 0; i < 13; i++)
+                               (btn++)->buttonDefinition = BT_SPEEDDIAL;
+                       
+                       break;
+               case SKINNY_DEVICE_12SPPLUS:
+               case SKINNY_DEVICE_12SP:
+               case SKINNY_DEVICE_12:
+                       /* 6 rows, 2 columns */
+                       for (i = 0; i < 2; i++)
+                               (btn++)->buttonDefinition = BT_LINE;
+                       (btn++)->buttonDefinition = BT_REDIAL;
+                       for (i = 0; i < 3; i++)
+                               (btn++)->buttonDefinition = BT_SPEEDDIAL;
+                       (btn++)->buttonDefinition = BT_HOLD;
+                       (btn++)->buttonDefinition = BT_TRANSFER;
+                       (btn++)->buttonDefinition = BT_FORWARDALL;
+                       (btn++)->buttonDefinition = BT_CALLPARK;
+                       (btn++)->buttonDefinition = BT_VOICEMAIL;
+                       (btn++)->buttonDefinition = BT_CONFERENCE;
+                       break;
+               case SKINNY_DEVICE_7910:
+                       (btn++)->buttonDefinition = BT_LINE;
+                       (btn++)->buttonDefinition = BT_HOLD;
+                       (btn++)->buttonDefinition = BT_TRANSFER;
+                       (btn++)->buttonDefinition = BT_DISPLAY;
+                       (btn++)->buttonDefinition = BT_VOICEMAIL;
+                       (btn++)->buttonDefinition = BT_CONFERENCE;
+                       (btn++)->buttonDefinition = BT_FORWARDALL;
+                       for (i = 0; i < 2; i++)
+                               (btn++)->buttonDefinition = BT_SPEEDDIAL;
+                       (btn++)->buttonDefinition = BT_REDIAL;
+                       break;
+               case SKINNY_DEVICE_7960:
+               case SKINNY_DEVICE_7961:
+               case SKINNY_DEVICE_7961GE:
+                       for (i = 0; i < 6; i++)
+                               (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
+                       break;
+               case SKINNY_DEVICE_7940:
+               case SKINNY_DEVICE_7941:
+               case SKINNY_DEVICE_7941GE:
+                       for (i = 0; i < 2; i++)
+                               (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
+                       break;
+               case SKINNY_DEVICE_7935:
+               case SKINNY_DEVICE_7936:
+                       for (i = 0; i < 2; i++)
+                               (btn++)->buttonDefinition = BT_LINE;
+                       break;
+               case SKINNY_DEVICE_ATA186:
+                       (btn++)->buttonDefinition = BT_LINE;
+                       break;
+               case SKINNY_DEVICE_7970:
+               case SKINNY_DEVICE_7971:
+               case SKINNY_DEVICE_CIPC:
+                       for (i = 0; i < 8; i++)
+                               (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
+                       break;
+               case SKINNY_DEVICE_7985:
+                       /* XXX I have no idea what the buttons look like on these. */
+                       ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
+                       break;
+               case SKINNY_DEVICE_7912:
+               case SKINNY_DEVICE_7911:
+               case SKINNY_DEVICE_7905:
+                       (btn++)->buttonDefinition = BT_LINE;
+                       (btn++)->buttonDefinition = BT_HOLD;
+                       break;
+               case SKINNY_DEVICE_7920:
+                       /* XXX I don't know if this is right. */
+                       for (i = 0; i < 4; i++)
+                               (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
+                       break;
+               case SKINNY_DEVICE_7902:
+                       ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
+                       break;
+               case SKINNY_DEVICE_SCCPGATEWAY_AN:
+               case SKINNY_DEVICE_SCCPGATEWAY_BRI:
+                       ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
+                       break;
+               default:
+                       ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
+                       break;
+       }
+
+       for (a = d->addons; a; a = a->next) {
+               if (!strcasecmp(a->type, "7914")) {
+                       for (i = 0; i < 14; i++)
+                               (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
+               } else {
+                       ast_log(LOG_WARNING, "Unknown addon type '%s' found.  Skipping.\n", a->type);
+               }
+       }
+
+       return btn;
+}
+
+static skinny_req *req_alloc(size_t size, int response_message)
 {
        skinny_req *req;
-       req = malloc(size+12);
-       if (!req) {
+
+       if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
                return NULL;
-       }
-       memset(req, 0, size+12);
+
+       req->len = htolel(size+4);
+       req->e = htolel(response_message);
+
        return req;
 }
 
-static struct skinny_subchannel *find_subchannel_by_line(struct skinny_line *l)
+static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
 {
-       /* XXX Need to figure out how to determine which sub we want */
-       struct skinny_subchannel *sub = l->sub;
-       return sub;
+       struct skinny_line *l;
+
+       for (l = d->lines; l; l = l->next) {
+               if (l->instance == instance)
+                       break;
+       }
+
+       if (!l) {
+               ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
+       }
+       return l;
 }
 
-static struct skinny_subchannel *find_subchannel_by_name(const char *dest)
+static struct skinny_line *find_line_by_name(const char *dest)
 {
        struct skinny_line *l;
        struct skinny_device *d;
@@ -969,15 +1188,14 @@ static struct skinny_subchannel *find_subchannel_by_name(const char *dest)
        ast_mutex_lock(&devicelock);
        for (d = devices; d; d = d->next) {
                if (!strcasecmp(d->name, device)) {
-                       if (skinnydebug) {
+                       if (skinnydebug)
                                ast_verbose("Found device: %s\n", d->name);
-                       }
                        /* Found the device */
                        for (l = d->lines; l; l = l->next) {
                                /* Search for the right line */
                                if (!strcasecmp(l->name, line)) {
                                        ast_mutex_unlock(&devicelock);
-                                       return l->sub;
+                                       return l;
                                }
                        }
                }
@@ -987,18 +1205,82 @@ static struct skinny_subchannel *find_subchannel_by_name(const char *dest)
        return NULL;
 }
 
+/* It's quicker/easier to find the subchannel when we know the instance number too */
+static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
+{
+       struct skinny_line *l = find_line_by_instance(d, instance);
+       struct skinny_subchannel *sub;
+
+       if (!l) {
+               return NULL;
+       }
+
+       for (sub = l->sub; sub; sub = sub->next) {
+               if (sub->callid == reference)
+                       break;
+       }
+
+       if (!sub) {
+               ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
+       }
+       return sub;
+}
+
+/* Find the subchannel when we only have the callid - this shouldn't happen often */
+static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
+{
+       struct skinny_line *l;
+       struct skinny_subchannel *sub = NULL;
+
+       for (l = d->lines; l; l = l->next) {
+               for (sub = l->sub; sub; sub = sub->next) {
+                       if (sub->callid == reference)
+                               break;
+               }
+               if (sub)
+                       break;
+       }
+
+       if (!l) {
+               ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
+       } else {
+               if (!sub) {
+                       ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
+               }
+       }
+       return sub;
+}
+
+static struct skinny_speeddial *find_speeddial_by_instance(struct skinny_device *d, int instance)
+{
+       struct skinny_speeddial *sd;
+
+       for (sd = d->speeddials; sd; sd = sd->next) {
+               if (sd->instance == instance)
+                       break;
+       }
+
+       if (!sd) {
+               ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
+       }
+       return sd;
+}
+
 static int transmit_response(struct skinnysession *s, skinny_req *req)
 {
        int res = 0;
        ast_mutex_lock(&s->lock);
 
 #if 0
-       if (skinnydebug) {
+       if (skinnydebug)
                ast_verbose("writing packet type %04X (%d bytes) to socket %d\n", letohl(req->e), letohl(req->len)+8, s->fd);
-       }
 #endif
 
-       res = write(s->fd, req, letohl(req->len)+8);
+       memset(s->outbuf,0,sizeof(s->outbuf));
+       memcpy(s->outbuf, req, skinny_header_size);
+       memcpy(s->outbuf+skinny_header_size, &req->data, sizeof(skinny_data));
+
+       res = write(s->fd, s->outbuf, letohl(req->len)+8);
        if (res != letohl(req->len)+8) {
                ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
        }
@@ -1017,75 +1299,73 @@ static void transmit_speaker_mode(struct skinnysession *s, int mode)
 {
        skinny_req *req;
 
-       req = req_alloc(sizeof(struct set_speaker_message));
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
                return;
-       }
-       req->len = htolel(sizeof(set_speaker_message)+4);
-       req->e = htolel(SET_SPEAKER_MESSAGE);
+
        req->data.setspeaker.mode = htolel(mode);
        transmit_response(s, req);
 }
+/*
+static void transmit_microphone_mode(struct skinnysession *s, int mode)
+{
+       skinny_req *req;
+
+       if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
+               return;
 
+       req->data.setmicrophone.mode = htolel(mode);
+       transmit_response(s, req);
+}
+*/
 static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
 {
        skinny_req *req;
-       int memsize = sizeof(struct call_state_message);
 
-       req = req_alloc(memsize);
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
                return;
-       }
+
        if (state == SKINNY_ONHOOK) {
                transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
        }
-       req->len = htolel(sizeof(call_state_message)+4);
-       req->e = htolel(CALL_STATE_MESSAGE);
        req->data.callstate.callState = htolel(state);
        req->data.callstate.lineInstance = htolel(instance);
        req->data.callstate.callReference = htolel(callid);
        transmit_response(s, req);
        if (state == SKINNY_OFFHOOK) {
-               memset(req, 0, memsize);
-               req->len = htolel(sizeof(activate_call_plane_message)+4);
-               req->e = htolel(ACTIVATE_CALL_PLANE_MESSAGE);
+               if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
+                       return;
+
                req->data.activatecallplane.lineInstance = htolel(instance);
                transmit_response(s, req);
        } else if (state == SKINNY_ONHOOK) {
-               memset(req, 0, memsize);
-               req->len = htolel(sizeof(activate_call_plane_message)+4);
-               req->e = htolel(ACTIVATE_CALL_PLANE_MESSAGE);
-               req->data.activatecallplane.lineInstance = 0;
+               if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
+                       return;
+
+               req->data.activatecallplane.lineInstance = htolel(instance);
                transmit_response(s, req);
-               memset(req, 0, memsize);
-               req->len = htolel(sizeof(close_receive_channel_message)+4);
-               req->e = htolel(CLOSE_RECEIVE_CHANNEL_MESSAGE);
+
+               if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
+                       return;
+
                req->data.closereceivechannel.conferenceId = 0;
-               req->data.closereceivechannel.partyId = 0;
+               req->data.closereceivechannel.partyId = htolel(callid);
                transmit_response(s, req);
-               memset(req, 0, memsize);
-               req->len = htolel(sizeof(stop_media_transmission_message)+4);
-               req->e = htolel(STOP_MEDIA_TRANSMISSION_MESSAGE);
+
+               if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
+                       return;
+
                req->data.stopmedia.conferenceId = 0;
-               req->data.stopmedia.passThruPartyId = 0;
+               req->data.stopmedia.passThruPartyId = htolel(callid);
                transmit_response(s, req);
        }
 }
 
-static void transmit_callinfo(struct skinnysession *s, char *fromname, char *fromnum, char *toname, char *tonum, int instance, int callid, int calltype)
+static void transmit_callinfo(struct skinnysession *s, const char *fromname, const char *fromnum, const char *toname, const char *tonum, int instance, int callid, int calltype)
 {
        skinny_req *req;
 
-       req = req_alloc(sizeof(struct call_info_message));
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
                return;
-       }
-
-       req->len = htolel(sizeof(struct call_info_message));
-       req->e = htolel(CALL_INFO_MESSAGE);
 
        if (fromname) {
                ast_copy_string(req->data.callinfo.callingPartyName, fromname, sizeof(req->data.callinfo.callingPartyName));
@@ -1105,20 +1385,16 @@ static void transmit_callinfo(struct skinnysession *s, char *fromname, char *fro
        transmit_response(s, req);
 }
 
-static void transmit_connect(struct skinnysession *s)
+static void transmit_connect(struct skinnysession *s, struct skinny_subchannel *sub)
 {
        skinny_req *req;
-       struct skinny_line *l = s->device->lines;
+       struct skinny_line *l = sub->parent;
 
-       req = req_alloc(sizeof(struct open_receive_channel_message));
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct open_receive_channel_message), OPEN_RECEIVE_CHANNEL_MESSAGE)))
                return;
-       }
-       req->len = htolel(sizeof(struct open_receive_channel_message));
-       req->e = htolel(OPEN_RECEIVE_CHANNEL_MESSAGE);
+
        req->data.openreceivechannel.conferenceId = 0;
-       req->data.openreceivechannel.partyId = 0;
+       req->data.openreceivechannel.partyId = htolel(sub->callid);
        req->data.openreceivechannel.packets = htolel(20);
        req->data.openreceivechannel.capability = htolel(convert_cap(l->capability));
        req->data.openreceivechannel.echo = 0;
@@ -1130,59 +1406,46 @@ static void transmit_tone(struct skinnysession *s, int tone)
 {
        skinny_req *req;
 
+       if (tone == SKINNY_NOTONE) {
+               /* This is bad, mmm'kay? */
+               return;
+       }
+
        if (tone > 0) {
-               req = req_alloc(sizeof(struct start_tone_message));
+               if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
+                       return;
        } else {
-               req = req_alloc(4);
-       }
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
-               return;
+               if (!(req = req_alloc(0, STOP_TONE_MESSAGE)))
+                       return;
        }
+
        if (tone > 0) {
-               req->len = htolel(sizeof(start_tone_message)+4);
-               req->e = htolel(START_TONE_MESSAGE);
                req->data.starttone.tone = htolel(tone);
-       } else {
-               req->len = htolel(4);
-               req->e = htolel(STOP_TONE_MESSAGE);
        }
        transmit_response(s, req);
 }
 
-#if 0
-/* XXX need to properly deal with softkeys */
 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
 {
        skinny_req *req;
-       int memsize = sizeof(struct select_soft_keys_message);
 
-       req = req_alloc(memsize);
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
                return;
-       }
-       memset(req, 0, memsize);
-       req->len = htolel(sizeof(select_soft_keys_message)+4);
-       req->e = htolel(SELECT_SOFT_KEYS_MESSAGE);
+
        req->data.selectsoftkey.instance = htolel(instance);
        req->data.selectsoftkey.reference = htolel(callid);
        req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
+       req->data.selectsoftkey.validKeyMask = htolel(0xFFFFFFFF);
        transmit_response(s, req);
 }
-#endif
 
 static void transmit_lamp_indication(struct skinnysession *s, int stimulus, int instance, int indication)
 {
        skinny_req *req;
 
-       req = req_alloc(sizeof(struct set_lamp_message));
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
                return;
-       }
-       req->len = htolel(sizeof(set_lamp_message)+4);
-       req->e = htolel(SET_LAMP_MESSAGE);
+
        req->data.setlamp.stimulus = htolel(stimulus);
        req->data.setlamp.stimulusInstance = htolel(instance);
        req->data.setlamp.deviceStimulus = htolel(indication);
@@ -1193,107 +1456,90 @@ static void transmit_ringer_mode(struct skinnysession *s, int mode)
 {
        skinny_req *req;
 
-       req = req_alloc(sizeof(struct set_ringer_message));
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (skinnydebug)
+               ast_verbose("Setting ringer mode to '%d'.\n", mode);
+
+       if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
                return;
-       }
-       req->len = htolel(sizeof(set_ringer_message)+4);
-       req->e = htolel(SET_RINGER_MESSAGE);
+
        req->data.setringer.ringerMode = htolel(mode);
+       /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
+          Note: The phone will always show as ringing on the display.
+
+          1: phone will audibly ring over and over
+          2: phone will audibly ring only once
+          any other value, will NOT cause the phone to audibly ring
+       */
+       req->data.setringer.unknown1 = htolel(1);
+       /* XXX the value here doesn't seem to change anything.  Must be higher than 0.
+          Perhaps a packet capture can shed some light on this. */
+       req->data.setringer.unknown2 = htolel(1);
        transmit_response(s, req);
 }
 
-static void transmit_displaymessage(struct skinnysession *s, char *text)
+static void transmit_displaymessage(struct skinnysession *s, const char *text)
 {
        skinny_req *req;
 
        if (text == 0) {
-               req = req_alloc(4);
-               if (req) {
-                       req->len = htolel(4);
-                       req->e = htolel(CLEAR_DISPLAY_MESSAGE);
-               }
+               if (!(req = req_alloc(0, CLEAR_DISPLAY_MESSAGE)))
+                       return;
+
+               if (skinnydebug)
+                       ast_verbose("Clearing Display\n");
        } else {
-               req = req_alloc(sizeof(struct displaytext_message));
-               if (req) {
-                       ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
-                       req->len = htolel(sizeof(displaytext_message) + 4);
-                       req->e = htolel(DISPLAYTEXT_MESSAGE);
-                       if (skinnydebug) {
-                               ast_verbose("Displaying message '%s'\n", req->data.displaytext.text);
-                       }
-               }
-       }
+               if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
+                       return;
 
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
-               return;
+               ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
+               if (skinnydebug)
+                       ast_verbose("Displaying message '%s'\n", req->data.displaytext.text);
        }
+
        transmit_response(s, req);
 }
 
-static void transmit_displaynotify(struct skinnysession *s, char *text, int t)
+static void transmit_displaynotify(struct skinnysession *s, const char *text, int t)
 {
        skinny_req *req;
 
-       req = req_alloc(sizeof(struct display_notify_message));
-
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
                return;
-       }
 
-       req->e = htolel(DISPLAY_NOTIFY_MESSAGE);
-       req->len = htolel(sizeof(display_notify_message) + 4);
        ast_copy_string(req->data.displaynotify.displayMessage, text, sizeof(req->data.displaynotify.displayMessage));
        req->data.displaynotify.displayTimeout = htolel(t);
 
-       if (skinnydebug) {
+       if (skinnydebug)
                ast_verbose("Displaying notify '%s'\n", text);
-       }
 
        transmit_response(s, req);
 }
 
-static void transmit_displaypromptstatus(struct skinnysession *s, char *text, int t, int instance, int callid)
+static void transmit_displaypromptstatus(struct skinnysession *s, const char *text, int t, int instance, int callid)
 {
        skinny_req *req;
 
-       req = req_alloc(sizeof(struct display_prompt_status_message));
-
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct display_prompt_status_message), DISPLAY_PROMPT_STATUS_MESSAGE)))
                return;
-       }
 
-       req->e = htolel(DISPLAY_PROMPT_STATUS_MESSAGE);
-       req->len = htolel(sizeof(display_prompt_status_message) + 4);
        ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
        req->data.displaypromptstatus.messageTimeout = htolel(t);
        req->data.displaypromptstatus.lineInstance = htolel(instance);
        req->data.displaypromptstatus.callReference = htolel(callid);
 
-       if (skinnydebug) {
+       if (skinnydebug)
                ast_verbose("Displaying Prompt Status '%s'\n", text);
-       }
 
        transmit_response(s, req);
 }
 
-static void transmit_dialednumber(struct skinnysession *s, char *text, int instance, int callid)
+static void transmit_dialednumber(struct skinnysession *s, const char *text, int instance, int callid)
 {
        skinny_req *req;
 
-       req = req_alloc(sizeof(struct dialed_number_message));
-
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
                return;
-       }
 
-       req->e = htolel(DIALED_NUMBER_MESSAGE);
-       req->len = htolel(sizeof(dialed_number_message) + 4);
        ast_copy_string(req->data.dialednumber.dialedNumber, text, sizeof(req->data.dialednumber.dialedNumber));
        req->data.dialednumber.lineInstance = htolel(instance);
        req->data.dialednumber.callReference = htolel(callid);
@@ -1301,41 +1547,50 @@ static void transmit_dialednumber(struct skinnysession *s, char *text, int insta
        transmit_response(s, req);
 }
 
+/*
 static int has_voicemail(struct skinny_line *l)
 {
        return ast_app_has_voicemail(l->mailbox, NULL);
 }
-
+*/
 
 static void do_housekeeping(struct skinnysession *s)
 {
+/*
        int new;
        int old;
-       struct skinny_subchannel *sub;
-       struct skinny_line *l = s->device->lines;
+       struct skinny_device *d = s->device;
+       struct skinny_line *l;
+*/
 
-       sub = find_subchannel_by_line(l);
        transmit_displaymessage(s, NULL);
 
-       if (has_voicemail(sub->parent)) {
-               if (skinnydebug) {
-                       ast_verbose("Checking for voicemail Skinny %s@%s\n", sub->parent->name, sub->parent->parent->name);
-               }
-               ast_app_inboxcount(sub->parent->mailbox, &new, &old);
-               if (skinnydebug) {
-                       ast_verbose("Skinny %s@%s has voicemail!\n", sub->parent->name, sub->parent->parent->name);
+/*
+       for (l = d->lines; l; l = l->next) {
+               if (has_voicemail(l)) {
+                       if (skinnydebug)
+                               ast_verbose("Checking for voicemail Skinny %s@%s\n", l->name, d->name);
+                       ast_app_inboxcount(l->mailbox, &new, &old);
+                       if (skinnydebug)
+                               ast_verbose("Skinny %s@%s has voicemail!\n", l->name, d->name);
+                       transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, l->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
+               } else {
+                       transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, SKINNY_LAMP_OFF);
                }
-               transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, l->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
-       } else {
-               transmit_lamp_indication(s, STIMULUS_VOICEMAIL, l->instance, SKINNY_LAMP_OFF);
        }
-
+*/
 }
 
 /* I do not believe skinny can deal with video.
    Anyone know differently? */
+/* Yes, it can.  Currently 7985 and Cisco VT Advantage do video. */
 static struct ast_rtp *skinny_get_vrtp_peer(struct ast_channel *chan)
 {
+       struct skinny_subchannel *sub;
+       sub = chan->tech_pvt;
+       if (sub && sub->vrtp) {
+               return sub->vrtp;
+       }
        return NULL;
 }
 
@@ -1387,36 +1642,87 @@ static int skinny_no_debug(int fd, int argc, char *argv[])
        return RESULT_SUCCESS;
 }
 
-static int skinny_show_devices(int fd, int argc, char *argv[])
+static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
 {
        struct skinny_device *d;
-       struct skinny_line *l;
-       int numlines = 0;
-       char iabuf[INET_ADDRSTRLEN];
 
-       if (argc != 3) {
+       char *result = NULL;
+       int wordlen = strlen(word);
+       int which = 0;
+
+       if (pos == 2) {
+               for (d = devices; d && !result; d = d->next) {
+                       if (!strncasecmp(word, d->id, wordlen) && ++which > state)
+                               result = ast_strdup(d->id);
+               }
+       }
+
+       return result;
+}
+
+static int skinny_reset_device(int fd, int argc, char *argv[])
+{
+       struct skinny_device *d;
+       skinny_req *req;
+
+       if (argc < 3 || argc > 4) {
                return RESULT_SHOWUSAGE;
        }
        ast_mutex_lock(&devicelock);
-       d = devices;
 
-       ast_cli(fd, "Name                 DeviceId         IP              TypeId R Model  NL\n");
-       ast_cli(fd, "-------------------- ---------------- --------------- ------ - ------ --\n");
-       while(d) {
-               l = d->lines;
+       for (d = devices; d; d = d->next) {
+               int fullrestart = 0;
+               if (!strcasecmp(argv[2], d->id) || !strcasecmp(argv[2], "all")) {
+                       if (!(d->session))
+                               continue;
+
+                       if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
+                               continue;
+
+                       if (argc == 4 && !strcasecmp(argv[3], "restart"))
+                               fullrestart = 1;
+
+                       if (fullrestart)
+                               req->data.reset.resetType = 2;
+                       else
+                               req->data.reset.resetType = 1;
+
+                       if (option_verbose > 2)
+                               ast_verbose(VERBOSE_PREFIX_3 "%s device %s.\n", (fullrestart) ? "Restarting" : "Resetting", d->id);
+                       transmit_response(d->session, req);
+               }
+       }
+       ast_mutex_unlock(&devicelock);
+       return RESULT_SUCCESS;
+}
+
+static int skinny_show_devices(int fd, int argc, char *argv[])
+{
+       struct skinny_device *d;
+       struct skinny_line *l;
+       int numlines = 0;
+       char iabuf[INET_ADDRSTRLEN];
+
+       if (argc != 3) {
+               return RESULT_SHOWUSAGE;
+       }
+       ast_mutex_lock(&devicelock);
+
+       ast_cli(fd, "Name                 DeviceId         IP              TypeId R NL\n");
+       ast_cli(fd, "-------------------- ---------------- --------------- ------ - --\n");
+       for (d = devices; d; d = d->next) {
                numlines = 0;
-               while(l) { numlines++; l = l->next; }
+               for (l = d->lines; l; l = l->next) {
+                       numlines++;
+               }
 
-               ast_cli(fd, "%-20s %-16s %-16s %6X %c %-6s %2d\n",
+               ast_cli(fd, "%-20s %-16s %-15s %6X %c %2d\n",
                                d->name,
                                d->id,
                                ast_inet_ntoa(iabuf, sizeof(iabuf), d->addr.sin_addr),
                                d->type,
                                d->registered?'Y':'N',
-                               d->model,
                                numlines);
-
-               d = d->next;
        }
        ast_mutex_unlock(&devicelock);
        return RESULT_SUCCESS;
@@ -1431,21 +1737,19 @@ static int skinny_show_lines(int fd, int argc, char *argv[])
                return RESULT_SHOWUSAGE;
        }
        ast_mutex_lock(&devicelock);
-       d = devices;
-       while(d) {
-               l = d->lines;
-               while (l) {
-                       ast_cli(fd, "%-20s %2d %-20s %-20s  %c  %c\n",
-                               l->parent->name,
+
+       ast_cli(fd, "Device Name          Instance Name                 Label               \n");
+       ast_cli(fd, "-------------------- -------- -------------------- --------------------\n");
+       for (d = devices; d; d = d->next) {
+               for (l = d->lines; l; l = l->next) {
+                       ast_cli(fd, "%-20s %8d %-20s %-20s\n",
+                               d->name,
                                l->instance,
                                l->name,
-                               l->label,
-                               l->sub->owner?'Y':'N',
-                               l->sub->rtp?'Y':'N');
-                       l = l->next;
+                               l->label);
                }
-               d = d->next;
        }
+
        ast_mutex_unlock(&devicelock);
        return RESULT_SUCCESS;
 }
@@ -1466,6 +1770,10 @@ static char no_debug_usage[] =
 "Usage: skinny no debug\n"
 "       Disables dumping of Skinny packets for debugging purposes\n";
 
+static char reset_usage[] =
+"Usage: skinny reset <DeviceId|all> [restart]\n"
+"       Causes a Skinny device to reset itself, optionally with a full restart\n";
+
 static struct ast_cli_entry cli_show_devices =
        { { "skinny", "show", "devices", NULL }, skinny_show_devices, "Show defined Skinny devices", show_devices_usage };
 
@@ -1478,24 +1786,31 @@ static struct ast_cli_entry cli_debug =
 static struct ast_cli_entry cli_no_debug =
        { { "skinny", "no", "debug", NULL }, skinny_no_debug, "Disable Skinny debugging", no_debug_usage };
 
+static struct ast_cli_entry cli_reset_device =
+       { { "skinny", "reset", NULL }, skinny_reset_device, "Reset Skinny device(s)", reset_usage, complete_skinny_reset };
+
 #if 0
-static struct skinny_paging_device *build_paging_device(char *cat, struct ast_variable *v)
+static struct skinny_paging_device *build_paging_device(const char *cat, struct ast_variable *v)
 {
        return NULL;
 }
 #endif
 
-static struct skinny_device *build_device(char *cat, struct ast_variable *v)
+static struct skinny_device *build_device(const char *cat, struct ast_variable *v)
 {
        struct skinny_device *d;
        struct skinny_line *l;
-       struct skinny_subchannel *sub;
-       int i=0, y=0;
+       struct skinny_speeddial *sd;
+       struct skinny_addon *a;
+       int lineInstance = 1;
+       int speeddialInstance = 1;
+       int y = 0;
 
-       d = malloc(sizeof(struct skinny_device));
-       if (d) {
-               memset(d, 0, sizeof(struct skinny_device));
+       if (!(d = ast_calloc(1, sizeof(struct skinny_device)))) {
+               return NULL;
+       } else {
                ast_copy_string(d->name, cat, sizeof(d->name));
+               d->lastlineinstance = 1;
                while(v) {
                        if (!strcasecmp(v->name, "host")) {
                                if (ast_get_ip(&d->addr, v->value)) {
@@ -1505,17 +1820,15 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
                        } else if (!strcasecmp(v->name, "port")) {
                                d->addr.sin_port = htons(atoi(v->value));
                        } else if (!strcasecmp(v->name, "device")) {
-                               strncpy(d->id, v->value, sizeof(d->id)-1);
+                               ast_copy_string(d->id, v->value, sizeof(d->id));
                        } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
                                d->ha = ast_append_ha(v->name, v->value, d->ha);
                        } else if (!strcasecmp(v->name, "context")) {
-                               strncpy(context, v->value, sizeof(context) - 1);
+                               ast_copy_string(context, v->value, sizeof(context));
                        } else if (!strcasecmp(v->name, "version")) {
-                               strncpy(d->version_id, v->value, sizeof(d->version_id) -1);
+                               ast_copy_string(d->version_id, v->value, sizeof(d->version_id));
                        } else if (!strcasecmp(v->name, "nat")) {
                                nat = ast_true(v->value);
-                       } else if (!strcasecmp(v->name, "model")) {
-                               strncpy(d->model, v->value, sizeof(d->model) - 1);
                        } else if (!strcasecmp(v->name, "callerid")) {
                                if (!strcasecmp(v->value, "asreceived")) {
                                        cid_num[0] = '\0';
@@ -1524,9 +1837,9 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
                                        ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
                                }
                        } else if (!strcasecmp(v->name, "language")) {
-                               strncpy(language, v->value, sizeof(language)-1);
+                               ast_copy_string(language, v->value, sizeof(language));
                        } else if (!strcasecmp(v->name, "accountcode")) {
-                               strncpy(accountcode, v->value, sizeof(accountcode)-1);
+                               ast_copy_string(accountcode, v->value, sizeof(accountcode));
                        } else if (!strcasecmp(v->name, "amaflags")) {
                                y = ast_cdr_amaflags2int(v->value);
                                if (y < 0) {
@@ -1535,7 +1848,7 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
                                        amaflags = y;
                                }
                        } else if (!strcasecmp(v->name, "musiconhold")) {
-                               strncpy(musicclass, v->value, sizeof(musicclass)-1);
+                               ast_copy_string(musicclass, v->value, sizeof(musicclass));
                        } else if (!strcasecmp(v->name, "callgroup")) {
                                cur_callergroup = ast_get_group(v->value);
                        } else if (!strcasecmp(v->name, "pickupgroup")) {
@@ -1545,7 +1858,7 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
                        } else if (!strcasecmp(v->name, "cancallforward")) {
                                cancallforward = ast_true(v->value);
                        } else if (!strcasecmp(v->name, "mailbox")) {
-                               strncpy(mailbox, v->value, sizeof(mailbox) -1);
+                               ast_copy_string(mailbox, v->value, sizeof(mailbox));
                        } else if (!strcasecmp(v->name, "callreturn")) {
                                callreturn = ast_true(v->value);
                        } else if (!strcasecmp(v->name, "callwaiting")) {
@@ -1557,23 +1870,52 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
                        } else if (!strcasecmp(v->name, "mwiblink")) {
                                mwiblink = ast_true(v->value);
                        } else if (!strcasecmp(v->name, "linelabel")) {
-                               strncpy(linelabel, v->value, sizeof(linelabel)-1);
+                               ast_copy_string(linelabel, v->value, sizeof(linelabel));
+                       } else if (!strcasecmp(v->name, "speeddial")) {
+                               if (!(sd = ast_calloc(1, sizeof(struct skinny_speeddial)))) {
+                                       return NULL;
+                               } else {
+                                       char *stringp, *exten, *label;
+                                       stringp = v->value;
+                                       exten = strsep(&stringp, ",");
+                                       label = strsep(&stringp, ",");
+                                       ast_mutex_init(&sd->lock);
+                                       ast_copy_string(sd->exten, exten, sizeof(sd->exten));
+                                       if (label)
+                                               ast_copy_string(sd->label, label, sizeof(sd->label));
+                                       else
+                                               ast_copy_string(sd->label, exten, sizeof(sd->label));
+                                       sd->instance = speeddialInstance++;
+
+                                       sd->next = d->speeddials;
+                                       d->speeddials = sd;
+                               }
+                       } else if (!strcasecmp(v->name, "addon")) {
+                               if (!(a = ast_calloc(1, sizeof(struct skinny_addon)))) {
+                                       return NULL;
+                               } else {
+                                       ast_mutex_init(&a->lock);
+                                       ast_copy_string(a->type, v->value, sizeof(a->type));
+
+                                       a->next = d->addons;
+                                       d->addons = a;
+                               }
                        } else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
-                               l = malloc(sizeof(struct skinny_line));;
-                               if (l) {
-                                       memset(l, 0, sizeof(struct skinny_line));
+                               if (!(l = ast_calloc(1, sizeof(struct skinny_line)))) {
+                                       return NULL;
+                               } else {
                                        ast_mutex_init(&l->lock);
-                                       strncpy(l->name, v->value, sizeof(l->name) - 1);
+                                       ast_copy_string(l->name, v->value, sizeof(l->name));
 
                                        /* XXX Should we check for uniqueness?? XXX */
-                                       strncpy(l->context, context, sizeof(l->context) - 1);
-                                       strncpy(l->cid_num, cid_num, sizeof(l->cid_num) - 1);
-                                       strncpy(l->cid_name, cid_name, sizeof(l->cid_name) - 1);
-                                       strncpy(l->label, linelabel, sizeof(l->label) - 1);
-                                       strncpy(l->language, language, sizeof(l->language) - 1);
-                                       strncpy(l->musicclass, musicclass, sizeof(l->musicclass)-1);
-                                       strncpy(l->mailbox, mailbox, sizeof(l->mailbox)-1);
-                                       strncpy(l->mailbox, mailbox, sizeof(l->mailbox)-1);
+                                       ast_copy_string(l->context, context, sizeof(l->context));
+                                       ast_copy_string(l->cid_num, cid_num, sizeof(l->cid_num));
+                                       ast_copy_string(l->cid_name, cid_name, sizeof(l->cid_name));
+                                       ast_copy_string(l->label, linelabel, sizeof(l->label));
+                                       ast_copy_string(l->language, language, sizeof(l->language));
+                                       ast_copy_string(l->musicclass, musicclass, sizeof(l->musicclass));
+                                       ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
+                                       ast_copy_string(l->mailbox, mailbox, sizeof(l->mailbox));
                                        if (!ast_strlen_zero(mailbox)) {
                                                if (option_verbose > 2)
                                                        ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
@@ -1596,37 +1938,13 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
                                        l->threewaycalling = threewaycalling;
                                        l->mwiblink = mwiblink;
                                        l->onhooktime = time(NULL);
-                                       l->instance = 1;
+                                       l->instance = lineInstance++;
                                        /* ASSUME we're onhook at this point */
                                        l->hookstate = SKINNY_ONHOOK;
+                                       l->nat = nat;
 
-                                       for (i = 0; i < MAX_SUBS; i++) {
-                                               sub = malloc(sizeof(struct skinny_subchannel));
-                                               if (sub) {
-                                                       if (option_verbose > 2)
-                                                               ast_verbose(VERBOSE_PREFIX_3 "Allocating Skinny subchannel '%d' on %s@%s\n", i, l->name, d->name);
-                                                       memset(sub, 0, sizeof(struct skinny_subchannel));
-                                                       ast_mutex_init(&sub->lock);
-                                                       sub->parent = l;
-                                                       /* Make a call*ID */
-                                                       sub->callid = callnums;
-                                                       callnums++;
-                                                       sub->cxmode = SKINNY_CX_INACTIVE;
-                                                       sub->nat = nat;
-                                                       sub->next = l->sub;
-                                                       l->sub = sub;
-                                               } else {
-                                                       /* XXX Should find a way to clean up our memory */
-                                                       ast_log(LOG_WARNING, "Out of memory allocating subchannel");
-                                                       return NULL;
-                                               }
-                                       }
                                        l->next = d->lines;
                                        d->lines = l;
-                               } else {
-                                       /* XXX Should find a way to clean up our memory */
-                                       ast_log(LOG_WARNING, "Out of memory allocating line");
-                                       return NULL;
                                }
                        } else {
                                ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
@@ -1638,16 +1956,20 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
                        ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
                        return NULL;
                }
-               if (d->addr.sin_addr.s_addr && !ntohs(d->addr.sin_port)) {
+               if (/*d->addr.sin_addr.s_addr && */!ntohs(d->addr.sin_port)) {
                        d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
                }
+#if 0
+               /* I don't think we need this anymore at all, since d->ourip is set in skinny_register now */
                if (d->addr.sin_addr.s_addr) {
+                       /* XXX See note above, in 'host' option. */
                        if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {
-                               memcpy(&d->ourip, &__ourip, sizeof(d->ourip));
+                               d->ourip = __ourip;
                        }
                } else {
-                       memcpy(&d->ourip, &__ourip, sizeof(d->ourip));
+                       d->ourip = __ourip;
                }
+#endif
        }
        return d;
 }
@@ -1655,6 +1977,8 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
 static int skinny_register(skinny_req *req, struct skinnysession *s)
 {
        struct skinny_device *d;
+       struct sockaddr_in sin;
+       socklen_t slen;
 
        ast_mutex_lock(&devicelock);
        for (d = devices; d; d = d->next) {
@@ -1663,19 +1987,45 @@ static int skinny_register(skinny_req *req, struct skinnysession *s)
                        s->device = d;
                        d->type = letohl(req->data.reg.type);
                        if (ast_strlen_zero(d->version_id)) {
-                               strncpy(d->version_id, version_id, sizeof(d->version_id) - 1);
+                               ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
                        }
                        d->registered = 1;
                        d->session = s;
+
+                       slen = sizeof(sin);
+                       if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
+                               ast_log(LOG_WARNING, "Cannot get socket name\n");
+                               sin.sin_addr = __ourip;
+                       }
+                       d->ourip = sin.sin_addr;
                        break;
                }
        }
        ast_mutex_unlock(&devicelock);
-       return d ? 1 : 0;
+       if (!d) {
+               return 0;
+       }
+       return 1;
+}
+
+static int skinny_unregister(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d;
+
+       d = s->device;
+
+       if (d) {
+               d->session = NULL;
+               d->registered = 0;
+       }
+
+       return -1; /* main loop will destroy the session */
 }
 
 static void start_rtp(struct skinny_subchannel *sub)
 {
+       struct skinny_line *l = sub->parent;
+       struct skinny_device *d = l->parent;
        ast_mutex_lock(&sub->lock);
        /* Allocate the RTP */
        sub->rtp = ast_rtp_new(sched, io, 1, 0);
@@ -1683,10 +2033,10 @@ static void start_rtp(struct skinny_subchannel *sub)
                sub->owner->fds[0] = ast_rtp_fd(sub->rtp);
        }
        if (sub->rtp) {
-               ast_rtp_setnat(sub->rtp, sub->nat);
+               ast_rtp_setnat(sub->rtp, l->nat);
        }
        /* Create the RTP connection */
-       transmit_connect(sub->parent->parent->session);
+       transmit_connect(d->session, sub);
        ast_mutex_unlock(&sub->lock);
 }
 
@@ -1695,7 +2045,8 @@ static void *skinny_ss(void *data)
        struct ast_channel *chan = data;
        struct skinny_subchannel *sub = chan->tech_pvt;
        struct skinny_line *l = sub->parent;
-       struct skinnysession *s = l->parent->session;
+       struct skinny_device *d = l->parent;
+       struct skinnysession *s = d->session;
        char exten[AST_MAX_EXTENSION] = "";
        int len = 0;
        int timeout = firstdigittimeout;
@@ -1703,14 +2054,13 @@ static void *skinny_ss(void *data)
        int getforward=0;
 
        if (option_verbose > 2)
-               ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s@%s'\n", l->name, l->parent->name);
+               ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s@%s'\n", l->name, d->name);
        while (len < AST_MAX_EXTENSION-1) {
                res = ast_waitfordigit(chan, timeout);
                timeout = 0;
                if (res < 0) {
-                       if (skinnydebug) {
-                               ast_verbose("Skinny(%s@%s): waitfordigit returned < 0\n", l->name, l->parent->name);
-                       }
+                       if (skinnydebug)
+                               ast_verbose("Skinny(%s@%s): waitfordigit returned < 0\n", l->name, d->name);
                        ast_indicate(chan, -1);
                        ast_hangup(chan);
                        return NULL;
@@ -1725,7 +2075,7 @@ static void *skinny_ss(void *data)
                        if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, l->cid_num)) {
                                if (getforward) {
                                        /* Record this as the forwarding extension */
-                                       strncpy(l->call_forward, exten, sizeof(l->call_forward) - 1);
+                                       ast_copy_string(l->call_forward, exten, sizeof(l->call_forward));
                                        if (option_verbose > 2)
                                                ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %s\n",
                                                        l->call_forward, chan->name);
@@ -1741,20 +2091,21 @@ static void *skinny_ss(void *data)
                                        len = 0;
                                        getforward = 0;
                                } else {
-                                       strncpy(chan->exten, exten, sizeof(chan->exten)-1);
+                                       ast_copy_string(chan->exten, exten, sizeof(chan->exten));
+                                       ast_copy_string(l->lastnumberdialed, exten, sizeof(l->lastnumberdialed));
                                        if (!ast_strlen_zero(l->cid_num)) {
                                                if (!l->hidecallerid) {
                                                        chan->cid.cid_num = strdup(l->cid_num);
                                                        chan->cid.cid_ani = strdup(l->cid_num);
                                                }
-                                               ast_setstate(chan, AST_STATE_RING);
-                                               res = ast_pbx_run(chan);
-                                               if (res) {
-                                                       ast_log(LOG_WARNING, "PBX exited non-zero\n");
-                                                       transmit_tone(s, SKINNY_REORDER);
-                                               }
-                                               return NULL;
                                        }
+                                       ast_setstate(chan, AST_STATE_RING);
+                                       res = ast_pbx_run(chan);
+                                       if (res) {
+                                               ast_log(LOG_WARNING, "PBX exited non-zero\n");
+                                               transmit_tone(s, SKINNY_REORDER);
+                                       }
+                                       return NULL;
                                }
                        } else {
                                /* It's a match, but they just typed a digit, and there is an ambiguous match,
@@ -1766,122 +2117,6 @@ static void *skinny_ss(void *data)
                        transmit_tone(s, SKINNY_REORDER);
                        ast_hangup(chan);
                        return NULL;
-               } else if (l->callwaiting && !strcmp(exten, "*70")) {
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
-                       /* Disable call waiting if enabled */
-                       l->callwaiting = 0;
-                       transmit_tone(s, SKINNY_DIALTONE);
-                       len = 0;
-                       memset(exten, 0, sizeof(exten));
-                       timeout = firstdigittimeout;
-               } else if (!strcmp(exten,ast_pickup_ext())) {
-                       /* Scan all channels and see if any there
-                        * ringing channels with that have call groups
-                        * that equal this channels pickup group
-                        */
-                       if (ast_pickup_call(chan)) {
-                               ast_log(LOG_WARNING, "No call pickup possible...\n");
-                               transmit_tone(s, SKINNY_REORDER);
-                       }
-                       ast_hangup(chan);
-                       return NULL;
-               } else if (!l->hidecallerid && !strcmp(exten, "*67")) {
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
-                       /* Disable Caller*ID if enabled */
-                       l->hidecallerid = 1;
-                       if (chan->cid.cid_num) {
-                               free(chan->cid.cid_num);
-                       }
-                       chan->cid.cid_num = NULL;
-                       if (chan->cid.cid_name) {
-                               free(chan->cid.cid_name);
-                       }
-                       chan->cid.cid_name = NULL;
-                       transmit_tone(s, SKINNY_DIALTONE);
-                       len = 0;
-                       memset(exten, 0, sizeof(exten));
-                       timeout = firstdigittimeout;
-               } else if (l->callreturn && !strcmp(exten, "*69")) {
-                       res = 0;
-                       if (!ast_strlen_zero(l->lastcallerid)) {
-                               res = ast_say_digit_str(chan, l->lastcallerid, "", chan->language);
-                       }
-                       if (!res) {
-                               transmit_tone(s, SKINNY_DIALTONE);
-                       }
-                       break;
-               } else if (!strcmp(exten, "*78")) {
-                       /* Do not disturb */
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %s\n", chan->name);
-                       transmit_tone(s, SKINNY_DIALTONE);
-                       l->dnd = 1;
-                       getforward = 0;
-                       memset(exten, 0, sizeof(exten));
-                       len = 0;
-               } else if (!strcmp(exten, "*79")) {
-                       /* Do not disturb */
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %s\n", chan->name);
-                       transmit_tone(s, SKINNY_DIALTONE);
-                       l->dnd = 0;
-                       getforward = 0;
-                       memset(exten, 0, sizeof(exten));
-                       len = 0;
-               } else if (l->cancallforward && !strcmp(exten, "*72")) {
-                       transmit_tone(s, SKINNY_DIALTONE);
-                       getforward = 1;
-                       memset(exten, 0, sizeof(exten));
-                       len = 0;
-               } else if (l->cancallforward && !strcmp(exten, "*73")) {
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %s\n", chan->name);
-                       transmit_tone(s, SKINNY_DIALTONE);
-                       memset(l->call_forward, 0, sizeof(l->call_forward));
-                       getforward = 0;
-                       memset(exten, 0, sizeof(exten));
-                       len = 0;
-               } else if (!strcmp(exten, ast_parking_ext()) &&
-                       sub->next->owner &&
-                       ast_bridged_channel(sub->next->owner)) {
-                       /* This is a three way call, the main call being a real channel,
-                          and we're parking the first call. */
-                       ast_masq_park_call(ast_bridged_channel(sub->next->owner), chan, 0, NULL);
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
-                       break;
-               } else if (!ast_strlen_zero(l->lastcallerid) && !strcmp(exten, "*60")) {
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", l->lastcallerid);
-                       res = ast_db_put("blacklist", l->lastcallerid, "1");
-                       if (!res) {
-                               transmit_tone(s, SKINNY_DIALTONE);
-                               memset(exten, 0, sizeof(exten));
-                               len = 0;
-                       }
-               } else if (l->hidecallerid && !strcmp(exten, "*82")) {
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
-                       /* Enable Caller*ID if enabled */
-                       l->hidecallerid = 0;
-                       if (chan->cid.cid_num) {
-                               free(chan->cid.cid_num);
-                       }
-                       if (!ast_strlen_zero(l->cid_num)) {
-                               chan->cid.cid_num = strdup(l->cid_num);
-                       }
-                       if (chan->cid.cid_name) {
-                               free(chan->cid.cid_name);
-                       }
-                       if (!ast_strlen_zero(l->cid_name)) {
-                               chan->cid.cid_name = strdup(l->cid_name);
-                       }
-                       transmit_tone(s, SKINNY_DIALTONE);
-                       len = 0;
-                       memset(exten, 0, sizeof(exten));
-                       timeout = firstdigittimeout;
                } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
                           ((exten[0] != '*') || (!ast_strlen_zero(exten) > 2))) {
                        ast_log(LOG_WARNING, "Can't match [%s] from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "<Unknown Caller>", chan->context);
@@ -1907,15 +2142,12 @@ static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
 {
        int res = 0;
        int tone = 0;
-       struct skinny_line *l;
-       struct skinny_subchannel *sub;
-       struct skinnysession *session;
-
-       sub = ast->tech_pvt;
-       l = sub->parent;
-       session = l->parent->session;
+       struct skinny_subchannel *sub = ast->tech_pvt;
+       struct skinny_line *l = sub->parent;
+       struct skinny_device *d = l->parent;
+       struct skinnysession *session = d->session;
 
-       if (!l->parent->registered) {
+       if (!d->registered) {
                ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
                return -1;
        }
@@ -1925,9 +2157,8 @@ static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
                return -1;
        }
 
-       if (skinnydebug) {
+       if (skinnydebug)
                ast_verbose(VERBOSE_PREFIX_3 "skinny_call(%s)\n", ast->name);
-       }
 
        if (l->dnd) {
                ast_queue_control(ast, AST_CONTROL_BUSY);
@@ -1949,40 +2180,11 @@ static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
        transmit_lamp_indication(session, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
        transmit_ringer_mode(session, SKINNY_RING_INSIDE);
 
-       if (ast->cid.cid_num) {
-               char ciddisplay[41];
-               char *work;
-               size_t size = sizeof(ciddisplay);
-               int l = strlen(ast->cid.cid_num);
-
-               /* For now, we'll assume that if it is 10 numbers, it is a standard NANPA number */
-               if (l == 10) {
-                       ast_build_string(&work, &size, "(xxx)xxx-xxxx      %s",
-                                        ast->cid.cid_name ? ast->cid.cid_name : "");
-                       memcpy(&ciddisplay[1], ast->cid.cid_num, 3);
-                       memcpy(&ciddisplay[5], &ast->cid.cid_num[3], 3);
-                       memcpy(&ciddisplay[9], &ast->cid.cid_num[6], 4);
-               } else {
-                       if (l < sizeof(ciddisplay)) {
-                               ast_build_string(&work, &size, "%s -- %s", ast->cid.cid_num,
-                                                S_OR(ast->cid.cid_name, ""));
-                       } else {
-                               strncpy(ciddisplay, "Number too long!", 15);    /* XXX what is magic in 15 ? */
-                       }
-               }
-               if (skinnydebug) {
-                       ast_verbose("Trying to send: '%s'\n",ciddisplay);
-               }
-               transmit_displaymessage(session, ciddisplay);
-       } else {
-               transmit_displaymessage(session, "Unknown Name");
-       }
        transmit_tone(session, tone);
        transmit_callstate(session, l->instance, SKINNY_RINGIN, sub->callid);
        transmit_displaypromptstatus(session, "Ring-In", 0, l->instance, sub->callid);
        transmit_callinfo(session, ast->cid.cid_name, ast->cid.cid_num, l->cid_name, l->cid_num, l->instance, sub->callid, 1);
-
-       /* XXX need to deal with softkeys */
+       transmit_selectsoftkeys(session, l->instance, sub->callid, KEYDEF_RINGIN);
 
        ast_setstate(ast, AST_STATE_RINGING);
        ast_queue_control(ast, AST_CONTROL_RINGING);
@@ -1993,24 +2195,27 @@ static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
 static int skinny_hangup(struct ast_channel *ast)
 {
        struct skinny_subchannel *sub = ast->tech_pvt;
-       struct skinny_line *l = sub->parent;
-       struct skinnysession *s = l->parent->session;
+       struct skinny_line *l;
+       struct skinny_device *d;
+       struct skinnysession *s;
 
-       if (skinnydebug) {
-               ast_verbose("skinny_hangup(%s) on %s@%s\n", ast->name, l->name, l->parent->name);
-       }
-       if (!ast->tech_pvt) {
+       if (!sub) {
                ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
                return 0;
        }
-
-       if (l->parent->registered) {
-               if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_OFFHOOK)) {
-                       sub->parent->hookstate = SKINNY_ONHOOK;
+       l = sub->parent;
+       d = l->parent;
+       s = d->session;
+       if (skinnydebug)
+               ast_verbose("skinny_hangup(%s) on %s@%s\n", ast->name, l->name, d->name);
+
+       if (d->registered) {
+               if ((l->type = TYPE_LINE) && (l->hookstate == SKINNY_OFFHOOK)) {
+                       l->hookstate = SKINNY_ONHOOK;
                        transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
                        transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
                        transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
-               } else if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_ONHOOK)) {
+               } else if ((l->type = TYPE_LINE) && (l->hookstate == SKINNY_ONHOOK)) {
                        transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
                        transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
                        transmit_ringer_mode(s, SKINNY_RING_OFF);
@@ -2037,37 +2242,39 @@ static int skinny_answer(struct ast_channel *ast)
        int res = 0;
        struct skinny_subchannel *sub = ast->tech_pvt;
        struct skinny_line *l = sub->parent;
-       struct skinnysession *s = l->parent->session;
+       struct skinny_device *d = l->parent;
+       struct skinnysession *s = d->session;
 
        sub->cxmode = SKINNY_CX_SENDRECV;
        if (!sub->rtp) {
                start_rtp(sub);
        }
-       if (skinnydebug) {
-               ast_verbose("skinny_answer(%s) on %s@%s-%d\n", ast->name, l->name, l->parent->name, sub->callid);
-       }
+       if (skinnydebug)
+               ast_verbose("skinny_answer(%s) on %s@%s-%d\n", ast->name, l->name, d->name, sub->callid);
        if (ast->_state != AST_STATE_UP) {
                ast_setstate(ast, AST_STATE_UP);
        }
-       transmit_tone(s, SKINNY_NOTONE);
+       transmit_tone(s, SKINNY_SILENCE);
        transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
        transmit_displaypromptstatus(s, "Connected", 0, l->instance, sub->callid);
        return res;
 }
 
+/* Retrieve audio/etc from channel.  Assumes sub->lock is already held. */
 static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
 {
-       /* Retrieve audio/etc from channel.  Assumes sub->lock is already held. */
+       struct ast_channel *ast = sub->owner;
        struct ast_frame *f;
+
        f = ast_rtp_read(sub->rtp);
-       if (sub->owner) {
+       if (ast) {
                /* We already hold the channel lock */
                if (f->frametype == AST_FRAME_VOICE) {
-                       if (f->subclass != sub->owner->nativeformats) {
+                       if (f->subclass != ast->nativeformats) {
                                ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
-                               sub->owner->nativeformats = f->subclass;
-                               ast_set_read_format(sub->owner, sub->owner->readformat);
-                               ast_set_write_format(sub->owner, sub->owner->writeformat);
+                               ast->nativeformats = f->subclass;
+                               ast_set_read_format(ast, ast->readformat);
+                               ast_set_write_format(ast, ast->writeformat);
                        }
                }
        }
@@ -2128,10 +2335,12 @@ static int skinny_senddigit(struct ast_channel *ast, char digit)
 {
 #if 0
        struct skinny_subchannel *sub = ast->tech_pvt;
+       struct skinny_line *l = sub->parent;
+       struct skinny_device *d = l->parent;
        int tmp;
        /* not right */
        sprintf(tmp, "%d", digit);
-       transmit_tone(sub->parent->parent->session, digit);
+       transmit_tone(d->session, digit);
 #endif
        return -1;
 }
@@ -2186,11 +2395,11 @@ static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, s
 {
        struct skinny_subchannel *sub = ast->tech_pvt;
        struct skinny_line *l = sub->parent;
-       struct skinnysession *s = l->parent->session;
+       struct skinny_device *d = l->parent;
+       struct skinnysession *s = d->session;
 
-       if (skinnydebug) {
+       if (skinnydebug)
                ast_verbose(VERBOSE_PREFIX_3 "Asked to indicate '%s' condition on channel %s\n", control2str(ind), ast->name);
-       }
        switch(ind) {
        case AST_CONTROL_RINGING:
                if (ast->_state != AST_STATE_UP) {
@@ -2245,23 +2454,45 @@ static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, s
        return 0;
 }
 
-static struct ast_channel *skinny_new(struct skinny_subchannel *sub, int state)
+static struct ast_channel *skinny_new(struct skinny_line *l, int state)
 {
        struct ast_channel *tmp;
-       struct skinny_line *l = sub->parent;
+       struct skinny_subchannel *sub;
+       struct skinny_device *d = l->parent;
        int fmt;
-       l = sub->parent;
+
        tmp = ast_channel_alloc(1);
-       if (tmp) {
+       if (!tmp) {
+               ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
+               return NULL;
+       } else {
+               sub = ast_calloc(1, sizeof(struct skinny_subchannel));
+               if (!sub) {
+                       ast_log(LOG_WARNING, "Unable to allocate Skinny subchannel\n");
+                       return NULL;
+               } else {
+                       ast_mutex_init(&sub->lock);
+
+                       sub->owner = tmp;
+                       sub->callid = callnums++;
+                       d->lastlineinstance = l->instance;
+                       d->lastcallreference = sub->callid;
+                       sub->cxmode = SKINNY_CX_INACTIVE;
+                       sub->nat = l->nat;
+                       sub->parent = l;
+
+                       sub->next = l->sub;
+                       l->sub = sub;
+               }
                tmp->tech = &skinny_tech;
+               tmp->tech_pvt = sub;
                tmp->nativeformats = l->capability;
                if (!tmp->nativeformats)
                        tmp->nativeformats = capability;
                fmt = ast_best_codec(tmp->nativeformats);
-               if (skinnydebug) {
+               if (skinnydebug)
                        ast_verbose("skinny_new: tmp->nativeformats=%d fmt=%d\n", tmp->nativeformats, fmt);
-               }
-               ast_string_field_build(tmp, name, "Skinny/%s@%s-%d", l->name, l->parent->name, sub->callid);
+               ast_string_field_build(tmp, name, "Skinny/%s@%s-%d", l->name, d->name, sub->callid);
                if (sub->rtp) {
                        tmp->fds[0] = ast_rtp_fd(sub->rtp);
                }
@@ -2273,14 +2504,13 @@ static struct ast_channel *skinny_new(struct skinny_subchannel *sub, int state)
                tmp->rawwriteformat = fmt;
                tmp->readformat = fmt;
                tmp->rawreadformat = fmt;
-               tmp->tech_pvt = sub;
                if (!ast_strlen_zero(l->language))
                        ast_string_field_set(tmp, language, l->language);
                if (!ast_strlen_zero(l->accountcode))
                        ast_string_field_set(tmp, accountcode, l->accountcode);
                if (l->amaflags)
                        tmp->amaflags = l->amaflags;
-               sub->owner = tmp;
+
                ast_mutex_lock(&usecnt_lock);
                usecnt++;
                ast_mutex_unlock(&usecnt_lock);
@@ -2304,530 +2534,1236 @@ static struct ast_channel *skinny_new(struct skinny_subchannel *sub, int state)
                }
 
                /* Configure the new channel jb */
-               if (tmp && sub && sub->rtp)
+               if (tmp && sub->rtp)
                        ast_jb_configure(tmp, &global_jbconf);
-       } else {
-               ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
        }
        return tmp;
 }
 
-static int handle_message(skinny_req *req, struct skinnysession *s)
+static int handle_keep_alive_message(skinny_req *req, struct skinnysession *s)
+{
+       if (!(req = req_alloc(0, KEEP_ALIVE_ACK_MESSAGE)))
+               return -1;
+
+       transmit_response(s, req);
+       do_housekeeping(s);
+       return 1;
+}
+
+static int handle_register_message(skinny_req *req, struct skinnysession *s)
 {
-       struct skinny_subchannel *sub;
-       struct ast_channel *c;
-       struct ast_frame f = { 0, };
-       struct sockaddr_in sin;
-       struct sockaddr_in us;
-       struct skinny_line *lines;
        char name[16];
-       char addr[4];
+       int res;
+
+       memcpy(&name, req->data.reg.name, sizeof(name));
+
+       res = skinny_register(req, s);
+       if (!res) {
+               ast_log(LOG_ERROR, "Rejecting Device %s: Device not found\n", name);
+               if (!(req = req_alloc(sizeof(register_rej_message), REGISTER_REJ_MESSAGE)))
+                       return -1;
+
+               snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
+               transmit_response(s, req);
+               return 0;
+       }
+       if (option_verbose > 2)
+               ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfully registered\n", name);
+
+       if (!(req = req_alloc(sizeof(register_ack_message), REGISTER_ACK_MESSAGE)))
+               return -1;
+
+       req->data.regack.res[0] = '0';
+       req->data.regack.res[1] = '\0';
+       req->data.regack.keepAlive = htolel(keep_alive);
+       ast_copy_string(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
+       req->data.regack.res2[0] = '0';
+       req->data.regack.res2[1] = '\0';
+       req->data.regack.secondaryKeepAlive = htolel(keep_alive);
+       transmit_response(s, req);
+       if (skinnydebug)
+               ast_verbose("Requesting capabilities\n");
+
+       if (!(req = req_alloc(0, CAPABILITIES_REQ_MESSAGE)))
+               return -1;
+
+       transmit_response(s, req);
+
+       return res;
+}
+
+static int handle_ip_port_message(skinny_req *req, struct skinnysession *s)
+{
+       /* no response necessary */
+       return 1;
+}
+
+static int handle_keypad_button_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_subchannel *sub = NULL;
+       struct skinny_line *l;
+       struct ast_frame f = { 0, };
        char d;
-       char iabuf[INET_ADDRSTRLEN];
        int digit;
-       int res=0;
-       int speedDialNum;
-       int lineNumber;
-       int stimulus;
-       int stimulusInstance;
-       int status;
-       int port;
-       int i;
-       time_t timer;
-       struct tm *cmtime;
-       pthread_t t;
-       button_defs_t *b, *buse;
+       int lineInstance;
+       int callReference;
 
-       if ((!s->device) && (letohl(req->e) != REGISTER_MESSAGE && letohl(req->e) != ALARM_MESSAGE)) {
-               ast_log(LOG_WARNING, "Client sent message #%d without first registering.\n", req->e);
-               free(req);
-               return 0;
+       digit = letohl(req->data.keypad.button);
+       lineInstance = letohl(req->data.keypad.lineInstance);
+       callReference = letohl(req->data.keypad.callReference);
+       f.frametype = AST_FRAME_DTMF;
+       if (digit == 14) {
+               d = '*';
+       } else if (digit == 15) {
+               d = '#';
+       } else if (digit >=0 && digit <= 9) {
+               d = '0' + digit;
+       } else {
+               /* digit=10-13 (A,B,C,D ?), or
+                * digit is bad value
+                *
+                * probably should not end up here, but set
+                * value for backward compatibility, and log
+                * a warning.
+                */
+               d = '0' + digit;
+               ast_log(LOG_WARNING, "Unsupported digit %d\n", digit);
        }
+       f.subclass = d;
+       f.src = "skinny";
 
-       switch(letohl(req->e))  {
-       case ALARM_MESSAGE:
-               /* no response necessary */
-               break;
-       case REGISTER_MESSAGE:
-               if (skinnydebug) {
-                       ast_verbose("Device %s is attempting to register\n", req->data.reg.name);
-               }
-               res = skinny_register(req, s);
-               if (!res) {
-                       ast_log(LOG_ERROR, "Rejecting Device %s: Device not found\n", req->data.reg.name);
-                       memcpy(&name, req->data.reg.name, sizeof(req->data.reg.name));
-                       memset(req, 0, sizeof(skinny_req));
-                       req->len = htolel(sizeof(register_rej_message)+4);
-                       req->e = htolel(REGISTER_REJ_MESSAGE);
-                       snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
-                       transmit_response(s, req);
-                       break;
+       if (lineInstance && callReference)
+               sub = find_subchannel_by_instance_reference(s->device, lineInstance, callReference);
+
+       if (!sub)
+               return 0;
+
+       l = sub->parent;
+       if (sub->owner) {
+               /* XXX MUST queue this frame to all lines in threeway call if threeway call is active */
+               ast_queue_frame(sub->owner, &f);
+               /* XXX This seriously needs to be fixed */
+               if (sub->next && sub->next->owner) {
+                       ast_queue_frame(sub->next->owner, &f);
                }
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfuly registered\n", s->device->name);
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(register_ack_message)+4);
-               req->e = htolel(REGISTER_ACK_MESSAGE);
-               req->data.regack.res[0] = '0';
-               req->data.regack.res[1] = '\0';
-               req->data.regack.keepAlive = htolel(keep_alive);
-               ast_copy_string(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
-               req->data.regack.res2[0] = '0';
-               req->data.regack.res2[1] = '\0';
-               req->data.regack.secondaryKeepAlive = htolel(keep_alive);
-               transmit_response(s, req);
-               if (skinnydebug) {
-                       ast_verbose("Requesting capabilities\n");
+       } else {
+               if (skinnydebug)
+                       ast_verbose("No owner: %s\n", l->name);
+       }
+       return 1;
+}
+
+static int handle_stimulus_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_line *l;
+       struct skinny_subchannel *sub;
+       /*struct skinny_speeddial *sd;*/
+       struct ast_channel *c;
+       pthread_t t;
+       int event;
+       int instance;
+       int unknown1;
+       /*int res = 0;*/
+
+       event = letohl(req->data.stimulus.stimulus);
+       instance = letohl(req->data.stimulus.stimulusInstance);
+       unknown1 = letohl(req->data.stimulus.unknown1); /* No clue.. */
+       if (skinnydebug)
+               ast_verbose("unknown1 in handle_stimulus_message is '%d'\n", unknown1);
+
+       sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
+
+       if (!sub) {
+               l = find_line_by_instance(d, d->lastlineinstance);
+               if (!l) {
+                       return 0;
                }
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(4);
-               req->e = htolel(CAPABILITIES_REQ_MESSAGE);
-               transmit_response(s, req);
-               break;
-       case UNREGISTER_MESSAGE:
-               /* XXX Actually unregister the device */
-               break;
-       case IP_PORT_MESSAGE:
-               /* no response necessary */
-               break;
-       case STIMULUS_MESSAGE:
-               stimulus = letohl(req->data.stimulus.stimulus);
-               stimulusInstance = letohl(req->data.stimulus.stimulusInstance);
-
-               switch(stimulus) {
-               case STIMULUS_REDIAL:
-                       /* If we can keep an array of dialed frames we can implement a quick
-                          and dirty redial, feeding the frames we last got into the queue
-                          function */
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Redial(%d)\n", stimulusInstance);
-                       }
-                       break;
-               case STIMULUS_SPEEDDIAL:
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: SpeedDial(%d)\n", stimulusInstance);
-                       }
-                       break;
-               case STIMULUS_HOLD:
-                       /* start moh? set RTP to 0.0.0.0? */
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Hold(%d)\n", stimulusInstance);
+       } else {
+               l = sub->parent;
+       }
+
+       switch(event) {
+       case STIMULUS_REDIAL:
+               /* If we can keep an array of dialed frames we can implement a quick
+                  and dirty redial, feeding the frames we last got into the queue
+                  function */
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Redial(%d)\n", instance);
+
+#if 0
+               c = skinny_new(l, AST_STATE_DOWN);
+               if(c) {
+                       sub = c->tech_pvt;
+                       l = sub->parent;
+                       transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
+                       if (skinnydebug)
+                               ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
+                       transmit_displaymessage(s, NULL); /* clear display */
+                       transmit_tone(s, SKINNY_DIALTONE);
+
+                       if (ast_strlen_zero(l->lastnumberdialed)) {
+                               ast_log(LOG_WARNING, "Attempted redial, but no previously dialed number found.\n");
+                               return 0;
                        }
-                       break;
-               case STIMULUS_TRANSFER:
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Transfer(%d)\n", stimulusInstance);
+                       if (!ast_ignore_pattern(c->context, l->lastnumberdialed)) {
+                               transmit_tone(s, SKINNY_SILENCE);
                        }
-                       transmit_tone(s, SKINNY_DIALTONE);
-                       /* XXX figure out how to transfer */
-                       break;
-               case STIMULUS_CONFERENCE:
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Transfer(%d)\n", stimulusInstance);
+                       if (ast_exists_extension(c, c->context, l->lastnumberdialed, 1, l->cid_num)) {
+                               if (!ast_matchmore_extension(c, c->context, l->lastnumberdialed, 1, l->cid_num)) {
+                                       ast_copy_string(c->exten, l->lastnumberdialed, sizeof(c->exten));
+                                       if (!ast_strlen_zero(l->cid_num)) {
+                                               if (!l->hidecallerid) {
+                                                       c->cid.cid_num = strdup(l->cid_num);
+                                                       c->cid.cid_ani = strdup(l->cid_num);
+                                               }
+                                       }
+                                       ast_setstate(c, AST_STATE_RING);
+                                       res = ast_pbx_run(c);
+                                       if (res) {
+                                               ast_log(LOG_WARNING, "PBX exited non-zero\n");
+                                               transmit_tone(s, SKINNY_REORDER);
+                                       }
+                                       break;
+                               }
                        }
+               } else {
+                       ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
+               }
+#endif
+               break;
+       case STIMULUS_SPEEDDIAL:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: SpeedDial(%d)\n", instance);
+
+#if 0
+               if (!(sd = find_speeddial_by_instance(d, instance))) {
+                       return 0;
+               }
+
+               c = skinny_new(l, AST_STATE_DOWN);
+               if(c) {
+                       sub = c->tech_pvt;
+                       l = sub->parent;
+                       transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
+                       if (skinnydebug)
+                               ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
+                       transmit_displaymessage(s, NULL); /* clear display */
                        transmit_tone(s, SKINNY_DIALTONE);
-                       /* XXX determine the best way to pull off a conference.  Meetme? */
-                       break;
-               case STIMULUS_VOICEMAIL:
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Voicemail(%d)\n", stimulusInstance);
+
+                       if (!ast_ignore_pattern(c->context, sd->exten)) {
+                               transmit_tone(s, SKINNY_SILENCE);
                        }
-                       /* XXX Find and dial voicemail extension */
-                       break;
-               case STIMULUS_CALLPARK:
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Park Call(%d)\n", stimulusInstance);
+                       if (ast_exists_extension(c, c->context, sd->exten, 1, l->cid_num)) {
+                               if (!ast_matchmore_extension(c, c->context, sd->exten, 1, l->cid_num)) {
+                                       ast_copy_string(c->exten, sd->exten, sizeof(c->exten));
+                                       ast_copy_string(l->lastnumberdialed, sd->exten, sizeof(l->lastnumberdialed));
+                                       if (!ast_strlen_zero(l->cid_num)) {
+                                               if (!l->hidecallerid) {
+                                                       c->cid.cid_num = strdup(l->cid_num);
+                                                       c->cid.cid_ani = strdup(l->cid_num);
+                                               }
+                                       }
+                                       ast_setstate(c, AST_STATE_RING);
+                                       res = ast_pbx_run(c);
+                                       if (res) {
+                                               ast_log(LOG_WARNING, "PBX exited non-zero\n");
+                                               transmit_tone(s, SKINNY_REORDER);
+                                       }
+                                       break;
+                               }
                        }
-                       /* XXX Park the call */
-                       break;
-               case STIMULUS_FORWARDALL:
-                       /* Why is DND under FORWARDALL ? */
+               } else {
+                       ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
+               }
+#endif
+               break;
+       case STIMULUS_HOLD:
+               /* start moh? set RTP to 0.0.0.0? */
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Hold(%d)\n", instance);
+               break;
+       case STIMULUS_TRANSFER:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Transfer(%d)\n", instance);
+               transmit_tone(s, SKINNY_DIALTONE);
+               /* XXX figure out how to transfer */
+               break;
+       case STIMULUS_CONFERENCE:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Transfer(%d)\n", instance);
+               transmit_tone(s, SKINNY_DIALTONE);
+               /* XXX determine the best way to pull off a conference.  Meetme? */
+               break;
+       case STIMULUS_VOICEMAIL:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Voicemail(%d)\n", instance);
+               /* XXX Find and dial voicemail extension */
+               break;
+       case STIMULUS_CALLPARK:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Park Call(%d)\n", instance);
+               /* XXX Park the call */
+               break;
+       case STIMULUS_FORWARDALL:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Forward All(%d)\n", instance);
+               /* Why is DND under FORWARDALL? */
+               /* Because it's the same thing. */
+
+               /* Do not disturb */
+               transmit_tone(s, SKINNY_DIALTONE);
+               if (l->dnd != 0){
+                       if (option_verbose > 2)
+                               ast_verbose(VERBOSE_PREFIX_3 "Disabling DND on %s@%s\n", l->name, d->name);
+                       l->dnd = 0;
+                       transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_ON);
+                       transmit_displaynotify(s, "DnD disabled", 10);
+               } else {
+                       if (option_verbose > 2)
+                               ast_verbose(VERBOSE_PREFIX_3 "Enabling DND on %s@%s\n", l->name, d->name);
+                       l->dnd = 1;
+                       transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_OFF);
+                       transmit_displaynotify(s, "DnD enabled", 10);
+               }
+               break;
+       case STIMULUS_FORWARDBUSY:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Forward Busy (%d)\n", instance);
+               break;
+       case STIMULUS_FORWARDNOANSWER:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Forward No Answer (%d)\n", instance);
+               break;
+       case STIMULUS_DISPLAY:
+               /* Not sure what this is */
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Display(%d)\n", instance);
+               break;
+       case STIMULUS_LINE:
+               if (skinnydebug)
+                       ast_verbose("Received Stimulus: Line(%d)\n", instance);
+
+               l = find_line_by_instance(s->device, instance);
+
+               if (!l) {
+                       return 0;
+               }
+
+               /* turn the speaker on */
+               transmit_speaker_mode(s, SKINNY_SPEAKERON);
+               transmit_ringer_mode(s, SKINNY_RING_OFF);
+               transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
+
+               l->hookstate = SKINNY_OFFHOOK;
+
+               if (sub && sub->outgoing) {
+                       /* We're answering a ringing call */
+                       ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
+                       transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
+                       transmit_tone(s, SKINNY_SILENCE);
+                       transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
+                       transmit_displaypromptstatus(s, "Connected", 0, l->instance, sub->callid);
+                       transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
+                       start_rtp(sub);
+                       ast_setstate(sub->owner, AST_STATE_UP);
+               } else {
+                       if (sub && sub->owner) {
+                               ast_log(LOG_DEBUG, "Current subchannel [%s] already has owner\n", sub->owner->name);
+                       } else {
+                               c = skinny_new(l, AST_STATE_DOWN);
+                               if(c) {
+                                       sub = c->tech_pvt;
+                                       transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
+                                       if (skinnydebug)
+                                               ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
+                                       transmit_displaymessage(s, NULL); /* clear display */
+                                       transmit_tone(s, SKINNY_DIALTONE);
+                                       transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
+
+                                       /* start the switch thread */
+                                       if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
+                                               ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
+                                               ast_hangup(c);
+                                       }
+                               } else {
+                                       ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
+                               }
+                       }
+               }
+               break;
+       default:
+               if (skinnydebug)
+                       ast_verbose("RECEIVED UNKNOWN STIMULUS:  %d(%d)\n", event, instance);
+               break;
+       }
+       return 1;
+}
+
+static int handle_offhook_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_line *l;
+       struct skinny_subchannel *sub;
+       struct ast_channel *c;
+       pthread_t t;
+       int unknown1;
+       int unknown2;
+
+       unknown1 = letohl(req->data.offhook.unknown1);
+       unknown2 = letohl(req->data.offhook.unknown2);
+
+       sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
+
+       if (!sub) {
+               l = find_line_by_instance(d, d->lastlineinstance);
+               if (!l) {
+                       return 0;
+               }
+       } else {
+               l = sub->parent;
+       }
+
+       transmit_ringer_mode(s, SKINNY_RING_OFF);
+       transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
+       l->hookstate = SKINNY_OFFHOOK;
+
+       if (sub && sub->outgoing) {
+               /* We're answering a ringing call */
+               ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
+               transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
+               transmit_tone(s, SKINNY_SILENCE);
+               transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
+               transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
+               start_rtp(sub);
+               ast_setstate(sub->owner, AST_STATE_UP);
+       } else {
+               if (sub && sub->owner) {
+                       ast_log(LOG_DEBUG, "Current sub [%s] already has owner\n", sub->owner->name);
+               } else {
+                       c = skinny_new(l, AST_STATE_DOWN);
+                       if(c) {
+                               sub = c->tech_pvt;
+                               transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
+                               if (skinnydebug)
+                                       ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
+                               transmit_displaymessage(s, NULL); /* clear display */
+                               transmit_tone(s, SKINNY_DIALTONE);
+                               transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
+
+                               /* start the switch thread */
+                               if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
+                                       ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
+                                       ast_hangup(c);
+                               }
+                       } else {
+                               ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
+                       }
+               }
+       }
+       return 1;
+}
+
+static int handle_onhook_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_line *l;
+       struct skinny_subchannel *sub;
+       int unknown1;
+       int unknown2;
+
+       unknown1 = letohl(req->data.onhook.unknown1);
+       unknown2 = letohl(req->data.onhook.unknown2);
+
+       sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
+
+       if (!sub) {
+               return 0;
+       } else {
+               l = sub->parent;
+       }
+
+       if (l->hookstate == SKINNY_ONHOOK) {
+               /* Something else already put us back on hook */
+               return 0;
+       }
+       sub->cxmode = SKINNY_CX_RECVONLY;
+       l->hookstate = SKINNY_ONHOOK;
+       transmit_callstate(s, l->instance, l->hookstate, sub->callid);
+       if (skinnydebug)
+               ast_verbose("Skinny %s@%s went on hook\n", l->name, d->name);
+       if (l->transfer && (sub->owner && sub->next && sub->next->owner) && ((!sub->outgoing) || (sub->next && !sub->next->outgoing))) {
+               /* We're allowed to transfer, we have two active calls and
+                  we made at least one of the calls.  Let's try and transfer */
+
+#if 0
+               if ((res = attempt_transfer(p)) < 0) {
+                       if (sub->next && sub->next->owner) {
+                               sub->next->alreadygone = 1;
+                               ast_queue_hangup(sub->next->owner,1);
+                       }
+               } else if (res) {
+                       ast_log(LOG_WARNING, "Transfer attempt failed\n");
+                       return 0;
+               }
+#endif
+       } else {
+               /* Hangup the current call */
+               /* If there is another active call, skinny_hangup will ring the phone with the other call */
+               if (sub->owner) {
+                       sub->alreadygone = 1;
+                       ast_queue_hangup(sub->owner);
+               } else {
+                       ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
+                               l->name, d->name, sub->callid);
+               }
+       }
+       if ((l->hookstate == SKINNY_ONHOOK) && (sub->next && !sub->next->rtp)) {
+               do_housekeeping(s);
+       }
+       return 1;
+}
+
+static int handle_capabilities_res_message(skinny_req *req, struct skinnysession *s)
+{
+       /* XXX process the capabilites */
+       return 1;
+}
+
+static int handle_speed_dial_stat_req_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_speeddial *sd;
+       int instance;
+
+       instance = letohl(req->data.speeddialreq.speedDialNumber);
+
+       sd = find_speeddial_by_instance(d, instance);
+
+       if (!sd) {
+               return 0;
+       }
+
+       if (!(req = req_alloc(sizeof(speed_dial_stat_res_message), SPEED_DIAL_STAT_RES_MESSAGE)))
+               return -1;
+
+       req->data.speeddialreq.speedDialNumber = htolel(instance);
+       snprintf(req->data.speeddial.speedDialDirNumber, sizeof(req->data.speeddial.speedDialDirNumber), sd->exten);
+       snprintf(req->data.speeddial.speedDialDisplayName, sizeof(req->data.speeddial.speedDialDisplayName), sd->label);
+
+       transmit_response(s, req);
+       return 1;
+}
+
+static int handle_line_state_req_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_line *l;
+       int instance;
+
+       instance = letohl(req->data.line.lineNumber);
+
+       ast_mutex_lock(&devicelock);
+
+       l = find_line_by_instance(d, instance);
+
+       if (!l) {
+               return 0;
+       }
+
+       ast_mutex_unlock(&devicelock);
+
+       if (!(req = req_alloc(sizeof(line_stat_res_message), LINE_STAT_RES_MESSAGE)))
+               return -1;
+
+       req->data.linestat.lineNumber = letohl(instance);
+       memcpy(req->data.linestat.lineDirNumber, l->name,
+                       sizeof(req->data.linestat.lineDirNumber));
+       memcpy(req->data.linestat.lineDisplayName, l->label,
+                       sizeof(req->data.linestat.lineDisplayName));
+       transmit_response(s,req);
+       return 1;
+}
+
+static int handle_time_date_req_message(skinny_req *req, struct skinnysession *s)
+{
+       time_t timer;
+       struct tm *cmtime;
+
+       if (!(req = req_alloc(sizeof(definetimedate_message), DEFINETIMEDATE_MESSAGE)))
+               return -1;
+
+       timer = time(NULL);
+       cmtime = localtime(&timer);
+       req->data.definetimedate.year = htolel(cmtime->tm_year+1900);
+       req->data.definetimedate.month = htolel(cmtime->tm_mon+1);
+       req->data.definetimedate.dayofweek = htolel(cmtime->tm_wday);
+       req->data.definetimedate.day = htolel(cmtime->tm_mday);
+       req->data.definetimedate.hour = htolel(cmtime->tm_hour);
+       req->data.definetimedate.minute = htolel(cmtime->tm_min);
+       req->data.definetimedate.seconds = htolel(cmtime->tm_sec);
+       transmit_response(s, req);
+       return 1;
+}
+
+static int handle_button_template_req_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_line *l;
+       int i;
+
+       if (!(req = req_alloc(sizeof(button_template_res_message), BUTTON_TEMPLATE_RES_MESSAGE)))
+               return -1;
+
+       struct skinny_speeddial *sd;
+       button_definition_template btn[42];
+       int lineInstance = 1;
+       int speeddialInstance = 1;
+       int buttonCount = 0;
+
+       memset(&btn, 0, sizeof(btn));
+
+       get_button_template(s, btn);
+
+       for (i=0; i<42; i++) {
+               int btnSet = 0;
+               switch (btn[i].buttonDefinition) {
+                       case BT_CUST_LINESPEEDDIAL:
+                               /* assume failure */
+                               req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_NONE);
+                               req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
+
+                               for (l = d->lines; l; l = l->next) {
+                                       if (l->instance == lineInstance) {
+                                               ast_verbose("Adding button: %d, %d\n", BT_LINE, lineInstance);
+                                               req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_LINE);
+                                               req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance++);
+                                               buttonCount++;
+                                               btnSet = 1;
+                                               break;
+                                       }
+                               }
+
+                               if (!btnSet) {
+                                       for (sd = d->speeddials; sd; sd = sd->next) {
+                                               if (sd->instance == speeddialInstance) {
+                                                       ast_verbose("Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
+                                                       req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_SPEEDDIAL);
+                                                       req->data.buttontemplate.definition[i].instanceNumber = htolel(speeddialInstance++);
+                                                       buttonCount++;
+                                                       btnSet = 1;
+                                                       break;
+                                               }
+                                       }
+                               }
+                               break;
+                       case BT_LINE:
+                               req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_NONE);
+                               req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
+
+                               for (l = d->lines; l; l = l->next) {
+                                       if (l->instance == lineInstance) {
+                                               ast_verbose("Adding button: %d, %d\n", BT_LINE, lineInstance);
+                                               req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_LINE);
+                                               req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance++);
+                                               buttonCount++;
+                                               btnSet = 1;
+                                               break;
+                                       }
+                               }
+                               break;
+                       case BT_SPEEDDIAL:
+                               req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
+                               req->data.buttontemplate.definition[i].instanceNumber = 0;
+
+                               for (sd = d->speeddials; sd; sd = sd->next) {
+                                       if (sd->instance == speeddialInstance) {
+                                               ast_verbose("Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
+                                               req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_SPEEDDIAL);
+                                               req->data.buttontemplate.definition[i].instanceNumber = htolel(speeddialInstance++);
+                                               buttonCount++;
+                                               btnSet = 1;
+                                               break;
+                                       }
+                               }
+                               break;
+                       case BT_CUST_HINT:
+                               break;
+                       case BT_NONE:
+                               break;
+                       default:
+                               ast_verbose("Adding button: %d, %d\n", btn[i].buttonDefinition, 0);
+                               req->data.buttontemplate.definition[i].buttonDefinition = htolel(btn[i].buttonDefinition);
+                               req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
+                               buttonCount++;
+                               btnSet = 1;
+                               break;
+               }
+       }
+
+       req->data.buttontemplate.buttonOffset = htolel(0);
+       req->data.buttontemplate.buttonCount = htolel(buttonCount);
+       req->data.buttontemplate.totalButtonCount = htolel(buttonCount);
+
+       if (skinnydebug)
+               ast_verbose("Sending %d template to %s\n",
+                                       d->type,
+                                       d->name);
+       transmit_response(s, req);
+       return 1;
+}
+
+static int handle_version_req_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       if (!(req = req_alloc(sizeof(version_res_message), VERSION_RES_MESSAGE)))
+               return -1;
+
+       snprintf(req->data.version.version, sizeof(req->data.version.version), d->version_id);
+       transmit_response(s, req);
+       return 1;
+}
+
+static int handle_server_request_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       if (!(req = req_alloc(sizeof(server_res_message), SERVER_RES_MESSAGE)))
+               return -1;
+
+       memcpy(req->data.serverres.server[0].serverName, ourhost,
+                       sizeof(req->data.serverres.server[0].serverName));
+       req->data.serverres.serverListenPort[0] = htolel(ourport);
+       req->data.serverres.serverIpAddr[0] = htolel(d->ourip.s_addr);
+       transmit_response(s, req);
+       return 1;
+}
+
+static int handle_alarm_message(skinny_req *req, struct skinnysession *s)
+{
+       /* no response necessary */
+       if (skinnydebug)
+               ast_verbose("Received Alarm Message: %s\n", req->data.alarm.displayMessage);
+
+       return 1;
+}
+
+static int handle_open_receive_channel_ack_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_line *l;
+       struct skinny_subchannel *sub;
+       struct sockaddr_in sin;
+       struct sockaddr_in us;
+       char iabuf[INET_ADDRSTRLEN];
+       uint32_t addr;
+       int port;
+       int status;
+       int passthruid;
+
+       status = letohl(req->data.openreceivechannelack.status);
+       if (status) {
+               ast_log(LOG_ERROR, "Open Receive Channel Failure\n");
+               return 0;
+       }
+       addr = letohl(req->data.openreceivechannelack.ipAddr);
+       port = letohl(req->data.openreceivechannelack.port);
+       passthruid = letohl(req->data.openreceivechannelack.passThruId);
+
+       sin.sin_family = AF_INET;
+       sin.sin_addr.s_addr = addr;
+       sin.sin_port = htons(port);
+
+       sub = find_subchannel_by_reference(d, passthruid);
+
+       if (!sub)
+               return 0;
+
+       l = sub->parent;
+
+       if (sub->rtp) {
+               ast_rtp_set_peer(sub->rtp, &sin);
+               ast_rtp_get_us(sub->rtp, &us);
+       } else {
+               ast_log(LOG_ERROR, "No RTP structure, this is very bad\n");
+               return 0;
+       }
+
+       if (skinnydebug) {
+               ast_verbose("ipaddr = %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
+               ast_verbose("ourip = %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), d->ourip), ntohs(us.sin_port));
+       }
+
+       if (!(req = req_alloc(sizeof(start_media_transmission_message), START_MEDIA_TRANSMISSION_MESSAGE)))
+               return -1;
+
+       req->data.startmedia.conferenceId = 0;
+       req->data.startmedia.passThruPartyId = htolel(sub->callid);
+       req->data.startmedia.remoteIp = htolel(d->ourip.s_addr);
+       req->data.startmedia.remotePort = htolel(ntohs(us.sin_port));
+       req->data.startmedia.packetSize = htolel(20);
+       req->data.startmedia.payloadType = htolel(convert_cap(l->capability));
+       req->data.startmedia.qualifier.precedence = htolel(127);
+       req->data.startmedia.qualifier.vad = 0;
+       req->data.startmedia.qualifier.packets = 0;
+       req->data.startmedia.qualifier.bitRate = 0;
+       transmit_response(s, req);
+       return 1;
+}
+
+static int handle_soft_key_set_req_message(skinny_req *req, struct skinnysession *s)
+{
+       int i;
+       int x;
+       int y;
+
+       if (!(req = req_alloc(sizeof(soft_key_set_res_message), SOFT_KEY_SET_RES_MESSAGE)))
+               return -1;
+
+       req->data.softkeysets.softKeySetOffset = htolel(0);
+       req->data.softkeysets.softKeySetCount = htolel(11);
+       req->data.softkeysets.totalSoftKeySetCount = htolel(11);
+       const soft_key_definitions *softkeymode = soft_key_default_definitions;
+       for (x = 0; x < sizeof(soft_key_default_definitions) / sizeof(soft_key_definitions); x++) {
+               const uint8_t *defaults = softkeymode->defaults;
+               /* XXX I wanted to get the size of the array dynamically, but that wasn't wanting to work.
+                  This will have to do for now. */
+               for (y = 0; y < softkeymode->count; y++) {
+                       for (i = 0; i < (sizeof(soft_key_template_default) / sizeof(soft_key_template_definition)); i++) {
+                               if (defaults[y] == i+1) {
+                                       req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyTemplateIndex[y] = htolel(i+1);
+                               }
+                       }
+               }
+               softkeymode++;
+       }
+       transmit_response(s,req);
+       transmit_selectsoftkeys(s, 0, 0, KEYDEF_ONHOOK);
+       return 1;
+}
+
+static int handle_soft_key_event_message(skinny_req *req, struct skinnysession *s)
+{
+       struct skinny_device *d = s->device;
+       struct skinny_line *l;
+       struct skinny_subchannel *sub = NULL;
+       struct ast_channel *c;
+       pthread_t t;
+       int event;
+       int instance;
+       int reference;
+       /*int res;*/
+
+       event = letohl(req->data.softkeyeventmessage.softKeyEvent);
+       instance = letohl(req->data.softkeyeventmessage.instance);
+       reference = letohl(req->data.softkeyeventmessage.reference);
+
+       if (instance) {
+               l = find_line_by_instance(d, instance);
+               if (reference) {
+                       sub = find_subchannel_by_instance_reference(d, instance, reference);
+               } else {
+                       sub = find_subchannel_by_instance_reference(d, instance, d->lastcallreference);
+               }
+       } else {
+               l = find_line_by_instance(d, d->lastlineinstance);
+       }
+
+       if (!l) {
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: %d(%d)\n", event, instance);
+               return 0;
+       }
+
+       switch(event) {
+       case SOFTKEY_NONE:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: None(%d)\n", instance);
+               break;
+       case SOFTKEY_REDIAL:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Redial(%d)\n", instance);
+
+#if 0
+               if (sub)
+                       transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
+               if (skinnydebug)
+                       ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
+               transmit_displaymessage(s, NULL); /* clear display */
+               transmit_tone(s, SKINNY_DIALTONE);
+
+               if (!sub) {
+                       c = skinny_new(l, AST_STATE_DOWN);
+               } else {
+                       c = sub->owner;
+               }
+
+               if(c) {
+                       sub = c->tech_pvt;
+                       if (ast_strlen_zero(l->lastnumberdialed)) {
+                               ast_log(LOG_WARNING, "Attempted redial, but no previously dialed number found.\n");
+                               return 0;
+                       }
+                       if (!ast_ignore_pattern(c->context, l->lastnumberdialed)) {
+                               transmit_tone(s, SKINNY_SILENCE);
+                       }
+                       start_rtp(sub);
+                       if (ast_exists_extension(c, c->context, l->lastnumberdialed, 1, l->cid_num)) {
+                               if (!ast_matchmore_extension(c, c->context, l->lastnumberdialed, 1, l->cid_num)) {
+                                       ast_copy_string(c->exten, l->lastnumberdialed, sizeof(c->exten));
+                                       if (!ast_strlen_zero(l->cid_num)) {
+                                               if (!l->hidecallerid) {
+                                                       c->cid.cid_num = strdup(l->cid_num);
+                                                       c->cid.cid_ani = strdup(l->cid_num);
+                                               }
+                                       }
+                                       ast_setstate(c, AST_STATE_RING);
+                                       res = ast_pbx_run(c);
+                                       if (res) {
+                                               ast_log(LOG_WARNING, "PBX exited non-zero\n");
+                                               transmit_tone(s, SKINNY_REORDER);
+                                       }
+                                       break;
+                               }
+                       }
+               } else {
+                       ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
+               }
+#endif
+               break;
+       case SOFTKEY_NEWCALL:
+               /* XXX Untested */
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: New Call(%d)\n", instance);
+
+               transmit_ringer_mode(s,SKINNY_RING_OFF);
+               transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
 
-                       /* Do not disturb */
+               l->hookstate = SKINNY_OFFHOOK;
+
+               if (sub) {
+                       transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
+                       if (skinnydebug)
+                               ast_verbose("Attempting to Clear display on Skinny %s@%s\n", l->name, d->name);
+                       transmit_displaymessage(s, NULL); /* clear display */
                        transmit_tone(s, SKINNY_DIALTONE);
-                       if (s->device->lines->dnd != 0){
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "Disabling DND on %s@%s\n",find_subchannel_by_line(s->device->lines)->parent->name,find_subchannel_by_line(s->device->lines)->parent->name);
-                               s->device->lines->dnd = 0;
-                               transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_ON);
-                               transmit_displaynotify(s, "DnD disabled",10);
+                       transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_OFFHOOK);
+                       c = skinny_new(l, AST_STATE_DOWN);
+                       if(c) {
+                               /* start the switch thread */
+                               if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
+                                       ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
+                                       ast_hangup(c);
+                               }
                        } else {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "Enabling DND on %s@%s\n",find_subchannel_by_line(s->device->lines)->parent->name,find_subchannel_by_line(s->device->lines)->parent->name);
-                               s->device->lines->dnd = 1;
-                               transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_OFF);
-                               transmit_displaynotify(s, "DnD enabled",10);
-                       }
-                       break;
-               case STIMULUS_FORWARDBUSY:
-               case STIMULUS_FORWARDNOANSWER:
-                       /* Gonna be fun, not */
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Forward (%d)\n", stimulusInstance);
-                       }
-                       break;
-               case STIMULUS_DISPLAY:
-                       /* Not sure what this is */
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Display(%d)\n", stimulusInstance);
+                               ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
                        }
-                       break;
-               case STIMULUS_LINE:
-                       if (skinnydebug) {
-                               ast_verbose("Received Stimulus: Line(%d)\n", stimulusInstance);
-                       }
-                       sub = find_subchannel_by_line(s->device->lines);
-                       /* turn the speaker on */
-                       transmit_speaker_mode(s, 1);
-               break;
-               default:
-                       if (skinnydebug) {
-                               ast_verbose("RECEIVED UNKNOWN STIMULUS:  %d(%d)\n", stimulus, stimulusInstance);
-                       }
-                       break;
                }
                break;
-       case VERSION_REQ_MESSAGE:
-               if (skinnydebug) {
-                       ast_verbose("Version Request\n");
-               }
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(version_res_message)+4);
-               req->e = htolel(VERSION_RES_MESSAGE);
-               snprintf(req->data.version.version, sizeof(req->data.version.version), s->device->version_id);
-               transmit_response(s, req);
+       case SOFTKEY_HOLD:
+               /* start moh? set RTP to 0.0.0.0? */
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Hold(%d)\n", instance);
                break;
-       case SERVER_REQUEST_MESSAGE:
-               if (skinnydebug) {
-                       ast_verbose("Received Server Request\n");
-               }
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(server_res_message)+4);
-               req->e = htolel(SERVER_RES_MESSAGE);
-               memcpy(req->data.serverres.server[0].serverName, ourhost,
-                               sizeof(req->data.serverres.server[0].serverName));
-               req->data.serverres.serverListenPort[0] = htolel(ourport);
-               req->data.serverres.serverIpAddr[0] = htolel(__ourip.s_addr);
-               transmit_response(s, req);
+       case SOFTKEY_TRNSFER:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Transfer(%d)\n", instance);
+               transmit_tone(s, SKINNY_DIALTONE);
+               /* XXX figure out how to transfer */
                break;
-       case BUTTON_TEMPLATE_REQ_MESSAGE:
-               if (skinnydebug) {
-                       ast_verbose("Buttontemplate requested\n");
-               }
-               sub = find_subchannel_by_line(s->device->lines);
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->e = htolel(BUTTON_TEMPLATE_RES_MESSAGE);
-               req->len = htolel(sizeof(button_template_res_message)+4);
-
-               /* Find a matching button definition, default to first in the
-                  list */
-               buse = button_defs;
-               for(b=button_defs; b->type; b++) {
-                       if (!strcmp(s->device->model, b->type)) {
-                               buse = b;
-                       }
-               }
-               req->data.buttontemplate.buttonOffset = 0;
-               req->data.buttontemplate.buttonCount = htolel(buse->num_buttons);
-               req->data.buttontemplate.totalButtonCount = htolel(buse->num_buttons);
-               for (i=0; i<42; i++) {
-                       if (i < buse->num_buttons) {
-                               memcpy(&(req->data.buttontemplate.definition[i]),
-                                       &(buse->button_def[i]),
-                                       sizeof(button_definition));
-                       } else {
-                               memcpy(&(req->data.buttontemplate.definition[i]),
-                                       &(button_def_none),
-                                       sizeof(button_definition));
-                       }
-               }
+       case SOFTKEY_CFWDALL:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Forward All(%d)\n", instance);
 
-               if (skinnydebug) {
-                       ast_verbose("Sending %s template to %s@%s (%s)\n",
-                                               buse->type,
-                                               sub->parent->name,
-                                               sub->parent->parent->name,
-                                               s->device->model);
+               /* Do not disturb */
+               transmit_tone(s, SKINNY_DIALTONE);
+               if (l->dnd != 0){
+                       if (option_verbose > 2)
+                               ast_verbose(VERBOSE_PREFIX_3 "Disabling DND on %s@%s\n", l->name, d->name);
+                       l->dnd = 0;
+                       transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_ON);
+                       transmit_displaynotify(s, "DnD disabled", 10);
+               } else {
+                       if (option_verbose > 2)
+                               ast_verbose(VERBOSE_PREFIX_3 "Enabling DND on %s@%s\n", l->name, d->name);
+                       l->dnd = 1;
+                       transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_OFF);
+                       transmit_displaynotify(s, "DnD enabled", 10);
                }
-               transmit_response(s, req);
                break;
-       case SOFT_KEY_SET_REQ_MESSAGE:
-               if (skinnydebug) {
-                       ast_verbose("Received SoftKeySetReq\n");
-               }
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(soft_key_sets)+4);
-               req->e = htolel(SOFT_KEY_SET_RES_MESSAGE);
-               req->data.softkeysets.softKeySetOffset = 0;
-               req->data.softkeysets.softKeySetCount = htolel(11);
-               req->data.softkeysets.totalSoftKeySetCount = htolel(11);
-               /* XXX Wicked hack XXX */
-               memcpy(req->data.softkeysets.softKeySetDefinition,
-                       soft_key_set_hack,
-                       sizeof(req->data.softkeysets.softKeySetDefinition));
-               transmit_response(s,req);
+       case SOFTKEY_CFWDBUSY:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Forward Busy (%d)\n", instance);
                break;
-       case SOFT_KEY_TEMPLATE_REQ_MESSAGE:
-               if (skinnydebug) {
-                       ast_verbose("Received SoftKey Template Request\n");
-               }
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(soft_key_template)+4);
-               req->e = htolel(SOFT_KEY_TEMPLATE_RES_MESSAGE);
-               req->data.softkeytemplate.softKeyOffset = 0;
-               req->data.softkeytemplate.softKeyCount  = htolel(sizeof(soft_key_template_default) / sizeof(soft_key_template_definition));
-               req->data.softkeytemplate.totalSoftKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(soft_key_template_definition));
-               memcpy(req->data.softkeytemplate.softKeyTemplateDefinition,
-                       soft_key_template_default,
-                       sizeof(soft_key_template_default));
-               transmit_response(s,req);
+       case SOFTKEY_CFWDNOANSWER:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Forward No Answer (%d)\n", instance);
                break;
-       case TIME_DATE_REQ_MESSAGE:
-               if (skinnydebug) {
-                       ast_verbose("Received Time/Date Request\n");
-               }
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(definetimedate_message)+4);
-               req->e = htolel(DEFINETIMEDATE_MESSAGE);
-               timer=time(NULL);
-               cmtime = localtime(&timer);
-               req->data.definetimedate.year = htolel(cmtime->tm_year+1900);
-               req->data.definetimedate.month = htolel(cmtime->tm_mon+1);
-               req->data.definetimedate.dayofweek = htolel(cmtime->tm_wday);
-               req->data.definetimedate.day = htolel(cmtime->tm_mday);
-               req->data.definetimedate.hour = htolel(cmtime->tm_hour);
-               req->data.definetimedate.minute = htolel(cmtime->tm_min);
-               req->data.definetimedate.seconds = htolel(cmtime->tm_sec);
-               transmit_response(s, req);
+       case SOFTKEY_BKSPC:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Backspace(%d)\n", instance);
                break;
-       case SPEED_DIAL_STAT_REQ_MESSAGE:
-               /* Not really sure how Speed Dial's are different than the
-                  Softkey templates */
-               speedDialNum = letohl(req->data.speeddialreq.speedDialNumber);
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(speed_dial_stat_res_message)+4);
-               req->e = htolel(SPEED_DIAL_STAT_RES_MESSAGE);
+       case SOFTKEY_ENDCALL:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: End Call(%d)\n", instance);
+
+               if (l->hookstate == SKINNY_ONHOOK) {
+                       /* Something else already put us back on hook */
+                       break;
+               }
+               if (sub) {
+                       sub->cxmode = SKINNY_CX_RECVONLY;
+                       l->hookstate = SKINNY_ONHOOK;
+                       transmit_callstate(s, l->instance, l->hookstate, sub->callid);
+                       if (skinnydebug)
+                               ast_verbose("Skinny %s@%s went on hook\n", l->name, d->name);
+                       if (l->transfer && (sub->owner && sub->next && sub->next->owner) && ((!sub->outgoing) || (sub->next && !sub->next->outgoing))) {
+                               /* We're allowed to transfer, we have two active calls and
+                                  we made at least one of the calls.  Let's try and transfer */
+
 #if 0
-               /* XXX Do this right XXX */
-               /* If the redial function works the way I think it will, a modification of it
-                  can work here as well. Yikes. */
-               req->data.speeddialreq.speedDialNumber = speedDialNum;
-               snprintf(req->data.speeddial.speedDialDirNumber, sizeof(req->data.speeddial.speedDialDirNumber), "31337");
-               snprintf(req->data.speeddial.speedDialDisplayName, sizeof(req->data.speeddial.speedDialDisplayName),"Asterisk Rules!");
+                               if ((res = attempt_transfer(p)) < 0) {
+                                       if (sub->next && sub->next->owner) {
+                                               sub->next->alreadygone = 1;
+                                               ast_queue_hangup(sub->next->owner, 1);
+                                       }
+                               } else if (res) {
+                                       ast_log(LOG_WARNING, "Transfer attempt failed\n");
+                                       return 0;
+                               }
 #endif
-               transmit_response(s, req);
-               break;
-       case LINE_STATE_REQ_MESSAGE:
-               lineNumber = letohl(req->data.line.lineNumber);
-               if (skinnydebug) {
-                       ast_verbose("Received LineStateReq\n");
-               }
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(line_stat_res_message)+4);
-               req->e = htolel(LINE_STAT_RES_MESSAGE);
-               sub = find_subchannel_by_line(s->device->lines);
-               if (!sub) {
-                       ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
-                       return 0;
-               }
-               lines = sub->parent;
-               ast_mutex_lock(&devicelock);
-               for (i=1; i < lineNumber; i++) {
-                       lines = lines->next;
-               }
-               ast_mutex_unlock(&devicelock);
-               req->data.linestat.linenumber = letohl(lineNumber);
-               memcpy(req->data.linestat.lineDirNumber, lines->name,
-                               sizeof(req->data.linestat.lineDirNumber));
-               memcpy(req->data.linestat.lineDisplayName, lines->label,
-                               sizeof(req->data.linestat.lineDisplayName));
-               transmit_response(s,req);
-               break;
-       case CAPABILITIES_RES_MESSAGE:
-               if (skinnydebug) {
-                       ast_verbose("Received CapabilitiesRes\n");
+                       } else {
+                               /* Hangup the current call */
+                               /* If there is another active call, skinny_hangup will ring the phone with the other call */
+                               if (sub->owner) {
+                                       sub->alreadygone = 1;
+                                       ast_queue_hangup(sub->owner);
+                               } else {
+                                       ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
+                                               l->name, d->name, sub->callid);
+                               }
+                       }
+                       if ((l->hookstate == SKINNY_ONHOOK) && (sub->next && !sub->next->rtp)) {
+                               do_housekeeping(s);
+                       }
                }
-               /* XXX process the capabilites */
                break;
-       case KEEP_ALIVE_MESSAGE:
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(4);
-               req->e = htolel(KEEP_ALIVE_ACK_MESSAGE);
-               transmit_response(s, req);
-               do_housekeeping(s);
+       case SOFTKEY_RESUME:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Resume(%d)\n", instance);
                break;
-       case OFFHOOK_MESSAGE:
+       case SOFTKEY_ANSWER:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Answer(%d)\n", instance);
+
                transmit_ringer_mode(s,SKINNY_RING_OFF);
-               transmit_lamp_indication(s, STIMULUS_LINE, s->device->lines->instance, SKINNY_LAMP_ON);
-               sub = find_subchannel_by_line(s->device->lines);
-               if (!sub) {
-                       ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
-                       return 0;
-               }
-               sub->parent->hookstate = SKINNY_OFFHOOK;
+               transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
 
-               if (sub->outgoing) {
+               l->hookstate = SKINNY_OFFHOOK;
+
+               if (sub && sub->outgoing) {
                        /* We're answering a ringing call */
                        ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
-                       transmit_callstate(s, s->device->lines->instance, SKINNY_OFFHOOK, sub->callid);
+                       transmit_callstate(s, l->instance, SKINNY_OFFHOOK, sub->callid);
                        transmit_tone(s, SKINNY_SILENCE);
-                       transmit_callstate(s, s->device->lines->instance, SKINNY_CONNECTED, sub->callid);
+                       transmit_callstate(s, l->instance, SKINNY_CONNECTED, sub->callid);
+                       transmit_selectsoftkeys(s, l->instance, sub->callid, KEYDEF_CONNECTED);
                        start_rtp(sub);
                        ast_setstate(sub->owner, AST_STATE_UP);
-                       /* XXX select the appropriate soft key here */
-               } else {
-                       if (!sub->owner) {
-                               transmit_callstate(s, s->device->lines->instance, SKINNY_OFFHOOK, sub->callid);
-                               if (skinnydebug) {
-                                       ast_verbose("Attempting to Clear display on Skinny %s@%s\n",sub->parent->name, sub->parent->parent->name);
-                               }
-                               transmit_displaymessage(s, NULL); /* clear display */
-                               transmit_tone(s, SKINNY_DIALTONE);
-                               c = skinny_new(sub, AST_STATE_DOWN);
-                               if(c) {
-                                       /* start the switch thread */
-                                       if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
-                                               ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
-                                               ast_hangup(c);
-                                       }
-                               } else {
-                                       ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", sub->parent->name, s->device->name);
-                               }
-                       } else {
-                               ast_log(LOG_DEBUG, "Current sub [%s] already has owner\n", sub->owner->name);
-                       }
                }
                break;
-       case ONHOOK_MESSAGE:
-               sub = find_subchannel_by_line(s->device->lines);
-               if (sub->parent->hookstate == SKINNY_ONHOOK) {
-                       /* Something else already put us back on hook */
-                       break;
-               }
-               sub->cxmode = SKINNY_CX_RECVONLY;
-               sub->parent->hookstate = SKINNY_ONHOOK;
-               transmit_callstate(s, s->device->lines->instance, sub->parent->hookstate,sub->callid);
-               if (skinnydebug) {
-                       ast_verbose("Skinny %s@%s went on hook\n",sub->parent->name, sub->parent->parent->name);
-               }
-               if (sub->parent->transfer && (sub->owner && sub->next->owner) && ((!sub->outgoing) || (!sub->next->outgoing))) {
-                       /* We're allowed to transfer, we have two active calls and
-                          we made at least one of the calls.  Let's try and transfer */
+       case SOFTKEY_INFO:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Info(%d)\n", instance);
+               break;
+       case SOFTKEY_CONFRN:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Transfer(%d)\n", instance);
+               transmit_tone(s, SKINNY_DIALTONE);
+               /* XXX determine the best way to pull off a conference.  Meetme? */
+               break;
+       case SOFTKEY_PARK:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Park Call(%d)\n", instance);
+               /* XXX Park the call */
+               break;
+       case SOFTKEY_JOIN:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Join(%d)\n", instance);
+               break;
+       case SOFTKEY_MEETME:
+               /* XXX How is this different from CONFRN? */
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Meetme(%d)\n", instance);
+               break;
+       case SOFTKEY_PICKUP:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: None(%d)\n", instance);
+               break;
+       case SOFTKEY_GPICKUP:
+               if (skinnydebug)
+                       ast_verbose("Received Softkey Event: Group Pickup (%d)\n", instance);
+               break;
+       default:
+               if (skinnydebug)
+                       ast_verbose("Received unknown Softkey Event: %d(%d)\n", event, instance);
+               break;
+       }
+       return 1;
+}
 
-#if 0
-                       if ((res = attempt_transfer(p)) < 0) {
-                                if (p->sub->next->owner) {
-                                       sub->next->alreadygone = 1;
-                                       ast_queue_hangup(sub->next->owner,1);
-                               }
-                       } else if (res) {
-                               ast_log(LOG_WARNING, "Transfer attempt failed\n");
-                               return -1;
-                       }
-#endif
-               } else {
-                       /* Hangup the current call */
-                       /* If there is another active call, skinny_hangup will ring the phone with the other call */
-                       if (sub->owner) {
-                               sub->alreadygone = 1;
-                               ast_queue_hangup(sub->owner);
-                       } else {
-                               ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
-                                       sub->parent->name, sub->parent->parent->name, sub->callid);
-                       }
-               }
-               if ((sub->parent->hookstate == SKINNY_ONHOOK) && (!sub->next->rtp)) {
-                       do_housekeeping(s);
-               }
+static int handle_unregister_message(skinny_req *req, struct skinnysession *s)
+{
+       return skinny_unregister(req, s);
+}
+
+static int handle_soft_key_template_req_message(skinny_req *req, struct skinnysession *s)
+{
+       if (!(req = req_alloc(sizeof(soft_key_template_res_message), SOFT_KEY_TEMPLATE_RES_MESSAGE)))
+               return -1;
+
+       req->data.softkeytemplate.softKeyOffset = htolel(0);
+       req->data.softkeytemplate.softKeyCount  = htolel(sizeof(soft_key_template_default) / sizeof(soft_key_template_definition));
+       req->data.softkeytemplate.totalSoftKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(soft_key_template_definition));
+       memcpy(req->data.softkeytemplate.softKeyTemplateDefinition,
+               soft_key_template_default,
+               sizeof(soft_key_template_default));
+       transmit_response(s,req);
+       return 1;
+}
+
+static int handle_headset_status_message(skinny_req *req, struct skinnysession *s)
+{
+       /* XXX umm...okay?  Why do I care? */
+       return 1;
+}
+
+static int handle_register_available_lines_message(skinny_req *req, struct skinnysession *s)
+{
+       /* XXX I have no clue what this is for, but my phone was sending it, so... */
+       return 1;
+}
+
+static int handle_message(skinny_req *req, struct skinnysession *s)
+{
+       int res = 0;
+
+       if ((!s->device) && (letohl(req->e) != REGISTER_MESSAGE && letohl(req->e) != ALARM_MESSAGE)) {
+               ast_log(LOG_WARNING, "Client sent message #%d without first registering.\n", req->e);
+               free(req);
+               return 0;
+       }
+
+       switch(letohl(req->e))  {
+       case KEEP_ALIVE_MESSAGE:
+               res = handle_keep_alive_message(req, s);
+               break;
+       case REGISTER_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Device %s is attempting to register\n", req->data.reg.name);
+
+               res = handle_register_message(req, s);
+               break;
+       case IP_PORT_MESSAGE:
+               res = handle_ip_port_message(req, s);
                break;
        case KEYPAD_BUTTON_MESSAGE:
-               digit = letohl(req->data.keypad.button);
-               if (skinnydebug) {
-                       ast_verbose("Collected digit: [%d]\n", digit);
-               }
-               f.frametype = AST_FRAME_DTMF;
-               if (digit == 14) {
-                       d = '*';
-               } else if (digit == 15) {
-                       d = '#';
-               } else if (digit >=0 && digit <= 9) {
-                       d = '0' + digit;
-               } else {
-                       /* digit=10-13 (A,B,C,D ?), or
-                        * digit is bad value
-                        *
-                        * probably should not end up here, but set
-                        * value for backward compatibility, and log
-                        * a warning.
-                        */
-                       d = '0' + digit;
-                       ast_log(LOG_WARNING, "Unsupported digit %d\n", digit);
-               }
-               f.subclass = d;
-               f.src = "skinny";
-               sub = find_subchannel_by_line(s->device->lines);
-               if (sub->owner) {
-                       /* XXX MUST queue this frame to all subs in threeway call if threeway call is active */
-                       ast_queue_frame(sub->owner, &f);
-                       if (sub->next->owner) {
-                               ast_queue_frame(sub->next->owner, &f);
-                       }
-               } else {
-                       if (skinnydebug) {
-                               ast_verbose("No owner: %s\n", s->device->lines->name);
-                       }
-               }
+               if (skinnydebug)
+                       ast_verbose("Collected digit: [%d]\n", letohl(req->data.keypad.button));
+
+               res = handle_keypad_button_message(req, s);
+               break;
+       case STIMULUS_MESSAGE:
+               res = handle_stimulus_message(req, s);
+               break;
+       case OFFHOOK_MESSAGE:
+               res = handle_offhook_message(req, s);
+               break;
+       case ONHOOK_MESSAGE:
+               res = handle_onhook_message(req, s);
+               break;
+       case CAPABILITIES_RES_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Received CapabilitiesRes\n");
+
+               res = handle_capabilities_res_message(req, s);
+               break;
+       case SPEED_DIAL_STAT_REQ_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Received SpeedDialStatRequest\n");
+
+               res = handle_speed_dial_stat_req_message(req, s);
+               break;
+       case LINE_STATE_REQ_MESSAGE:
+               res = handle_line_state_req_message(req, s);
+               break;
+       case TIME_DATE_REQ_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Received Time/Date Request\n");
+
+               res = handle_time_date_req_message(req, s);
+               break;
+       case BUTTON_TEMPLATE_REQ_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Buttontemplate requested\n");
+
+               res = handle_button_template_req_message(req, s);
+               break;
+       case VERSION_REQ_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Version Request\n");
+
+               res = handle_version_req_message(req, s);
+               break;
+       case SERVER_REQUEST_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Received Server Request\n");
+
+               res = handle_server_request_message(req, s);
+               break;
+       case ALARM_MESSAGE:
+               res = handle_alarm_message(req, s);
                break;
        case OPEN_RECEIVE_CHANNEL_ACK_MESSAGE:
-               if (skinnydebug) {
+               if (skinnydebug)
                        ast_verbose("Received Open Receive Channel Ack\n");
-               }
-               status = letohl(req->data.openreceivechannelack.status);
-               if (status) {
-                       ast_log(LOG_ERROR, "Open Receive Channel Failure\n");
-                       break;
-               }
-               /* ENDIAN */
-               memcpy(addr, req->data.openreceivechannelack.ipAddr, sizeof(addr));
-               port = htolel(req->data.openreceivechannelack.port);
-               sin.sin_family = AF_INET;
-               /* I smell endian problems */
-               memcpy(&sin.sin_addr, addr, sizeof(sin.sin_addr));
-               sin.sin_port = htons(port);
-               if (skinnydebug) {
-                       ast_verbose("ipaddr = %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
-               }
-               sub = find_subchannel_by_line(s->device->lines);
-               if (sub->rtp) {
-                       ast_rtp_set_peer(sub->rtp, &sin);
-                       ast_rtp_get_us(sub->rtp, &us);
-               } else {
-                       ast_log(LOG_ERROR, "No RTP structure, this is very bad\n");
-                       break;
-               }
-               memset(req, 0, SKINNY_MAX_PACKET);
-               req->len = htolel(sizeof(start_media_transmission_message)+4);
-               req->e = htolel(START_MEDIA_TRANSMISSION_MESSAGE);
-               req->data.startmedia.conferenceId = 0;
-               req->data.startmedia.passThruPartyId = 0;
-               memcpy(req->data.startmedia.remoteIp, &s->device->ourip, 4); /* Endian? */
-               req->data.startmedia.remotePort = htolel(ntohs(us.sin_port));
-               req->data.startmedia.packetSize = htolel(20);
-               req->data.startmedia.payloadType = htolel(convert_cap(s->device->lines->capability));
-               req->data.startmedia.qualifier.precedence = htolel(127);
-               req->data.startmedia.qualifier.vad = 0;
-               req->data.startmedia.qualifier.packets = 0;
-               req->data.startmedia.qualifier.bitRate = 0;
-               transmit_response(s, req);
+
+               res = handle_open_receive_channel_ack_message(req, s);
+               break;
+       case SOFT_KEY_SET_REQ_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Received SoftKeySetReq\n");
+
+               res = handle_soft_key_set_req_message(req, s);
+               break;
+       case SOFT_KEY_EVENT_MESSAGE:
+               res = handle_soft_key_event_message(req, s);
+               break;
+       case UNREGISTER_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Received Unregister Request\n");
+
+               res = handle_unregister_message(req, s);
+               break;
+       case SOFT_KEY_TEMPLATE_REQ_MESSAGE:
+               if (skinnydebug)
+                       ast_verbose("Received SoftKey Template Request\n");
+
+               res = handle_soft_key_template_req_message(req, s);
+               break;
+       case HEADSET_STATUS_MESSAGE:
+               res = handle_headset_status_message(req, s);
+               break;
+       case REGISTER_AVAILABLE_LINES_MESSAGE:
+               res = handle_register_available_lines_message(req, s);
                break;
        default:
-               if (skinnydebug) {
+               if (skinnydebug)
                        ast_verbose("RECEIVED UNKNOWN MESSAGE TYPE:  %x\n", letohl(req->e));
-               }
                break;
        }
-       free(req);
-       return 1;
+       if (res >= 0 && req)
+               free(req);
+       return res;
 }
 
 static void destroy_session(struct skinnysession *s)
@@ -2867,15 +3803,25 @@ static int get_input(struct skinnysession *s)
 
        fds[0].fd = s->fd;
        fds[0].events = POLLIN;
+       fds[0].revents = 0;
        res = poll(fds, 1, -1);
 
        if (res < 0) {
-               ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
-       } else if (res > 0) {
+               if (errno != EINTR)
+               {
+                       ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
+                       return res;
+               }
+       }
+       if (fds[0].revents) {
+               ast_mutex_lock(&s->lock);
                memset(s->inbuf,0,sizeof(s->inbuf));
                res = read(s->fd, s->inbuf, 4);
-               if (res != 4) {
-                       ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
+               if (res < 0) {
+                       ast_log(LOG_WARNING, "read() returned error: %s\n", strerror(errno));
+                       return res;
+               } else if (res != 4) {
+                       ast_log(LOG_WARNING, "Skinny Client sent less data than expected.  Expected 4 but got %d.\n", res);
                        return -1;
                }
                dlen = letohl(*(int *)s->inbuf);
@@ -2883,33 +3829,40 @@ static int get_input(struct skinnysession *s)
                        dlen = sizeof(s->inbuf) - 8;
                }
                *(int *)s->inbuf = htolel(dlen);
+
                res = read(s->fd, s->inbuf+4, dlen+4);
                ast_mutex_unlock(&s->lock);
-               if (res != (dlen+4)) {
+               if (res < 0) {
+                       ast_log(LOG_WARNING, "read() returned error: %s\n", strerror(errno));
+                       return res;
+               } else if (res != (dlen+4)) {
                        ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
                        return -1;
                }
+               return res;
        }
-       return res;
+       return 0;
 }
 
 static skinny_req *skinny_req_parse(struct skinnysession *s)
 {
        skinny_req *req;
 
-       req = malloc(SKINNY_MAX_PACKET);
-       if (!req) {
-               ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
+       if (!(req = ast_calloc(1, SKINNY_MAX_PACKET)))
                return NULL;
-       }
-       memset(req, 0, sizeof(skinny_req));
-       /* +8 to account for reserved and length fields */
-       memcpy(req, s->inbuf, letohl(*(int*)(s->inbuf))+8);
+
+       ast_mutex_lock(&s->lock);
+       memcpy(req, s->inbuf, skinny_header_size);
+       memcpy(&req->data, s->inbuf+skinny_header_size, letohl(*(int*)(s->inbuf))-4);
+
+       ast_mutex_unlock(&s->lock);
+
        if (letohl(req->e) < 0) {
                ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
                free(req);
                return NULL;
        }
+
        return req;
 }
 
@@ -2922,20 +3875,25 @@ static void *skinny_session(void *data)
 
        if (option_verbose > 2)
                ast_verbose(VERBOSE_PREFIX_3 "Starting Skinny session from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
+
        for (;;) {
-               res = 0;
                res = get_input(s);
                if (res < 0) {
                        break;
                }
-               req = skinny_req_parse(s);
-               if (!req) {
-                       return NULL;
-               }
-               res = handle_message(req, s);
-               if (res < 0) {
-                       destroy_session(s);
-                       return NULL;
+
+               if (res > 0)
+               {
+                       if (!(req = skinny_req_parse(s))) {
+                               destroy_session(s);
+                               return NULL;
+                       }
+
+                       res = handle_message(req, s);
+                       if (res < 0) {
+                               destroy_session(s);
+                               return NULL;
+                       }
                }
        }
        ast_log(LOG_NOTICE, "Skinny Session returned: %s\n", strerror(errno));
@@ -2969,12 +3927,9 @@ static void *accept_thread(void *ignore)
                                ast_log(LOG_WARNING, "Failed to set Skinny tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
                        }
                }
-               s = malloc(sizeof(struct skinnysession));
-               if (!s) {
-                       ast_log(LOG_WARNING, "Failed to allocate Skinny session: %s\n", strerror(errno));
+               if (!(s = ast_calloc(1, sizeof(struct skinnysession))))
                        continue;
-               }
-               memset(s, 0, sizeof(struct skinnysession));
+
                memcpy(&s->sin, &sin, sizeof(sin));
                ast_mutex_init(&s->lock);
                s->fd = as;
@@ -2987,9 +3942,8 @@ static void *accept_thread(void *ignore)
                        destroy_session(s);
                }
        }
-       if (skinnydebug) {
+       if (skinnydebug)
                ast_verbose("killing accept thread\n");
-       }
        close(as);
        return 0;
 }
@@ -3052,7 +4006,7 @@ static int restart_monitor(void)
 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause)
 {
        int oldformat;
-       struct skinny_subchannel *sub;
+       struct skinny_line *l;
        struct ast_channel *tmpc = NULL;
        char tmp[256];
        char *dest = data;
@@ -3068,17 +4022,15 @@ static struct ast_channel *skinny_request(const char *type, int format, void *da
                ast_log(LOG_NOTICE, "Skinny channels require a device\n");
                return NULL;
        }
-       sub = find_subchannel_by_name(tmp);
-       if (!sub) {
+       l = find_line_by_name(tmp);
+       if (!l) {
                ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
                return NULL;
        }
        if (option_verbose > 2) {
                ast_verbose(VERBOSE_PREFIX_3 "skinny_request(%s)\n", tmp);
-               ast_verbose(VERBOSE_PREFIX_3 "Skinny cw: %d, dnd: %d, so: %d, sno: %d\n",
-                       sub->parent->callwaiting, sub->parent->dnd, sub->owner ? 1 : 0, sub->next->owner ? 1: 0);
        }
-       tmpc = skinny_new(sub->owner ? sub->next : sub, AST_STATE_DOWN);
+       tmpc = skinny_new(l, AST_STATE_DOWN);
        if (!tmpc) {
                ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
        }
@@ -3129,10 +4081,10 @@ static int reload_config(void)
                        } else {
                                memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
                        }
-               } else if (!strcasecmp(v->name, "keepAlive")) {
+               } else if (!strcasecmp(v->name, "keepalive")) {
                        keep_alive = atoi(v->value);
-               } else if (!strcasecmp(v->name, "dateFormat")) {
-                       strncpy(date_format, v->value, sizeof(date_format) - 1);
+               } else if (!strcasecmp(v->name, "dateformat")) {
+                       ast_copy_string(date_format, v->value, sizeof(date_format));
                } else if (!strcasecmp(v->name, "allow")) {
                        format = ast_getformatbyname(v->value);
                        if (format < 1) {
@@ -3147,17 +4099,21 @@ static int reload_config(void)
                        } else {
                                capability &= ~format;
                        }
-               } else if (!strcasecmp(v->name, "port")) {
+               } else if (!strcasecmp(v->name, "bindport") || !strcasecmp(v->name, "port")) {
                        if (sscanf(v->value, "%d", &ourport) == 1) {
                                bindaddr.sin_port = htons(ourport);
                        } else {
-                               ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
+                               ast_log(LOG_WARNING, "Invalid bindport '%s' at line %d of %s\n", v->value, v->lineno, config);
+                       }
+                       if (!strcasecmp(v->name, "port")) { /*! \todo Remove 'port' option after 1.4 */
+                               ast_log(LOG_WARNING, "Option 'port' at line %d of %s has been deprecated.  Please use 'bindport' instead.\n", v->lineno, config);
                        }
                }
                v = v->next;
        }
+
        if (ntohl(bindaddr.sin_addr.s_addr)) {
-               memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
+               __ourip = bindaddr.sin_addr;
        } else {
                hp = ast_gethostbyname(ourhost, &ahp);
                if (!hp) {
@@ -3244,7 +4200,8 @@ static void delete_devices(void)
 {
        struct skinny_device *d, *dlast;
        struct skinny_line *l, *llast;
-       struct skinny_subchannel *sub, *slast;
+       struct skinny_speeddial *sd, *sdlast;
+       struct skinny_addon *a, *alast;
 
        ast_mutex_lock(&devicelock);
 
@@ -3252,18 +4209,25 @@ static void delete_devices(void)
        for (d=devices;d;) {
                /* Delete all lines for this device */
                for (l=d->lines;l;) {
-                       /* Delete all subchannels for this line */
-                       for (sub=l->sub;sub;) {
-                               slast = sub;
-                               sub = sub->next;
-                               ast_mutex_destroy(&slast->lock);
-                               free(slast);
-                       }
                        llast = l;
                        l = l->next;
                        ast_mutex_destroy(&llast->lock);
                        free(llast);
                }
+               /* Delete all speeddials for this device */
+               for (sd=d->speeddials;sd;) {
+                       sdlast = sd;
+                       sd = sd->next;
+                       ast_mutex_destroy(&sdlast->lock);
+                       free(sdlast);
+               }
+               /* Delete all addons for this device */
+               for (a=d->addons;a;) {
+                       alast = a;
+                       a = a->next;
+                       ast_mutex_destroy(&alast->lock);
+                       free(alast);
+               }
                dlast = d;
                d = d->next;
                free(dlast);
@@ -3296,6 +4260,7 @@ static int load_module(void *mod)
        ast_cli_register(&cli_show_lines);
        ast_cli_register(&cli_debug);
        ast_cli_register(&cli_no_debug);
+       ast_cli_register(&cli_reset_device);
        sched = sched_context_create();
        if (!sched) {
                ast_log(LOG_WARNING, "Unable to create schedule context\n");
@@ -3321,9 +4286,8 @@ static int load_module(void *mod)
 static int unload_module(void *mod)
 {
 #if 0
-       struct skinny_session *session, s;
-       struct skinny_subchannel *sub;
-       struct skinny_line *line = session;
+       struct skinnysession *session, s;
+       struct skinny_line *l = session;
 
        /* close all IP connections */
        if (!ast_mutex_lock(&devicelock)) {
@@ -3367,6 +4331,7 @@ static int unload_module(void *mod)
        ast_cli_unregister(&cli_show_lines);
        ast_cli_unregister(&cli_debug);
        ast_cli_unregister(&cli_no_debug);
+       ast_cli_unregister(&cli_reset_device);
 
        return 0;
 #endif