From d376f75f6c5ac4a2ab136675b80af947439d0a94 Mon Sep 17 00:00:00 2001
From: ucart <ucart_groundstation@iastate.edu>
Date: Mon, 3 Apr 2017 05:29:01 -0500
Subject: [PATCH] found memory issue on quad side. Creating branch to handle
 fixes.

---
 groundStation/scripts/bypass_kill_switch.sh |  5 ++
 groundStation/scripts/eric.sh               |  4 +-
 groundStation/src/backend/backend.c         | 87 ++++++++++++++++-----
 groundStation/src/backend/config.h          |  8 +-
 4 files changed, 81 insertions(+), 23 deletions(-)

diff --git a/groundStation/scripts/bypass_kill_switch.sh b/groundStation/scripts/bypass_kill_switch.sh
index b1061e271..183df7b8e 100755
--- a/groundStation/scripts/bypass_kill_switch.sh
+++ b/groundStation/scripts/bypass_kill_switch.sh
@@ -1,4 +1,9 @@
 #! /bin/bash
+
+if [ -z "$1" ]; then
+		echo "No argument supplied"
+		exit 0
+fi
 cd ..
 while true ; do
 	./setparam 37 0 $1
diff --git a/groundStation/scripts/eric.sh b/groundStation/scripts/eric.sh
index b84457723..0fe0e845e 100755
--- a/groundStation/scripts/eric.sh
+++ b/groundStation/scripts/eric.sh
@@ -18,11 +18,11 @@
 
 #outer loop
 ./setparam 'X pos PID' 'kp' -0.015
-./setparam 'X pos PID' 'ki' -0.0005
+./setparam 'X pos PID' 'ki' -0
 ./setparam 'X pos PID' 'kd' -0.25
 ./setparam 'X pos PID' 'alpha' 0.88
 
 ./setparam 'Y pos PID' 'kp' 0.015
-./setparam 'Y pos PID' 'ki' 0.0005
+./setparam 'Y pos PID' 'ki' 0
 ./setparam 'Y pos PID' 'kd' 0.25
 ./setparam 'Y pos PID' 'alpha' 0.88
diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index 9ebf7752b..d6b49449d 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -27,6 +27,8 @@
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <netinet/tcp.h>
+#include <fcntl.h>
+#include <sys/stat.h>
 
 //user created includes
 #include "commands.h"
@@ -88,6 +90,9 @@ char * create_log_name(char * buffer, size_t max);
 pthread_mutex_t quadSocketMutex;
 static ssize_t writeQuad(const uint8_t * buf, size_t count);
 static ssize_t readQuad(char * buf, size_t count);
+static int local_comm_channel;
+static int zybo_fifo_rx;
+static int zybo_fifo_tx;
 
 /* Functions for recording Latencies */
 void findTimeDiff(int respID);
@@ -120,6 +125,10 @@ pthread_mutex_t quadResponseMutex, cliInputMutex ;
 unsigned char *commandBuf;
 int newQuadResponse = 0, newCliInput = 0;
 
+static void sig_handler(int s) {
+	printf("Caught SIGPIPE from quad fifo..\n");
+}
+
 // Callback to be ran whenever the tracker receives data.
 // Currently doing much more than it should. It will be slimmed down 
 // 		in the future.
@@ -191,7 +200,13 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
-	printf("zyboSocket = %d\n", zyboSocket);
+	if (!local_comm_channel) {
+		printf("zyboSocket = %d\n", zyboSocket);
+	} else {
+		/* if we are using fifo's we don't want the quad to be able to shut us down. */
+		signal(SIGPIPE, sig_handler);
+		printf("zybo_fifo_rx = %d\nzybo_fifo_tx = %d\n", zybo_fifo_rx, zybo_fifo_tx);
+	}
 
 	if (pthread_mutex_unlock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
@@ -268,9 +283,18 @@ int main(int argc, char **argv)
 		}
 	}
 
+
 	ucart_vrpn_tracker_freeInstance(tracker);
-	safe_close_fd(zyboSocket, &quadSocketMutex);
+
+	if (!local_comm_channel) {
+		safe_close_fd(zyboSocket, &quadSocketMutex);
+	} else {
+		safe_close_fd(zybo_fifo_rx, &quadSocketMutex);
+		safe_close_fd(zybo_fifo_tx, &quadSocketMutex);
+	}
+
 	fclose(quadlog_file);
+
 	return 0;
 }
 
@@ -329,22 +353,33 @@ int connectToZybo() {
 	if (getenv(QUAD_COMM_ENV)) {
 		/* Determine if we are using bluetooth or local */
 		if(strcmp(getenv(QUAD_COMM_ENV), "local") == 0) {
-			printf("Using Local Socket Settings\n");
+			printf("Using Local Fifo Settings\n");
+			local_comm_channel = 1;
+			char * fifo_rx = QUAD_LOCAL_DEFAULT_TX;
+			char * fifo_tx = QUAD_LOCAL_DEFAULT_RX;
 			
-			struct sockaddr_un remote;
-			char str[100];
-
-			if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
-				perror("socket");
-				exit(1);
+			if (getenv(QUAD_LOCAL_RX)) {
+				fifo_tx = getenv(QUAD_LOCAL_RX);
+			}
+			if (getenv(QUAD_LOCAL_TX)) {
+				fifo_rx = getenv(QUAD_LOCAL_TX);
 			}
 
-			remote.sun_family = AF_UNIX;
-			char * sock_env = getenv(QUAD_LOCAL_SOCKET);
-			strcpy(remote.sun_path, sock_env ? sock_env : QUAD_DEFAULT_LOCAL_SOCKET);
-			printf("Attempting to connect to local socket at '%s'. please be patiend.\n", remote.sun_path);
-
-			status = connect(sock, (struct sockaddr *)&remote, sizeof(remote));
+			zybo_fifo_tx = open(fifo_tx, O_WRONLY | O_NONBLOCK);
+			if (zybo_fifo_tx < 0) {
+				warnx("Opening zybo_fifo_tx...");
+				return -1;
+			}
+			/* Must use O_RDWR so that there is at least one writer on the fifo
+				and each subsequent call to select() won't return EOF
+			*/
+ 			zybo_fifo_rx = open(fifo_rx, O_RDWR | O_NONBLOCK);
+			if (zybo_fifo_rx < 0) {
+				warnx("Opening zybo_fifo_rx...");
+				return -1;
+			}
+			status = 0;
+			sock = zybo_fifo_rx;
 		} else if (strcmp(getenv(QUAD_COMM_ENV), "bluetooth") == 0) {
 			printf("Using BT Settings\n");
 			struct sockaddr_rc addr;
@@ -450,7 +485,13 @@ static ssize_t writeQuad(const uint8_t * buf, size_t count) {
 	if (pthread_mutex_lock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__);
 	}
-	retval = write(zyboSocket, buf, count);
+
+	if (local_comm_channel) {
+		retval = write(zybo_fifo_tx, buf, count);
+	} else {	
+		retval = write(zyboSocket, buf, count);
+	}	
+
 	if (pthread_mutex_unlock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
 	}
@@ -466,7 +507,13 @@ static ssize_t readQuad(char * buf, size_t count) {
 	if (pthread_mutex_lock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__);
 	}
-	retval = read(zyboSocket, buf, count);
+
+	if (local_comm_channel) {
+		retval = read(zybo_fifo_rx, buf, count);
+	} else {
+		retval = read(zyboSocket, buf, count);	
+	}
+	
 	if (pthread_mutex_unlock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
 	}
@@ -722,6 +769,7 @@ static void client_recv(int fd) {
 			
 			int retval = writeQuad(packet, psize);
 			// printf("sent %d bytes\n", retval);
+
 			free(data);
 		}
 
@@ -758,7 +806,8 @@ static void quad_recv() {
 	// }
 	// printf("'\n");
 
-	while(respBufLen){
+
+	while(respBufLen) {
 		datalen = DecodePacket(&m, data, CMD_MAX_LENGTH, respBuf, respBufLen);
 		if (datalen == -1) {
 			warnx("No start Byte");
@@ -836,7 +885,7 @@ static void quad_recv() {
 				printf("(Backend): message type %d ignored from quad\n", m.msg_type);
 				break;
 		}
-	}	
+	}
 }
 
 static void handleResponse(struct metadata *m, uint8_t * data)
diff --git a/groundStation/src/backend/config.h b/groundStation/src/backend/config.h
index 1e8ad0d20..4236b1328 100644
--- a/groundStation/src/backend/config.h
+++ b/groundStation/src/backend/config.h
@@ -13,8 +13,12 @@
 // 	backend with sudo elevation and with the --preserve-env flag or -E
 #define QUAD_COMM_ENV "UCART_COMM_CHANNEL"
 
-#define QUAD_DEFAULT_LOCAL_SOCKET "./virtquad.socket"
-#define QUAD_LOCAL_SOCKET "VIRT_QUAD_SOCKET"
+#define QUAD_LOCAL_RX "UCART_LOCAL_RX"
+#define QUAD_LOCAL_TX "UCART_LOCAL_TX"
+#define QUAD_LOCAL_DEFAULT_RX "../quad/bin/virt-quad-fifos/uart-rx"
+#define QUAD_LOCAL_DEFAULT_TX "../quad/bin/virt-quad-fifos/uart-tx"
+
+
 #define QUAD_IP_ENV "UCART_QUAD_IP"
 #define QUAD_IP_DEFAULT "192.168.1.1"
 #define QUAD_PORT_ENV "UCART_QUAD_PORT"
-- 
GitLab