Index: xbee.c
===================================================================
--- xbee.c	(revision 1610)
+++ xbee.c	(revision 1611)
@@ -35,8 +35,8 @@
 #include <string.h>
 #include <avr/interrupt.h>
 #include <time.h>
-#include <serial.h> // TODO: integrate serial xbee functions into this file
 #include "wl_defs.h"
+#include "wireless.h"
 #include "xbee.h"
 
 
@@ -45,26 +45,26 @@
 // TODO: convert all int references to int16_t syntax (see stdint.h)
 
 /* I/O Functions */
-static int8_t xbee_send_string(unsigned char* c);
+static int8_t xbee_send_string(uint8_t* c);
 
 /* Command Mode Functions */
-static int xbee_enter_command_mode(void);
-static int xbee_exit_command_mode(void);
-static int xbee_enter_api_mode(void);
-static int xbee_exit_api_mode(void);
-static int xbee_wait_for_string(char* s, uint16_t len);
-static int xbee_wait_for_ok(void);
+static int8_t xbee_enter_command_mode(void);
+static int8_t xbee_exit_command_mode(void);
+static int8_t xbee_enter_api_mode(void);
+static int8_t xbee_exit_api_mode(void);
+static int8_t xbee_wait_for_string(uint8_t* s, uint16_t len);
+static int8_t xbee_wait_for_ok(void);
 
 /* API Mode Functions */
-static int xbee_handle_packet(uint8_t* packet, uint16_t len);
-static int xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len);
-static int xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len);
+static int8_t xbee_handle_packet(uint8_t* packet, uint16_t len);
+static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len);
+static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len);
 static void xbee_handle_status(uint8_t status);
-static int xbee_verify_checksum(uint8_t* packet, uint16_t len);
-static char xbee_compute_checksum(uint8_t* packet, uint16_t len);
-static int xbee_send_frame(uint8_t* buf, int len);
-int xbee_send_read_at_command(uint8_t* command);
-static int xbee_send_modify_at_command(uint8_t* command, uint8_t* value, uint8_t len);
+static int8_t xbee_verify_checksum(uint8_t* packet, uint16_t len);
+static uint8_t xbee_compute_checksum(uint8_t* packet, uint16_t len);
+static int8_t xbee_send_frame(uint8_t* buf, uint16_t len);
+int8_t xbee_send_read_at_command(uint8_t* command);
+static int8_t xbee_send_modify_at_command(uint8_t* command, uint8_t* value, uint8_t len);
 
 /* Buffer Manipulation Functions */
 int8_t xbee_basic_buf_add(uint8_t *ptr, uint8_t byte);
@@ -172,7 +172,7 @@
   // do something based on the type
   i=1;
   switch(apitype) {
-  case XBEE_FRAME_AT_COMMAND_RESPONSE:
+  case XBEE_FRAME_AT_COMMAND_RESPONSE: {
     // AT command response
     if ((xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
       return; // we're currently processing a command, so drop the incoming one
@@ -198,10 +198,9 @@
     }  
     // handle AT command  
     xbee_handle_at_command_response(atcommand,apitype,i-5); // TODO: rewrite function
-    break;
-  case XBEE_FRAME_TX_STATUS:
+    break; }
+  case XBEE_FRAME_TX_STATUS: {
     // TX status
-    if (0) { WL_DEBUG_PRINT("test"); } // why do I need this statement?
     uint8_t frame_id = 0;
     while(i<len) {
       if (FLAG) {
@@ -214,11 +213,11 @@
         i++;
       }
     }
-    break;
-  case XBEE_FRAME_RX_64:
+    break; }
+  case XBEE_FRAME_RX_64: {
     // receive a packet with 64bit address
-    break; // TODO: implement this (even if we don't use it)
-  case XBEE_FRAME_RX_16:
+    break; } // TODO: implement this (even if we don't use it)
+  case XBEE_FRAME_RX_16: {
     // receive a packet with 16bit address
     uint16_t source = 0;
     uint8_t framenum = 0;
@@ -292,7 +291,7 @@
         }
       }
     }
-    break;
+    break; }
   } // end of switch statement
   while (1) {
     if (FLAG) {
@@ -310,7 +309,7 @@
     WL_DEBUG_PRINT("basic buffer full\r\n");
     return -1;
   }
-  xbee_basic_buf[(*ptr)++] = byte);
+  xbee_basic_buf[(*ptr)++] = byte;
   if (*ptr == PACKET_BUFFER_SIZE)
     *ptr = 0;
   return 0;
@@ -330,7 +329,7 @@
     return -1;
   }
   xbee_other_buf[(*ptr)++] = byte;
-  if (ptr == PACKET_BUFFER_SIZE)
+  if (*ptr == PACKET_BUFFER_SIZE)
     *ptr = 0;
   return 0;
 }
@@ -343,7 +342,7 @@
 {
 	WL_DEBUG_PRINT("in xbee_init\n");
   
-  if(xbee_status&0x08 == XBEE_INITD) {
+  if((xbee_status&0x08) == XBEE_INITD) {
     return WL_ERROR_INIT_ALREADY_INITD;
   }
   
@@ -368,7 +367,7 @@
 
 	WL_DEBUG_PRINT("Entering command mode.\r\n");
 	if (xbee_enter_command_mode() != 0) {
-    usb_puts("error entering command mode\r\n");
+    WL_DEBUG_PRINT("error entering command mode\r\n");
 		return -1;
 	}
 	WL_DEBUG_PRINT("Entered command mode.\r\n");
@@ -380,13 +379,14 @@
   
   // set baud on xbee
 #if (XBEE_BAUD == 115200)
-  xbee_send_string("ATBD7\r");
+  xbee_send_string((uint8_t*)"ATBD7\r");
+  xbee_send_string((uint8_t*)"ATBD7\r");
 #elif (XBEE_BAUD == 57600)
-  xbee_send_string("ATBD6\r");
+  xbee_send_string((uint8_t*)"ATBD6\r");
 #elif (XBEE_BAUD == 38400)
-  xbee_send_string("ATBD5\r");
+  xbee_send_string((uint8_t*)"ATBD5\r");
 #elif (XBEE_BAUD == 19200)
-  xbee_send_string("ATBD4\r");
+  xbee_send_string((uint8_t*)"ATBD4\r");
 #elif (XBEE_BAUD == 9600)
   // already at this baud rate
   //xbee_send_string("ATBD3\r\n");
@@ -397,7 +397,7 @@
   // exit command mode
   xbee_wait_for_ok();
   WL_DEBUG_PRINT("got ok from baud reset\r\n");
-  xbee_send_string("ATCN\r");
+  xbee_send_string((uint8_t*)"ATCN\r");
   xbee_wait_for_ok();
   WL_DEBUG_PRINT("got ok from exiting command mode\r\n");
   
@@ -449,7 +449,7 @@
 
   // enter command mode
   WL_DEBUG_PRINT("entering command mode 2\r\n");
-  xbee_send_string("+++");
+  xbee_send_string((uint8_t*)"+++");
   xbee_wait_for_ok();
   WL_DEBUG_PRINT("entered command mode 2\r\n");
   
@@ -495,7 +495,7 @@
  * @param c the byte to send
  * @return 0 for success, nonzero for failure
  **/
-int xbee_putc(char c) {
+int8_t xbee_putc(uint8_t c) {
   if(!(xbee_status&XBEE_INITD))
     return WL_ERROR_LIBRARY_NOT_INITD;
 
@@ -573,8 +573,9 @@
  **/
 static int8_t xbee_send(uint8_t* buf, uint16_t size)
 {
-	while(--size > 0)
-		if (xbee_putc(buf[i]) != WL_SUCCESS)
+  uint16_t i=0; // check if we need this variable
+	while(i<size) {
+		if (xbee_putc(buf[i++]) != WL_SUCCESS)
       return WL_ERROR_SEND;
 	}  
   if (xbee_putc(buf[0]) != WL_SUCCESS)
@@ -588,9 +589,9 @@
  *
  * @param c the string to send to the XBEE
  **/
-static int8_t xbee_send_string(unsigned char* c)
+static int8_t xbee_send_string(uint8_t* c)
 {
-	return xbee_send(c, strlen(c));
+	return xbee_send(c, strlen((char*)c));
 }
 
 
@@ -597,9 +598,9 @@
 /**
  * Enter into command mode.
  **/
-static int8_t xbee_enter_command_mode()
+static int8_t xbee_enter_command_mode(void)
 {
-	if (xbee_send_string("+++") != WL_SUCCESS) {
+	if (xbee_send_string((uint8_t*)"+++") != WL_SUCCESS) {
 		return WL_ERROR_XBEE_COMMAND;
 	}
 
@@ -615,7 +616,7 @@
  **/
 static int8_t xbee_exit_command_mode()
 {
-	if (xbee_send_string("ATCN\r") != 0) {
+	if (xbee_send_string((uint8_t*)"ATCN\r") != 0) {
 		return WL_ERROR_SEND;
 	}
 	xbee_wait_for_ok();
@@ -626,9 +627,8 @@
 /**
  * Enter API mode.
  **/
-static int8_t xbee_enter_api_mode()
-{
-	if (xbee_send_string("ATAP 1\r") != 0) {
+static int8_t xbee_enter_api_mode(void) {
+	if (xbee_send_string((uint8_t*)"ATAP 1\r") != 0) {
 		return WL_ERROR_SEND;
 	}
 	xbee_wait_for_ok();
@@ -641,9 +641,8 @@
 /**
  * Enter API mode 2.
  **/
-static int8_t xbee_enter_api_mode2()
-{
-	if (xbee_send_string("ATAP 2\r") != 0) {
+static int8_t xbee_enter_api_mode2(void) {
+	if (xbee_send_string((uint8_t*)"ATAP 2\r") != 0) {
 		return WL_ERROR_SEND;
 	}
 	xbee_wait_for_ok();
@@ -658,19 +657,19 @@
  **/
 static int8_t xbee_exit_api_mode()
 {
-	if (xbee_status&0xC0 == XBEE_COMMAND_WAIT
-    || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
+	if ((xbee_status&0xC0) == XBEE_COMMAND_WAIT
+    || (xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
     return WL_ERROR_XBEE_COMMAND; // can't do command right now
   
   int16_t i=0;
   // change status to command wait
   xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
-  xbee_send_modify_at_command("AP","0",1); // send command
+  xbee_send_modify_at_command((uint8_t*)"AP",(uint8_t*)"0",1); // send command
   // wait for up to 30 ms
-  while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) {
+  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
     delay_us(1); // wait 3us
   }
-  if (i < 1000 && xbee_command[0] == 'A' && xbee_command[1] == 'P') {
+  if (i < 1000 && xbee_command[0] == 'A' && xbee_command[1] == 'P')
     i = WL_SUCCESS;
   else
     i = WL_ERROR_XBEE_COMMAND; // set error code
@@ -681,9 +680,9 @@
 /**
  * Wait until the string "OK\r" is received from the XBee.
  **/
-static int xbee_wait_for_ok()
+static int8_t xbee_wait_for_ok()
 {
-	return xbee_wait_for_string("OK\r", 3);
+	return xbee_wait_for_string((uint8_t*)"OK\r", 3);
 }
 
 /**
@@ -695,12 +694,12 @@
  * @param s the string to receive
  * @param len the length of the string
  **/
-static int8_t xbee_wait_for_string(char* s, int len)
+static int8_t xbee_wait_for_string(uint8_t* s, uint16_t len)
 {
   uint8_t i=0;
-  if (xbee_status&0x03 == XBEE_API_OFF) {
+  if ((xbee_status&0x03) == XBEE_API_OFF) {
     // wait until the response is received (only wait 1 second)
-    while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 1000) {
+    while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 1000) {
       delay_us(1);
     }
     // check response
@@ -728,18 +727,18 @@
  * @param s the string to store the response in
  * @param len the length of the string
  */
-static int8_t xbee_wait_for_response(char* s, int len) {
+static int8_t xbee_wait_for_response(uint8_t* s, int16_t len) {
   uint8_t i=0;
-  if (xbee_status&0x03 == XBEE_API_OFF) {
+  if ((xbee_status&0x03) == XBEE_API_OFF) {
     // wait until the response is received (only wait 1 second)
-    while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 1000) {
+    while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 1000) {
       delay_us(1);
     }
     // check response
-    if (i >= 1000)
+    if (i >= 1000) {
       return -1;
     } else {
-      i=strcspn(xbee_basic_buf,"\r");
+      i=strcspn((char*)xbee_basic_buf,"\r");
       if (i<PACKET_BUFFER_SIZE) {
         memcpy(s,xbee_basic_buf,i);
         xbee_status = xbee_status&0x3F; // clear response
@@ -768,7 +767,7 @@
  * @return 0 if the checksum is incorrect, nonzero
  * otherwise
  **/
-uint8_t xbee_verify_checksum(uint8_t* packet, uint16_t len)
+int8_t xbee_verify_checksum(uint8_t* packet, uint16_t len)
 {
 	uint8_t sum = 0;
 	while(--len > 0) {
@@ -834,12 +833,6 @@
   if (xbee_putc((uint8_t)(len & 0x00FF)) != WL_SUCCESS)
     return WL_ERROR_SEND;
 
-	if (xbee_send(buf, len) != WL_SUCCESS)
-		return WL_ERROR_SEND;
-	
-	if (xbee_putc(checksum) != WL_SUCCESS)
-		return WL_ERROR_SEND;
-	
 	return WL_SUCCESS;
 }
 
@@ -988,7 +981,7 @@
  *
  * @param status the type of status packet received.
  **/
-void xbee_handle_status(char status)
+void xbee_handle_status(uint8_t status)
 {
 	switch (status)
 	{
@@ -1075,19 +1068,19 @@
  **/
 int8_t xbee_set_pan_id(uint16_t id)
 {
-	if (xbee_status&0xC0 == XBEE_COMMAND_WAIT
-    || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
+	if ((xbee_status&0xC0) == XBEE_COMMAND_WAIT
+    || (xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
     return WL_ERROR_XBEE_COMMAND; // can't do command right now
   
   int16_t i=0;
   // change status to command wait
   xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
-  xbee_send_modify_at_command("ID",id,2); // send command to set the channel
+  xbee_send_modify_at_command((uint8_t*)"ID",(uint8_t*)(&id),2); // send command to set the channel
   // wait for up to 30 ms
-  while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) {
+  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
     delay_us(1); // wait 3us
   }
-  if (i < 1000 && xbee_command[0] == 'O' && xbee_command[1] == 'K') {
+  if (i < 1000 && xbee_command[0] == 'O' && xbee_command[1] == 'K')
     i = WL_SUCCESS;
   else
     i = WL_ERROR_XBEE_COMMAND; // set error code
@@ -1103,19 +1096,19 @@
  **/
 uint16_t xbee_get_pan_id()
 {
-  if (xbee_status&0xC0 == XBEE_COMMAND_WAIT
-    || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
+  if ((xbee_status&0xC0) == XBEE_COMMAND_WAIT
+    || (xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
     return WL_ERROR_XBEE_COMMAND_16BIT; // can't do command right now
   
   uint16_t i=0;
   // change status to command wait
   xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
-  xbee_send_read_at_command("ID"); // send command to get the PAN
+  xbee_send_read_at_command((uint8_t*)"ID"); // send command to get the PAN
   // wait for up to 30 ms
-  while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) {
+  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
     delay_us(1); // wait 3us
   }
-  if (i < 1000 && xbee_command[0] == 'I' && xbee_command[1] == 'D') {
+  if (i < 1000 && xbee_command[0] == 'I' && xbee_command[1] == 'D')
     i = (xbee_command[2]<<8)|xbee_command[3]; // get PAN
   else
     i = WL_ERROR_XBEE_COMMAND_16BIT; // set error code
@@ -1139,14 +1132,14 @@
 		return -1;
 	}
 
-	if (xbee_status&0xC0 == XBEE_COMMAND_WAIT
-    || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
+	if ((xbee_status&0xC0) == XBEE_COMMAND_WAIT
+    || (xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
     return WL_ERROR_XBEE_COMMAND; // can't do command right now
   
   int16_t i=0;
   // change status to command wait
   xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
-  xbee_send_modify_at_command("CH",channel,1); // send command to set the channel
+  xbee_send_modify_at_command((uint8_t*)"CH",&channel,1); // send command to set the channel
   // wait for up to 30 ms
   while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
     delay_us(1); // wait 3us
@@ -1175,7 +1168,7 @@
   int16_t i=0;
   // change status to command wait
   xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
-  xbee_send_read_at_command("ID"); // send command to get the channel
+  xbee_send_read_at_command((uint8_t*)"ID"); // send command to get the channel
   // wait for up to 30 ms
   while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
     delay_us(1); // wait 3us
@@ -1204,7 +1197,7 @@
   uint16_t i=0;
   // change status to command wait
   xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
-  xbee_send_read_at_command("MY"); // send command to get the address
+  xbee_send_read_at_command((uint8_t*)"MY"); // send command to get the address
   // wait for up to 30 ms
   while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
     delay_us(1); // wait 3us
