Index: xbee.c
===================================================================
--- xbee.c	(revision 580)
+++ xbee.c	(revision 581)
@@ -55,6 +55,7 @@
 #include <string.h>
 
 #define XBEE_FRAME_START 0x7E
+#define XBEE_GET_PACKET_TIMEOUT 1000
 
 /*Frame Types*/
 #define XBEE_FRAME_STATUS 0x8A
@@ -83,8 +84,8 @@
 static int xbee_exit_command_mode(void);
 static int xbee_enter_api_mode(void);
 static int xbee_exit_api_mode(void);
-static void xbee_wait_for_string(char* s, int len);
-static void xbee_wait_for_ok(void);
+static int xbee_wait_for_string(char* s, int len);
+static int xbee_wait_for_ok(void);
 
 /*API Mode Functions*/
 
@@ -124,7 +125,7 @@
 static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
 static int xbee_channel = XBEE_CHANNEL_DEFAULT;
 static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
-static unsigned int xbee_address = 0;
+static volatile unsigned int xbee_address = 0;
 
 /*Function Implementations*/
 
@@ -207,7 +208,7 @@
 	arrival_buf[0] = 'A';
 	arrival_buf[1] = 'A';
 	arrival_buf[2] = 'A';
-	#ifdef ROBOT
+#ifdef ROBOT
 
 	//enable the receiving interrupt
 #ifdef FIREFLY
@@ -223,6 +224,8 @@
 	{
 		printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port);
 		return -1;
+	} else {
+	  printf("Successfully opened connection to XBee on port %s\r\n", xbee_com_port);
 	}
 
 	// set baud rate, etc. correctly
@@ -246,6 +249,8 @@
 	{
 		fprintf(stderr, "Error setting attributes.\n");
 		return -1;
+	} else {
+	  //printf("Successfully set termios attributes.\n");
 	}
 
 	//lockf(xbee_stream, F_LOCK, 0);
@@ -262,29 +267,72 @@
 	{
 		fprintf(stderr, "Failed to create listener thread.\r\n");
 		return -1;
+	} else {
+	  //printf("Successfully created listener thread.\n");
 	}
 #endif
 
 	if (xbee_enter_command_mode() != 0) {
+#ifndef ROBOT
+	  printf("Error returned from xbee_enter_command_mode\n");
+#endif
 		return -1;
 	}
 
 	if (xbee_enter_api_mode() != 0) {
+#ifndef ROBOT
+	  printf("Error returned from xbee_enter_api_mode\n");
+#endif
 		return -1;
 	}
 
 	if (xbee_exit_command_mode() != 0) {
+#ifndef ROBOT
+	  printf("Error returned from xbee_exit_command_mode\n");
+#endif
 		return -1;
 	}
 
 	if (xbee_send_read_at_command("MY")) {
+#ifndef ROBOT
+	  printf("Error returned from xbee_send_read_at_command\n");
+#endif
 		return -1;
 	}
 
+#ifndef ROBOT
+	//printf("About to enter while loop to get xbee_address.\n");
+	int i;
+	for (i = 0; xbee_address == 0 && i < XBEE_GET_PACKET_TIMEOUT; i++) {
+	  ret = xbee_get_packet(NULL);
+
+	  usleep(1000);
+
+	  if (ret == -1) {
+	    printf("xbee_get_packet(NULL) failed.\n");
+	    return -1;
+	  }
+	}
+
+	//	printf("After exiting while loop to get xbee_address.\n");
+#else
 	//wait to return until the address is set
-	while (xbee_address == 0) xbee_get_packet(NULL);
+	while (xbee_address == 0) {
+	  xbee_get_packet(NULL);
+	}
+#endif
 
+#ifndef ROBOT
+	if (i == XBEE_GET_PACKET_TIMEOUT) { // We timed-out.
+
+	  printf("xbee_get_packet timed out.\n");
+	  return -1;
+	} else {
+	  return 0;
+	}
+#else
 	return 0;
+#endif
 }
 
 /**
@@ -371,9 +419,18 @@
 		return -1;
 	}
 
-	xbee_wait_for_ok();
+#ifndef ROBOT
+	//	printf("In xbee_enter_command_mode about to call xbee_wait_for_ok()\n");
+#endif
 
-	return 0;
+	if (xbee_wait_for_ok() != 0) {
+#ifndef ROBOT
+	  printf("xbee_wait_for_ok failed.\n");
+#endif
+	  return -1;
+	} else {
+	  return 0;
+	}
 }
 
 /**
@@ -414,9 +471,9 @@
 /**
  * Wait until the string "OK\r" is received from the XBee.
  **/
-static void xbee_wait_for_ok()
+static int xbee_wait_for_ok()
 {
-	xbee_wait_for_string("OK\r", 3);
+	return xbee_wait_for_string("OK\r", 3);
 }
 
 /**
@@ -426,8 +483,12 @@
  * @param s the string to receive
  * @param len the length of the string
  **/
-static void xbee_wait_for_string(char* s, int len)
+static int xbee_wait_for_string(char* s, int len)
 {
+#ifndef ROBOT
+  //printf("In xbee_wait_for_string.\n");
+#endif
+
 	char* curr = s;
 	while (curr - s < len) {
 		// check if buffer is empty
@@ -440,14 +501,20 @@
 			if (c == *curr) {
 				curr++;
 			} else {
-				curr = s;
+#ifndef ROBOT
+			  return -1; // Computer is less forgiving.
+#else
+			  curr = s;
+#endif
 			}
-		}
+		} // else buffer is empty.
 
 #ifndef ROBOT
-		usleep(1000);
+		usleep(100);
 #endif
 	}
+
+	return 0;
 }
 
 /**
@@ -510,7 +577,7 @@
 	if (xbee_send(prefix, 3) != 0) {
 		return -1;
 	}
-	
+
 	if (xbee_send(buf, len) != 0) {
 		return -1;
 	}
@@ -681,8 +748,9 @@
 			if (buffer_first == XBEE_BUFFER_SIZE)
 				buffer_first = 0;
 			// check if buffer is empty
-			if (buffer_first == buffer_last)
-				return -1;
+			if (buffer_first == buffer_last) {
+				return 0;
+			}
 		} while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
 
 		if (buffer_first == XBEE_BUFFER_SIZE) {
@@ -713,7 +781,7 @@
 
 		// check if buffer is empty
 		if (buffer_first == buffer_last) {
-			return -1;
+			return 0;
 		}
 		xbee_buf[currentBufPos++] = arrival_buf[buffer_first++];
 		if (buffer_first == XBEE_BUFFER_SIZE) {
@@ -730,12 +798,12 @@
 	}
 
 	//we will take care of the packet
-	if (xbee_handle_packet(xbee_buf + 3, len)) {
-		return -1;
+	if (xbee_handle_packet(xbee_buf + 3, len) != 0) {
+		return 0;
 	}
 
 	if (dest == NULL) {
-		return -1;
+		return 0;
 	}
 
 	int i;
@@ -819,7 +887,7 @@
 	if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0)
 	{
 //							printf("reading xbee_address\n");
-		
+
 		xbee_address = 0;
 		int i;
 		for (i = 0; i < extraLen; i++) {
@@ -955,4 +1023,3 @@
 	xbee_com_port = port;
 }
 #endif
-
