From df6006c10b69074f09b4417511d0125c252ac9f8 Mon Sep 17 00:00:00 2001
From: Kris Burney <burneykb@iastate.edu>
Date: Mon, 7 Nov 2016 03:17:57 -0600
Subject: [PATCH] start of socket creation and command parsing of cli

---
 groundStation/.gitignore            |  1 +
 groundStation/Makefile              |  8 +--
 groundStation/src/backend/backend.c |  2 +-
 groundStation/src/backend/config.h  |  4 +-
 groundStation/src/cli/cli.c         | 91 +++++++++++++++++++++++++++++
 groundStation/src/cli/cli.h         | 15 +++++
 groundStation/src/cli/monitorQuad.c |  4 --
 groundStation/src/cli/testClient.c  | 55 +++++++++++++++++
 8 files changed, 168 insertions(+), 12 deletions(-)
 create mode 100644 groundStation/src/cli/cli.c
 create mode 100644 groundStation/src/cli/cli.h
 delete mode 100644 groundStation/src/cli/monitorQuad.c
 create mode 100644 groundStation/src/cli/testClient.c

diff --git a/groundStation/.gitignore b/groundStation/.gitignore
index 243744841..a22e45034 100644
--- a/groundStation/.gitignore
+++ b/groundStation/.gitignore
@@ -42,4 +42,5 @@ logs
 client
 BackEnd
 obj
+cli
 monitorQuad
diff --git a/groundStation/Makefile b/groundStation/Makefile
index 1f4d4be95..372c220dc 100644
--- a/groundStation/Makefile
+++ b/groundStation/Makefile
@@ -32,13 +32,11 @@ all: logs objdir backend cli
 
 vrpn: vrpn/build
 
-cli: $(CLIOBJECTS) $(CLIBINARIES)
+cli:  $(CLIBINARIES)
 
-$(CLIOBJECTS) : $(OBJDIR)/%.o : $(CLISRCDIR)/%.c
-	$(GCC)  $(CFLAGS) -c $^ -o $@ $(INCLUDES) $(LIBS)
+$(CLIBINARIES): % : $(CLISRCDIR)/%.c
+	$(GCC)  $(CFLAGS) $< -o $@ $(INCLUDES) $(LIBS)
 
-$(CLIBINARIES): % : $(OBJDIR)/%.o
-	$(GCC)  $(CFLAGS) $^ -o $@ $(INCLUDES) $(LIBS)
 
 backend: $(BECPPOBJECTS) $(BECOBJECTS)
 	$(GXX) $(CXXFLAGS) $^ -o $(BEBINARY) $(INCLUDES) $(LIBS)
diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index 8d1bf6b8e..5815c758d 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -106,7 +106,7 @@ static void cb(struct ucart_vrpn_TrackerData * td)
 	static int count = 0;
 
 	if(!(count % 10)) {
-		sendVrpnPacket(td);
+		//sendVrpnPacket(td);
 		//updateLogFile(td);
 	}
 	count++;
diff --git a/groundStation/src/backend/config.h b/groundStation/src/backend/config.h
index 74b0bc671..db6d68ca7 100644
--- a/groundStation/src/backend/config.h
+++ b/groundStation/src/backend/config.h
@@ -1,5 +1,5 @@
-#ifndef __BACKEND_H
-#define __BACKEND_H
+#ifndef __CONFIG_H
+#define __CONFIG_H
 
 
 #define DEFAULT_SOCKET "/var/run/ucart.socket"
diff --git a/groundStation/src/cli/cli.c b/groundStation/src/cli/cli.c
new file mode 100644
index 000000000..39a12ebf4
--- /dev/null
+++ b/groundStation/src/cli/cli.c
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <err.h>
+
+
+#include "cli.h"
+
+
+int parseInput(int argc, char **argv);
+int connectToBackEnd();
+
+int main(int argc, char **argv)
+{
+	int cmdID = -1;
+	if((cmdID = parseInput(argc, argv)) == -1) {
+		exit(1);
+	}
+	int s;
+	if((s = connectToBackEnd()) == -1) {
+		err(-1, "connectToBackEnd");
+	}
+
+
+
+    close(s);
+
+    return 0;
+}
+
+int connectToBackEnd() {
+
+    int s, t, len;
+    struct sockaddr_un remote;
+    char str[100];
+
+    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+        perror("socket");
+        exit(1);
+    }
+
+    printf("Trying to connect...\n");
+
+    remote.sun_family = AF_UNIX;
+    strcpy(remote.sun_path, SOCK_PATH);
+    len = strlen(remote.sun_path) + sizeof(remote.sun_family);
+    if (connect(s, (struct sockaddr *)&remote, sizeof(remote))  == -1) {
+        perror("connect");
+        exit(1);
+    } else {
+		printf("Connected.\n");
+    	return s;
+    }
+}
+
+int parseInput(int argc, char **argv) {
+	static int cmdID = -1;
+	int c;
+	static struct option long_options[] =
+	{
+		/* These options don’t set a flag. We distinguish them by their indices. */
+		{"monitor",     no_argument,       &cmdID, CMD_MONITOR},
+		{0, 0, 0, 0}
+	};
+
+	while (1)
+    {
+      
+      /* getopt_long stores the option index here. */
+      int option_index = 0;
+
+      c = getopt_long (argc, argv, "",
+                       long_options, &option_index);
+
+      /* Detect the end of the options. */
+      if (c == -1)
+        break;
+    }
+    if(cmdID == -1)
+    {
+    	printf("Invalid command\n");
+    	return cmdID;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git a/groundStation/src/cli/cli.h b/groundStation/src/cli/cli.h
new file mode 100644
index 000000000..2e6654be1
--- /dev/null
+++ b/groundStation/src/cli/cli.h
@@ -0,0 +1,15 @@
+#ifndef __CLI_H
+#define __CLI_H
+
+#define SOCK_PATH "/var/run/ucart.socket"
+
+enum CommandNameIds{
+	CMD_MONITOR,
+	MAX_COMMANDS
+};
+
+char* commandNames[MAX_COMMANDS] = {
+	"monitor",
+};
+
+#endif
\ No newline at end of file
diff --git a/groundStation/src/cli/monitorQuad.c b/groundStation/src/cli/monitorQuad.c
deleted file mode 100644
index 09540d023..000000000
--- a/groundStation/src/cli/monitorQuad.c
+++ /dev/null
@@ -1,4 +0,0 @@
-int main(int argc, char **argv)
-{
-	return 0;
-}
\ No newline at end of file
diff --git a/groundStation/src/cli/testClient.c b/groundStation/src/cli/testClient.c
new file mode 100644
index 000000000..c6db27cd2
--- /dev/null
+++ b/groundStation/src/cli/testClient.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#define SOCK_PATH "/var/run/ucart.socket"
+
+int main(void)
+{
+    int s, t, len;
+    struct sockaddr_un remote;
+    char str[100];
+
+    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+        perror("socket");
+        exit(1);
+    }
+
+    printf("Trying to connect...\n");
+
+    remote.sun_family = AF_UNIX;
+    strcpy(remote.sun_path, SOCK_PATH);
+    len = strlen(remote.sun_path) + sizeof(remote.sun_family);
+    if (connect(s, (struct sockaddr *)&remote, sizeof(remote))  == -1) {
+        perror("connect");
+        exit(1);
+    }
+
+    printf("Connected.\n");
+
+    while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
+        if (send(s, str, strlen(str), 0) == -1) {
+            perror("send");
+            exit(1);
+        }
+				/*
+        if ((t=recv(s, str, 100, 0)) > 0) {
+            str[t] = '\0';
+            printf("echo> %s", str);
+        } else {
+            if (t < 0) perror("recv");
+            else printf("Server closed connection\n");
+            exit(1);
+        }
+*/
+    }
+
+    close(s);
+
+    return 0;
+}
-- 
GitLab