Index: chan_skinny.c
===================================================================
--- chan_skinny.c	(revision 246666)
+++ chan_skinny.c	(working copy)
@@ -71,6 +71,8 @@
 #include "asterisk/indications.h"
 #include "asterisk/linkedlists.h"
 
+#define SKINNY_DEVMODE
+
 #ifdef SKINNY_DEVMODE
 #define SKINNY_DEVONLY(code)	\
 	code
@@ -969,7 +971,7 @@
  * Asterisk specific globals *
  *****************************/
 
-static int skinnydebug = 0;
+static int skinnydebug = 2;
 static int skinnyreload = 0;
 
 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
@@ -1913,7 +1915,7 @@
 {
 	char *tmp;
 
-	switch (type) {
+	switch (letohl(type)) { // that var needs to be byteswapped
 	case KEEP_ALIVE_MESSAGE:
 		return "KEEP_ALIVE_MESSAGE";
 	case REGISTER_MESSAGE:
@@ -2053,8 +2055,8 @@
 
 	SKINNY_DEVONLY(if (skinnydebug>1) ast_verb(4, "Transmitting %s to %s\n", message2str(req->e), d->name);)
 
-	if (letohl(req->len > SKINNY_MAX_PACKET) || letohl(req->len < 0)) {
-		ast_log(LOG_WARNING, "transmit_response: the length of the request is out of bounds\n");
+        if ((letohl(req->len) > SKINNY_MAX_PACKET) || (letohl(req->len) < 0)) {  // wrong () of that expression, corrected
+		ast_log(LOG_WARNING, "transmit_response: the length of the request (%d) is out of bounds (%d)\n", req->len , letohl(req->len), SKINNY_MAX_PACKET); //added value display
 		ast_mutex_unlock(&s->lock);
 		return -1;
 	}
@@ -2655,10 +2657,10 @@
 		req->data.startmedia.passThruPartyId = htolel(sub->callid);
 		if (!(l->directmedia) || (l->nat)){
 			ast_rtp_get_us(rtp, &us);
-			req->data.startmedia.remoteIp = htolel(d->ourip.s_addr);
+			req->data.startmedia.remoteIp = d->ourip.s_addr; //removed unneeded byteswapping
 			req->data.startmedia.remotePort = htolel(ntohs(us.sin_port));
 		} else {
-			req->data.startmedia.remoteIp = htolel(them.sin_addr.s_addr);
+			req->data.startmedia.remoteIp = them.sin_addr.s_addr; //removed unneeded byteswapping
 			req->data.startmedia.remotePort = htolel(ntohs(them.sin_port));
 		}
 		req->data.startmedia.packetSize = htolel(fmt.cur_ms);
@@ -4534,8 +4536,8 @@
 		/* transmit_respons in line as we don't have a valid d */
 		ast_mutex_lock(&s->lock);
 
-		if (letohl(req->len > SKINNY_MAX_PACKET) || letohl(req->len < 0)) {
-			ast_log(LOG_WARNING, "transmit_response: the length of the request is out of bounds\n");
+		if (letohl(req->len) > SKINNY_MAX_PACKET || letohl(req->len) < 0) { //wrong (), corrected
+			ast_log(LOG_WARNING, "transmit_response: the length (%d) of the request is out of bounds (%d) \n",  letohl(req->len), SKINNY_MAX_PACKET); //added value display
 			ast_mutex_unlock(&s->lock);
 			return -1;
 		}
@@ -5350,13 +5352,13 @@
 			case BT_CUST_LINE:
 				/* assume failure */
 				req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
-				req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
+				req->data.buttontemplate.definition[i].instanceNumber = 0; //removed unneeded byteswapping
 
 				AST_LIST_TRAVERSE(&d->lines, l, list) {
 					if (l->instance == lineInstance) {
 						ast_verb(0, "Adding button: %d, %d\n", BT_LINE, lineInstance);
 						req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
-						req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance);
+						req->data.buttontemplate.definition[i].instanceNumber = lineInstance; //removed unneeded byteswapping
 						lineInstance++;
 						buttonCount++;
 						btnSet = 1;
@@ -5369,7 +5371,7 @@
 						if (sd->isHint && sd->instance == lineInstance) {
 							ast_verb(0, "Adding button: %d, %d\n", BT_LINE, lineInstance);
 							req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
-							req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance);
+							req->data.buttontemplate.definition[i].instanceNumber = lineInstance; //removed unneeded byteswapping
 							lineInstance++;
 							buttonCount++;
 							btnSet = 1;
@@ -5381,13 +5383,13 @@
 			case BT_CUST_LINESPEEDDIAL:
 				/* assume failure */
 				req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
-				req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
+				req->data.buttontemplate.definition[i].instanceNumber = 0; //removed unneeded byteswapping
 
 				AST_LIST_TRAVERSE(&d->lines, l, list) {
 					if (l->instance == lineInstance) {
 						ast_verb(0, "Adding button: %d, %d\n", BT_LINE, lineInstance);
 						req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
-						req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance);
+						req->data.buttontemplate.definition[i].instanceNumber = lineInstance; //removed unneeded byteswapping
 						lineInstance++;
 						buttonCount++;
 						btnSet = 1;
@@ -5400,7 +5402,7 @@
 						if (sd->isHint && sd->instance == lineInstance) {
 							ast_verb(0, "Adding button: %d, %d\n", BT_LINE, lineInstance);
 							req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
-							req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance);
+							req->data.buttontemplate.definition[i].instanceNumber = lineInstance; //removed unneeded byteswapping
 							lineInstance++;
 							buttonCount++;
 							btnSet = 1;
@@ -5408,7 +5410,7 @@
 						} else if (!sd->isHint && sd->instance == speeddialInstance) {
 							ast_verb(0, "Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
 							req->data.buttontemplate.definition[i].buttonDefinition = BT_SPEEDDIAL;
-							req->data.buttontemplate.definition[i].instanceNumber = htolel(speeddialInstance);
+							req->data.buttontemplate.definition[i].instanceNumber = speeddialInstance; //removed unneeded byteswapping
 							speeddialInstance++;
 							buttonCount++;
 							btnSet = 1;
@@ -5425,7 +5427,7 @@
 					if (l->instance == lineInstance) {
 						ast_verb(0, "Adding button: %d, %d\n", BT_LINE, lineInstance);
 						req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
-						req->data.buttontemplate.definition[i].instanceNumber = htolel(lineInstance);
+						req->data.buttontemplate.definition[i].instanceNumber = lineInstance; //removed unneeded byteswapping
 						lineInstance++;
 						buttonCount++;
 						btnSet = 1;
@@ -5441,7 +5443,7 @@
 					if (!sd->isHint && sd->instance == speeddialInstance) {
 						ast_verb(0, "Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
 						req->data.buttontemplate.definition[i].buttonDefinition = BT_SPEEDDIAL;
-						req->data.buttontemplate.definition[i].instanceNumber = htolel(speeddialInstance - 1);
+						req->data.buttontemplate.definition[i].instanceNumber = speeddialInstance - 1; //removed unneeded byteswapping
 						speeddialInstance++;
 						buttonCount++;
 						btnSet = 1;
@@ -5454,16 +5456,18 @@
 			default:
 				ast_verb(0, "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);
+				req->data.buttontemplate.definition[i].instanceNumber = 0;
 				buttonCount++;
 				btnSet = 1;
 				break;
 		}
 	}
 
-	req->data.buttontemplate.buttonOffset = htolel(0);
+	req->data.buttontemplate.buttonOffset = 0;
 	req->data.buttontemplate.buttonCount = htolel(buttonCount);
 	req->data.buttontemplate.totalButtonCount = htolel(buttonCount);
+//req->data.buttontemplate.buttonCount = htolel(buttonCount);
+//req->data.buttontemplate.totalButtonCount = htolel(buttonCount);
 
 	if (skinnydebug)
 		ast_verb(1, "Sending %d template to %s\n",
@@ -5525,7 +5529,7 @@
 		ast_log(LOG_ERROR, "Open Receive Channel Failure\n");
 		return 0;
 	}
-	addr = letohl(req->data.openreceivechannelack.ipAddr);
+	addr = req->data.openreceivechannelack.ipAddr; //removed unneeded byteswapping
 	port = letohl(req->data.openreceivechannelack.port);
 	passthruid = letohl(req->data.openreceivechannelack.passThruId);
 
@@ -5561,7 +5565,7 @@
 
 	req->data.startmedia.conferenceId = htolel(sub->callid);
 	req->data.startmedia.passThruPartyId = htolel(sub->callid);
-	req->data.startmedia.remoteIp = htolel(d->ourip.s_addr);
+	req->data.startmedia.remoteIp = d->ourip.s_addr; //removed unneeded byteswapping
 	req->data.startmedia.remotePort = htolel(ntohs(us.sin_port));
 	req->data.startmedia.packetSize = htolel(fmt.cur_ms);
 	req->data.startmedia.payloadType = htolel(codec_ast2skinny(fmt.bits));
@@ -5646,8 +5650,10 @@
 		for (y = 0; y < softkeymode->count; y++) {
 			for (i = 0; i < (sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition)); i++) {
 				if (defaults[y] == i+1) {
-					req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyTemplateIndex[y] = htolel(i+1);
-					req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyInfoIndex[y] = htolel(i+301);
+					req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyTemplateIndex[y] = (i+1); //removed unneeded byteswapping
+					req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyInfoIndex[y] = htoles(i+301); //corrected byteswapping
+				        if (skinnydebug)	
+						ast_verbose("softKeySetDefinition : softKeyTemplateIndex: %d softKeyInfoIndex: %d\n", i+1, i+301); //added debug information
 				}
 			}
 		}
