#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, 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); public: Packet(); // builds a packet from raw data and returns the status code uint8_t read(uint8_t* buf, const 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 getSender(); void setSender(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); protected: resizeMessage(){ msg.data = realloc(msg.data, (msg.size+1) * sizeof(uint8_t)); memset(msg.data, 0, msg.size+1); } }; #endif