update protocol add 'discover.sender'
This commit is contained in:
parent
53ac51febd
commit
d73ccb187d
|
@ -25,41 +25,59 @@ void setup() {
|
|||
|
||||
void loop() {
|
||||
|
||||
delay(500);
|
||||
|
||||
// 1. ignore if no received data
|
||||
if( xbee.receive(recv) == XBWRECV_OK ){
|
||||
if( xbee.receive(recv) != XBWRECV_OK )
|
||||
return;
|
||||
|
||||
|
||||
uint8_t opcode = recv.getOpcode();
|
||||
Serial.print("+ opcode = ");
|
||||
Serial.println(opcode);
|
||||
|
||||
// DISCOVER REQUEST
|
||||
if( opcode == 0 ) {
|
||||
Serial.println("+ discover request");
|
||||
Serial.print(" + wave = ");
|
||||
Serial.println(recv.getWave());
|
||||
Serial.print(" + dist = ");
|
||||
Serial.println(recv.getDist());
|
||||
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(wave_id);
|
||||
Serial.print(" + dist = ");
|
||||
Serial.println(dist);
|
||||
|
||||
send.setOpcode(0);
|
||||
send.setWave(wave_id);
|
||||
send.setDist(dist);
|
||||
send.setSender(42);
|
||||
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
|
||||
} else {
|
||||
|
||||
Serial.println("+ message data");
|
||||
Serial.print(" + ttl = ");
|
||||
Serial.println(recv.getTTL());
|
||||
Serial.print(" + dist = ");
|
||||
Serial.println(recv.getDist());
|
||||
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 ){
|
||||
|
@ -68,13 +86,15 @@ void loop() {
|
|||
send.setDist(dist);
|
||||
send.setData(recv.getData(), recv.getSize());
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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[]){
|
||||
|
|
3
packet.h
3
packet.h
|
@ -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);
|
||||
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue