2018-12-03 09:54:53 +00:00
|
|
|
#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
|
2018-12-11 11:42:34 +00:00
|
|
|
uint8_t read_discover(uint8_t* buf, const size_t size);
|
|
|
|
uint8_t read_message(uint8_t* buf, const size_t size);
|
|
|
|
size_t write_discover(uint8_t* buf);
|
|
|
|
size_t write_message(uint8_t* buf);
|
2018-12-03 09:54:53 +00:00
|
|
|
|
|
|
|
public:
|
2018-12-11 11:42:34 +00:00
|
|
|
|
|
|
|
Packet();
|
2018-12-03 09:54:53 +00:00
|
|
|
// builds a packet from raw data and returns the status code
|
2018-12-11 11:42:34 +00:00
|
|
|
uint8_t read(uint8_t* buf, const size_t size);
|
2018-12-03 09:54:53 +00:00
|
|
|
|
|
|
|
// writes the binary representation of the packet returns the size
|
2018-12-11 11:42:34 +00:00
|
|
|
size_t write(uint8_t* buf);
|
2018-12-03 09:54:53 +00:00
|
|
|
|
|
|
|
// GETTERS / SETTERS
|
|
|
|
uint8_t getOpcode();
|
|
|
|
void setOpcode(uint8_t value);
|
|
|
|
|
2018-12-03 10:24:32 +00:00
|
|
|
uint8_t getSender();
|
|
|
|
void setSender(uint8_t value);
|
|
|
|
|
2018-12-03 09:54:53 +00:00
|
|
|
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();
|
2018-12-11 11:42:34 +00:00
|
|
|
void setData(uint8_t *buffer);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
resizeMessage(){
|
|
|
|
msg.data = realloc(msg.data, (msg.size+1) * sizeof(uint8_t));
|
|
|
|
// memset(msg.data, 0, msg.size+1);
|
|
|
|
}
|
2018-12-03 09:54:53 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|