From 3304b4cd93613fa0e2dfc41856463f08e20ce2c6 Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Fri, 23 Feb 2018 21:51:37 -0500
Subject: [PATCH] nearly port forwarding

---
 .../mkrouter-v04/mkrouter-v04/src/apaport.c   | 55 +++++++++++++++----
 .../mkrouter-v04/mkrouter-v04/src/apaport.h   |  6 +-
 .../mkrouter-v04/mkrouter-v04/src/hardware.h  |  2 +
 embedded/mkrouter-v04/mkrouter-v04/src/main.c | 22 +++++---
 .../mkrouter-v04/src/ringbuffer.c             | 13 ++---
 .../mkrouter-v04/mkrouter-v04/src/uartport.c  | 11 +++-
 .../mkrouter-v04/mkrouter-v04/src/uartport.h  |  6 +-
 7 files changed, 85 insertions(+), 30 deletions(-)

diff --git a/embedded/mkrouter-v04/mkrouter-v04/src/apaport.c b/embedded/mkrouter-v04/mkrouter-v04/src/apaport.c
index f7a9f27..738e2a4 100644
--- a/embedded/mkrouter-v04/mkrouter-v04/src/apaport.c
+++ b/embedded/mkrouter-v04/mkrouter-v04/src/apaport.c
@@ -7,10 +7,13 @@
 
 #include "apaport.h"
 
-void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, pin_t *stlr, pin_t *stlb){
+void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, uartport_t *uarts, uint8_t numports, pin_t *stlr, pin_t *stlg, pin_t *stlb){
 	apap->uart = uart;
+	apap->uarts = uarts;
 	apap->portnum = portnum;
+	apap->numports = numports;
 	apap->stlr = stlr;
+	apap->stlg = stlg;
 	apap->stlb = stlb;
 }
 
@@ -21,17 +24,22 @@ void apaport_reset(apaport_t *apap){
 	apap->packet_position = 0;
 	
 	pin_set(apap->stlr);
+	pin_set(apap->stlg);
 	pin_set(apap->stlb);
 }
 
 void apaport_scan(apaport_t *apap, uint32_t maxpackets){
 	// scan through for completely received packets
-	while(apap->packets_ready <= maxpackets && !rb_empty(apap->uart->rbrx)){
+	while(apap->packets_ready <= maxpackets){
+		if(rb_empty(apap->uart->rbrx)){
+			pin_set(apap->stlr);
+			break;
+		}
 		// pull bytes out of buffer and segment into packets
-		pin_clear(apap->stlb);
-		delay_ms(15);
-		pin_set(apap->stlb);
-		delay_ms(15);
+		pin_clear(apap->stlg);
+		delay_ms(1);
+		pin_set(apap->stlg);
+		delay_ms(1);
 		
 		apap->packets[apap->packet_num][apap->packet_position] = rb_get(apap->uart->rbrx);
 		apap->packet_position ++;
@@ -49,13 +57,12 @@ void apaport_scan(apaport_t *apap, uint32_t maxpackets){
 	while(apap->packets_ready > 0){
 		uint32_t p = (apap->packet_num + APAPORT_NUM_STATIC_PACKETS - apap->packets_ready) % APAPORT_NUM_STATIC_PACKETS;
 		// handle these!
-		pin_clear(apap->stlr);
+		pin_clear(apap->stlg);
 		delay_ms(150);
-		pin_set(apap->stlr);
+		pin_set(apap->stlg);
 		delay_ms(150);
 		
-		// now to handle: 1st, a reply for debug of what we received
-		uart_sendchars_buffered(apap->uart, apap->packets[p], apap->packets[p][0]);//apap->packets[p][0]);
+		// now to handle: 1st, a reply for debug of what we received		
 		/*
 		now, handle:
 		 - packet is for us [1]th element is pointer
@@ -63,7 +70,35 @@ void apaport_scan(apaport_t *apap, uint32_t maxpackets){
 		 - next port, and fire on
 		 - floods
 		*/
+		if(apap->packets[p][1] == APA_ADDR_POINTER){
+			// packet is ours! mark and somehow get to level up?
+			// application_apa_handler_t takes it?
+		} else if(apap->packets[p][1] == APA_ADDR_FLOOD){ 
+			// packet is flood, increment pointer and flood!
+		} else { 
+			// increment pointer
+			for(int i = 2; i < apap->packets[p][0]; i ++){ 
+				// loop through bytes to find pointer
+				if(apap->packets[p][i] == APA_ADDR_POINTER){
+					apap->packets[p][i-1] = APA_ADDR_POINTER; // increment pointer forwards
+					apap->packets[p][i] = apap->portnum; // port received on is this one
+				}
+				// [1]th element is port to forward on (and check that we have that port)
+			}
+			// check that the outgoing port exists on this hardware
+			// if not, send to topmost
+			// HERE: mod this so avoid the != APA_ADDR_POINTER (should store and pass before updating, in some cases
+			// we send to port where [p][1] is flood ... how else u gonna send?
+			// launches should take place at end of ifs
+			if(apap->packets[p][1] > APAPORT_NUM_PORTS - 1 && apap->packets[p][1] != APA_ADDR_POINTER){
+				apap->packets[p][1] = APAPORT_NUM_PORTS - 1;
+			}
+			// send on next port
+			// HERE: finish mod & rename for ports being 0-n not 1-n
+			uart_sendchars_buffered(&apap->uarts[apap->packets[p][1] - 1], apap->packets[p], apap->packets[p][0]);
+		}
 		
+		uart_sendchars_buffered(apap->uart, apap->packets[p], apap->packets[p][0]);//apap->packets[p][0]);
 		apap->packets_ready --;
 	}
 }
\ No newline at end of file
diff --git a/embedded/mkrouter-v04/mkrouter-v04/src/apaport.h b/embedded/mkrouter-v04/mkrouter-v04/src/apaport.h
index 2391cec..21b37e2 100644
--- a/embedded/mkrouter-v04/mkrouter-v04/src/apaport.h
+++ b/embedded/mkrouter-v04/mkrouter-v04/src/apaport.h
@@ -14,6 +14,7 @@
 #include "pin.h"
 
 #define APAPORT_NUM_STATIC_PACKETS 4
+#define APAPORT_NUM_PORTS 5
 
 #define APAPORT_OUTSIDE_PACKET 0
 #define APAPORT_INSIDE_PACKET 1
@@ -24,10 +25,13 @@
 
 typedef struct{
 	uartport_t *uart;
+	uartport_t *uarts;
 	pin_t *stlr;
 	pin_t *stlb;
+	pin_t *stlg;
 	
 	uint8_t portnum; // which port are we
+	uint8_t numports; // how many in the array we got
 	
 	uint32_t packet_num;
 	uint32_t packet_position;
@@ -36,7 +40,7 @@ typedef struct{
 	uint8_t packets[APAPORT_NUM_STATIC_PACKETS][256]; // packets for handling by app
 }apaport_t;
 
-void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, pin_t *stlr, pin_t *stlb);
+void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, uartport_t *uarts, uint8_t numports, pin_t *stlr, pin_t *stlg, pin_t *stlb);
 
 void apaport_reset(apaport_t *apap);
 
diff --git a/embedded/mkrouter-v04/mkrouter-v04/src/hardware.h b/embedded/mkrouter-v04/mkrouter-v04/src/hardware.h
index b4ede01..0ab67d1 100644
--- a/embedded/mkrouter-v04/mkrouter-v04/src/hardware.h
+++ b/embedded/mkrouter-v04/mkrouter-v04/src/hardware.h
@@ -16,6 +16,7 @@
 
 // status lights
 pin_t np1stlr;
+pin_t np1stlg;
 pin_t np1stlb;
 pin_t np2stlr;
 pin_t np2stlb;
@@ -24,6 +25,7 @@ pin_t np3stlb;
 pin_t np4stlr;
 pin_t np4stlb;
 pin_t np5stlr;
+pin_t np5stlg;
 pin_t np5stlb;
 pin_t *lights[] = {&np1stlr, &np1stlb, &np2stlr, &np2stlb, &np3stlr, &np3stlb, &np4stlr, &np4stlb, &np5stlr, &np5stlb};
 
diff --git a/embedded/mkrouter-v04/mkrouter-v04/src/main.c b/embedded/mkrouter-v04/mkrouter-v04/src/main.c
index 8ee58e4..57362c1 100644
--- a/embedded/mkrouter-v04/mkrouter-v04/src/main.c
+++ b/embedded/mkrouter-v04/mkrouter-v04/src/main.c
@@ -78,6 +78,8 @@ void setupinterrupts(void){
 void lightsetup(void){
 	pin_init(&np1stlr, PIOD, PIO_PER_P12);
 	pin_output(&np1stlr);
+	pin_init(&np1stlg, PIOA, PIO_PER_P3);
+	pin_output(&np1stlg);
 	pin_init(&np1stlb, PIOA, PIO_PER_P2);
 	pin_output(&np1stlb);
 	
@@ -98,15 +100,16 @@ void lightsetup(void){
 	
 	pin_init(&np5stlr, PIOB, PIO_PER_P0);
 	pin_output(&np5stlr);
+	pin_init(&np5stlg, PIOA, PIO_PER_P20);
+	pin_output(&np5stlg);
 	pin_init(&np5stlb, PIOB, PIO_PER_P1);
 	pin_output(&np5stlb);
 }
 
 void lightstoggle(void){
-	for(int i = 2; i < 10; i++){
+	for(int i = 0; i < 10; i++){
 		pin_toggle(lights[i]);
 	}
-	
 }
 
 void initports(void){
@@ -114,35 +117,35 @@ void initports(void){
 	rb_init(&np1rbrx);
 	rb_init(&np1rbtx);
 	// UP1 on UART0, RX 9 TX 10 on PIOA
-	uart_build(&up1, UART0, PIOA, 9, 10, &np1rbrx, &np1rbtx);
+	uart_build(&up1, UART0, PIOA, 9, 10, &np1rbrx, &np1rbtx, &np1stlr, &np1stlb);
 	uart_init(&up1, 81, UART_IS_PERIPHERAL_A);
 	
 	// RBs 2
 	rb_init(&np2rbrx);
 	rb_init(&np2rbtx);
 	// UP2 on UART1, RX 5 TX 4 on PIOA
-	uart_build(&up2, UART1, PIOA, 5, 4, &np2rbrx, &np2rbtx);
+	uart_build(&up2, UART1, PIOA, 5, 4, &np2rbrx, &np2rbtx, &np2stlr, &np2stlb);
 	uart_init(&up2, 81, UART_IS_PERIPHERAL_C); // 81 for FTDI 115200 :|
 
 	// RBs 3
 	rb_init(&np3rbrx);
 	rb_init(&np3rbtx);
 	// UP3 on UART2, RX 25 TX 26 on PIOD
-	uart_build(&up3, UART2, PIOD, 25, 26, &np3rbrx, &np3rbtx);
+	uart_build(&up3, UART2, PIOD, 25, 26, &np3rbrx, &np3rbtx, &np3stlr, &np3stlb);
 	uart_init(&up3, 81, UART_IS_PERIPHERAL_C);
 	
 	// RBs 4
 	rb_init(&np4rbrx);
 	rb_init(&np4rbtx);
 	// UP4 on UART3, RX 28 TX 30 on PIOD
-	uart_build(&up4, UART3, PIOD, 28, 30, &np4rbrx, &np4rbtx);
+	uart_build(&up4, UART3, PIOD, 28, 30, &np4rbrx, &np4rbtx, &np4stlr, &np4stlb);
 	uart_init(&up4, 81, UART_IS_PERIPHERAL_A);
 	
 	// RBs 5
 	rb_init(&np5rbrx);
 	rb_init(&np5rbtx);
 	// UP5 on UART4, RX 18 TX 19 on PIOD
-	uart_build(&up5, UART4, PIOD, 18, 19, &np5rbrx, &np5rbtx);
+	uart_build(&up5, UART4, PIOD, 18, 19, &np5rbrx, &np5rbtx, &np5stlr, &np5stlb);
 	uart_init(&up5, 81, UART_IS_PERIPHERAL_C);
 }
 
@@ -154,13 +157,14 @@ int main (void){
 	lightsetup();
 	initports();
 	
-	apaport_build(&apap1, 1, &up1, &np1stlr, &np1stlb);
+	apaport_build(&apap1, 1, &up1, ups[0], 5, &np1stlr, &np1stlg, &np1stlb);
 	
 	apaport_reset(&apap1);
 	
 	while(1){
-		lightstoggle();
+		//lightstoggle();
 		apaport_scan(&apap1, 2);
+		pin_toggle(&np5stlg);
 		/*
 		uint8_t hello[] = {'h', 'e', 'l', 'l', 'o'};
 		for(int i = 0; i < 5; i ++){
diff --git a/embedded/mkrouter-v04/mkrouter-v04/src/ringbuffer.c b/embedded/mkrouter-v04/mkrouter-v04/src/ringbuffer.c
index e2e5e6a..fb87a8c 100644
--- a/embedded/mkrouter-v04/mkrouter-v04/src/ringbuffer.c
+++ b/embedded/mkrouter-v04/mkrouter-v04/src/ringbuffer.c
@@ -53,14 +53,11 @@ uint8_t rb_putdata(ringbuffer_t *rb, uint8_t *data, uint8_t size){
 		return 0;
 	} else {
 		*/
-		uint8_t i = 0;
-		while(i < size){
-			// TODO: check if this is interrupt safe?
-			// should be... 
-			rb_putchar(rb, data[i]);
-			i ++;
-		}
-		return 1;
+	for(int i = 0; i < size; i ++){
+		rb_putchar(rb, data[i]);
+	}
+	
+	return 1;
 	//}
 }
 
diff --git a/embedded/mkrouter-v04/mkrouter-v04/src/uartport.c b/embedded/mkrouter-v04/mkrouter-v04/src/uartport.c
index 542fa7e..0034c45 100644
--- a/embedded/mkrouter-v04/mkrouter-v04/src/uartport.c
+++ b/embedded/mkrouter-v04/mkrouter-v04/src/uartport.c
@@ -7,7 +7,7 @@
 
 #include "uartport.h"
 
-void uart_build(uartport_t *uart, Uart *com, Pio *port, uint32_t pinrx, uint32_t pintx, ringbuffer_t *rbrx, ringbuffer_t *rbtx){	
+void uart_build(uartport_t *uart, Uart *com, Pio *port, uint32_t pinrx, uint32_t pintx, ringbuffer_t *rbrx, ringbuffer_t *rbtx, pin_t *stlr, pin_t *stlb){	
 	uart->com = com;
 	uart->port = port;
 	
@@ -16,6 +16,9 @@ void uart_build(uartport_t *uart, Uart *com, Pio *port, uint32_t pinrx, uint32_t
 	uart->pintx = pintx;
 	uart->pintx_bm = 1 << pintx;
 	
+	uart->stlr = stlr;
+	uart->stlb = stlb;
+	
 	uart->rbrx = rbrx;
 	uart->rbtx = rbtx;
 }
@@ -46,6 +49,9 @@ void uart_init(uartport_t *uart, uint32_t baud, uint32_t abcdsr){
 	
 	rb_reset(uart->rbrx);
 	rb_reset(uart->rbtx);
+	
+	pin_set(uart->stlr);
+	pin_set(uart->stlb);
 }
 
 void uart_sendchar_polled(uartport_t *uart, uint8_t data){
@@ -73,13 +79,16 @@ void uart_handler(uartport_t *uart){
 }
 
 void uart_rxhandler(uartport_t *uart){
+	pin_clear(uart->stlr); // set with whatever reads it (apa)
 	rb_putchar(uart->rbrx, uart->com->UART_RHR);
 }
 
 void uart_txhandler(uartport_t *uart){
 	if(!rb_empty(uart->rbtx)){
+		pin_clear(uart->stlb);
 		uart->com->UART_THR = rb_get(uart->rbtx); // transmit if non-empty
 	} else {
+		pin_set(uart->stlb);
 		uart->com->UART_IDR = UART_IDR_TXRDY; // or turn this interrupt off
 	}
 }
\ No newline at end of file
diff --git a/embedded/mkrouter-v04/mkrouter-v04/src/uartport.h b/embedded/mkrouter-v04/mkrouter-v04/src/uartport.h
index e1b38c0..9a16328 100644
--- a/embedded/mkrouter-v04/mkrouter-v04/src/uartport.h
+++ b/embedded/mkrouter-v04/mkrouter-v04/src/uartport.h
@@ -11,6 +11,7 @@
 
 #include <asf.h>
 #include "ringbuffer.h"
+#include "pin.h"
 
 #define UART_IS_PERIPHERAL_A 0x00
 #define UART_IS_PERIPHERAL_B 0x01
@@ -26,11 +27,14 @@ typedef struct{
 	uint32_t pintx;
 	uint32_t pintx_bm;
 	
+	pin_t *stlr;
+	pin_t *stlb;
+	
 	ringbuffer_t *rbrx;
 	ringbuffer_t *rbtx;
 }uartport_t;
 
-void uart_build(uartport_t *uart, Uart *com, Pio *port, uint32_t pinrx, uint32_t pintx, ringbuffer_t *rbrx, ringbuffer_t *rbtx);
+void uart_build(uartport_t *uart, Uart *com, Pio *port, uint32_t pinrx, uint32_t pintx, ringbuffer_t *rbrx, ringbuffer_t *rbtx, pin_t *stlr, pin_t *stlb);
 
 void uart_init(uartport_t *uart, uint32_t baud, uint32_t abcdsr);
 
-- 
GitLab