From 4adf95a6528378a3acfd4e11f42b8fdc1ef95879 Mon Sep 17 00:00:00 2001
From: burneykb <burneykb@iastate.edu>
Date: Mon, 13 Feb 2017 09:20:44 -0600
Subject: [PATCH] data dumping after auto. flight works correctly

---
 groundStation/src/backend/backend.c | 37 +++++++++++++++++++++--------
 groundStation/src/backend/config.h  |  4 ++--
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index db4ec5d73..d206bb5b2 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -44,7 +44,7 @@
 
 #define QUAD_BT_ADDR  "00:06:66:64:61:D6"
 #define QUAD_BT_CHANNEL  0x01
-#define CMD_MAX_LENGTH 1024
+#define CMD_MAX_LENGTH 4096
 #define MAX_HASH_SIZE 50
 
 /* Backend-internal command magics */
@@ -269,7 +269,6 @@ int main(int argc, char **argv)
 						 * Ignore stdin from the backend
 						 */
 					} else if (fd == zyboSocket) {
-						printf("recieving from quad\n");
 						quad_recv();
 					} else if (fd == backendSocket) {
 						int new_fd = 0;
@@ -714,11 +713,11 @@ static void client_recv(int fd) {
 }
 
 static void quad_recv() {
-	static unsigned char respBuf[4096];
+	static unsigned char respBuf[CMD_MAX_LENGTH];
 	static size_t respBufLen;
 
 	struct metadata m;
-	uint8_t data[4096];
+	uint8_t data[CMD_MAX_LENGTH];
 	size_t respLen;
 	ssize_t datalen;
 	size_t packetlen;
@@ -727,31 +726,48 @@ static void quad_recv() {
 			CMD_MAX_LENGTH - respBufLen);
 	if (respLen <= 0) {
 		perror("ERROR reading from quad...\n");
+		respBufLen = 0;
 		return;
 	}
 	respBufLen += respLen;
 
 	// printf("packet = '");
 	// for(int i = 0; i < (int)respBufLen; ++i) {
-	// 	printf(" %.2x ", respBuf[i]);
+	// 	printf(" 0x%.2x ", respBuf[i]);
 	// }
 	// printf("'\n");
 
 	while(respBufLen){
-		datalen = DecodePacket(&m, data, 2048, respBuf, respBufLen);
-	
+		datalen = DecodePacket(&m, data, CMD_MAX_LENGTH, respBuf, respBufLen);
 		if (datalen == -1) {
-			warnx("No start Byte!\n");
+			warnx("No start Byte");
+			for (size_t i = 0; i < respBufLen; ++i) {
+				if (respBuf[i] == BEGIN_CHAR) {
+					memmove(respBuf, respBuf + i, respBufLen - i);
+					respBufLen -=i;
+					return;
+				}
+
+			}
 			respBufLen = 0;
 			return;
 		}
 		if (datalen == -5) {
-			warnx("Chechsum mismatch!\n");
+			warnx("Chechsum mismatch");
+			for (size_t i = 0; i < respBufLen; ++i) {
+				if (respBuf[i] == BEGIN_CHAR) {
+					memmove(respBuf, respBuf + i, respBufLen - i);
+					respBufLen -=i;
+					return;
+				}
+
+			}
 			respBufLen = 0;
 			return;
 		}
 		if (datalen < 0){
 			/* Not enough data yet. We need to wait for more */
+			// printf("not enough\n");
 			return;	
 		}
 
@@ -769,8 +785,9 @@ static void quad_recv() {
 				break;
 			case LOG_ID:
 				/* something like this */
+				printf("(Quad) : Log found\n");
 				fwrite((char *) data, sizeof(char), m.data_len, quadlog_file);
-				fflush(quadlog_file);
+				// fflush(quadlog_file);
 				break;
 			case RESPONSE_ID:
 				printf("(Backend): response id found\n");
diff --git a/groundStation/src/backend/config.h b/groundStation/src/backend/config.h
index 64691610d..9bb8e0983 100644
--- a/groundStation/src/backend/config.h
+++ b/groundStation/src/backend/config.h
@@ -13,8 +13,8 @@
 // 	backend with sudo elevation and with the --preserve-env flag or -E
 #define QUAD_WIFI_ENV "UCART_USE_WIFI"
 #define QUAD_IP_ENV "UCART_QUAD_IP"
-#define QUAD_IP_DEFAULT "192.168.4.1"
+#define QUAD_IP_DEFAULT "192.168.1.1"
 #define QUAD_PORT_ENV "UCART_QUAD_PORT"
 #define QUAD_PORT_DEFAULT 8080
 
-#endif /* __CONFIG_H */
\ No newline at end of file
+#endif /* __CONFIG_H */
-- 
GitLab