From 53ac51febd53edaf78311becb732070fd58751fe Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 3 Dec 2018 10:54:53 +0100 Subject: [PATCH] share libs with symlinks | fix + finish xbee_wrapper and packet | update mains --- node/main/main.ino | 73 ++++++++++++++++++--------- node/main/packet.cpp | 66 +----------------------- node/main/packet.h | 38 +------------- node/main/xbee_wrapper.cpp | 31 +----------- node/main/xbee_wrapper.h | 32 +----------- packet.cpp | 100 +++++++++++++++++++++++++++++++++++++ packet.h | 55 ++++++++++++++++++++ protocol.h | 8 +-- well/main/main.ino | 56 ++++++++------------- well/main/packet.cpp | 1 + well/main/packet.h | 1 + well/main/xbee_wrapper.cpp | 1 + well/main/xbee_wrapper.h | 1 + xbee_wrapper.cpp | 69 +++++++++++++++++++++++++ xbee_wrapper.h | 38 ++++++++++++++ 15 files changed, 345 insertions(+), 225 deletions(-) mode change 100644 => 120000 node/main/packet.cpp mode change 100644 => 120000 node/main/packet.h mode change 100644 => 120000 node/main/xbee_wrapper.cpp mode change 100644 => 120000 node/main/xbee_wrapper.h create mode 100644 packet.cpp create mode 100644 packet.h create mode 120000 well/main/packet.cpp create mode 120000 well/main/packet.h create mode 120000 well/main/xbee_wrapper.cpp create mode 120000 well/main/xbee_wrapper.h create mode 100644 xbee_wrapper.cpp create mode 100644 xbee_wrapper.h diff --git a/node/main/main.ino b/node/main/main.ino index 2478f58..fdfeae4 100644 --- a/node/main/main.ino +++ b/node/main/main.ino @@ -2,7 +2,6 @@ #include #include "packet.h" // Packet #include "xbee_wrapper.h" // WBeeWrapper -#include "protocol.h" // Peripherals LiquidCrystal_I2C screen(0x27, 16, 2); @@ -11,9 +10,8 @@ XBeeWrapper xbee = XBeeWrapper(); // ACTUAL DATA uint8_t wave_id = 255; // last wave uint8_t dist = 255; // actual distance -struct discover dsc = {0,0,0}; -struct message msg = {1, dist, 0, 0, {}}; -Packet pkt; +Packet recv; +Packet send; void setup() { Serial.begin(38400); @@ -28,26 +26,55 @@ void setup() { void loop() { // 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 } - // 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)); - // } - - // } } diff --git a/node/main/packet.cpp b/node/main/packet.cpp deleted file mode 100644 index 67869ba..0000000 --- a/node/main/packet.cpp +++ /dev/null @@ -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; -}; \ No newline at end of file diff --git a/node/main/packet.cpp b/node/main/packet.cpp new file mode 120000 index 0000000..fda818d --- /dev/null +++ b/node/main/packet.cpp @@ -0,0 +1 @@ +../../packet.cpp \ No newline at end of file diff --git a/node/main/packet.h b/node/main/packet.h deleted file mode 100644 index f15dfc1..0000000 --- a/node/main/packet.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _PACKET_H_ - #define _PACKET_H_ - - #include "protocol.h" - #include - - #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 \ No newline at end of file diff --git a/node/main/packet.h b/node/main/packet.h new file mode 120000 index 0000000..8f158fd --- /dev/null +++ b/node/main/packet.h @@ -0,0 +1 @@ +../../packet.h \ No newline at end of file diff --git a/node/main/xbee_wrapper.cpp b/node/main/xbee_wrapper.cpp deleted file mode 100644 index 471d5fa..0000000 --- a/node/main/xbee_wrapper.cpp +++ /dev/null @@ -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; -}; \ No newline at end of file diff --git a/node/main/xbee_wrapper.cpp b/node/main/xbee_wrapper.cpp new file mode 120000 index 0000000..431ce9f --- /dev/null +++ b/node/main/xbee_wrapper.cpp @@ -0,0 +1 @@ +../../xbee_wrapper.cpp \ No newline at end of file diff --git a/node/main/xbee_wrapper.h b/node/main/xbee_wrapper.h deleted file mode 100644 index c252c12..0000000 --- a/node/main/xbee_wrapper.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef _WBEE_WRAPPER_H_ - #define _WBEE_WRAPPER_H_ - - #include - #include - #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 \ No newline at end of file diff --git a/node/main/xbee_wrapper.h b/node/main/xbee_wrapper.h new file mode 120000 index 0000000..637624b --- /dev/null +++ b/node/main/xbee_wrapper.h @@ -0,0 +1 @@ +../../xbee_wrapper.h \ No newline at end of file diff --git a/packet.cpp b/packet.cpp new file mode 100644 index 0000000..2f125df --- /dev/null +++ b/packet.cpp @@ -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; +}; \ No newline at end of file diff --git a/packet.h b/packet.h new file mode 100644 index 0000000..c3dc120 --- /dev/null +++ b/packet.h @@ -0,0 +1,55 @@ +#ifndef _PACKET_H_ + #define _PACKET_H_ + + #include "protocol.h" + #include + + #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 \ No newline at end of file diff --git a/protocol.h b/protocol.h index 220e2fe..446e4e3 100644 --- a/protocol.h +++ b/protocol.h @@ -6,7 +6,7 @@ #define DISCOVER_TTL 10000 // discover request (c.f. class node) - #define DISCOVER_SIZE sizeof(uint8_t)*3 + 1 + #define DISCOVER_SIZE sizeof(uint8_t)*3 struct discover { uint8_t opcode; // opcode = 0 uint8_t wave; // id de la wave @@ -14,14 +14,14 @@ }; - #define MESSAGE_MIN_SIZE sizeof(uint8_t)*4 + 1 - #define MESSAGE_MAX_SIZE sizeof(uint8_t)*4+sizeof(uint8_t)*255 + 1 + #define MESSAGE_MIN_SIZE sizeof(uint8_t)*4 + #define MESSAGE_MAX_SIZE sizeof(uint8_t)*4+sizeof(uint8_t)*255 struct message { uint8_t opcode; // opcode = 1 uint8_t dist; // distance of the last sender uint8_t ttl; // time to live default = 10 uint8_t size; // size of message in bytes - uint8_t data[]; // actual message + uint8_t *data; // actual message }; diff --git a/well/main/main.ino b/well/main/main.ino index 6047087..6ace736 100644 --- a/well/main/main.ino +++ b/well/main/main.ino @@ -1,58 +1,44 @@ #include -#include -#include "protocol.h" #include +#include "xbee_wrapper.h" +#include "packet.h" #define WAVE_TIMEOUT 5000 // Peripherals LiquidCrystal_I2C screen(0x27, 16, 2); -XBee xbee = XBee(); +XBeeWrapper xbee = XBeeWrapper(); // ACTUAL DATA uint8_t wave_id = 250; -struct discover dsc = {0,0,0}; +Packet send; void setup() { - Serial.begin(38400); - Serial.println("+ ready"); + Serial.begin(38400); + Serial.println("+ ready"); - screen.begin(); - screen.backlight(); + screen.begin(); + screen.backlight(); - xbee.setSerial(Serial1); - Serial1.begin(38400); - - // xbee.begin(38400); + xbee.begin(38400); } 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.print("+ wave"); + screen.print(wave_id+1); - screen.clear(); - screen.print("+ wave"); - screen.print(dsc.wave); + // increment wave id (will overflow from 255 to 0) + send.setOpcode(0); + send.setWave(++wave_id); + send.setDist(0); - uint8_t payload[3] = {dsc.opcode, dsc.wave, dsc.dist}; + if( xbee.broadcast(send) == XBWSEND_OK ) + Serial.println("sent"); + else + Serial.println("failed"); - XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x0000FFFF); - Tx64Request tx = Tx64Request(addr64, payload, sizeof(payload)); - xbee.send(tx); - - /* 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"); - } - } - - */ } diff --git a/well/main/packet.cpp b/well/main/packet.cpp new file mode 120000 index 0000000..fda818d --- /dev/null +++ b/well/main/packet.cpp @@ -0,0 +1 @@ +../../packet.cpp \ No newline at end of file diff --git a/well/main/packet.h b/well/main/packet.h new file mode 120000 index 0000000..8f158fd --- /dev/null +++ b/well/main/packet.h @@ -0,0 +1 @@ +../../packet.h \ No newline at end of file diff --git a/well/main/xbee_wrapper.cpp b/well/main/xbee_wrapper.cpp new file mode 120000 index 0000000..431ce9f --- /dev/null +++ b/well/main/xbee_wrapper.cpp @@ -0,0 +1 @@ +../../xbee_wrapper.cpp \ No newline at end of file diff --git a/well/main/xbee_wrapper.h b/well/main/xbee_wrapper.h new file mode 120000 index 0000000..637624b --- /dev/null +++ b/well/main/xbee_wrapper.h @@ -0,0 +1 @@ +../../xbee_wrapper.h \ No newline at end of file diff --git a/xbee_wrapper.cpp b/xbee_wrapper.cpp new file mode 100644 index 0000000..9033d4f --- /dev/null +++ b/xbee_wrapper.cpp @@ -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; +}; \ No newline at end of file diff --git a/xbee_wrapper.h b/xbee_wrapper.h new file mode 100644 index 0000000..861fea9 --- /dev/null +++ b/xbee_wrapper.h @@ -0,0 +1,38 @@ +#ifndef _WBEE_WRAPPER_H_ + #define _WBEE_WRAPPER_H_ + + #include + #include + #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 \ No newline at end of file