From 51f9fdc290dffa0a9572ac20277c7aacf12eadad Mon Sep 17 00:00:00 2001
From: Kris Burney <burneykb@iastate.edu>
Date: Mon, 14 Nov 2016 16:01:20 -0600
Subject: [PATCH] Fixed seg fault with getpid

---
 groundStation/.gitignore                     |  2 +-
 groundStation/Makefile                       |  6 +++---
 groundStation/src/backend/backend.c          | 11 +++++++++--
 groundStation/src/cli/cli.c                  |  7 ++++---
 groundStation/src/cli/cli_getpid.c           |  2 +-
 groundStation/src/frontend/frontend_common.c |  2 ++
 groundStation/src/frontend/frontend_getpid.c | 20 ++++++++++++++++++++
 7 files changed, 40 insertions(+), 10 deletions(-)
 create mode 100644 groundStation/src/frontend/frontend_getpid.c

diff --git a/groundStation/.gitignore b/groundStation/.gitignore
index eedef6d30..508a8f243 100644
--- a/groundStation/.gitignore
+++ b/groundStation/.gitignore
@@ -41,4 +41,4 @@ src/vrpn/pc_linux64/*
 logs
 BackEnd
 obj
-CLI
+Cli
diff --git a/groundStation/Makefile b/groundStation/Makefile
index 5f285d6b4..9123a2455 100644
--- a/groundStation/Makefile
+++ b/groundStation/Makefile
@@ -19,7 +19,7 @@ BECPPSOURCES := $(wildcard $(BESRCDIR)/*.cpp )
 BECPPOBJECTS = $(BECPPSOURCES:$(BESRCDIR)/%.cpp=$(OBJDIR)/%.o)
 
 # CLI Specific Variables
-CLIBINARY=CLI
+CLIBINARY=Cli
 CLISRCDIR=src/cli
 CLISOURCES := $(wildcard $(CLISRCDIR)/*.c)
 CLIOBJECTS = $(CLISOURCES:$(CLISRCDIR)/%.c=$(OBJDIR)/%.o)
@@ -38,7 +38,7 @@ all: logs objdir backend cli
 vrpn: vrpn/build
 
 cli:  $(CLIOBJECTS) $(FECOBJECTS)
-	$(GCC) $(CFLAGS) $^ -o $@ $(INCLUDES) $(LIBS)
+	$(GCC) $(CFLAGS) $^ -o $(CLIBINARY) $(INCLUDES) $(LIBS)
 
 $(CLIOBJECTS) : $(OBJDIR)/%.o : $(CLISRCDIR)/%.c
 	$(GCC)  $(CFLAGS) -c $^ -o $@ $(INCLUDES) $(LIBS)
@@ -70,7 +70,7 @@ clean_logs:
 	rm -f logs/*
 
 clean:
-	rm -rf $(OBJDIR)/ $(BEBINARY) $(CLIBINARIES)
+	rm -rf $(OBJDIR)/ $(BEBINARY) $(CLIBINARY)
 
 debug:
 	@echo $(OBJECTS)
diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index 5815c758d..316491367 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -187,9 +187,12 @@ int main(int argc, char **argv)
 
 	// watch for input from stdin (fd 0) to see when it has input
 	safe_fd_set(fileno(stdin), &rfds_master, &max_fd);
-	// watch for input from the zybo socket
-	safe_fd_set(zyboSocket, &rfds_master, &max_fd);
 
+	if (!getenv(NOQUAD_ENV)) {
+		// watch for input from the zybo socket
+		safe_fd_set(zyboSocket, &rfds_master, &max_fd);
+
+	}
 	// Tell the quad we are ready to send it vrpn data
 	sendStartPacket();
 
@@ -383,6 +386,10 @@ int connectToZybo() {
 	int sock;
 	int status = 0;
 
+	if (getenv(NOQUAD_ENV)) {
+		return 0;
+	}
+
 	/* Use bluetooth by default */
 	if (!getenv(QUAD_WIFI_ENV)) {
 		printf("Using BT Settings\n");
diff --git a/groundStation/src/cli/cli.c b/groundStation/src/cli/cli.c
index b8ad2f738..17d9f8c8a 100644
--- a/groundStation/src/cli/cli.c
+++ b/groundStation/src/cli/cli.c
@@ -4,8 +4,6 @@
 
 #include "cli.h"
 
-int connectToBackEnd();
-
 int main(int argc, char **argv)
 {
 	int cmdID = -1;
@@ -39,11 +37,14 @@ int main(int argc, char **argv)
 
 	printf("Parsed Command : %s\n", commandNames[cmdID]);
 	conn = ucart_backendConnect();
+	if(conn == NULL) {
+		return -1;
+	}
 
 	if(useSymlink) {
 		//TODO Call correct command function pointer with (argv[1] ... argc[argc])
 	}else {
-		(*cli_functions[0]) (conn, argc - 1, &argv[2]);
+		(*cli_functions[0]) (conn, argc-1, &argv[1]);
 	}
 
 	ucart_backendDisconnect(conn);
diff --git a/groundStation/src/cli/cli_getpid.c b/groundStation/src/cli/cli_getpid.c
index fb1c627c8..92e0fbf3b 100644
--- a/groundStation/src/cli/cli_getpid.c
+++ b/groundStation/src/cli/cli_getpid.c
@@ -31,7 +31,7 @@ int cli_getpid(struct backend_conn * conn,	int argc, char **argv) {
 			getAll = 1;
 		}
 	}
-	
+
 	int result;
 	if(getAll) {
 		pid_data.pid = ROLL;
diff --git a/groundStation/src/frontend/frontend_common.c b/groundStation/src/frontend/frontend_common.c
index 7b9f29dfd..81e0c2eef 100644
--- a/groundStation/src/frontend/frontend_common.c
+++ b/groundStation/src/frontend/frontend_common.c
@@ -43,12 +43,14 @@ struct backend_conn * ucart_backendConnect()
 		goto fail_final;
 	}
 
+
 	conn = malloc(sizeof(struct backend_conn));
 	if (conn == NULL) {
 		perror("malloc");
 		goto fail_sock;
 	}
 
+
 	conn->len = 0;
 	conn->buf = NULL;
 	conn->socket = fdopen(s, "rw");
diff --git a/groundStation/src/frontend/frontend_getpid.c b/groundStation/src/frontend/frontend_getpid.c
new file mode 100644
index 000000000..8143f9400
--- /dev/null
+++ b/groundStation/src/frontend/frontend_getpid.c
@@ -0,0 +1,20 @@
+#include "frontend_getpid.h"
+
+/* Get a specified PID.
+ *
+ * Example:
+ *
+ * struct frontend_pid_data pid_data;
+ * pid_data.pid = PITCH;
+ * if (frontend_getpid(conn, &pid_data)) {
+ * 		error
+ * } else {
+ * 		pid_data.p, pid_data.i, and pid_data.d are filled
+ * }
+ *
+ * Returns 0 on success, 1 on error
+ */
+int frontend_getpid(
+		struct backend_conn * conn, struct frontend_pid_data * pid_data) {
+	return 0;
+}
\ No newline at end of file
-- 
GitLab