From 7f35957a7dc9371978ea0d5e80bd3d01a8eff36a Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Sat, 9 Dec 2017 21:24:52 -0500
Subject: [PATCH] first successful multi-hop tx/rx

---
 embedded/README.md                            |   9 +-
 .../atsams70-tinyrouter/src/main.c            |   6 +-
 .../atsams70-tinyrouter/src/node.h            |   2 +-
 .../atsams70-tinyrouter/src/packet_handling.c | 210 ++++++++----------
 .../atsams70-tinyrouter/src/packet_handling.h |   4 +-
 .../atsams70-tinyrouter/src/tinyport.c        |  11 +-
 6 files changed, 111 insertions(+), 131 deletions(-)

diff --git a/embedded/README.md b/embedded/README.md
index a1288ff..643e82e 100644
--- a/embedded/README.md
+++ b/embedded/README.md
@@ -1,10 +1,11 @@
 # 'API'
 
-// test with two, ack to host?
-// test with four, ack to host?
-// node program for packet with address, lights on off
+// test with 8 or so?
+// test at higher bitrates?
+// how to do big packet pushing?
+// better app layer - and make sure to clear space?
 
-[0:start][1:destination-msb:9][2:destination-8:lsb][3:hopcount][4:source][5:source][6:#bytestotal][byte_7][byte_6]...[byte_n] 0-255 bytes
+[0:start][1:destination-msb:9][2:hopcount][3:source][4:#bytestotal][byte_5][byte_7]...[byte_n] 0-255 bytes
 
 Packet Loops ...
 
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/main.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/main.c
index 4f22847..cb1c915 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/main.c
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/main.c
@@ -212,7 +212,7 @@ int main (void){
 	
 	for (int i = 0; i < 1024; i++) {
 		for (int port = 0; port < 4; port++) {
-			LUT[i][port] = 255; 
+			LUT[i][port] = MAX_HOPCOUNT; 
 		}
 	}
 
@@ -235,9 +235,7 @@ int main (void){
 				ports[i]->haspacket = TP_NO_PACKET;
 				
 				handle_packet(&packetlooper, i);
-				
-				//tp_putdata(ports[i], packetlooper.raw, packetlooper.size); // non-blocking put
-				
+								
 				packet_clean(&packetlooper);
 			}
 		}
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h
index 71b0ecb..dbe27ea 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h
@@ -2,7 +2,7 @@
 #define NODE_H_
 
 #define MAX_HOPCOUNT 6
-#define MYADDRESS 2
+#define MYADDRESS 1
 
 uint8_t LUT[1024][4];
 uint8_t myAddress;
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c
index 901b16c..158ddf3 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c
@@ -29,111 +29,11 @@ void broadcast_packet(packet_t* p, uint8_t exclude) {
 }
 
 int in_table(uint8_t dest) {
-  return (LUT[dest][0] != MAX_HOPCOUNT && LUT[dest][1] != MAX_HOPCOUNT &&
-         LUT[dest][2] != MAX_HOPCOUNT && LUT[dest][3] != MAX_HOPCOUNT);
+  return !(LUT[dest][0] == MAX_HOPCOUNT && LUT[dest][1] == MAX_HOPCOUNT &&
+         LUT[dest][2] == MAX_HOPCOUNT && LUT[dest][3] == MAX_HOPCOUNT);
 }
 
-void handle_packet(packet_t* p, uint8_t port) {
-	
-  if (p->hopcount > MAX_HOPCOUNT && p->destination != myAddress) {
-    free((void*)p);
-    return;
-  } else {
-	  p->hopcount ++;
-  }
-  
-  update_LUT(p->source, p->hopcount, port);
-  
-  switch (parse_type(p)) {
-    case P_STANDARD:
-      if (p->destination == myAddress) {
-		  app_onpacket(*p);
-		  acknowledge(p);
-      } else {
-        if (in_table(p->destination)) {
-          int bestPort = 0;
-          int bestHopCount = LUT[p->destination][0];
-          for (int i = 0; i < 4; i++) {
-            if (LUT[p->destination][i] < bestHopCount) {
-              bestPort = i;
-              bestHopCount = LUT[p->destination][i];
-            }
-          }
-          send_packet(p, bestPort);
-        } else {
-          p->raw[0] = P_STANDARD_FLOOD;
-          broadcast_packet(p, port);
-        }
-      }
-      break;
-    case P_ACK:
-      if (p->destination == myAddress) {
-        app_onack(*p);
-      } else {
-        if (in_table(p->destination)) {
-          int bestPort = 0;
-          int bestHopCount = LUT[p->destination][0];
-          for (int i = 0; i < 4; i++) {
-            if (LUT[p->destination][i] < bestHopCount) {
-              bestPort = i;
-              bestHopCount = LUT[p->destination][i];
-            }
-          }
-          send_packet(p, bestPort);
-        } else {
-          p->raw[0] = P_ACK_FLOOD;
-          broadcast_packet(p, port);
-        }
-      }
-      break;
-    case P_STANDARD_FLOOD:
-      if (p->destination == myAddress) {
-        app_onpacket(*p);
-		acknowledge(p);
-      } else {
-		LUT[p->destination][port] = MAX_HOPCOUNT;
-        if (LUT[p->destination]) {
-          int bestPort = 0;
-          int bestHopCount = LUT[p->destination][0];
-          for (int i = 0; i < 4; i++) {
-            if (LUT[p->destination][i] < bestHopCount) {
-              bestPort = i;
-              bestHopCount = LUT[p->destination][i];
-            }
-          }
-          send_packet(p, bestPort);
-        } else {
-          broadcast_packet(p, port);
-        }
-      }
-      break;
-    case P_ACK_FLOOD:
-    if (p->destination == myAddress) {
-      app_onack(*p);
-    } else {
-	  LUT[p->destination][port] = MAX_HOPCOUNT; 
-      if (LUT[p->destination]) {
-        int bestPort = 0;
-        int bestHopCount = LUT[p->destination][0];
-        for (int i = 0; i < 4; i++) {
-          if (LUT[p->destination][i] < bestHopCount) {
-            bestPort = i;
-            bestHopCount = LUT[p->destination][i];
-          }
-        }
-        send_packet(p, bestPort);
-      } else {
-        broadcast_packet(p, port);
-      }
-    }
-      break;
-    default:
-	  pin_clear(&stlr); // err indicator
-	  break;
-  }
-}
-
-void acknowledge(packet_t* p){
+void acknowledge_packet(packet_t* p){
 	packet_t ackpack = packet_new();
 	
 	ackpack.type = P_ACK;
@@ -142,22 +42,100 @@ void acknowledge(packet_t* p){
 	ackpack.hopcount = 0;
 	ackpack.size = 5;
 	
-	packet_buildraw(&ackpack);
+	packet_buildraw(&ackpack); // from pointers -> raw bytes
 	
 	if (in_table(ackpack.destination)) {
-		int bestPort = 0;
-		int bestHopCount = LUT[ackpack.destination][0];
-		for (int i = 0; i < 4; i++) {
-			if (LUT[ackpack.destination][i] < bestHopCount) {
-				bestPort = i;
-				bestHopCount = LUT[ackpack.destination][i];
-			}
-		}
-		send_packet(&ackpack, bestPort);
-		} else {
-		p->raw[0] = P_STANDARD_FLOOD;
+		send_on_bestport(&ackpack);
+	} else {
+		// altho, this should not happen - we have presumably just seen this come in.
+		p->raw[0] = P_ACK_FLOOD;
 		broadcast_packet(p, 4);
 	}
 	
-	//handle_packet(&ackpack, 4); // port is self
+	// add: 
+	//free(&ackpack);
+}
+
+void send_on_bestport(packet_t* p){
+	int bestPort = 0;
+	int bestHopCount = LUT[p->destination][0];
+	for (int i = 0; i < 4; i++) {
+		if (LUT[p->destination][i] < bestHopCount) {
+			bestPort = i;
+			bestHopCount = LUT[p->destination][i];
+		}
+	}
+	send_packet(p, bestPort);
 }
+
+void handle_packet(packet_t* p, uint8_t port) {
+
+	if (p->hopcount > MAX_HOPCOUNT && p->destination != myAddress) {
+		free((void*)p); // kill it!
+		return; // bail!
+	} else {
+		update_LUT(p->source, p->hopcount, port); // always, and before the hopcount goes up!
+		p->hopcount ++; // sloppy double-set: we should be just using raw values everywhere?
+		p->raw[2] = p->hopcount; 
+	}
+	
+	switch (parse_type(p)) {
+		case P_STANDARD:
+			if (p->destination == myAddress) {
+				app_onpacket(*p);
+				acknowledge_packet(p);
+			} else {
+				if (in_table(p->destination)) {
+				send_on_bestport(p);
+				} else {
+					p->raw[0] = P_STANDARD_FLOOD;
+					broadcast_packet(p, port);
+				}
+			}
+		break;
+		
+		case P_ACK:
+			if (p->destination == myAddress) {
+				app_onack(*p);
+			} else {
+				if (in_table(p->destination)) {
+					send_on_bestport(p);
+				} else {
+					p->raw[0] = P_ACK_FLOOD;
+					broadcast_packet(p, port);
+				}
+			}
+		break;
+		
+		case P_STANDARD_FLOOD:
+			LUT[p->destination][port] = MAX_HOPCOUNT; // likely no good path exists on this port
+			if (p->destination == myAddress) {
+				app_onpacket(*p);
+				acknowledge_packet(p);
+			} else {
+				if(in_table(p->destination)){
+					send_on_bestport(p);
+				} else {
+					broadcast_packet(p, port);
+				}
+			}
+		break;
+		
+		case P_ACK_FLOOD:
+			LUT[p->destination][port] = MAX_HOPCOUNT; // lngpeotp
+			if (p->destination == myAddress) {
+				app_onack(*p);
+			} else {
+				if(in_table(p->destination)){
+					send_on_bestport(p);
+				} else {
+					broadcast_packet(p, port);
+				}
+			}
+		break;
+		
+		default:
+			pin_clear(&stlr); // err indicator
+		break;
+	}
+}
\ No newline at end of file
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h
index ab1af3f..8d4019c 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h
@@ -21,7 +21,9 @@ void broadcast_packet(packet_t* p, uint8_t exclude);
 
 void handle_packet(packet_t* p, uint8_t port);
 
-void acknowledge(packet_t* p);
+void send_on_bestport(packet_t* p);
+
+void acknowledge_packet(packet_t* p);
 
 int in_table(uint8_t dest);
 
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/tinyport.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/tinyport.c
index bc3611b..2e7086c 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/tinyport.c
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/tinyport.c
@@ -94,7 +94,12 @@ void tp_packetparser(tinyport_t *tp){
 				// check for end of packet w/ counter 
 				// (counter is _current_ byte, is incremented at end of handle)
 				// when done, fill in fields for easy access in handling
-				if(tp->packet.counter >= tp->packet.size - 1){ // check counter against packet size to see if @ end of packet
+				tp->packet.raw[tp->packet.counter] = data;
+				tp->packet.counter ++;
+				if(tp->packet.counter == 5){
+					tp->packet.size = data; // 5th byte in packet structure is size
+				}
+				if(tp->packet.counter >= tp->packet.size){ // check counter against packet size to see if @ end of packet
 					tp->packet.type = tp->packet.raw[0];
 					tp->packet.destination = tp->packet.raw[1];//((uint16_t)tp->packet.raw[1] << 8) | tp->packet.raw[2];
 					tp->packet.hopcount = tp->packet.raw[2];
@@ -102,11 +107,7 @@ void tp_packetparser(tinyport_t *tp){
 					tp->haspacket = TP_HAS_PACKET; // this data is final byte, we have packet, this will be last tick in loop
 					tp->packetstate = TP_PACKETSTATE_OUTSIDE; // and we're outside again
 					pin_set(tp->stlb);
-				} else if(tp->packet.counter == 4){ 
-					tp->packet.size = data; // 5th byte in packet structure is size
 				}
-				tp->packet.raw[tp->packet.counter] = data;
-				tp->packet.counter ++;
 				break;
 				
 			default:
-- 
GitLab