MeshNet  1.0.0
meshPacket.h
Go to the documentation of this file.
1 
13 #ifndef MESH_PACKET_H
14 #define MESH_PACKET_H
15 
16 #include "meshFrame.h"
17 
18 //SystemPacketTypes from 0 to 4 can be sent between networks.
19 typedef enum: uint8_t {
20  SPT_ACK = 0,
29  SPT_Ping = 10,
32  SPT_DATA = 254,
33  SPT_ERROR = 255
34 }SystemPacketType;
35 
43  MeshPacketHeader() = default;
44 
50 
55  uint8_t ID = 0;
56 
60  inline uint16_t GetUniquePacketID() const {
61  return (uint16_t)ID << 8 | Source;
62  }
63 
68 
73 
78 
83 
87  uint8_t Hops = 0;
88 
92  inline bool HasError() const {
97  }
98 
102  void Clear();
103 
104  void operator=(const MeshFrameHeader& val) {
106  ID = val.ID;
107  Source = val.Source;
108  }
109 
115  inline uint16_t GetRawLength(bool includeCarriageCount) const {
117  }
118 
124  bool FromMeshFrame(const MeshFrame& frame);
125 
133  bool FromMeshFrame(MeshFrameHeader frameHeader, const uint8_t* data, uint16_t dataSize);
134 
142 };
143 
144 
158 class MeshPacket: public RawPrintable {
159 public:
160 
161  MeshPacket();
162 
166  MeshPacket(const MeshPacket& mp);
167 
171  MeshPacket(MeshPacket&& mp) noexcept;
172 
176  ~MeshPacket();
177 
181  MeshPacket& operator=(const MeshPacket& mp);
182 
186  MeshPacket& operator=(MeshPacket&& mp) noexcept;
187 
193 
197  inline uint16_t Length() const{
198  return PayloadLength;
199  }
200 
201  uint8_t operator [](uint16_t i) const{
202  return Payload[i];
203  }
204 
205  uint8_t& operator [](uint16_t i) {
206  return Payload[i];
207  }
208 
213  SystemPacketType GetSystemPacketType() const;
214 
220  uint8_t GetPayloadByteAt(uint16_t index) const;
221 
228  virtual bool SetPayloadByteAt(uint16_t index, uint8_t value);
229 
236  virtual bool SetPayload(const uint8_t* payload, const uint16_t payloadLength);
237 
243  inline bool SetPayload(const MeshVariableArray<uint8_t>& va) {
244  return SetPayload(va.d_ptr(), va.Length());
245  }
246 
253  virtual bool SetPayload(MeshVariableArray<uint8_t>&& va);
254 
260  void GetPayload(uint8_t* payload, uint16_t& payloadLength) const;
261 
268  inline bool GetPayload(MeshVariableArray<uint8_t>& payload) const {
269  return payload.SetArray(Payload, PayloadLength);
270  }
271 
278 
285  bool ResizePayload(uint16_t size, bool doNotCopy = false);
286 
290  void ClearPayload();
291 
295  virtual void Clear();
296 
302  virtual bool copyFrom(const MeshPacket& another);
303 
310  virtual void moveFrom(MeshPacket& another);
311 
315  inline const uint8_t* p_ptr() const{
316  return Payload;
317  }
318 
324  size_t prettyPrint(OSTREAM& stream) const;
325 
331  size_t printTo(Print& p) const override;
332 
333  size_t printHeader(OSTREAM* stream) const override;
334 
335  size_t printLine(OSTREAM* stream) const override;
336 
341  inline size_t sizeOf() const override {
342  return sizeof(*this) + Length();
343  }
344 
351  size_t printRaw(OSTREAM* stream, void(*CRC_calculation)(uint8_t*, size_t)) const override;
352 
357  virtual size_t getRawSize() const override;
358 
362  virtual inline bool HasError() const{
363  return error || PacketHeader.HasError();
364  }
365 
369  inline void SetError() {
370  error = true;
371  }
372 
376  inline bool GetError() const {
377  return error;
378  }
379 
383  inline void ClearError() {
384  error = false;
385  }
386 
387 protected:
388 
392  uint8_t* Payload = NULL;
393 
397  uint16_t PayloadLength : 10;
398 
402  bool error : 1;
403 
404  static inline char toHEXChar(OSTREAM& stream, uint8_t val) {
405  uint8_t raw = val & 0x0F;
406  if (raw < 10) return '0' + raw;
407  else return 'A' + raw - 10;
408  }
409 
410  static size_t printHex(OSTREAM& stream, uint8_t val);
411 };
412 
413 #endif // !MESH_PACKET_H
MeshPacket::GetPayload
bool GetPayload(MeshVariableArray< uint8_t > &payload) const
Copyies payload data.
Definition: meshPacket.h:268
IS_VALID_MESH_DEST_ADDRESS
#define IS_VALID_MESH_DEST_ADDRESS(address)
Checks if mesh "destination" address from packet or frame header is valid. Accepted values from 0 to ...
Definition: meshConfig.h:94
MeshPacket::GetPayloadByteAt
uint8_t GetPayloadByteAt(uint16_t index) const
Reads payload byte at specified index.
Definition: meshPacket.cpp:133
MeshPacketHeader::HasError
bool HasError() const
Checks if packet header is not valid.
Definition: meshPacket.h:92
MeshPacket::sizeOf
size_t sizeOf() const override
Gets size of the whole class in bytes including dynamically allocated part.
Definition: meshPacket.h:341
MeshPacket::SetPayload
bool SetPayload(const MeshVariableArray< uint8_t > &va)
Copyies payload data from variable array to current packet payload.
Definition: meshPacket.h:243
MeshPacket::toHEXChar
static char toHEXChar(OSTREAM &stream, uint8_t val)
Definition: meshPacket.h:404
MeshVariableArray::SetArray
bool SetArray(const T *newData, const uint16_t newDataLength)
Sets new data to whole and resizes (expand/shrink) it.
Definition: meshVariableArray.h:154
MeshFrameHeader::Source
uint8_t Source
Address of message source node.
Definition: meshFrame.h:57
MeshVariableArray::Length
uint16_t Length() const
Length of data.
Definition: meshVariableArray.h:113
MeshFrameControl
Structure, that contains bit flags used for Frame Control in MeshPacket. Size of this structure is al...
Definition: meshPacketFlags.h:25
SPT_NeighborScan
typedef SPT_NeighborScan
Definition: meshPacket.h:27
MeshPacket::moveFrom
virtual void moveFrom(MeshPacket &another)
Copyies data from another packet to current packet and clears data in another packet.
Definition: meshPacket.cpp:206
MeshPacketHeader::operator=
void operator=(const MeshFrameHeader &val)
Definition: meshPacket.h:104
SPT_Reserved
typedef SPT_Reserved
Definition: meshPacket.h:24
MeshPacketHeader::GetRawLength
uint16_t GetRawLength(bool includeCarriageCount) const
Gets size of raw packet header including BSSID(4 bytes), which is not stored in packet.
Definition: meshPacket.h:115
MeshPacket::GetSystemPacketType
SystemPacketType GetSystemPacketType() const
Gets type of system packet.
Definition: meshPacket.cpp:119
MeshPacketHeader::ID
uint8_t ID
Unique packet ID.
Definition: meshPacket.h:55
MeshPacket::ClearPayload
void ClearPayload()
Clears payload data.
Definition: meshPacket.cpp:181
MeshFrameHeader::FrameControl
MeshFrameControl FrameControl
Contains flags, that controls frame.
Definition: meshFrame.h:34
IS_VALID_MESH_TO_ADDRESS
#define IS_VALID_MESH_TO_ADDRESS(address)
Checks if mesh "to" address from packet or frame header is valid. Accepted values from 0 to MAX_MESH_...
Definition: meshConfig.h:106
MeshVariableArray::d_ptr
const T * d_ptr() const
Pointer to data.
Definition: meshVariableArray.h:330
MeshFrameControl::ContainsHopCount
bool ContainsHopCount() const
Check if frame with current control frame has to contain hop count field.
Definition: meshPacketFlags.h:137
SPT_ERROR
typedef SPT_ERROR
Definition: meshPacket.h:33
MeshPacket::PacketHeader
MeshPacketHeader PacketHeader
Mesh packet header.
Definition: meshPacket.h:192
MeshPacket::getRawSize
virtual size_t getRawSize() const override
Calculates size of raw bytes.
Definition: meshPacket.cpp:471
MeshPacket::HasError
virtual bool HasError() const
Checks if packet is not valid.
Definition: meshPacket.h:362
MeshPacket::SetPayload
virtual bool SetPayload(const uint8_t *payload, const uint16_t payloadLength)
Sets payload data.
Definition: meshPacket.cpp:85
MeshPacketHeader::Clear
void Clear()
Clears all data in packet header.
Definition: meshPacket.cpp:3
MeshPacket::Payload
uint8_t * Payload
Packet payload.
Definition: meshPacket.h:392
MeshPacket::printLine
size_t printLine(OSTREAM *stream) const override
Prints this data as row to table.
Definition: meshPacket.cpp:313
MeshFrameControl::ContainsFromAddress
bool ContainsFromAddress() const
Check if frame with current control frame has to contain From address field.
Definition: meshPacketFlags.h:144
MeshFrame
Definition: meshFrame.h:126
MeshPacket::operator[]
uint8_t operator[](uint16_t i) const
Definition: meshPacket.h:201
MeshPacket::printRaw
size_t printRaw(OSTREAM *stream, void(*CRC_calculation)(uint8_t *, size_t)) const override
Prints current instance into stream in raw bytes.
Definition: meshPacket.cpp:451
MeshVariableArray
This class safetly handles allocation and deallocation of memory. It can be used in conjunction with ...
Definition: meshVariableArray.h:32
MeshPacket::Clear
virtual void Clear()
Clears all data in packet.
Definition: meshPacket.cpp:190
MeshPacket
Class, that stores mesh packet with it's data. The size of this class can be slightly bigger then rea...
Definition: meshPacket.h:158
MeshPacket::SetPayloadByteAt
virtual bool SetPayloadByteAt(uint16_t index, uint8_t value)
Sets payload byte at specified index.
Definition: meshPacket.cpp:141
RawPrintable
Interface for converting class or structure instance to raw bytes, which can be sent.
Definition: meshHelper.h:161
MeshPacket::ResizePayload
bool ResizePayload(uint16_t size, bool doNotCopy=false)
Resizes payload.
Definition: meshPacket.cpp:150
MeshPacket::p_ptr
const uint8_t * p_ptr() const
Pointer to payload.
Definition: meshPacket.h:315
MeshPacketHeader::FrameControl
MeshFrameControl FrameControl
Contains flags, that controls frame.
Definition: meshPacket.h:49
MeshPacket::copyFrom
virtual bool copyFrom(const MeshPacket &another)
Copyies data from another packet to current packet.
Definition: meshPacket.cpp:196
MeshPacketHeader::Destination
uint8_t Destination
Address of destination source node.
Definition: meshPacket.h:72
MeshPacketHeader::MeshPacketHeader
MeshPacketHeader()=default
SPT_NetScan
typedef SPT_NetScan
Definition: meshPacket.h:22
SPT_Disconnect
typedef SPT_Disconnect
Definition: meshPacket.h:26
OSTREAM
#define OSTREAM
Definition: meshConfig.h:41
MeshPacket::error
bool error
True when packet has error.
Definition: meshPacket.h:402
SPT_DATA
typedef SPT_DATA
Definition: meshPacket.h:32
MeshFrameHeader::ID
uint8_t ID
Unique packet ID.
Definition: meshFrame.h:45
MeshPacketHeader::From
uint8_t From
Address of neighbor node, which last time relayied current packet.
Definition: meshPacket.h:77
MeshFrameControl::ContainsDestinationAddress
bool ContainsDestinationAddress() const
Check if frame with current control frame has to contain Destination address field.
Definition: meshPacketFlags.h:130
MeshMAC
Structure that represents physical MAC address used in MeshNEt protocol. MeshMAC address unlike MAC a...
Definition: meshHelper.h:207
MeshPacket::GetError
bool GetError() const
Checks if error is set by calling SetError()
Definition: meshPacket.h:376
IS_VALID_MESH_SRC_ADDRESS
#define IS_VALID_MESH_SRC_ADDRESS(address)
Checks if mesh "source" address from packet or frame header is valid. Accepted values from 0 to MAX_M...
Definition: meshConfig.h:88
MeshPacketHeader::GetUniquePacketID
uint16_t GetUniquePacketID() const
Gets unique packed identifier which is composed from ID and source address.
Definition: meshPacket.h:60
INVALID_MESH_ADDRESS
#define INVALID_MESH_ADDRESS
Address, that is invalid and cannot be used.
Definition: meshConfig.h:56
MeshPacket::printHex
static size_t printHex(OSTREAM &stream, uint8_t val)
Definition: meshPacket.cpp:475
MeshPacket::operator=
MeshPacket & operator=(const MeshPacket &mp)
Copy assignment operator.
Definition: meshPacket.cpp:75
MeshPacketHeader
This structure contains informations about mesh packet, that are stored in it's header.
Definition: meshPacket.h:42
SPT_ERR_ACK
typedef SPT_ERR_ACK
Definition: meshPacket.h:21
MeshPacketHeader::ToMeshFrameHeader
MeshFrameHeader ToMeshFrameHeader(MeshMAC BSSID)
Converts packet header to frame packet header.
Definition: meshPacket.cpp:45
MeshPacket::printTo
size_t printTo(Print &p) const override
Makes this class printable.
Definition: meshPacket.cpp:304
MeshPacket::Length
uint16_t Length() const
Length of payload.
Definition: meshPacket.h:197
MeshPacket::MeshPacket
MeshPacket()
Definition: meshPacket.cpp:56
MeshPacketHeader::Hops
uint8_t Hops
Count of hops, that message done during transmission. Source MNode has 0 hops, next one 1 hop,...
Definition: meshPacket.h:87
MeshPacket::~MeshPacket
~MeshPacket()
Destructor.
Definition: meshPacket.cpp:71
SPT_Connect
typedef SPT_Connect
Definition: meshPacket.h:23
MeshPacketHeader::To
uint8_t To
Address of neighbor node, which has to receive current packet and other neighbor nodes has to ignore ...
Definition: meshPacket.h:82
SPT_PathDiscovery
typedef SPT_PathDiscovery
Definition: meshPacket.h:28
MeshPacket::SetError
void SetError()
Sets that packet is not valid.
Definition: meshPacket.h:369
MeshPacketHeader::FromMeshFrame
bool FromMeshFrame(const MeshFrame &frame)
Converts MeshFrame to current structure.
Definition: meshPacket.cpp:13
MeshPacket::PayloadLength
uint16_t PayloadLength
Packet payload length. Maximum value can be 1023, because only 10 bits are used.
Definition: meshPacket.h:397
MeshPacket::TakePayload
MeshVariableArray< uint8_t > TakePayload()
Takes payload data. Payload will be cleared after this operation, because std::move was used....
Definition: meshPacket.cpp:112
MeshFrameControl::ContainsToAddress
bool ContainsToAddress() const
Check if frame with current control frame has to contain To address field.
Definition: meshPacketFlags.h:151
meshFrame.h
This file contains classes and structures which can be used to handling mesh frames.
SPT_Ping
typedef SPT_Ping
Definition: meshPacket.h:29
MeshPacketHeader::Source
uint8_t Source
Address of message source node.
Definition: meshPacket.h:67
MeshPacket::ClearError
void ClearError()
Sets that packet is valid.
Definition: meshPacket.h:383
MeshPacket::GetPayload
void GetPayload(uint8_t *payload, uint16_t &payloadLength) const
Gets payload data.
Definition: meshPacket.cpp:106
MeshFrameHeader
Definition: meshFrame.h:20
IS_VALID_MESH_FROM_ADDRESS
#define IS_VALID_MESH_FROM_ADDRESS(address)
Checks if mesh "from" address from packet or frame header is valid. Accepted values from 0 to MAX_MES...
Definition: meshConfig.h:100
MeshPacket::printHeader
size_t printHeader(OSTREAM *stream) const override
Prints header of table.
Definition: meshPacket.cpp:309
MeshPacket::prettyPrint
size_t prettyPrint(OSTREAM &stream) const
Pretty prints packet to stream.
Definition: meshPacket.cpp:216