share libs with symlinks | fix + finish xbee_wrapper and packet | update mains

This commit is contained in:
Adrien Marquès 2018-12-03 10:54:53 +01:00
parent 7c65091230
commit 53ac51febd
15 changed files with 345 additions and 225 deletions

View File

@ -2,7 +2,6 @@
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
#include "packet.h" // Packet #include "packet.h" // Packet
#include "xbee_wrapper.h" // WBeeWrapper #include "xbee_wrapper.h" // WBeeWrapper
#include "protocol.h"
// Peripherals // Peripherals
LiquidCrystal_I2C screen(0x27, 16, 2); LiquidCrystal_I2C screen(0x27, 16, 2);
@ -11,9 +10,8 @@ XBeeWrapper xbee = XBeeWrapper();
// ACTUAL DATA // ACTUAL DATA
uint8_t wave_id = 255; // last wave uint8_t wave_id = 255; // last wave
uint8_t dist = 255; // actual distance uint8_t dist = 255; // actual distance
struct discover dsc = {0,0,0}; Packet recv;
struct message msg = {1, dist, 0, 0, {}}; Packet send;
Packet pkt;
void setup() { void setup() {
Serial.begin(38400); Serial.begin(38400);
@ -28,26 +26,55 @@ void setup() {
void loop() { void loop() {
// 1. ignore if no received data // 1. ignore if no received data
if( xbee.receive(pkt) == XBWRCV_OK ){ if( xbee.receive(recv) == XBWRECV_OK ){
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());
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");
}
}
// manage packet // manage packet
} }
// xbee.readPacket();
// if( xbee.getResponse().isAvailable() ){
// if( xbee.getResponse().getApiId() == ZB_RX_RESPONSE ){
// // got a zb rx packet
// // now extract response
// xbee.getResponse().getZBRxResponse(res);
// // get data type (byte 0)
// uint8_t type = res.getData(0)
// res.getBytes()
// analogWrite(dataLed, res.getData(0));
// }
// }
} }

View File

@ -1,65 +0,0 @@
#include "packet.h"
/* PUBLIC
----------------------------------------*/
// builds a packet from raw data and returns the error code
uint8_t Packet::read(uint8_t buf[], uint8_t size){
// 1. fail on invalid size
if( size < 1 ) return PKTREAD_EMPTY;
if( size > PROTO_SIZE ) return PKTREAD_OVERFLOW;
// 2. extract packet type
type = buf[0] == 0;
// 3. extract depending on type
return type ? read_discover(buf, size) : read_message(buf, size);
};
// writes the binary representation of the packet
uint8_t Packet::write(uint8_t buf[], uint8_t size){
/* TODO */
return 0;
};
/* PRIVATE
----------------------------------------*/
uint8_t Packet::read_discover(uint8_t buf[], uint8_t size){
// 1. fail on invalid size
if( size != DISCOVER_SIZE )
return PKTREAD_INVALID_DISCOVER_FORMAT;
// 2. extract fixed
dsc.opcode = buf[0];
dsc.wave = buf[1];
dsc.dist = buf[2];
return PKTREAD_OK;
};
uint8_t Packet::read_message(uint8_t buf[], uint8_t size){
// 1. fail on invalid size
if( size < MESSAGE_MIN_SIZE || size > MESSAGE_MAX_SIZE )
return PKTREAD_INVALID_MESSAGE_FORMAT;
// 2. extract fixed
msg.opcode = buf[0];
msg.dist = buf[1];
msg.ttl = buf[2];
msg.size = buf[3];
// 3. check message size
if( size - 4 - 1 != msg.size )
return PKTREAD_INVALID_MESSAGE_FORMAT;
// 4. extract message
strncpy(
(char*) msg.data,
(char*)buf+4,
msg.size
);
return PKTREAD_OK;
};

1
node/main/packet.cpp Symbolic link
View File

@ -0,0 +1 @@
../../packet.cpp

View File

@ -1,37 +0,0 @@
#ifndef _PACKET_H_
#define _PACKET_H_
#include "protocol.h"
#include <Wire.h>
#define PKTREAD_OK 0
#define PKTREAD_EMPTY 1
#define PKTREAD_OVERFLOW 2
#define PKTREAD_INVALID_DISCOVER_FORMAT 3
#define PKTREAD_INVALID_MESSAGE_FORMAT 4
class Packet {
private:
// 1. common
uint8_t buf[];
uint8_t bufsize;
bool type; // true = discover ; false = message
// 2. wrappers
struct discover dsc;
struct message msg;
// 3. helpers
uint8_t read_discover(uint8_t buf[], uint8_t size);
uint8_t read_message(uint8_t buf[], uint8_t size);
public:
// builds a packet from raw data and returns the status code
uint8_t read(uint8_t buf[], uint8_t size);
// writes the binary representation of the packet
uint8_t write(uint8_t buf[], uint8_t size);
};
#endif

1
node/main/packet.h Symbolic link
View File

@ -0,0 +1 @@
../../packet.h

View File

@ -1,30 +0,0 @@
#include "xbee_wrapper.h"
XBeeWrapper::XBeeWrapper(){ xbee = XBee(); };
void XBeeWrapper::begin(unsigned long baud){
xbee.setSerial(Serial1);
Serial1.begin(baud);
};
uint8_t XBeeWrapper::receive(Packet& pkt){
xbee.readPacket();
if( !xbee.getResponse().isAvailable() )
return XBWRCV_NONE;
if( xbee.getResponse().getApiId() != ZB_RX_RESPONSE )
return XBWRCV_NONE;
Rx64Response res;
xbee.getResponse().getZBRxResponse(res);
// extract data and propagate error
if( !pkt.read(res.getData(), res.getDataLength()) )
return XBWRCV_ERROR;
return XBWRCV_OK;
};

1
node/main/xbee_wrapper.cpp Symbolic link
View File

@ -0,0 +1 @@
../../xbee_wrapper.cpp

View File

@ -1,31 +0,0 @@
#ifndef _WBEE_WRAPPER_H_
#define _WBEE_WRAPPER_H_
#include <Wire.h>
#include <XBee.h>
#include "protocol.h"
#include "packet.h"
#define XBWRCV_OK 0
#define XBWRCV_NONE 1
#define XBWRCV_ERROR 2
class XBeeWrapper{
private:
XBee xbee;
public:
XBeeWrapper();
// initialises the XBee interface
void begin(unsigned long baud);
// tries to extract a received packet
uint8_t receive(Packet& pkt);
};
#endif

1
node/main/xbee_wrapper.h Symbolic link
View File

@ -0,0 +1 @@
../../xbee_wrapper.h

100
packet.cpp Normal file
View File

@ -0,0 +1,100 @@
#include "packet.h"
/* PUBLIC
----------------------------------------*/
// builds a packet from raw data and returns the error code
uint8_t Packet::read(uint8_t buf[], size_t size){
// 1. fail on invalid size
if( size < 1 ) return PKTREAD_EMPTY;
if( size > PROTO_SIZE ) return PKTREAD_OVERFLOW;
// 2. extract packet type
opcode = buf[0];
// 3. extract depending on type
return (opcode == 0) ? read_discover(buf, size) : read_message(buf, size);
};
// writes the binary representation of the packet
size_t Packet::write(uint8_t buf[]){
if( opcode == 0 )
return write_discover(buf);
return write_message(buf);
};
/* GETTERS / SETTERS
----------------------------------------*/
uint8_t Packet::getOpcode() { return opcode; }
void Packet::setOpcode(uint8_t value) { opcode = value; dsc.opcode = value; msg.opcode = value; }
uint8_t Packet::getWave() { return dsc.wave; }
void Packet::setWave(uint8_t value) { dsc.wave = 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; }
uint8_t Packet::getTTL() { return msg.ttl; }
void Packet::setTTL(uint8_t value) { msg.ttl = value; }
uint8_t Packet::getSize() { return msg.size; }
uint8_t* Packet::getData() { return msg.data; }
void Packet::setData(uint8_t *buffer, uint8_t size) { memcpy(msg.data, buffer, size); msg.size = size; }
/* PRIVATE
----------------------------------------*/
uint8_t Packet::read_discover(uint8_t buf[], size_t size){
// 1. fail on invalid size
if( size != DISCOVER_SIZE )
return PKTREAD_INVALID_DISCOVER_FORMAT;
dsc.opcode = buf[0];
dsc.wave = buf[1];
dsc.dist = buf[2];
return PKTREAD_OK;
};
uint8_t Packet::read_message(uint8_t buf[], size_t size){
// 1. fail on invalid size
if( size < MESSAGE_MIN_SIZE || size > MESSAGE_MAX_SIZE )
return PKTREAD_INVALID_MESSAGE_FORMAT;
msg.opcode = buf[0];
msg.dist = buf[1];
msg.ttl = buf[2];
msg.size = buf[3];
// 3. check message size
if( size - 4 - 1 != msg.size )
return PKTREAD_INVALID_MESSAGE_FORMAT;
// 4. extract message
memcpy(msg.data, buf+4, msg.size);
return PKTREAD_OK;
};
size_t Packet::write_discover(uint8_t buf[]){
buf[0] = dsc.opcode;
buf[1] = dsc.wave;
buf[2] = dsc.dist;
return 3;
};
size_t Packet::write_message(uint8_t buf[]){
buf[0] = msg.opcode;
buf[1] = msg.dist;
buf[2] = msg.ttl;
buf[3] = msg.size;
memcpy( buf+4, msg.data, msg.size );
return 4 + msg.size;
};

55
packet.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef _PACKET_H_
#define _PACKET_H_
#include "protocol.h"
#include <Wire.h>
#define PKTREAD_OK 0
#define PKTREAD_EMPTY 1
#define PKTREAD_OVERFLOW 2
#define PKTREAD_INVALID_DISCOVER_FORMAT 3
#define PKTREAD_INVALID_MESSAGE_FORMAT 4
class Packet {
private:
// 1. common
uint8_t opcode; // 0 = discover ; 1 = message
// 2. wrappers
struct discover dsc;
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[]);
public:
// builds a packet from raw data and returns the status code
uint8_t read(uint8_t buf[], size_t size);
// writes the binary representation of the packet returns the size
size_t write(uint8_t buf[]);
// GETTERS / SETTERS
uint8_t getOpcode();
void setOpcode(uint8_t value);
uint8_t getWave();
void setWave(uint8_t value);
uint8_t getDist();
void setDist(uint8_t value);
uint8_t getTTL();
void setTTL(uint8_t value);
uint8_t getSize();
uint8_t* getData();
void setData(uint8_t *buffer, uint8_t size);
};
#endif

View File

@ -6,7 +6,7 @@
#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 + 1 #define DISCOVER_SIZE sizeof(uint8_t)*3
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
@ -14,14 +14,14 @@
}; };
#define MESSAGE_MIN_SIZE sizeof(uint8_t)*4 + 1 #define MESSAGE_MIN_SIZE sizeof(uint8_t)*4
#define MESSAGE_MAX_SIZE sizeof(uint8_t)*4+sizeof(uint8_t)*255 + 1 #define MESSAGE_MAX_SIZE sizeof(uint8_t)*4+sizeof(uint8_t)*255
struct message { struct message {
uint8_t opcode; // opcode = 1 uint8_t opcode; // opcode = 1
uint8_t dist; // distance of the last sender uint8_t dist; // distance of the last sender
uint8_t ttl; // time to live default = 10 uint8_t ttl; // time to live default = 10
uint8_t size; // size of message in bytes uint8_t size; // size of message in bytes
uint8_t data[]; // actual message uint8_t *data; // actual message
}; };

View File

@ -1,17 +1,17 @@
#include <Wire.h> #include <Wire.h>
#include <XBee.h>
#include "protocol.h"
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
#include "xbee_wrapper.h"
#include "packet.h"
#define WAVE_TIMEOUT 5000 #define WAVE_TIMEOUT 5000
// Peripherals // Peripherals
LiquidCrystal_I2C screen(0x27, 16, 2); LiquidCrystal_I2C screen(0x27, 16, 2);
XBee xbee = XBee(); XBeeWrapper xbee = XBeeWrapper();
// ACTUAL DATA // ACTUAL DATA
uint8_t wave_id = 250; uint8_t wave_id = 250;
struct discover dsc = {0,0,0}; Packet send;
void setup() { void setup() {
Serial.begin(38400); Serial.begin(38400);
@ -20,39 +20,25 @@ void setup() {
screen.begin(); screen.begin();
screen.backlight(); screen.backlight();
xbee.setSerial(Serial1); xbee.begin(38400);
Serial1.begin(38400);
// xbee.begin(38400);
} }
void loop() { void loop() {
delay(WAVE_TIMEOUT); delay(WAVE_TIMEOUT);
// increment wave id (will overflow from 255 to 0)
dsc.wave = ++wave_id; // set wave id
screen.clear(); screen.clear();
screen.print("+ wave"); screen.print("+ wave");
screen.print(dsc.wave); screen.print(wave_id+1);
uint8_t payload[3] = {dsc.opcode, dsc.wave, dsc.dist}; // increment wave id (will overflow from 255 to 0)
send.setOpcode(0);
send.setWave(++wave_id);
send.setDist(0);
XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x0000FFFF); if( xbee.broadcast(send) == XBWSEND_OK )
Tx64Request tx = Tx64Request(addr64, payload, sizeof(payload)); Serial.println("sent");
xbee.send(tx); else
Serial.println("failed");
/* Check the status of our sent payload (not mandatory)
TxStatusResponse txStatus = TxStatusResponse();
xbee.readPacket();
if( xbee.getResponse().isAvailable() ){
if( xbee.getResponse().getApiId() == TX_STATUS_RESPONSE ){
xbee.getResponse().getTxStatusResponse(txStatus);
txStatus.isSuccess() && Serial.println(" ... sent") || Serial.println(" ... failed");
}
}
*/
} }

1
well/main/packet.cpp Symbolic link
View File

@ -0,0 +1 @@
../../packet.cpp

1
well/main/packet.h Symbolic link
View File

@ -0,0 +1 @@
../../packet.h

1
well/main/xbee_wrapper.cpp Symbolic link
View File

@ -0,0 +1 @@
../../xbee_wrapper.cpp

1
well/main/xbee_wrapper.h Symbolic link
View File

@ -0,0 +1 @@
../../xbee_wrapper.h

69
xbee_wrapper.cpp Normal file
View File

@ -0,0 +1,69 @@
#include "xbee_wrapper.h"
XBeeWrapper::XBeeWrapper(){ xbee = XBee(); };
void XBeeWrapper::begin(unsigned long baud){
xbee.setSerial(Serial1);
Serial1.begin(baud);
};
uint8_t XBeeWrapper::receive(Packet& pkt){
xbee.readPacket();
if( !xbee.getResponse().isAvailable() )
return XBWRECV_NONE;
uint8_t apiId = xbee.getResponse().getApiId();
if( apiId != RX_16_RESPONSE && apiId != RX_64_RESPONSE )
return XBWRECV_NONE;
if( apiId == RX_16_RESPONSE ){
Rx16Response res;
xbee.getResponse().getRx16Response(res);
// extract data and propagate error
if( !pkt.read(res.getData(), res.getDataLength()) )
return XBWRECV_ERROR;
} else {
Rx64Response res;
xbee.getResponse().getRx64Response(res);
// extract data and propagate error
if( !pkt.read(res.getData(), res.getDataLength()) )
return XBWRECV_ERROR;
}
return XBWRECV_OK;
};
uint8_t XBeeWrapper::broadcast(Packet& pkt){
// build broadcast addr
XBeeAddress64 bcast = XBeeAddress64(0x00000000, 0x0000FFFF);
// build payload from packet
uint8_t payload[PROTO_SIZE];
size_t payload_size = pkt.write(payload);
// send
Tx64Request tx = Tx64Request(bcast, payload, payload_size);
xbee.send(tx);
// // check status
// TxStatusResponse txStatus = TxStatusResponse();
// xbee.readPacket();
// if( xbee.getResponse().isAvailable() ){
// if( xbee.getResponse().getApiId() == TX_STATUS_RESPONSE ){
// xbee.getResponse().getTxStatusResponse(txStatus);
// return txStatus.isSuccess() ? XBWSEND_OK : XBWSEND_ERROR;
// }
// }
return XBWSEND_OK;
};

38
xbee_wrapper.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef _WBEE_WRAPPER_H_
#define _WBEE_WRAPPER_H_
#include <Wire.h>
#include <XBee.h>
#include "protocol.h"
#include "packet.h"
#define XBWRECV_OK 0
#define XBWRECV_NONE 1
#define XBWRECV_ERROR 2
#define XBWSEND_OK 0
#define XBWSEND_ERROR 1
#define XBWSEND_UNKNOWN 2
class XBeeWrapper{
private:
XBee xbee;
public:
XBeeWrapper();
// initialises the XBee interface
void begin(unsigned long baud);
// tries to extract a received packet
uint8_t receive(Packet& pkt);
// tries to broadcast a packet
uint8_t broadcast(Packet& pkt);
};
#endif