From 691deb57b775f7fbf6e596b0ff90b50f28ee9d49 Mon Sep 17 00:00:00 2001
From: Jake Drahos <j@kedrahos.com>
Date: Sun, 30 Oct 2016 20:15:54 -0500
Subject: [PATCH] Added support for using WiFi

---
 groundStation/Makefile            |  2 +-
 groundStation/src/config.h        |  5 +++
 groundStation/src/microcart_cli.c | 64 +++++++++++++++++++++++++------
 3 files changed, 58 insertions(+), 13 deletions(-)

diff --git a/groundStation/Makefile b/groundStation/Makefile
index d62850d9b..91b61ceb4 100644
--- a/groundStation/Makefile
+++ b/groundStation/Makefile
@@ -2,7 +2,7 @@
 GCC=gcc
 GXX=g++
 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
+CXXFLAGS= -Wall -Wno-reorder -std=c++0x -g
 INCLUDES = $(foreach dir, $(INCDIR), -I$(dir))
 
 # Directories
diff --git a/groundStation/src/config.h b/groundStation/src/config.h
index 0e2077cca..916058810 100644
--- a/groundStation/src/config.h
+++ b/groundStation/src/config.h
@@ -6,5 +6,10 @@
 #define SOCKET_ENV "UCART_SOCKET"
 #define NOQUAD_ENV "UCART_NO_QUAD"
 
+#define QUAD_WIFI_ENV "UCART_USE_WIFI"
+#define QUAD_IP_ENV "UCART_QUAD_IP"
+#define QUAD_IP_DEFAULT "192.168.4.1"
+#define QUAD_PORT_ENV "UCART_QUAD_PORT"
+#define QUAD_PORT_DEFAULT 8080
 
 #endif
diff --git a/groundStation/src/microcart_cli.c b/groundStation/src/microcart_cli.c
index a439a897a..9ee3712b8 100644
--- a/groundStation/src/microcart_cli.c
+++ b/groundStation/src/microcart_cli.c
@@ -4,6 +4,7 @@
  */
 
 #define _GNU_SOURCE
+#define _BSD_SOURCE
 
 //system includes
 #include <err.h>
@@ -13,6 +14,8 @@
 #include <signal.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #include <sys/select.h>
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/rfcomm.h>
@@ -366,25 +369,62 @@ void printVrpnData(struct ucart_vrpn_TrackerData * td) {
 
 int connectToZybo() {
 	int sock;
-	struct sockaddr_rc addr;
+	int status;
 
-	// allocate a socket	
-	sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
+	/* Use bluetooth by default */
+	if (getenv(QUAD_WIFI_ENV) == NULL) {
+		struct sockaddr_rc addr;
 
-	//set the connection params ie. who to connect to	
-	addr.rc_family = AF_BLUETOOTH;
-	addr.rc_channel = (uint8_t) QUAD_BT_CHANNEL;
-	str2ba( QUAD_BT_ADDR, &addr.rc_bdaddr );
-	
-	printf("Attempting to connect to zybo. Please be patient...\n");
-	// blocking call to connect to socket sock ie. zybo board
-	int status = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
+		// allocate a socket	
+		sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
+
+		//set the connection params ie. who to connect to	
+		addr.rc_family = AF_BLUETOOTH;
+		addr.rc_channel = (uint8_t) QUAD_BT_CHANNEL;
+		str2ba( QUAD_BT_ADDR, &addr.rc_bdaddr );
+		
+		printf("Attempting to connect to zybo. Please be patient...\n");
+		// blocking call to connect to socket sock ie. zybo board
+		status = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
+	} else {
+		struct sockaddr_in addr;
+
+		addr.sin_family = AF_INET;
+
+		/* Quick and Dirty */
+		if (getenv(QUAD_IP_ENV)) {
+			 if (!inet_aton(getenv(QUAD_IP_ENV), &addr.sin_addr)) {
+				fprintf(stderr, "Env var %s invalid IP %s\n",
+					     	QUAD_IP_ENV, getenv(QUAD_IP_ENV));
+				return -1;
+			 }
+		} else {
+			if (!inet_aton(QUAD_IP_DEFAULT, &addr.sin_addr)) {
+				fprintf(stderr, "Default IP %s is invalid\n",
+					     	QUAD_IP_DEFAULT);
+				return -1;
+			}
+		}
+
+		if (getenv(QUAD_PORT_ENV)) {
+			/* Quick 'n dirty, oh yeah! */
+			addr.sin_family = atoi(getenv(QUAD_PORT_ENV));		
+		} else {
+			addr.sin_family = QUAD_PORT_DEFAULT;
+		}
+
+		sock = socket(AF_INET, SOCK_STREAM, 0);
+		if (sock < 0) {
+			perror("socket");
+			return -1;
+		}
+	}
 
 	// connection failed
 	if(status < 0)
 	{
 		close(sock);
-		printf("Connection failed!...\n");
+		perror("connect");
 		return -1;
 	} 
 	else
-- 
GitLab