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,41 +25,59 @@ void setup() {
void loop() { void loop() {
delay(500);
// 1. ignore if no received data // 1. ignore if no received data
if( xbee.receive(recv) == XBWRECV_OK ){ if( xbee.receive(recv) != XBWRECV_OK )
return;
uint8_t opcode = recv.getOpcode(); uint8_t opcode = recv.getOpcode();
Serial.print("+ opcode = ");
Serial.println(opcode);
// DISCOVER REQUEST // DISCOVER REQUEST
if( opcode == 0 ) { if( opcode == 0 ) {
Serial.println("+ discover request"); Serial.print("+ discover(");
Serial.print(" + wave = "); Serial.print(recv.getSender());
Serial.println(recv.getWave()); Serial.print(", ");
Serial.print(" + dist = "); Serial.print(recv.getWave());
Serial.println(recv.getDist()); Serial.print(", ");
Serial.print(recv.getDist());
Serial.println(")");
if( recv.getWave() != wave_id ){ if( recv.getWave() != wave_id ){
wave_id = recv.getWave(); wave_id = recv.getWave();
dist = recv.getDist()+1; dist = recv.getDist()+1;
recv.setDist(dist); recv.setDist(dist);
Serial.print(" + wave = ");
Serial.println(wave_id);
Serial.print(" + dist = ");
Serial.println(dist);
send.setOpcode(0); send.setOpcode(0);
send.setWave(wave_id); send.setWave(wave_id);
send.setDist(dist); send.setDist(dist);
send.setSender(42);
xbee.broadcast(send); xbee.broadcast(send);
Serial.println(" + propagated"); Serial.print(" + send discover(");
Serial.print(send.getSender());
Serial.print(", ");
Serial.print(send.getWave());
Serial.print(", ");
Serial.print(send.getDist());
Serial.println(")");
} }
// MESSAGE DATA // MESSAGE DATA
} else { } else {
Serial.println("+ message data"); Serial.print("+ message(");
Serial.print(" + ttl = "); Serial.print(recv.getDist());
Serial.println(recv.getTTL()); Serial.print(", ");
Serial.print(" + dist = "); Serial.print(recv.getTTL());
Serial.println(recv.getDist()); Serial.print(", ");
Serial.print(recv.getSize());
Serial.println(")");
// propagation // propagation
if( recv.getTTL() > 0 && recv.getDist() <= dist ){ if( recv.getTTL() > 0 && recv.getDist() <= dist ){
@ -68,13 +86,15 @@ void loop() {
send.setDist(dist); send.setDist(dist);
send.setData(recv.getData(), recv.getSize()); send.setData(recv.getData(), recv.getSize());
xbee.broadcast(send); xbee.broadcast(send);
Serial.println(" + propagated"); 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; } uint8_t Packet::getWave() { return dsc.wave; }
void Packet::setWave(uint8_t value) { dsc.wave = value; } 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; } 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; } 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.opcode = buf[0];
dsc.wave = buf[1]; dsc.wave = buf[1];
dsc.dist = buf[2]; dsc.dist = buf[2];
dsc.sender = buf[3];
return PKTREAD_OK; return PKTREAD_OK;
}; };
@ -84,8 +88,9 @@ size_t Packet::write_discover(uint8_t buf[]){
buf[0] = dsc.opcode; buf[0] = dsc.opcode;
buf[1] = dsc.wave; buf[1] = dsc.wave;
buf[2] = dsc.dist; buf[2] = dsc.dist;
buf[3] = dsc.sender;
return 3; return 4;
}; };
size_t Packet::write_message(uint8_t buf[]){ size_t Packet::write_message(uint8_t buf[]){

View File

@ -36,6 +36,9 @@
uint8_t getOpcode(); uint8_t getOpcode();
void setOpcode(uint8_t value); void setOpcode(uint8_t value);
uint8_t getSender();
void setSender(uint8_t value);
uint8_t getWave(); uint8_t getWave();
void setWave(uint8_t value); void setWave(uint8_t value);

View File

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

View File

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