MeshNet  1.0.0
meshPacketBuilder.h
Go to the documentation of this file.
1 
12 #ifndef MESH_PACKET_BUILDER_H
13 #define MESH_PACKET_BUILDER_H
14 
15 #include "meshPacket.h"
16 
23 typedef enum : int8_t {
25  MPB_None = 0,
26 
27 //OK status:
34 
35 //WARNING status:
42 
43 //ERROR status:
78 
79 //FATAL ERROR status:
91 
92 //INTEGRITY ERROR status :
98 
100 
101 #ifdef SIMULATION
102 class MeshNet;
103 #endif
104 
127 public:
139  MeshPacketBuilderStatus Append(const uint8_t* rawData, uint8_t rawDataSize, int8_t rssi);
140 
151  MeshPacketBuilderStatus Append(MeshFrame& frame, int8_t rssi);
152 
165  MeshPacketBuilderStatus Append(MeshFrameHeader frameHeader, const uint8_t* data, uint8_t dataSize, int8_t rssi);
166 
170  void Clear();
171 
184  void inline SetFrameReceivedCallback(void (*callback)(MeshPacketBuilder*, MeshPacketBuilderStatus, const MeshFrameHeader&, const uint8_t*, uint8_t, int8_t)) {
185  FrameRecv_func = callback;
186  }
187 
203  void inline SetPacketCheckCallback(bool (*callback)(MeshPacketBuilder*, const MeshPacketHeader&, MeshMAC, SystemPacketType, const uint8_t*, uint8_t, int8_t)) {
204  PacketCheck_func = callback;
205  }
206 
211  void SetMaxFrameSize(uint8_t size);
212 
217  inline uint8_t GetMaxFrameSize() const {
218  return maxFrameSize;
219  }
220 
226  inline uint16_t GetPredictedMinPacketDataLength() const {
227  return predictedMinPacketDataLength;
228  }
229 
235  inline uint16_t GetPredictedMaxPacketDataLength() const {
236  return predictedMinPacketDataLength - 1 + headerFrameSize;
237  }
238 
243  inline bool IsEmpty() const {
244  return lastStatus == MPB_None;
245  }
246 
254  return lastStatus;
255  }
256 
260  inline bool PacketIsBuilding() const {
261  return lastStatus == MPB_PartPacketOK;
262  }
263 
267  inline bool PacketAvailable() const {
268  return lastStatus == MPB_FullPacketOK;
269  }
270 
274  inline bool HasError() const {
275  return lastStatus < MPB_None;
276  }
277 
282  inline const MeshPacket& GetBuiltPacket() const {
283  return LastPacket;
284  }
285 
291  return LastPacket;
292  }
293 
300 
305  inline MeshMAC GetLastPacketBSSID() const {
306  return lastBSSID;
307  }
308 
313  inline SystemPacketType GetLastPacketSystemType() const {
314  return lastSPT;
315  }
316 
322  return LastPacket.PacketHeader;
323  }
324 
330  return LastPacket.PacketHeader.FrameControl;
331  }
332 
338  uint8_t RemainingCarriageFramesCount() const;
339 
345  uint8_t CarriageFramesCount() const;
346 
351  inline uint8_t ReceivedFramesCount() const {
353  }
354 
358  int8_t GetAverageRSSI() const;
359 
363  inline int8_t GetMinRSSI() const{
364  return RSSI_min;
365  }
366 
372  inline uint32_t GetAppendingEndTime() const {
373  return lastReceiveTime;
374  }
375 
376 #ifdef SIMULATION
377  MeshNet* mn = NULL;
378 #endif
379 
380 protected:
381  bool (*PacketCheck_func)(MeshPacketBuilder*, const MeshPacketHeader&, MeshMAC, SystemPacketType, const uint8_t*, uint8_t, int8_t) = NULL;
382  void (*FrameRecv_func)(MeshPacketBuilder*, MeshPacketBuilderStatus, const MeshFrameHeader&, const uint8_t*, uint8_t, int8_t) = NULL;
383 
384  MeshPacketBuilderStatus lastStatus = MPB_None;
385 
386  uint32_t lastReceiveTime = 0;
387 
388  uint16_t predictedMinPacketDataLength = 0;
389  uint8_t maxFrameSize = 32;
390  uint8_t headerFrameSize = 0;
391  uint16_t receivedPayloadCount = 0;
392  uint8_t remainingCarriages = 0;
393  MeshMAC lastBSSID;
394  SystemPacketType lastSPT = SPT_ERROR;
395  uint8_t carriageCount = 0;
396  MeshPacket LastPacket;
397 
401  int16_t RSSI_sum = 0;
402 
406  int8_t RSSI_min = 0;
407 
408  void discardLastPacket();
409 
410 #ifdef SIMULATION
411  inline uint32_t millis();
412 #endif
413 };
414 
415 
420 struct MeshACKSetup {
421 
422  MeshACKSetup() = default;
423 
424  MeshACKSetup(const MeshACKSetup&) = default;
425 
426  MeshACKSetup(const MeshPacketBuilder& builder);
427 
432 
436  SystemPacketType Type = SPT_ERROR;
437 
442 
446  uint8_t remainingFrames = 0;
447 };
448 
449 #endif // !MESH_PACKET_BUILDER_H
MPB_FullPacketOK
@ MPB_FullPacketOK
Definition: meshPacketBuilder.h:29
MeshPacketBuilder::GetLastPacketBSSID
MeshMAC GetLastPacketBSSID() const
Gets BSSID of last packet.
Definition: meshPacketBuilder.h:305
MeshPacketBuilder::GetMinRSSI
int8_t GetMinRSSI() const
Gets minimum RSSI of received packet's frames.
Definition: meshPacketBuilder.h:363
MeshPacketBuilder::IsEmpty
bool IsEmpty() const
Checks if any packet was built or being built or had an error at built.
Definition: meshPacketBuilder.h:243
MPB_FrameSizeExceededError
@ MPB_FrameSizeExceededError
ERROR: Frame size exceeded and will be ignored. This may happened, when frame is larger then maximum ...
Definition: meshPacketBuilder.h:57
MeshPacketBuilder::GetPredictedMaxPacketDataLength
uint16_t GetPredictedMaxPacketDataLength() const
Gets maximum predicted packet data length in bytes of packet, that is currently being built.
Definition: meshPacketBuilder.h:235
MeshFrameControl
Structure, that contains bit flags used for Frame Control in MeshPacket. Size of this structure is al...
Definition: meshPacketFlags.h:25
MeshACKSetup::PacketHeader
MeshPacketHeader PacketHeader
Packet header of received packet.
Definition: meshPacketBuilder.h:431
MeshNet
This class implements MeshNet protocol for microcontrollers.
Definition: mesh.h:104
MeshACKSetup
Structure, which has data, that are needed for ACK packet creation.
Definition: meshPacketBuilder.h:420
MPB_AllocationError
@ MPB_AllocationError
FATAL ERROR: Cannot allocate space for pacet payload, whole packet will be discarded....
Definition: meshPacketBuilder.h:90
MeshPacketBuilder::GetStatus
MeshPacketBuilderStatus GetStatus() const
Gets status of packet, that was built or is being built or had build error. Ignored frames are not re...
Definition: meshPacketBuilder.h:253
MeshPacketBuilder::SetMaxFrameSize
void SetMaxFrameSize(uint8_t size)
Sets maximum frame size. Defaultly set to 32.
Definition: meshPacketBuilder.cpp:263
MeshPacketBuilder::SetFrameReceivedCallback
void SetFrameReceivedCallback(void(*callback)(MeshPacketBuilder *, MeshPacketBuilderStatus, const MeshFrameHeader &, const uint8_t *, uint8_t, int8_t))
Sets callback function, that is called when valid frame is received. This function can be used to cal...
Definition: meshPacketBuilder.h:184
MPB_PayloadSizeExceededError
@ MPB_PayloadSizeExceededError
FATAL ERROR: Payload size exceeded and whole packet will be discarded. This may happened,...
Definition: meshPacketBuilder.h:85
SPT_ERROR
typedef SPT_ERROR
Definition: meshPacket.h:33
MeshPacketBuilder::PacketAvailable
bool PacketAvailable() const
Checks if packet was built. If yes, you can get it using method GetBuiltPacket().
Definition: meshPacketBuilder.h:267
MPB_SameFrameReceived
@ MPB_SameFrameReceived
WARNING: Same frame from carriage was received multiple times. This may happened, when ACK was not re...
Definition: meshPacketBuilder.h:41
MPB_Ignored
@ MPB_Ignored
Definition: meshPacketBuilder.h:33
MeshFrame
Definition: meshFrame.h:126
MeshPacketBuilder
This class can build packet from multiple frames. It is usually used to concatinate data from packet,...
Definition: meshPacketBuilder.h:126
meshPacket.h
This file contains class which can be used to handling mesh packets.
MeshPacketBuilder::GetBuiltPacket
const MeshPacket & GetBuiltPacket() const
Gets packet, that was built.
Definition: meshPacketBuilder.h:282
MeshPacketBuilder::CarriageFramesCount
uint8_t CarriageFramesCount() const
Count of all frames the packet was splitted at.
Definition: meshPacketBuilder.cpp:292
MPB_InvalidFrameError
@ MPB_InvalidFrameError
ERROR: Invalid frame appended and will be ignored. This may happened, when frame was corrupted.
Definition: meshPacketBuilder.h:48
MeshACKSetup::Type
SystemPacketType Type
Type of received packet.
Definition: meshPacketBuilder.h:436
MeshPacketBuilder::PacketIsBuilding
bool PacketIsBuilding() const
Checks if packet is building right now and is not already built.
Definition: meshPacketBuilder.h:260
MeshPacketBuilder::GetLastPacketSystemType
SystemPacketType GetLastPacketSystemType() const
Gets system packet type of last packet. If packet is not system packet, SPT_ERROR is returned.
Definition: meshPacketBuilder.h:313
MeshPacketBuilderStatus
MeshPacketBuilderStatus
Mesh packet builder status.
Definition: meshPacketBuilder.h:23
MeshPacketBuilder::GetBuiltPacket
MeshPacket & GetBuiltPacket()
Gets packet, that was built.
Definition: meshPacketBuilder.h:290
MPB_PartPacketOK
@ MPB_PartPacketOK
Definition: meshPacketBuilder.h:31
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
MeshPacketBuilder::GetMaxFrameSize
uint8_t GetMaxFrameSize() const
Gets maximum frame size.
Definition: meshPacketBuilder.h:217
MPB_None
@ MPB_None
Definition: meshPacketBuilder.h:25
MeshACKSetup::BSSID
MeshMAC BSSID
BSSID of received packet.
Definition: meshPacketBuilder.h:441
MPB_PacketIntegrityError
@ MPB_PacketIntegrityError
ERROR: Packet integrity check failed. Some frame from carriage was corupted.
Definition: meshPacketBuilder.h:97
MeshPacketBuilder::GetLastPacketFrameControl
MeshFrameControl GetLastPacketFrameControl() const
Gets frame control of last packet.
Definition: meshPacketBuilder.h:329
MeshPacketBuilder::HasError
bool HasError() const
Checks if any error happened during packet building.
Definition: meshPacketBuilder.h:274
MeshMAC
Structure that represents physical MAC address used in MeshNEt protocol. MeshMAC address unlike MAC a...
Definition: meshHelper.h:207
MeshACKSetup::remainingFrames
uint8_t remainingFrames
Count of remaining frames.
Definition: meshPacketBuilder.h:446
MeshPacketBuilder::ReceivedFramesCount
uint8_t ReceivedFramesCount() const
Gets count of received frames from current packet.
Definition: meshPacketBuilder.h:351
MeshPacketHeader
This structure contains informations about mesh packet, that are stored in it's header.
Definition: meshPacket.h:42
MeshPacketBuilder::GetAppendingEndTime
uint32_t GetAppendingEndTime() const
Gets time, when appending of last packet has ended. This can be used as time, when was last part of t...
Definition: meshPacketBuilder.h:372
MeshPacketBuilder::SetPacketCheckCallback
void SetPacketCheckCallback(bool(*callback)(MeshPacketBuilder *, const MeshPacketHeader &, MeshMAC, SystemPacketType, const uint8_t *, uint8_t, int8_t))
Sets callback function, that is called when new packet is going to start build. This callback functio...
Definition: meshPacketBuilder.h:203
MPB_InvalidPacketError
@ MPB_InvalidPacketError
ERROR: Invalid packet appended and packet will be ignored. This may happened, when system packet was ...
Definition: meshPacketBuilder.h:77
MeshPacketBuilder::Clear
void Clear()
Clears all appended data and last packet.
Definition: meshPacketBuilder.cpp:234
MeshPacketBuilder::TakeBuiltPacket
MeshPacket && TakeBuiltPacket()
Takes packet, that was built and calls clear method.
Definition: meshPacketBuilder.cpp:271
MeshPacketBuilder::GetLastPacketHeader
MeshPacketHeader GetLastPacketHeader() const
Gets header of last packet.
Definition: meshPacketBuilder.h:321
MeshACKSetup::MeshACKSetup
MeshACKSetup()=default
MeshPacketBuilder::Append
MeshPacketBuilderStatus Append(const uint8_t *rawData, uint8_t rawDataSize, int8_t rssi)
Appends MeshFrame (that will be created from raw data) to packet builder.
Definition: meshPacketBuilder.cpp:3
enum
typedef enum
Definition: meshPacket.h:20
MeshPacketBuilder::RemainingCarriageFramesCount
uint8_t RemainingCarriageFramesCount() const
Count of remaining frames in carriage.
Definition: meshPacketBuilder.cpp:285
MeshPacketBuilder::GetAverageRSSI
int8_t GetAverageRSSI() const
Gets average RSSI of received packet's frames.
Definition: meshPacketBuilder.cpp:299
MeshPacketBuilder::GetPredictedMinPacketDataLength
uint16_t GetPredictedMinPacketDataLength() const
Gets minimum predicted packet data length in bytes of packet, that is currently being built.
Definition: meshPacketBuilder.h:226
MPB_FrameHeaderMissingError
@ MPB_FrameHeaderMissingError
ERROR: Appended frame from carriage, but frame header is missing. Frame will be ignored....
Definition: meshPacketBuilder.h:71
MeshFrameHeader
Definition: meshFrame.h:20
MPB_FrameOrderError
@ MPB_FrameOrderError
ERROR: Appended frame from carriage, that was not expected yet. Frame will be ignored....
Definition: meshPacketBuilder.h:63