From 9a6a3a6c5b498f2257ebe1522ccfd06e342703bb Mon Sep 17 00:00:00 2001
From: burneykb <burneykb@iastate.edu>
Date: Sat, 4 Feb 2017 21:44:36 -0600
Subject: [PATCH] Implemented and tested clientRemovePendRespons

---
 groundStation/src/backend/backend.c | 30 +++++++++++++++++++----------
 groundStation/src/backend/packet.c  | 15 ---------------
 groundStation/src/backend/packet.h  | 15 +++++++++++++++
 3 files changed, 35 insertions(+), 25 deletions(-)

diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index 82f698e2b..e0b8b8434 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -40,6 +40,7 @@
 #include "cmHandler.h"
 #include "getparam.h"
 #include "setparam.h"
+#include "bitwise.h"
 
 #define QUAD_BT_ADDR  "00:06:66:64:61:D6"
 #define QUAD_BT_CHANNEL  0x01
@@ -69,7 +70,9 @@ static char * get_client_buffer(int fd);
 /* 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, unsigned char *packet);
+static int clientAddPendResponses(int fd, uint16_t packet_id);
+/* Return positive integer if successful, -1 otherwise */
+static int clientRemovePendResponses(int fd, uint16_t packet_id);
 /* Returns -1 on error */
 static int remove_client(int fd);
 /* Receive data from client */
@@ -535,12 +538,22 @@ static int * get_client_pend_responses(int fd) {
 	}
 }
 
-static int clientAddPendResponses(int fd, unsigned char *packet) {
+static int clientAddPendResponses(int fd, uint16_t packet_id) {
 	int *pendingResponses = get_client_pend_responses(fd);
-	int packetID = (packet[4] << 8) | (packet[3]);
 	for(int i = 0; i < CLIENT_MAX_PENDING_RESPONSES; i++) {
 		if(pendingResponses[i] == -1) {
-			pendingResponses[i] = packetID;
+			pendingResponses[i] = packet_id;
+			return i;
+		}
+	}
+	return -1;
+}
+
+static int clientRemovePendResponses(int fd, uint16_t packet_id) {
+	int *pendingResponses = get_client_pend_responses(fd);
+	for(int i = 0; i < CLIENT_MAX_PENDING_RESPONSES; i++) {
+		if(pendingResponses[i] == packet_id) {
+			pendingResponses[i] = -1;
 			return i;
 		}
 	}
@@ -679,7 +692,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) {
-				if (clientAddPendResponses(fd, packet) == -1) {
+				if (clientAddPendResponses(fd, BytesTo16(packet[ID_L], packet[ID_H])) == -1) {
 					warnx("Ran out of room! Consider increasing CLIENT_MAX_PENDING_RESPONSES!\n");
 				}
 			}
@@ -700,7 +713,6 @@ static void quad_recv() {
 	static unsigned char respBuf[4096];
 	static size_t respBufLen;
 
-	int unfinished = 1;
 	struct metadata m;
 	uint8_t data[4096];
 	size_t respLen;
@@ -721,7 +733,7 @@ static void quad_recv() {
 	// }
 	// printf("'\n");
 
-	while(unfinished){
+	while(respBufLen){
 		datalen = DecodePacket(&m, data, 2048, respBuf, respBufLen);
 	
 		if (datalen == -1) {
@@ -770,9 +782,6 @@ static void quad_recv() {
 			default:
 				printf("(Backend): message type %d unrecognized\n", m.msg_type);
 		}
-		if (respBufLen == 0) {
-			unfinished = 0;
-		}
 	}
 	
 }
@@ -793,6 +802,7 @@ static void handleResponseParam(struct metadata *m, uint8_t * data)
 
 	for(int fd = 0; fd <= max_fd; ++fd) {
 		if (get_client_index(fd) > -1) {
+			clientRemovePendResponses(fd, m->msg_id);
 			write(fd, buffer, len);
 		}
 	}
diff --git a/groundStation/src/backend/packet.c b/groundStation/src/backend/packet.c
index d0aafbf04..7ddbb5363 100644
--- a/groundStation/src/backend/packet.c
+++ b/groundStation/src/backend/packet.c
@@ -4,21 +4,6 @@
 
 #include <string.h>
 
-enum PacketHeader {
-	BEGIN,
-	MTYPE_L,
-	MTYPE_H,
-	ID_L,
-	ID_H,
-	DLEN_L,
-	DLEN_H,
-	HDR_SIZE
-};
-
-enum ChecksumFormat {
-	CSUM_L,
-	CSUM_SIZE
-};
 
 /* Combine metadata and data to form a wire-sendable packet.
  * Returns the size of the encoded packet
diff --git a/groundStation/src/backend/packet.h b/groundStation/src/backend/packet.h
index 54532e478..87f8c5c7e 100644
--- a/groundStation/src/backend/packet.h
+++ b/groundStation/src/backend/packet.h
@@ -4,6 +4,21 @@
 #include <stdint.h>
 #include <sys/types.h>
 
+enum PacketHeader {
+	BEGIN,
+	MTYPE_L,
+	MTYPE_H,
+	ID_L,
+	ID_H,
+	DLEN_L,
+	DLEN_H,
+	HDR_SIZE
+};
+
+enum ChecksumFormat {
+	CSUM_L,
+	CSUM_SIZE
+};
 struct metadata {
 	int msg_type;
 	int msg_id;
-- 
GitLab