From 9e5f1e2624b7b9daf7a891f1ecc40a6ed6a5b5c7 Mon Sep 17 00:00:00 2001
From: Kris Burney <burneykb@iastate.edu>
Date: Tue, 15 Nov 2016 13:30:29 -0600
Subject: [PATCH] Fixed backend and getpid communication

---
 groundStation/src/backend/backend.c          | 22 ++++++--------------
 groundStation/src/backend/communication.c    |  2 +-
 groundStation/src/cli/cli.c                  |  2 --
 groundStation/src/cli/cli_getpid.c           |  2 ++
 groundStation/src/frontend/frontend_getpid.c |  9 ++++----
 5 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index b2f201bdc..396c84e52 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -567,6 +567,7 @@ static void client_recv(int fd) {
 	}
 	buffer[len_pre + r] = '\0';
 
+	int index = 0;
 	/* Parse buffer and handle commands */
 	while (1) {
 		/* not using strtok because reasons */
@@ -584,31 +585,20 @@ static void client_recv(int fd) {
 			break;
 		}
 		buffer[newline] = '\0';
+		
 		printf("Client(%d) : '%s'\n",fd, buffer);
 		unsigned char * packet;
-		// printf("newline =%li, Client sent: '%s'\n", newline, buffer);
 		if(formatCommand(buffer, &packet) == -1) {
 			printf("Could not recognize command '%s'\n", buffer);
+		} else {
+			int datalen = (packet[6] << 8) | (packet[5]);
+			writeQuad((char *) packet, datalen +7);
 		}
-
-		int datalen = (packet[6] << 8) | (packet[5]);
-		printf("data length = %d\n", datalen);
-		printf("packet = '");
-		for (int i = 0; i < datalen+7+1; ++i)
-		{
-			printf("%x ", packet[i]);
-		}
-		printf("'\n");
-		
-		writeQuad((char *) packet, datalen +7);
-		//free(packet);
 		
 		char * rest = &buffer[newline] + 1;
 		size_t restLen = (strlen(rest) == 0) ? 1 : strlen(rest);
 		/* Delete parsed data and move the rest to the left */
-		memmove(buffer, rest, restLen);
-
-		break;
+		memmove(buffer, rest, restLen + 1);
 	}
 }
 
diff --git a/groundStation/src/backend/communication.c b/groundStation/src/backend/communication.c
index e62d2a170..86784506d 100644
--- a/groundStation/src/backend/communication.c
+++ b/groundStation/src/backend/communication.c
@@ -182,7 +182,7 @@ int formatPacket(metadata_t *metadata, void *data, unsigned char **formattedComm
 	{
 		data_checksum ^= (*formattedCommand)[i];
 	}
-	printf("checksum = %x\n", (unsigned )data_checksum);
+	
 	(*formattedCommand)[7 + metadata->data_len] = data_checksum;
 	
 	return 0;
diff --git a/groundStation/src/cli/cli.c b/groundStation/src/cli/cli.c
index 556c6d649..17d9f8c8a 100644
--- a/groundStation/src/cli/cli.c
+++ b/groundStation/src/cli/cli.c
@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <err.h>
-#include <unistd.h>
 
 #include "cli.h"
 
@@ -47,7 +46,6 @@ int main(int argc, char **argv)
 	}else {
 		(*cli_functions[0]) (conn, argc-1, &argv[1]);
 	}
-	sleep(1);
 
 	ucart_backendDisconnect(conn);
     return 0;
diff --git a/groundStation/src/cli/cli_getpid.c b/groundStation/src/cli/cli_getpid.c
index 92e0fbf3b..55fadb4a3 100644
--- a/groundStation/src/cli/cli_getpid.c
+++ b/groundStation/src/cli/cli_getpid.c
@@ -75,6 +75,8 @@ int getValues(struct backend_conn * conn, struct frontend_pid_data * pid_data) {
 		return 1;
 	}
 
+	return 0;
+
 	switch(pid_data->pid) {
 		case PITCH :
 			printf("Pitch Constants: P = %f\tI = %f\tD = %f\n",
diff --git a/groundStation/src/frontend/frontend_getpid.c b/groundStation/src/frontend/frontend_getpid.c
index 09b2e62b7..0aa064076 100644
--- a/groundStation/src/frontend/frontend_getpid.c
+++ b/groundStation/src/frontend/frontend_getpid.c
@@ -22,22 +22,23 @@
 int frontend_getpid(
 		struct backend_conn * conn, struct frontend_pid_data * pid_data) {
 	
-	char line[100];
+	char line[100] = "";
 	switch (pid_data->pid) {
 		case PITCH :
-			strncpy(line, "getpitchp\ngetpitchd\n\0", 21);
+			strncpy(line, "getpitchp\ngetpitchd\n", 20);
 			break;
 		case ROLL :
-			strncpy(line, "getrollp\ngetrolld\n\0", 19);
+			strncpy(line, "getrollp\ngetrolld\n", 18);
 			break;
 		case YAW :
-			strncpy(line, "getyawp\ngetyawd\n\0", 17);
+			strncpy(line, "getyawp\ngetyawd\n", 17);
 			break;
 		default :
 			return 1;
 	}
 
 	int size;
+	printf("sending '%s'\n",line);
 	if((size = ucart_backendWrite(conn, line)) < 0 ) {
 		return 1;
 	}
-- 
GitLab