diff --git a/embedded/README.md b/embedded/README.md index 6b9a314f185ff3e730923685261cfc015ce423e4..c5b0dc12e3e0aaaef620b8b594d2e32084235529 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 00140d2d4b4b672e9d1e37dd61f1a1ded81e974f..8be3d33a357edb7637db7ef78d99f1ea2d220a46 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 af67d53d7ceaed84c7d79f89821c79490fcdb668..b54972c8133e06958514041e1b143108b8d2d68a 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 e93edb6121e75d5f009bf130861bdf3cf0c6057e..94e88c3f99a4ed783c22987dc608a456f84b4a78 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 0000000000000000000000000000000000000000..ca790042879188a248843b85bbf8766d88557fe6 --- /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 0000000000000000000000000000000000000000..a8efb21df8b0eea31f7ebc9df3722a43758886cf --- /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 7f8f5f63916f6cd4f331275585e1b721392fa545..c5bc8dbbea054cdce094168bb7ae8047a836dda3 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 b1bdda1c499e1db74e58c8f1b539914badfa4c8b..53b7c7fb7bdc008c6f5d3dc132860360cf294b67 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 c368071f9fa21126b57da23d2d6e33fe8564377b..c1a4fd6cc5a1f7470c1418f93edb6f875961ab3c 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 edaf347ca9b6be72430627a09704b2b150fa2222..901b16ce003a1f128fe691a4a4246ba3ba7b3e7c 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 927725b5c105a43ce24b9890ce4738515eb21dab..ab1af3f7b8b2ef38730c555b63416af5ab320209 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 8fa006582bce7ad37874cefafaebbd2491d6f4b3..9cee2851ee54970d25796f737918c20a7e9d8876 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 386edc1252a4b53f56e04c8892485b6a9a98aa24..3e300a686dc22001fe11fb12d59496a722b6428f 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) {