From 557b2a95a34dd310faf269f48656a00aa882d67b Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Fri, 8 Dec 2017 19:08:34 -0500
Subject: [PATCH] packet handling, acks, bones of application layer

---
 embedded/README.md                            |  6 +-
 .../atsams70-tinyrouter/Debug/Makefile        |  7 +++
 .../atsams70-tinyrouter/Debug/makedep.mk      |  2 +
 .../atsams70-tinyrouter.cproj                 |  6 ++
 .../atsams70-tinyrouter/src/application.c     | 36 ++++++++++++
 .../atsams70-tinyrouter/src/application.h     | 18 ++++++
 .../atsams70-tinyrouter/src/node.h            |  2 +-
 .../atsams70-tinyrouter/src/packet.c          |  9 +++
 .../atsams70-tinyrouter/src/packet.h          | 10 ++--
 .../atsams70-tinyrouter/src/packet_handling.c | 55 ++++++++++++++-----
 .../atsams70-tinyrouter/src/packet_handling.h |  2 +
 .../atsams70-tinyrouter/src/tinyport.c        | 13 +++--
 js/serialterminal.js                          |  5 +-
 13 files changed, 142 insertions(+), 29 deletions(-)
 create mode 100644 embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/application.c
 create mode 100644 embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/application.h

diff --git a/embedded/README.md b/embedded/README.md
index 6b9a314..c5b0dc1 100644
--- a/embedded/README.md
+++ b/embedded/README.md
@@ -1,8 +1,8 @@
 # 'API'
 
-// to echo with new system
- - do packet handler fills in packet data: addresses, hop counts, etc ... use raw[] as often as possible
-// to seeing flood on all ports
+// return acks: build packet and put on port?
+// lights: on blue when rbrx non empty, on green when rbtx non empy
+// test with four, reach fourth?
 
 [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
 
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/Debug/Makefile b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/Debug/Makefile
index 00140d2..8be3d33 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/Debug/Makefile
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/Debug/Makefile
@@ -79,6 +79,7 @@ SUBDIRS :=  \
 
 # Add inputs and outputs from these tool invocations to the build variables 
 C_SRCS +=  \
+../src/application.c \
 ../src/packet.c \
 ../src/packet_handling.c \
 ../src/pin.c \
@@ -103,6 +104,7 @@ ASM_SRCS +=
 
 
 OBJS +=  \
+src/application.o \
 src/packet.o \
 src/packet_handling.o \
 src/pin.o \
@@ -120,6 +122,7 @@ src/ASF/sam/utils/syscalls/gcc/syscalls.o \
 src/main.o
 
 OBJS_AS_ARGS +=  \
+src/application.o \
 src/packet.o \
 src/packet_handling.o \
 src/pin.o \
@@ -137,6 +140,7 @@ src/ASF/sam/utils/syscalls/gcc/syscalls.o \
 src/main.o
 
 C_DEPS +=  \
+src/application.d \
 src/packet.d \
 src/packet_handling.d \
 src/pin.d \
@@ -154,6 +158,7 @@ src/ASF/sam/utils/syscalls/gcc/syscalls.d \
 src/main.d
 
 C_DEPS_AS_ARGS +=  \
+src/application.d \
 src/packet.d \
 src/packet_handling.d \
 src/pin.d \
@@ -213,6 +218,8 @@ LINKER_SCRIPT_DEP+=  \
 
 
 
+
+
 
 
 
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/Debug/makedep.mk b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/Debug/makedep.mk
index af67d53..b54972c 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/Debug/makedep.mk
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/Debug/makedep.mk
@@ -2,6 +2,8 @@
 # Automatically-generated file. Do not edit or delete the file
 ################################################################################
 
+src\application.c
+
 src\packet.c
 
 src\packet_handling.c
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/atsams70-tinyrouter.cproj b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/atsams70-tinyrouter.cproj
index e93edb6..94e88c3 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/atsams70-tinyrouter.cproj
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/atsams70-tinyrouter.cproj
@@ -472,6 +472,12 @@
     <Folder Include="src\config\" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="src\application.c">
+      <SubType>compile</SubType>
+    </Compile>
+    <Compile Include="src\application.h">
+      <SubType>compile</SubType>
+    </Compile>
     <Compile Include="src\node.h">
       <SubType>compile</SubType>
     </Compile>
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/application.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/application.c
new file mode 100644
index 0000000..ca79004
--- /dev/null
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/application.c
@@ -0,0 +1,36 @@
+/*
+ * application.c
+ *
+ * Created: 12/8/2017 5:48:52 PM
+ *  Author: Jake
+ */ 
+
+#include "application.h"
+#include "ports.h"
+
+void app_onpacket(packet_t p){
+	switch (p.raw[5]){ // key:
+		case 1:
+			if (p.raw[6] == 1){
+				pin_clear(&stlr);
+			} else {
+				pin_set(&stlr);
+			}
+			break;
+		case 2:
+			if(p.raw[6] == 1){
+				pin_clear(&stlb);
+			} else {
+				pin_set(&stlb);
+			}
+			break;
+		default:
+			pin_set(&stlb);
+			pin_set(&stlr);
+			break;
+	}
+}
+
+void app_onack(packet_t p){
+	// windows?
+}
\ No newline at end of file
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/application.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/application.h
new file mode 100644
index 0000000..a8efb21
--- /dev/null
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/application.h
@@ -0,0 +1,18 @@
+/*
+ * application.h
+ *
+ * Created: 12/8/2017 5:45:27 PM
+ *  Author: Jake
+ */ 
+
+
+#ifndef APPLICATION_H_
+#define APPLICATION_H_
+
+#include "packet.h"
+
+void app_onpacket(packet_t p);
+
+void app_onack(packet_t p);
+
+#endif /* APPLICATION_H_ */
\ No newline at end of file
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h
index 7f8f5f6..c5bc8db 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h
@@ -5,6 +5,6 @@
 #define MYADDRESS 2;
 
 uint8_t LUT[1024][4];
-uint16_t myAddress;
+uint8_t myAddress;
 
 #endif
\ No newline at end of file
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.c
index b1bdda1..53b7c7f 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.c
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.c
@@ -15,8 +15,17 @@ packet_t packet_new(void){
 	return packet;
 }
 
+void packet_buildraw(packet_t *packet){
+	packet->raw[0] = packet->type;
+	packet->raw[1] = packet->destination;
+	packet->raw[2] = 0; // hop count
+	packet->raw[3] = packet->source;
+	packet->raw[4] = packet->size;
+}
+
 void packet_clean(packet_t *packet){
 	packet->counter = 0;
+	packet->type = 0;
 	packet->destination = 0;
 	packet->source = 0;
 	packet->hopcount = 0;
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h
index c368071..c1a4fd6 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h
@@ -18,17 +18,19 @@
 #include <stdint.h>
 
 typedef struct{
+	uint8_t counter;
 	uint8_t raw[255];
-	uint16_t destination;
-	uint16_t source;
+	uint8_t type;
+	uint8_t destination;
+	uint8_t source;
 	uint8_t hopcount;
 	uint8_t size;
-
-	uint8_t counter;
 }packet_t;
 
 packet_t packet_new(void);
 
+void packet_buildraw(packet_t *packet);
+
 void packet_clean(packet_t *packet);
 
 #endif /* PACKET_H_ */
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c
index edaf347..901b16c 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c
@@ -3,6 +3,7 @@
 #include "ports.h"
 #include "tinyport.h"
 #include "packet_handling.h"
+#include "application.h"
 #include <string.h>
 #include <stdlib.h>
 
@@ -17,6 +18,7 @@ void update_LUT(uint16_t src, uint8_t hopCount, uint8_t port) {
 void send_packet(packet_t* p, uint8_t port) {
 	tp_putdata(ports[port], p->raw, p->size); 
 	//free((void*)p); // need rethink packet passing ?
+	// @Dougie: when I use free here I don't get the full packet return?
 }
 
 void broadcast_packet(packet_t* p, uint8_t exclude) {
@@ -26,8 +28,8 @@ void broadcast_packet(packet_t* p, uint8_t exclude) {
   if (exclude != 3) send_packet(p, 3);
 }
 
-int in_table( uint8_t dest) {
-  return !(LUT[dest][0] != MAX_HOPCOUNT && LUT[dest][1] != MAX_HOPCOUNT &&
+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);
 }
 
@@ -45,8 +47,8 @@ void handle_packet(packet_t* p, uint8_t port) {
   switch (parse_type(p)) {
     case P_STANDARD:
       if (p->destination == myAddress) {
-		  pin_clear(&stlr);
-		  // TODO: process? send to application layer?
+		  app_onpacket(*p);
+		  acknowledge(p);
       } else {
         if (in_table(p->destination)) {
           int bestPort = 0;
@@ -66,8 +68,7 @@ void handle_packet(packet_t* p, uint8_t port) {
       break;
     case P_ACK:
       if (p->destination == myAddress) {
-        pin_clear(&stlb);
-		// TODO: process, window?
+        app_onack(*p);
       } else {
         if (in_table(p->destination)) {
           int bestPort = 0;
@@ -80,15 +81,15 @@ void handle_packet(packet_t* p, uint8_t port) {
           }
           send_packet(p, bestPort);
         } else {
-          p->raw[0] = P_STANDARD_FLOOD;
-          broadcast_packet(p, 4);
+          p->raw[0] = P_ACK_FLOOD;
+          broadcast_packet(p, port);
         }
       }
       break;
     case P_STANDARD_FLOOD:
       if (p->destination == myAddress) {
-        //TODO:
-		// process, reply
+        app_onpacket(*p);
+		acknowledge(p);
       } else {
 		LUT[p->destination][port] = MAX_HOPCOUNT;
         if (LUT[p->destination]) {
@@ -102,15 +103,13 @@ void handle_packet(packet_t* p, uint8_t port) {
           }
           send_packet(p, bestPort);
         } else {
-          // Dougie: I removed this: p->raw[0] = P_STANDARD_FLOOD;
           broadcast_packet(p, port);
         }
       }
       break;
     case P_ACK_FLOOD:
     if (p->destination == myAddress) {
-      //TODO:
-	  // process
+      app_onack(*p);
     } else {
 	  LUT[p->destination][port] = MAX_HOPCOUNT; 
       if (LUT[p->destination]) {
@@ -124,7 +123,6 @@ void handle_packet(packet_t* p, uint8_t port) {
         }
         send_packet(p, bestPort);
       } else {
-		// Dougie: I removed this: p->raw[0] = P_STANDARD_FLOOD;
         broadcast_packet(p, port);
       }
     }
@@ -134,3 +132,32 @@ void handle_packet(packet_t* p, uint8_t port) {
 	  break;
   }
 }
+
+void acknowledge(packet_t* p){
+	packet_t ackpack = packet_new();
+	
+	ackpack.type = P_ACK;
+	ackpack.destination = p->source;
+	ackpack.source = p->destination;
+	ackpack.hopcount = 0;
+	ackpack.size = 5;
+	
+	packet_buildraw(&ackpack);
+	
+	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;
+		broadcast_packet(p, 4);
+	}
+	
+	//handle_packet(&ackpack, 4); // port is self
+}
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h
index 927725b..ab1af3f 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h
@@ -21,6 +21,8 @@ void broadcast_packet(packet_t* p, uint8_t exclude);
 
 void handle_packet(packet_t* p, uint8_t port);
 
+void acknowledge(packet_t* p);
+
 int in_table(uint8_t dest);
 
 #endif /* PACKET_HANDLING_H_ */
diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/tinyport.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/tinyport.c
index 8fa0065..9cee285 100644
--- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/tinyport.c
+++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/tinyport.c
@@ -79,7 +79,7 @@ void tp_packetparser(tinyport_t *tp){
 			case TP_PACKETSTATE_OUTSIDE:
 				// check if start, add 1st byte, change state
 				// if not start, assume buffer depth data, update
-				if(data == TP_DELIMITER_START){ // TODO: more types, and types, not delimiters
+				if(data == P_STANDARD | data == P_STANDARD_FLOOD | data == P_ACK | data == P_ACK_FLOOD){ 
 					tp->packetstate = TP_PACKETSTATE_INSIDE;
 					tp->packet.raw[tp->packet.counter] = data;
 					tp->packet.counter ++;
@@ -95,14 +95,15 @@ void tp_packetparser(tinyport_t *tp){
 				// (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.hopcount = tp->packet.raw[3];
-					tp->packet.destination = ((uint16_t)tp->packet.raw[1] << 8) | tp->packet.raw[2];
-					tp->packet.source = ((uint16_t)tp->packet.raw[4] << 8) | tp->packet.raw[5];
+					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];
+					tp->packet.source = tp->packet.raw[3];//((uint16_t)tp->packet.raw[4] << 8) | tp->packet.raw[5];
 					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_clear(tp->stlb);
-				} else if(tp->packet.counter == 6){ 
-					tp->packet.size = data; // 7th byte in packet structure is size
+				} 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 ++;
diff --git a/js/serialterminal.js b/js/serialterminal.js
index 386edc1..3e300a6 100644
--- a/js/serialterminal.js
+++ b/js/serialterminal.js
@@ -17,7 +17,10 @@ const rl = readline.createInterface({
 rl.on('line', parseLineIn);
 
 // [type][destination][destination][hopcount][source][source][#bytestotal][byte_7][byte_6]...[byte_n]
-var buf = Buffer.from([255,0,3,1,3,3,12,8,9,10,11,12])
+//var buf = Buffer.from([255,0,2,1,0,1,9,1,1])
+
+// [type][destination][hopcount][source][#bytestotal][byte_7][byte_6]...[byte_n]
+var buf = Buffer.from([255,2,0,12,7,1,0])
 
 function parseLineIn(data) {
 	if (debug) {
-- 
GitLab