From acdf1cd7ba386919b2125e39c8bd4938217ff0d6 Mon Sep 17 00:00:00 2001
From: Dane Larson <dmlarson@iastate.edu>
Date: Sun, 1 Oct 2017 18:57:42 -0500
Subject: [PATCH] pre merge documentation update

---
 groundStation/README.md             | 20 +++++------
 groundStation/src/backend/backend.c | 55 +++++++++++++++--------------
 groundStation/src/backend/param.c   |  8 ++---
 groundStation/src/backend/source.c  |  4 +--
 4 files changed, 45 insertions(+), 42 deletions(-)

diff --git a/groundStation/README.md b/groundStation/README.md
index f9902a1c4..422ff8f50 100644
--- a/groundStation/README.md
+++ b/groundStation/README.md
@@ -2,13 +2,13 @@
 
 ## Make Process
 
-First, if submodules were not recursevly added through git. Run this command if you have made any attempt to make vrpn manually. 
+First, if submodules were not recursively added through git. Run this command if you have made any attempt to make vrpn manually.
 run
 ```bash
     git submodule update --init --recursive
 ```
 
-Now that you have all of the files necissary.
+Now that you have all of the files necessary.
 
 cd into the groundstation folder.
 ```bash
@@ -17,17 +17,17 @@ cd into the groundstation folder.
 	make
 ```
 
-run the program with privledges
+run the program with privileges
 ```bash
 	./BackEnd
 ```
 
 ## Changing Defaults
-If you wish to change the way the backend communicates to the quad and vice versa, look at `src/config.h`. 
+If you wish to change the way the backend communicates to the quad and vice versa, look at `src/config.h`.
 This provides a list of environment variables which you can set and use for your computer or time of use.
-	
+
 ## Using
-First, the backend daemon must be running. Run the backend with 
+First, the backend daemon must be running. Run the backend with
 ```bash
     ./BackEnd
 ```
@@ -44,7 +44,7 @@ running the `getoutput` program will effectively run `cli getoutput`.
 
 The names of the binaries is subject to change.
 
-For a list of cli commands and more in depth usage explainations, use the `--help` flag.
+For a list of cli commands and more in depth usage explanations, use the `--help` flag.
 ```bash
     ./Cli --help
 ```
@@ -77,8 +77,8 @@ or alternatively with symlinks
 
 This will fetch the `block_id`, `type_id` and name of every node in the current graph
 
-You can run any number of any combination of frontend tools at the same time. 
-If you find it helpful, with this setup you can create a bash script that simply 
+You can run any number of any combination of frontend tools at the same time.
+If you find it helpful, with this setup you can create a bash script that simply
 runs the same commands. This has been found to be of use when we are tuning.
 
-There are a couple of already written bash scripts in the `scripts/` folder.
\ No newline at end of file
+There are a couple of already written bash scripts in the `scripts/` folder.
diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index 2f49b313c..43e23cb4d 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -1,6 +1,9 @@
 /* Author: Kris Burney & Jake Drahos
  *
- * BlueTooth socket program for passing vrpn data to quad.
+ * Interface between Quad, front end, Virtual and VRPN tracker.
+ * Main uses a standard socket select loop to receive data from any source described above.
+ * Based on the data received from sockets it calls receive functions based on the socket received from.
+ * within the receive functions data is parsed using various
  */
 
 #define _GNU_SOURCE
@@ -67,7 +70,7 @@ static int new_client(int fd);
 static ssize_t get_client_index(int fd);
 /* Returns pointer to client buffer, or -1 */
 static char * get_client_buffer(int fd);
-/* Return pointer to client pending responses, or -1*/ 
+/* Return pointer to client pending responses, or -1*/
 static int * get_client_pend_responses(int fd);
 /* Return positive integer if successful, -1 otherwise */
 static int clientAddPendResponses(int fd, uint16_t packet_id);
@@ -131,7 +134,7 @@ static void sig_handler(int s) {
 }
 
 // Callback to be ran whenever the tracker receives data.
-// Currently doing much more than it should. It will be slimmed down 
+// Currently doing much more than it should. It will be slimmed down
 // 		in the future.
 static void cb(struct ucart_vrpn_TrackerData * td) {
 	static int count = 0;
@@ -143,7 +146,7 @@ int main(int argc, char **argv)
 {
 	int activity;
 	FD_ZERO(&rfds_master);
-	/* 
+	/*
 	 * Create backend listening socket
 	 */
 	/* Determine socket path */
@@ -212,7 +215,7 @@ int main(int argc, char **argv)
 	if (pthread_mutex_unlock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
 	}
-	
+
 	// watch for input from stdin (fd 0) to see when it has input
 	safe_fd_set(fileno(stdin), &rfds_master, &max_fd);
 
@@ -231,7 +234,7 @@ int main(int argc, char **argv)
 		// create vrpnTracker instance
 		tracker = ucart_vrpn_tracker_createInstance(TRACKER_IP);
 		// this function will be called whenever tracker receives data
-		ucart_vrpn_tracker_addCallback(tracker, cb);	
+		ucart_vrpn_tracker_addCallback(tracker, cb);
 	} else {
 		printf("Ignoring VRPN information...\n");
 	}
@@ -358,7 +361,7 @@ int connectToZybo() {
 			local_comm_channel = 1;
 			char * fifo_rx = QUAD_LOCAL_DEFAULT_TX;
 			char * fifo_tx = QUAD_LOCAL_DEFAULT_RX;
-			
+
 			if (getenv(QUAD_LOCAL_RX)) {
 				fifo_tx = getenv(QUAD_LOCAL_RX);
 			}
@@ -385,14 +388,14 @@ int connectToZybo() {
 			printf("Using BT Settings\n");
 			struct sockaddr_rc addr;
 
-			// allocate a socket	
+			// allocate a socket
 			sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
 
-			//set the connection params ie. who to connect to	
+			//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));
@@ -420,7 +423,7 @@ int connectToZybo() {
 
 		if (getenv(QUAD_PORT_ENV)) {
 			/* Quick 'n dirty, oh yeah! */
-			addr.sin_port = htons(atoi(getenv(QUAD_PORT_ENV)));		
+			addr.sin_port = htons(atoi(getenv(QUAD_PORT_ENV)));
 		} else {
 			addr.sin_port = htons(QUAD_PORT_DEFAULT);
 		}
@@ -489,9 +492,9 @@ static ssize_t writeQuad(const uint8_t * buf, size_t count) {
 
 	if (local_comm_channel) {
 		retval = write(zybo_fifo_tx, buf, count);
-	} else {	
+	} else {
 		retval = write(zyboSocket, buf, count);
-	}	
+	}
 
 	if (pthread_mutex_unlock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
@@ -512,9 +515,9 @@ static ssize_t readQuad(char * buf, size_t count) {
 	if (local_comm_channel) {
 		retval = read(zybo_fifo_rx, buf, count);
 	} else {
-		retval = read(zyboSocket, buf, count);	
+		retval = read(zyboSocket, buf, count);
 	}
-	
+
 	if (pthread_mutex_unlock(&quadSocketMutex)) {
 		err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
 	}
@@ -661,7 +664,7 @@ static void client_recv(int fd) {
 		// printf("tmpbuff = '%s'\n", tmp);
 		first_word = strtok(tmp, " ");
 		// printf("first word = '%s'\n", first_word);
-	
+
 		ssize_t msg_type, i;
 		for (i = 0; i < MAX_TYPE_ID; ++i) {
 			if ((msg_type = findCommand(first_word)) != -1)
@@ -682,7 +685,7 @@ static void client_recv(int fd) {
 					write(fd, dummy, strlen(dummy));
 				} else if (ucart_vrpn_tracker_getData(tracker, &td)) {
 					write(fd, TD_MAGIC " ERROR\n", strlen(TD_MAGIC " ERROR\n"));
-				} else { 
+				} else {
 					/* more than sufficient buffer */
 					char buffer[2048];
 					/* Map VRPN XYZ to Height Lat Long (right now it's
@@ -708,7 +711,7 @@ static void client_recv(int fd) {
 			uint8_t packet[64];
 			struct metadata m;
 			uint8_t *data = malloc(sizeof(*data) * 128);
-			ssize_t result;  
+			ssize_t result;
 			ssize_t psize;
 
 			printf(" found a msg_type of %ld\n", msg_type);
@@ -755,7 +758,7 @@ static void client_recv(int fd) {
 			}
 
 			/* Only add the client to the pending responses if it was a getparam command */
-			if (m.msg_type == GETPARAM_ID || m.msg_type == GETOUTPUT_ID || 
+			if (m.msg_type == GETPARAM_ID || m.msg_type == GETOUTPUT_ID ||
 				m.msg_type == GETSOURCE_ID || m.msg_type == GETNODES_ID || m.msg_type == ADDNODE_ID) {
 				if (clientAddPendResponses(fd, BytesTo16(packet[ID_L], packet[ID_H])) == -1) {
 					warnx("Ran out of room! Consider increasing CLIENT_MAX_PENDING_RESPONSES!\n");
@@ -767,7 +770,7 @@ static void client_recv(int fd) {
 			// 	printf(" %.2x ", packet[i]);
 			// }
 			// printf("'\n");
-			
+
 			int retval = writeQuad(packet, psize);
 			// printf("sent %d bytes\n", retval);
 
@@ -792,7 +795,7 @@ static void quad_recv() {
 	ssize_t datalen;
 	size_t packetlen;
 
-	respLen = readQuad((char *) respBuf + respBufLen, 
+	respLen = readQuad((char *) respBuf + respBufLen,
 			CMD_MAX_LENGTH - respBufLen);
 	if (respLen <= 0) {
 		perror("ERROR reading from quad...\n");
@@ -838,7 +841,7 @@ static void quad_recv() {
 		if (datalen < 0){
 			/* Not enough data yet. We need to wait for more */
 			// printf("not enough\n");
-			return;	
+			return;
 		}
 
 		packetlen = PacketSize(&m);
@@ -847,7 +850,7 @@ static void quad_recv() {
 
 		char * debug_string;
 		switch (m.msg_type) {
-			case DEBUG_ID: 
+			case DEBUG_ID:
 				/* in case of debug. Quad send null terminated string in data */
 				debug_string = strndup((char *)data, m.data_len);
 				printf(" (Quad) : %s\n", debug_string);
@@ -880,7 +883,7 @@ static void quad_recv() {
 			case LOG_END_ID:
 				if (quadlog_file_open) {
 					fclose(quadlog_file);
-					quadlog_file_open = 0;	
+					quadlog_file_open = 0;
 				}
 				printf("\n(Quad) : Log End found\n");
 				receiving_logs = 0;
@@ -996,10 +999,10 @@ char * create_log_name(char * buffer, size_t max) {
 	strftime(c_time_string, 256, format_string, tmp);
 
 	if(strcmp(user_specified_log_name, "") == 0) {
-		sprintf(buffer, "%s/%s_%lu.txt", prefix, c_time_string, num_logs++);	
+		sprintf(buffer, "%s/%s_%lu.txt", prefix, c_time_string, num_logs++);
 	} else {
 		sprintf(buffer, "%s/%s_%lu.txt", prefix, user_specified_log_name, num_logs++);
 	}
-	
+
 	return buffer;
 }
diff --git a/groundStation/src/backend/param.c b/groundStation/src/backend/param.c
index 5fdba8fd5..91170cbb8 100644
--- a/groundStation/src/backend/param.c
+++ b/groundStation/src/backend/param.c
@@ -61,7 +61,7 @@ ssize_t EncodeSetParam(
         struct metadata * m,        /* data_len and msg_type will be populated*/
         uint8_t * data,             /* Output buffer */
         size_t data_size,           /* Max buffer size */
-        const char * msg)       /* Message to encode */
+        const char * msg)       		/* Message to encode */
 {
 	m->msg_type = SETPARAM_ID;
 	m->data_len = SP_DATA_SIZE;
@@ -120,9 +120,9 @@ int DecodeResponseParam(
 		return -1;
 	}
 
-	return snprintf(msg, max_len, "getparam %" PRId16 " %" PRId16 " %f\n", 
-		BytesTo16(data[RESP_BLOCK_ID_L], data[RESP_BLOCK_ID_H]), 
-		BytesTo16(data[RESP_PARAM_ID_L], data[RESP_PARAM_ID_H]), 
+	return snprintf(msg, max_len, "getparam %" PRId16 " %" PRId16 " %f\n",
+		BytesTo16(data[RESP_BLOCK_ID_L], data[RESP_BLOCK_ID_H]),
+		BytesTo16(data[RESP_PARAM_ID_L], data[RESP_PARAM_ID_H]),
 		BytesToFloat(data[RESP_VAL_1], data[RESP_VAL_2],
 			data[RESP_VAL_3], data[RESP_VAL_4]));
 }
diff --git a/groundStation/src/backend/source.c b/groundStation/src/backend/source.c
index dd2540e37..6f3e7fbc4 100644
--- a/groundStation/src/backend/source.c
+++ b/groundStation/src/backend/source.c
@@ -68,7 +68,7 @@ ssize_t EncodeSetSource(
 	if (data_size < SS_DATA_SIZE) {
 		return -1;
 	}
-	
+
 	int16_t dst_block, dst_input;
 	int16_t src_block, src_output;
 
@@ -116,7 +116,7 @@ int DecodeResponseSource(
 		return -1;
 	}
 
-	return snprintf(msg, max_len, "getsource %" PRId16 " %" PRId16 " %" PRId16" %" PRId16 "\n", 
+	return snprintf(msg, max_len, "getsource %" PRId16 " %" PRId16 " %" PRId16" %" PRId16 "\n",
 		BytesTo16(data[RESP_DST_BLOCK_ID_L], data[RESP_DST_BLOCK_ID_H]),
 		BytesTo16(data[RESP_DST_INPUT_ID_L], data[RESP_DST_INPUT_ID_H]),
 		BytesTo16(data[RESP_SRC_BLOCK_ID_L], data[RESP_SRC_BLOCK_ID_H]),
-- 
GitLab