From 71c2555e7dad276174df7a5573e61240929eaa7c Mon Sep 17 00:00:00 2001
From: Jake Drahos <j@kedrahos.com>
Date: Thu, 27 Oct 2016 13:24:00 -0500
Subject: [PATCH] Still kinda broken

---
 groundStation/Makefile            |  2 +-
 groundStation/src/config.h        |  1 +
 groundStation/src/microcart_cli.c | 34 +++++++++++++++++++++++--------
 3 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/groundStation/Makefile b/groundStation/Makefile
index ec54cbcc2..d62850d9b 100644
--- a/groundStation/Makefile
+++ b/groundStation/Makefile
@@ -1,7 +1,7 @@
 # Declaration of variables
 GCC=gcc
 GXX=g++
-CFLAGS= -Wall -Wpedantic -Wextra -Werror -std=c99 -g -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function
+CFLAGS= -Wall -Wpedantic -Wextra -Werror -std=c99 -g -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-but-set-variable
 CXXFLAGS= -Wall -Wno-reorder -std=c++11 -g
 INCLUDES = $(foreach dir, $(INCDIR), -I$(dir))
 
diff --git a/groundStation/src/config.h b/groundStation/src/config.h
index c5a2582eb..0e2077cca 100644
--- a/groundStation/src/config.h
+++ b/groundStation/src/config.h
@@ -4,6 +4,7 @@
 
 #define DEFAULT_SOCKET "/var/run/ucart.socket"
 #define SOCKET_ENV "UCART_SOCKET"
+#define NOQUAD_ENV "UCART_NO_QUAD"
 
 
 #endif
diff --git a/groundStation/src/microcart_cli.c b/groundStation/src/microcart_cli.c
index db5d24e9e..7589cdb2f 100644
--- a/groundStation/src/microcart_cli.c
+++ b/groundStation/src/microcart_cli.c
@@ -110,10 +110,10 @@ static void cb(struct ucart_vrpn_TrackerData * td)
 int main(int argc, char **argv)
 {
 	// pthread_t quadResponse, cliInput;
-	fd_set rfds;
+	fd_set rfds_master;
 	int activity;
 	int max_fd = 0;
-	FD_ZERO(&rfds);
+	FD_ZERO(&rfds_master);
 
 	/* 
 	 * Create backend listening socket
@@ -147,7 +147,7 @@ int main(int argc, char **argv)
 	}
 
 	/* Add to socket set */
-	safe_fd_set(backendSocket, &rfds, &max_fd);
+	safe_fd_set(backendSocket, &rfds_master, &max_fd);
 	
 
 	/* Initialize client buffers */
@@ -185,11 +185,10 @@ int main(int argc, char **argv)
 	}
 //	writeStringToLog(logHeader);
 
-
 	// watch for input from stdin (fd 0) to see when it has input
-	safe_fd_set(fileno(stdin), &rfds, &max_fd);
+	safe_fd_set(fileno(stdin), &rfds_master, &max_fd);
 	// watch for input from the zybo socket
-	//safe_fd_set(zyboSocket, &rfds, &max_fd);
+	//safe_fd_set(zyboSocket, &rfds_master, &max_fd);
 
 	//printf("zyboSocket = %d, max_fd = %d\n", zyboSocket, max_fd);
 
@@ -203,14 +202,23 @@ int main(int argc, char **argv)
 
 	// start the prompt
 	fprintf(stdout, "$microcart> ");
+
+	struct timeval timeout = {
+		.tv_sec = 1,
+		.tv_usec = 0
+	};
 	while(keepRunning)
 	{
+		fd_set rfds;
+		rfds = rfds_master;
 		activity = select(max_fd+1, &rfds, NULL, NULL, NULL);
 		if(activity == -1) {
 			perror("select() ");
 		} else if (activity) {
+			fprintf(stderr, "Select activity = %d\n", activity);
 			for(int fd = 0; fd <= max_fd; ++fd) {
 				if (FD_ISSET(fd, &rfds)) {
+					fprintf(stderr, "Select woke for fd %d\n", fd);
 					if (fd == fileno(stdin)) {
 						unsigned char userCommand[CMD_MAX_LENGTH];
 						read(fileno(stdin), (char *)userCommand, sizeof(userCommand));
@@ -247,8 +255,10 @@ int main(int argc, char **argv)
 						if (new_fd < 0) {
 							warn("accept");
 						} else {
-							if (new_client(fd)) {
-								safe_fd_set(fd, &rfds, &max_fd);
+							fprintf(stderr, "Connection\n");
+							if (new_client(new_fd)) {
+								fprintf(stderr, "Added client\n");
+								safe_fd_set(new_fd, &rfds_master, &max_fd);
 							}
 						}
 					} else if (get_client_index(fd) > -1) {
@@ -264,6 +274,8 @@ int main(int argc, char **argv)
 						remove_client(client_fds[i]);
 				}
 			}
+			timeout.tv_sec = 1;
+			timeout.tv_usec = 0;
 		}
 	}
 
@@ -431,6 +443,9 @@ int safe_fd_clr(int fd, fd_set* fds, int* max_fd) {
 
 static ssize_t writeQuad(const char * buf, size_t count) {
 	ssize_t retval;
+	if (getenv(NOQUAD_ENV)) {
+		return count;
+	}
 	if (pthread_mutex_lock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__);
 	}
@@ -542,8 +557,9 @@ static void client_recv(int fd)
 
 		buffer[newline] = '\0';
 		unsigned char * packet;
+		fprintf(stderr, "Client sent: %s\n", buffer);
 		formatCommand((unsigned char *) buffer, &packet);
-		writeQuad((char *) packet, packet[6] << 8);
+		writeQuad((char *) packet, 4);
 		free(packet);
 		
 		char * rest = &buffer[newline] + 1;
-- 
GitLab