From 6384c4dc975bc04b03a2efc8add5b0befb204349 Mon Sep 17 00:00:00 2001
From: Kris Burney <burneykb@iastate.edu>
Date: Thu, 17 Nov 2016 13:30:30 -0600
Subject: [PATCH] creating monitoring code

---
 groundStation/src/backend/backend.c          |  4 +-
 groundStation/src/cli/cli.h                  |  7 +-
 groundStation/src/cli/cli_monitor.c          | 82 ++++++++++++++++++++
 groundStation/src/cli/cli_monitor.h          | 14 +++-
 groundStation/src/frontend/frontend_getpid.c |  1 -
 5 files changed, 98 insertions(+), 10 deletions(-)
 create mode 100644 groundStation/src/cli/cli_monitor.c

diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index 396c84e52..5e43f9188 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -592,13 +592,13 @@ static void client_recv(int fd) {
 			printf("Could not recognize command '%s'\n", buffer);
 		} else {
 			int datalen = (packet[6] << 8) | (packet[5]);
-			writeQuad((char *) packet, datalen +7);
+			writeQuad((char *) packet, datalen +8);
 		}
 		
 		char * rest = &buffer[newline] + 1;
 		size_t restLen = (strlen(rest) == 0) ? 1 : strlen(rest);
 		/* Delete parsed data and move the rest to the left */
-		memmove(buffer, rest, restLen + 1);
+		memmove(buffer, rest, restLen +1);
 	}
 }
 
diff --git a/groundStation/src/cli/cli.h b/groundStation/src/cli/cli.h
index fc72ae12b..455e5e600 100644
--- a/groundStation/src/cli/cli.h
+++ b/groundStation/src/cli/cli.h
@@ -1,8 +1,7 @@
 #ifndef __CLI_H
 #define __CLI_H
 
-#include "frontend_getpid.h"
-// #include "cli_monitor.h"
+#include "cli_monitor.h"
 // #include "cli_setpid.h"
 #include "cli_getpid.h"
 // #include "cli_getimu.h"
@@ -15,8 +14,8 @@ enum CommandNameIds{
 };
 
 typedef int (*cli_function_ptr)(struct backend_conn *, int, char **);
-static cli_function_ptr cli_functions[1] = {
-	// &cli_monitor,
+static cli_function_ptr cli_functions[2] = {
+	&cli_monitor,
 	// &cli_setpid,
 	&cli_getpid
 	// &cli_getimu
diff --git a/groundStation/src/cli/cli_monitor.c b/groundStation/src/cli/cli_monitor.c
new file mode 100644
index 000000000..0d433c7ba
--- /dev/null
+++ b/groundStation/src/cli/cli_monitor.c
@@ -0,0 +1,82 @@
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "cli_monitor.h"
+
+int cli_monitor(struct backend_conn * conn,	int argc, char **argv) {
+	int c, result;
+	int timeFlag = 0;
+
+	static struct timespec startTime;
+	static struct timespec elapsedTime;
+	static struct timespec lastChecked;
+	static int nsecs, rate = 10;
+
+
+ 	while ((c = getopt(argc, argv, "t:r:")) != -1) {
+		switch(c) {
+			case 't' :
+				nsecs = atoi(optarg);
+				timeFlag = 1;
+				break;
+			case 'r' :
+				rate = atoi(optarg) + 1;
+				break;
+			default :
+				break;
+		}
+	}
+
+	if(timeFlag) {
+		clock_gettime(CLOCK_REALTIME, &startTime);
+		clock_gettime(CLOCK_REALTIME, &lastChecked);
+		while((result = monitor(conn)) == 0) {
+			timespec_diff(&startTime, &elapsedTime);
+			if(elapsedTime.tv_sec > nsecs) {
+				break;
+			}
+			while(1) {
+				timespec_diff(&lastChecked, &elapsedTime);
+				if(elapsedTime.tv_nsec >= (long) ((SECOND_IN_NANO * 2) / rate)) {
+					clock_gettime(CLOCK_REALTIME, &lastChecked);
+					break;
+				}
+			}
+		}
+	} else {
+		return monitor(conn);
+	}
+
+	return result;
+}
+
+int monitor(struct backend_conn * conn) {
+	static char * line = "monitor\n";
+	printf("monitoring\n");
+	int size;
+	if((size = ucart_backendWrite(conn, line)) < 0 ) {
+		return 1;
+	}
+
+	return 0;
+}
+
+void timespec_diff(struct timespec *start, struct timespec *result)
+{
+	struct timespec stop;
+	clock_gettime(CLOCK_REALTIME, &stop);
+    if ((stop.tv_nsec - start->tv_nsec) < 0) {
+        result->tv_sec = stop.tv_sec - start->tv_sec - 1;
+        result->tv_nsec = stop.tv_nsec - start->tv_nsec + 1000000000;
+    } else {
+        result->tv_sec = stop.tv_sec - start->tv_sec;
+        result->tv_nsec = stop.tv_nsec - start->tv_nsec;
+    }
+
+    return;
+}
\ No newline at end of file
diff --git a/groundStation/src/cli/cli_monitor.h b/groundStation/src/cli/cli_monitor.h
index 2509cc256..fc3cbef43 100644
--- a/groundStation/src/cli/cli_monitor.h
+++ b/groundStation/src/cli/cli_monitor.h
@@ -1,9 +1,17 @@
 #ifndef CLI_MONITOR_H
 #define CLI_MONITOR_H
+
+#include <time.h>
+
 #include "frontend_getpid.h"
 
-int cli_getpid(
-		struct backend_conn * conn,
-		struct frontend_pid_data * pid_data);
+#define SECOND_IN_NANO 1000000000
+
+int cli_monitor(struct backend_conn * conn,	int argc, char **argv);
+
+// Return 0 on success and 1 otherwise
+int monitor(struct backend_conn * conn);
+
 
+void timespec_diff(struct timespec *start,  struct timespec *result);
 #endif
\ No newline at end of file
diff --git a/groundStation/src/frontend/frontend_getpid.c b/groundStation/src/frontend/frontend_getpid.c
index 0aa064076..0b25ec270 100644
--- a/groundStation/src/frontend/frontend_getpid.c
+++ b/groundStation/src/frontend/frontend_getpid.c
@@ -38,7 +38,6 @@ int frontend_getpid(
 	}
 
 	int size;
-	printf("sending '%s'\n",line);
 	if((size = ucart_backendWrite(conn, line)) < 0 ) {
 		return 1;
 	}
-- 
GitLab