From 0c731a4093168094cc28d161907fd73c27ba334b Mon Sep 17 00:00:00 2001
From: Jake Drahos <j@kedrahos.com>
Date: Thu, 27 Oct 2016 11:18:58 -0500
Subject: [PATCH] Began backend socket

---
 groundStation/src/config.h        |  9 ++++++
 groundStation/src/microcart_cli.c | 49 +++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 3 deletions(-)
 create mode 100644 groundStation/src/config.h

diff --git a/groundStation/src/config.h b/groundStation/src/config.h
new file mode 100644
index 00000000..c5a2582e
--- /dev/null
+++ b/groundStation/src/config.h
@@ -0,0 +1,9 @@
+#ifndef __BACKEND_H
+#define __BACKEND_H
+
+
+#define DEFAULT_SOCKET "/var/run/ucart.socket"
+#define SOCKET_ENV "UCART_SOCKET"
+
+
+#endif
diff --git a/groundStation/src/microcart_cli.c b/groundStation/src/microcart_cli.c
index bf2fbc95..4f9ca016 100644
--- a/groundStation/src/microcart_cli.c
+++ b/groundStation/src/microcart_cli.c
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <signal.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 #include <sys/select.h>
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/rfcomm.h>
@@ -21,6 +22,7 @@
 #include "vrpn_tracker.hpp"
 #include "type_def.h"
 #include "logger.h"
+#include "config.h"
 
 #define QUAD_BT_ADDR  "00:06:66:64:61:D6"
 #define QUAD_BT_CHANNEL  0x01
@@ -52,6 +54,7 @@ static ssize_t writeQuad(const char * buf, size_t count);
 static volatile int keepRunning = 1;
 const char *TRACKER_IP = "UAV@192.168.0.120:3883";
 static int zyboSocket;
+static int backendSocket;
 struct ucart_vrpn_tracker * tracker = NULL;
 const char *logHeader = "";//"#\n#\tDefault log header\n#\tEverything after '#'`s will be printed as is in the processed logs.\n#\n\0";
 
@@ -92,6 +95,41 @@ int main(int argc, char **argv)
 	fd_set rfds;
 	int activity;
 	int max_fd = 0;
+	FD_ZERO(&rfds);
+
+	/* 
+	 * Create backend listening socket
+	 */
+	/* Determine socket path */
+	char * backend_socket_path = DEFAULT_SOCKET;
+	if (getenv(SOCKET_ENV)) {
+		backend_socket_path = getenv(SOCKET_ENV);
+	}
+	/* Unlink if it exists */
+	unlink(backend_socket_path);
+
+	/* Create socket */
+	backendSocket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+	if (backendSocket < 0) {
+		err(-1, "socket");
+	}
+
+	/* Create sockaddr and bind */
+	struct sockaddr_un sa;
+	sa.sun_family = AF_UNIX;
+	strncpy(sa.sun_path, backend_socket_path, 107);
+	sa.sun_path[107] = '\0';
+	if (bind(backendSocket, (struct sockaddr *) &sa, sizeof(sa))) {
+		err(-1, "bind");
+	}
+
+	/* Listen */
+	if (listen(backendSocket, 16)) {
+		err(-1, "listen");
+	}
+
+	/* Add to socket set */
+	safe_fd_set(backendSocket, &rfds, &max_fd);
 	
 	signal(SIGINT, killHandler);
 
@@ -123,8 +161,6 @@ int main(int argc, char **argv)
 //	writeStringToLog(logHeader);
 
 
-	// clear the fd set
-	FD_ZERO(&rfds);
 	// watch for input from stdin (fd 0) to see when it has input
 	safe_fd_set(fileno(stdin), &rfds, &max_fd);
 	// watch for input from the zybo socket
@@ -148,7 +184,7 @@ int main(int argc, char **argv)
 		if(activity == -1) {
 			perror("select() ");
 		} else if (activity) {
-			for(unsigned int fd = 0; fd <= max_fd; ++fd) {
+			for(int fd = 0; fd <= max_fd; ++fd) {
 				if (FD_ISSET(fd, &rfds)) {
 					if (fd == fileno(stdin)) {
 						unsigned char userCommand[CMD_MAX_LENGTH] = {};
@@ -180,6 +216,13 @@ int main(int argc, char **argv)
 						memset(userCommand, 0, cmdLen);
 					} else if (fd == zyboSocket) {
 						
+					} else if (fd == backendSocket) {
+						int new_fd = 0;
+						new_fd = accept(backendSocket, NULL, NULL);
+						if (new_fd < 0) {
+							warn("accept");
+						} else {
+						}
 					}
 				}
 			}
-- 
GitLab