2018-12-03 09:54:53 +00:00
|
|
|
#include "packet.h"
|
|
|
|
|
2018-12-13 13:23:23 +00:00
|
|
|
|
2018-12-03 09:54:53 +00:00
|
|
|
/* PUBLIC
|
|
|
|
----------------------------------------*/
|
|
|
|
// builds a packet from raw data and returns the error code
|
2018-12-11 11:42:34 +00:00
|
|
|
uint8_t Packet::read(uint8_t* buf, const size_t size){
|
2018-12-03 09:54:53 +00:00
|
|
|
// 1. fail on invalid size
|
|
|
|
if( size < 1 ) return PKTREAD_EMPTY;
|
2018-12-13 13:23:23 +00:00
|
|
|
if( size > PROTO_SIZE ) return PKTREAD_OVERFLOW;
|
2018-12-03 09:54:53 +00:00
|
|
|
|
|
|
|
// 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
|
2018-12-11 11:42:34 +00:00
|
|
|
size_t Packet::write(uint8_t* buf){
|
2018-12-03 09:54:53 +00:00
|
|
|
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; }
|
|
|
|
|
2018-12-11 11:42:34 +00:00
|
|
|
uint8_t Packet::getSender() { return msg.sender; }
|
|
|
|
void Packet::setSender(uint8_t value) { msg.sender = value; }
|
2018-12-03 10:24:32 +00:00
|
|
|
|
2018-12-03 09:54:53 +00:00
|
|
|
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; }
|
|
|
|
|
2018-12-11 11:42:34 +00:00
|
|
|
uint8_t* Packet::getData(){ return msg.data; }
|
|
|
|
void Packet::setData(uint8_t *buffer) {
|
2018-12-13 13:23:23 +00:00
|
|
|
if( strlen(buffer) >= MESSAGE_MAX_PAYLOAD )
|
2018-12-11 11:42:34 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
msg.size = strlen(buffer);
|
|
|
|
|
2018-12-13 13:23:23 +00:00
|
|
|
strncpy(msg.data, buffer, MESSAGE_MAX_PAYLOAD);
|
2018-12-11 11:42:34 +00:00
|
|
|
}
|
2018-12-03 09:54:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* PRIVATE
|
|
|
|
----------------------------------------*/
|
2018-12-11 11:42:34 +00:00
|
|
|
uint8_t Packet::read_discover(uint8_t *buf, const size_t size){
|
2018-12-03 09:54:53 +00:00
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
2018-12-11 11:42:34 +00:00
|
|
|
|
|
|
|
size_t Packet::write_discover(uint8_t *buf){
|
|
|
|
buf[0] = dsc.opcode;
|
|
|
|
buf[1] = dsc.wave;
|
|
|
|
buf[2] = dsc.dist;
|
|
|
|
|
|
|
|
return 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t Packet::read_message(uint8_t *buf, const size_t size){
|
2018-12-03 09:54:53 +00:00
|
|
|
// 1. fail on invalid size
|
2018-12-13 13:23:23 +00:00
|
|
|
if( size < PROTO_MIN_SIZE || size > PROTO_SIZE )
|
2018-12-03 09:54:53 +00:00
|
|
|
return PKTREAD_INVALID_MESSAGE_FORMAT;
|
|
|
|
|
2018-12-11 11:42:34 +00:00
|
|
|
// 2. fill values
|
2018-12-03 09:54:53 +00:00
|
|
|
msg.opcode = buf[0];
|
2018-12-11 11:42:34 +00:00
|
|
|
msg.sender = buf[1];
|
|
|
|
msg.dist = buf[2];
|
|
|
|
msg.ttl = buf[3];
|
|
|
|
msg.size = buf[4];
|
2018-12-03 09:54:53 +00:00
|
|
|
|
|
|
|
// 3. check message size
|
2018-12-11 11:42:34 +00:00
|
|
|
if( size - 5 != msg.size )
|
2018-12-03 09:54:53 +00:00
|
|
|
return PKTREAD_INVALID_MESSAGE_FORMAT;
|
|
|
|
|
2018-12-13 13:23:23 +00:00
|
|
|
// 4. memset data
|
|
|
|
//memset(msg.data,0,MESSAGE_MAX_PAYLOAD);
|
|
|
|
|
|
|
|
// 5. extract message
|
2018-12-11 11:42:34 +00:00
|
|
|
strncpy(msg.data, buf+5, msg.size);
|
2018-12-13 13:23:23 +00:00
|
|
|
memset(msg.data+msg.size,0,MESSAGE_MAX_PAYLOAD-msg.size);
|
2018-12-03 14:29:56 +00:00
|
|
|
|
2018-12-03 09:54:53 +00:00
|
|
|
return PKTREAD_OK;
|
|
|
|
};
|
|
|
|
|
2018-12-11 11:42:34 +00:00
|
|
|
size_t Packet::write_message(uint8_t *buf){
|
2018-12-03 09:54:53 +00:00
|
|
|
buf[0] = msg.opcode;
|
2018-12-11 11:42:34 +00:00
|
|
|
buf[1] = msg.sender;
|
|
|
|
buf[2] = msg.dist;
|
|
|
|
buf[3] = msg.ttl;
|
|
|
|
buf[4] = msg.size;
|
2018-12-03 09:54:53 +00:00
|
|
|
|
2018-12-13 13:23:23 +00:00
|
|
|
strncpy(buf+5, msg.data, MESSAGE_MAX_PAYLOAD);
|
2018-12-03 14:29:56 +00:00
|
|
|
|
2018-12-11 11:42:34 +00:00
|
|
|
return 5 + msg.size;
|
2018-12-13 13:23:23 +00:00
|
|
|
};
|