From 7c7548c66012b6ca62f5c6afe057991e42345b0e Mon Sep 17 00:00:00 2001 From: Douglas Kogut <douglaskogut@dhcp-18-189-6-139.dyn.mit.edu> Date: Thu, 7 Dec 2017 13:58:51 -0500 Subject: [PATCH] packet handling --- .../atsams70-tinyrouter/src/node.h | 5 + .../atsams70-tinyrouter/src/packet.h | 8 +- .../atsams70-tinyrouter/src/packet_handling.c | 124 ++++++++++++++++++ 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h create mode 100644 embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h new file mode 100644 index 0000000..b40c692 --- /dev/null +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h @@ -0,0 +1,5 @@ +typedef struct { + uint8_t portBufferSizes[4]; + uint16_t LUT[][4]; // TODO: fix representation + uint16_t myAddress; +} node_t; diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h index 281a96a..63c9661 100644 --- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h @@ -3,7 +3,7 @@ * * Created: 12/5/2017 7:31:53 PM * Author: Jake - */ + */ #ifndef PACKET_H_ @@ -17,12 +17,14 @@ typedef struct{ uint16_t source; uint8_t hopcount; uint8_t size; - + uint8_t counter; }packet_t; +enum packetType {STANDARD, ACK, STANDARD_FLOOD, ACK_FLOOD, BUFFER_UPDATE}; + packet_t packet_new(void); void packet_clean(packet_t *packet); -#endif /* PACKET_H_ */ \ No newline at end of file +#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 new file mode 100644 index 0000000..4e2ed3c --- /dev/null +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c @@ -0,0 +1,124 @@ +#include "packet.h" +#include "node.h" + +int parse_type(packet_t* p) { + return p->raw[0]; +} + +void update_LUT(node_t* n, uint16_t src, uint8_t hopCount, uint8_t port) { + n->LUT[src][port] = n->LUT[src][port] > hopCount ? hopCount : n->LUT[src][port]; +} + +void send_packet(packet_t* p, uint8_t port) { + +} + +void broadcast_packet(packet_t* p, uint8_t exclude) { + if (exclude != 0) send_packet(p, 0); + if (exclude != 1) send_packet(p, 1); + if (exclude != 2) send_packet(p, 2); + if (exclude != 3) send_packet(p, 3); +} + +packet_t turn_to_standard_flood(node_t n, packet_t* p) { + packet_t newPacket = {.raw = p->raw, .destination = p->destination, .source = n->myAddress, + .hopcount = p->hopcount, .size = p->size, .counter = p->counter}; + return newPacket; +} + +void handle_packet(node_t* n, packet_t* p, uint8_t port) { + if (parse_type(p) != BUFFER_UPDATE) { + update_LUT(n, p->source, p->hopcount, port); + } + switch parse_type(p) { + case STANDARD: + if (p->destination == n.myAddress) { + //process + //reply + } else { + p->hopcount++; + if (n->LUT[p->destination]) { + int bestPort = 0; + int bestHopCount = n->LUT[p->destination][0]; + for (int i = 0; i < 4; i++) { + if (n->LUT[p->destination][i] < bestHopCount) { + bestPort = i; + bestHopCount = n->LUT[p->destination][i]; + } + } + send_packet(p, bestPort); + } else { + broadcast_packet(turn_to_standard_flood(n, p)); + } + } + break; + case ACK: + if (p->destination == n.myAddress) { + //process + } else { + p->hopcount++; + if (n->LUT[p->destination]) { + int bestPort = 0; + int bestHopCount = n->LUT[p->destination][0]; + for (int i = 0; i < 4; i++) { + if (n->LUT[p->destination][i] < bestHopCount) { + bestPort = i; + bestHopCount = n->LUT[p->destination][i]; + } + } + send_packet(p, bestPort); + } else { + broadcast_packet(turn_to_standard_flood(n, p)); + } + } + break; + case STANDARD_FLOOD: + n->LUT[p->destination][port] = 255; + if (p->destination == n.myAddress) { + //process + //reply + } else { + p->hopcount++; + if (n->LUT[p->destination]) { + int bestPort = 0; + int bestHopCount = n->LUT[p->destination][0]; + for (int i = 0; i < 4; i++) { + if (n->LUT[p->destination][i] < bestHopCount) { + bestPort = i; + bestHopCount = n->LUT[p->destination][i]; + } + } + send_packet(p, bestPort); + } else { + broadcast_packet(turn_to_standard_flood(n, p), port); + } + } + break; + case ACK_FLOOD: + n->LUT[p->destination][port] = 255; + if (p->destination == n.myAddress) { + //process + } else { + p->hopcount++; + if (n->LUT[p->destination]) { + int bestPort = 0; + int bestHopCount = n->LUT[p->destination][0]; + for (int i = 0; i < 4; i++) { + if (n->LUT[p->destination][i] < bestHopCount) { + bestPort = i; + bestHopCount = n->LUT[p->destination][i]; + } + } + send_packet(p, bestPort); + } else { + broadcast_packet(turn_to_standard_flood(n, p), port); + } + } + break; + case BUFFER_UPDATE: + n->portBufferSizes[port] = p->raw[0]; + break; + default: + // not possible + } +} -- GitLab