update protocol add 'discover.sender'

This commit is contained in:
Adrien Marquès 2018-12-03 11:24:32 +01:00
parent 53ac51febd
commit d73ccb187d
5 changed files with 78 additions and 48 deletions

View File

@ -25,56 +25,76 @@ void setup() {
void loop() {
delay(500);
// 1. ignore if no received data
if( xbee.receive(recv) == XBWRECV_OK ){
uint8_t opcode = recv.getOpcode();
Serial.print("+ opcode = ");
Serial.println(opcode);
if( xbee.receive(recv) != XBWRECV_OK )
return;
// DISCOVER REQUEST
if( opcode == 0 ) {
Serial.println("+ discover request");
uint8_t opcode = recv.getOpcode();
// DISCOVER REQUEST
if( opcode == 0 ) {
Serial.print("+ discover(");
Serial.print(recv.getSender());
Serial.print(", ");
Serial.print(recv.getWave());
Serial.print(", ");
Serial.print(recv.getDist());
Serial.println(")");
if( recv.getWave() != wave_id ){
wave_id = recv.getWave();
dist = recv.getDist()+1;
recv.setDist(dist);
Serial.print(" + wave = ");
Serial.println(recv.getWave());
Serial.println(wave_id);
Serial.print(" + dist = ");
Serial.println(recv.getDist());
if( recv.getWave() != wave_id ){
wave_id = recv.getWave();
dist = recv.getDist()+1;
recv.setDist(dist);
send.setOpcode(0);
send.setWave(wave_id);
send.setDist(dist);
xbee.broadcast(send);
Serial.println(" + propagated");
}
// MESSAGE DATA
} else {
Serial.println("+ message data");
Serial.print(" + ttl = ");
Serial.println(recv.getTTL());
Serial.print(" + dist = ");
Serial.println(recv.getDist());
// propagation
if( recv.getTTL() > 0 && recv.getDist() <= dist ){
send.setOpcode(1);
send.setTTL(recv.getTTL()-1);
send.setDist(dist);
send.setData(recv.getData(), recv.getSize());
xbee.broadcast(send);
Serial.println(" + propagated");
}
Serial.println(dist);
send.setOpcode(0);
send.setWave(wave_id);
send.setDist(dist);
send.setSender(42);
xbee.broadcast(send);
Serial.print(" + send discover(");
Serial.print(send.getSender());
Serial.print(", ");
Serial.print(send.getWave());
Serial.print(", ");
Serial.print(send.getDist());
Serial.println(")");
}
// MESSAGE DATA
} else {
Serial.print("+ message(");
Serial.print(recv.getDist());
Serial.print(", ");
Serial.print(recv.getTTL());
Serial.print(", ");
Serial.print(recv.getSize());
Serial.println(")");
// propagation
if( recv.getTTL() > 0 && recv.getDist() <= dist ){
send.setOpcode(1);
send.setTTL(recv.getTTL()-1);
send.setDist(dist);
send.setData(recv.getData(), recv.getSize());
xbee.broadcast(send);
Serial.print(" + send message(");
Serial.print(recv.getDist());
Serial.print(", ");
Serial.print(recv.getTTL());
Serial.print(", ");
Serial.print(recv.getSize());
Serial.println(")");
}
// manage packet
}
}

View File

@ -33,6 +33,9 @@ void Packet::setOpcode(uint8_t value) { opcode = value; dsc.opcode = value; msg.
uint8_t Packet::getWave() { return dsc.wave; }
void Packet::setWave(uint8_t value) { dsc.wave = value; }
uint8_t Packet::getSender() { return dsc.sender; }
void Packet::setSender(uint8_t value) { dsc.sender = value; }
uint8_t Packet::getDist() { return (opcode == 0) ? dsc.dist : msg.dist; }
void Packet::setDist(uint8_t value) { if(opcode == 0) dsc.dist = value; else msg.dist = value; }
@ -56,6 +59,7 @@ uint8_t Packet::read_discover(uint8_t buf[], size_t size){
dsc.opcode = buf[0];
dsc.wave = buf[1];
dsc.dist = buf[2];
dsc.sender = buf[3];
return PKTREAD_OK;
};
@ -84,8 +88,9 @@ size_t Packet::write_discover(uint8_t buf[]){
buf[0] = dsc.opcode;
buf[1] = dsc.wave;
buf[2] = dsc.dist;
buf[3] = dsc.sender;
return 3;
return 4;
};
size_t Packet::write_message(uint8_t buf[]){

View File

@ -20,10 +20,10 @@
struct message msg;
// 3. helpers
uint8_t read_discover(uint8_t buf[], size_t size);
uint8_t read_message(uint8_t buf[], size_t size);
size_t write_discover(uint8_t buf[]);
size_t write_message(uint8_t buf[]);
uint8_t read_discover(uint8_t buf[], size_t size);
uint8_t read_message(uint8_t buf[], size_t size);
size_t write_discover(uint8_t buf[]);
size_t write_message(uint8_t buf[]);
public:
// builds a packet from raw data and returns the status code
@ -36,6 +36,9 @@
uint8_t getOpcode();
void setOpcode(uint8_t value);
uint8_t getSender();
void setSender(uint8_t value);
uint8_t getWave();
void setWave(uint8_t value);

View File

@ -6,11 +6,12 @@
#define DISCOVER_TTL 10000
// discover request (c.f. class node)
#define DISCOVER_SIZE sizeof(uint8_t)*3
#define DISCOVER_SIZE sizeof(uint8_t)*4
struct discover {
uint8_t opcode; // opcode = 0
uint8_t wave; // id de la wave
uint8_t dist; // current node's distance
uint8_t sender; // sender id
};

View File

@ -35,6 +35,7 @@ void loop() {
send.setOpcode(0);
send.setWave(++wave_id);
send.setDist(0);
send.setSender(42);
if( xbee.broadcast(send) == XBWSEND_OK )
Serial.println("sent");