2018-12-01 17:30:09 +00:00
|
|
|
#ifndef _PROTOCOL_H_
|
|
|
|
#define _PROTOCOL_H_
|
|
|
|
|
2018-12-02 20:43:59 +00:00
|
|
|
#include <Wire.h>
|
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
#define DISCOVER_TTL 5000
|
|
|
|
#define SENDERID 42
|
2018-12-11 12:33:51 +00:00
|
|
|
#define MESSAGE_TTL 3
|
2018-12-01 17:30:09 +00:00
|
|
|
|
2018-12-11 11:42:34 +00:00
|
|
|
#define OPCODE_DISCOVER 0
|
|
|
|
#define OPCODE_MESSAGE 1
|
|
|
|
|
2018-12-01 17:30:09 +00:00
|
|
|
// discover request (c.f. class node)
|
2018-12-11 11:42:34 +00:00
|
|
|
#define DISCOVER_SIZE sizeof(uint8_t)*3
|
2018-12-01 17:30:09 +00:00
|
|
|
struct discover {
|
|
|
|
uint8_t opcode; // opcode = 0
|
|
|
|
uint8_t wave; // id de la wave
|
|
|
|
uint8_t dist; // current node's distance
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-12-03 14:29:56 +00:00
|
|
|
#define MESSAGE_MIN_SIZE sizeof(uint8_t)*5
|
2018-12-11 11:42:34 +00:00
|
|
|
#define MESSAGE_MAX_SIZE (5 + 255) * sizeof(uint8_t)
|
2018-12-01 17:36:02 +00:00
|
|
|
struct message {
|
|
|
|
uint8_t opcode; // opcode = 1
|
2018-12-11 11:42:34 +00:00
|
|
|
uint8_t sender; // sender id
|
2018-12-01 17:36:02 +00:00
|
|
|
uint8_t dist; // distance of the last sender
|
|
|
|
uint8_t ttl; // time to live default = 10
|
|
|
|
uint8_t size; // size of message in bytes
|
2018-12-03 14:29:56 +00:00
|
|
|
uint8_t *data; // actual message
|
2018-12-01 17:30:09 +00:00
|
|
|
};
|
|
|
|
|
2018-12-02 20:43:59 +00:00
|
|
|
#define PROTO_SIZE MESSAGE_MAX_SIZE
|
2018-12-01 17:30:09 +00:00
|
|
|
|
2018-12-11 13:45:23 +00:00
|
|
|
|
|
|
|
class Node{
|
|
|
|
private:
|
|
|
|
uint8_t wave = 0;
|
|
|
|
uint8_t dist = 255;
|
|
|
|
|
|
|
|
public:
|
|
|
|
getWave(){ return wave; }
|
|
|
|
getDist(){ return dist; }
|
|
|
|
|
|
|
|
bool update(uint8_t new_dist, uint8_t new_wave){
|
|
|
|
// ignore if already setup for this wave
|
|
|
|
if( new_wave == wave )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// update with new wave
|
|
|
|
//
|
|
|
|
// Note:
|
|
|
|
// - We consider that we will receive lowest dist(ance)
|
|
|
|
// in the first receive of each wave because of the XBee stack
|
|
|
|
wave = new_wave;
|
|
|
|
dist = new_dist+1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-01 17:30:09 +00:00
|
|
|
#endif
|