MeshNet  1.0.0
meshHelper.h
Go to the documentation of this file.
1 
11 #ifndef INC_MESHHELPER_H_
12 #define INC_MESHHELPER_H_
13 
14 #include "meshConfig.h"
15 
57 #define HAS_MEM_FUNC(func, name) \
58  template<typename T, typename Sign> \
59  struct name { \
60  typedef char yes[1]; \
61  typedef char no [2]; \
62  template <typename U, U> struct type_check; \
63  template <typename _1> static yes &chk(type_check<Sign, &_1::func> *); \
64  template <typename > static no &chk(...); \
65  static bool const value = sizeof(chk<T>(0)) == sizeof(yes); \
66  }
67 
68 
69 #ifdef SIMULATION
70 typedef enum {
71  //Prolonging row
72  MST_PROLONG,
73  //Removing from table
74  MST_REMOVE,
75  //Setting or adding item to table
76  MST_SET,
77  //Removed by validation
78  MST_REMOVE_VLD,
79  //Cleared table
80  MST_CLEAR,
81  //Defragmented
82  MST_DEFRAG,
83 
84 }TableChangeType;
85 
86 class MeshNet;
87 class MSHelper {
88 public:
89  //Descriptor of table that is used to identify table
90  std::string descriptor;
91  MeshNet* mn = NULL;
92  uint32_t millis() const;
93  uint32_t micros() const;
94  void delay(uint32_t time) const;
95  void delayMicroseconds(uint32_t time) const;
96  std::stringstream& NotifyTableChangeBegin(TableChangeType changeType);
97  void NotifyTableChangeEnd();
98 
99 protected:
100  std::stringstream tableChangeStream;
101  TableChangeType lastChangeType = MST_CLEAR;
102 };
103 
104 #endif // SIMULATION
105 
106 
107 #ifdef NO_ITOA_S
108 char* _itoa_s( int32_t value, char * buffer, size_t size, int radix);
109 #endif //NO_ITOA_S
110 
115 typedef enum {
120 
128 void alignText(char *text, int size, char fill, TextAlignment alignment);
129 
134 class InlinePrintable : public Printable {
135 public:
142  virtual size_t printHeader(OSTREAM* stream) const {
143  return 0;
144  }
145 
152  virtual size_t printLine(OSTREAM * stream) const {
153  return 0;
154  }
155 };
156 
162 public:
163 
168  virtual size_t sizeOf() const {
169  return sizeof(*this);
170  }
171 
178  virtual size_t printRaw(OSTREAM* stream, void(* CRC_calculation)(uint8_t*, size_t)) const {
179  return 0;
180  }
181 
186  virtual size_t getRawSize() const {
187  return 0;
188  }
189 };
190 
195 typedef enum {
201 
207 struct MeshMAC {
211  uint8_t octet4 = 0;
212 
216  uint8_t octet3 = 0;
217 
221  uint8_t octet2 = 0;
222 
226  uint8_t octet1 = 0;
227 
231  MeshMAC() = default;
232 
240  MeshMAC(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4);
241 
246  MeshMAC(uint32_t mac);
247 
251  MeshMAC(const MeshMAC &mac) = default;
252 
257  inline bool isValid() const {
258  return getRaw() != 0;
259  }
260 
265  inline uint32_t getRaw() const {
266  return *(reinterpret_cast<const uint32_t*>(this));
267  //return (uint32_t)octet4 | ((uint32_t)octet3 << 8) | ((uint32_t)octet2 << 16) | ((uint32_t)octet1 << 24);
268  }
269 
274  inline void setRaw(const uint32_t mac) {
275  const MeshMAC* tmp = reinterpret_cast<const MeshMAC*>(&mac);
276  *this = *tmp;
277  /*octet4 = mac;
278  octet3 = mac >> 8;
279  octet2 = mac >> 16;
280  octet1 = mac >> 24;*/
281  }
282 
288  size_t printTo(Print& p) const;
289 
296  static char digitToHex(uint8_t digit, bool low = true);
297 
298  void operator=(const uint32_t& val) {
299  setRaw(val);
300  }
301 
302  operator uint32_t() const { return getRaw(); }
303  operator uint32_t() { return getRaw(); }
304 };
305 
310 struct MeshVersion {
311 
315  uint16_t Patch : 5;
316 
320  uint16_t Minor : 5;
321 
325  uint16_t Major : 6;
326 
331  Patch(0),
332  Minor(0),
333  Major(0)
334  { }
335 
342  MeshVersion(uint8_t major, uint8_t minor, uint8_t patch);
343 
348  MeshVersion(uint16_t raw);
349 
353  MeshVersion(const MeshVersion &mac) = default;
354 
359  inline uint16_t getRaw() const {
360  return *(reinterpret_cast<const uint16_t*>(this));
361  }
362 
367  inline void setRaw(const uint16_t version) {
368  const MeshVersion *tmp = reinterpret_cast<const MeshVersion*>(&version);
369  *this = *tmp;
370  /*Major = tmp->Major;
371  Minor = tmp->Minor;
372  Patch = tmp->Patch;*/
373  }
374 
380  size_t printTo(Print& p) const;
381 
382  //Casting operators
383  void operator=(const uint16_t& val) {
384  setRaw(val);
385  }
386 
387  operator uint16_t() const { return getRaw(); }
388  operator uint16_t() { return getRaw(); }
389 
390  //Equality operators
391  bool operator==(const uint16_t& val){ return getRaw() == val;}
392  bool operator==(const MeshVersion& val){ return getRaw() == val.getRaw();}
393 
394  bool operator!=(const uint16_t& val){ return getRaw() != val;}
395  bool operator!=(const MeshVersion& val){ return getRaw() != val.getRaw();}
396 
397  bool operator>=(const uint16_t& val){ return getRaw() >= val;}
398  bool operator>=(const MeshVersion& val){ return getRaw() >= val.getRaw();}
399 
400  bool operator<=(const uint16_t& val){ return getRaw() <= val;}
401  bool operator<=(const MeshVersion& val){ return getRaw() <= val.getRaw();}
402 
403  bool operator>(const uint16_t& val){ return getRaw() > val;}
404  bool operator>(const MeshVersion& val){ return getRaw() > val.getRaw();}
405 
406  bool operator<(const uint16_t& val){ return getRaw() < val;}
407  bool operator<(const MeshVersion& val){ return getRaw() < val.getRaw();}
408 };
409 
410 
411 #endif /* INC_MESHHELPER_H_ */
412 
MeshMAC::setRaw
void setRaw(const uint32_t mac)
Sets Mesh MAC address from raw form.
Definition: meshHelper.h:274
MeshMAC::operator=
void operator=(const uint32_t &val)
Definition: meshHelper.h:298
MeshVersion::operator>
bool operator>(const uint16_t &val)
Definition: meshHelper.h:403
T_RIGHT
@ T_RIGHT
Definition: meshHelper.h:118
MeshVersion::Major
uint16_t Major
Major part of version. It's value can be between 0-63.
Definition: meshHelper.h:325
MeshPacketStatus
MeshPacketStatus
Definition: meshHelper.h:195
InlinePrintable
Interface for printing derived class collection as table with header and rows.
Definition: meshHelper.h:134
MeshVersion::setRaw
void setRaw(const uint16_t version)
Sets MeshVersion from raw form.
Definition: meshHelper.h:367
MeshNet
This class implements MeshNet protocol for microcontrollers.
Definition: mesh.h:104
MeshVersion::operator<
bool operator<(const uint16_t &val)
Definition: meshHelper.h:406
MeshVersion::operator>
bool operator>(const MeshVersion &val)
Definition: meshHelper.h:404
MeshVersion::printTo
size_t printTo(Print &p) const
Pretty prints MeshVersion.
Definition: meshHelper.cpp:158
RawPrintable::sizeOf
virtual size_t sizeOf() const
Gets size of whole current instance uncompressed (including dynamically allocated memory) in bytes.
Definition: meshHelper.h:168
MeshMAC::octet3
uint8_t octet3
Third octet of MeshMAC address.
Definition: meshHelper.h:216
MeshMAC::printTo
size_t printTo(Print &p) const
Pretty prints MeshMAC.
Definition: meshHelper.cpp:132
meshConfig.h
This file contains macros, which can MeshNet behavior can be configured.
MeshMAC::octet1
uint8_t octet1
First octet of MeshMAC address.
Definition: meshHelper.h:226
alignText
void alignText(char *text, int size, char fill, TextAlignment alignment)
Aligns text in char array.
Definition: meshHelper.cpp:97
MeshVersion::operator>=
bool operator>=(const MeshVersion &val)
Definition: meshHelper.h:398
MeshVersion::operator!=
bool operator!=(const uint16_t &val)
Definition: meshHelper.h:394
RawPrintable::printRaw
virtual size_t printRaw(OSTREAM *stream, void(*CRC_calculation)(uint8_t *, size_t)) const
Prints current instance into stream in raw bytes.
Definition: meshHelper.h:178
MeshVersion::operator<
bool operator<(const MeshVersion &val)
Definition: meshHelper.h:407
T_LEFT
@ T_LEFT
Definition: meshHelper.h:116
MeshVersion::getRaw
uint16_t getRaw() const
Gets raw form of MeshVersion.
Definition: meshHelper.h:359
MeshVersion::operator!=
bool operator!=(const MeshVersion &val)
Definition: meshHelper.h:395
MPS_Sent
@ MPS_Sent
Definition: meshHelper.h:197
MeshVersion::MeshVersion
MeshVersion()
Constructor.
Definition: meshHelper.h:330
MeshMAC::isValid
bool isValid() const
Checks if Mesh MAC address is valid.
Definition: meshHelper.h:257
T_CENTER
@ T_CENTER
Definition: meshHelper.h:117
RawPrintable
Interface for converting class or structure instance to raw bytes, which can be sent.
Definition: meshHelper.h:161
MPS_Unknown
@ MPS_Unknown
Definition: meshHelper.h:196
MeshVersion::operator==
bool operator==(const uint16_t &val)
Definition: meshHelper.h:391
OSTREAM
#define OSTREAM
Definition: meshConfig.h:41
MeshVersion::operator==
bool operator==(const MeshVersion &val)
Definition: meshHelper.h:392
RawPrintable::getRawSize
virtual size_t getRawSize() const
Gets size of data, that will be printed to stream using printRaw() in bytes.
Definition: meshHelper.h:186
MPS_WaitingForPath
@ MPS_WaitingForPath
Definition: meshHelper.h:199
InlinePrintable::printHeader
virtual size_t printHeader(OSTREAM *stream) const
Prints header of table.
Definition: meshHelper.h:142
MeshMAC
Structure that represents physical MAC address used in MeshNEt protocol. MeshMAC address unlike MAC a...
Definition: meshHelper.h:207
MeshMAC::getRaw
uint32_t getRaw() const
Gets raw form of Mesh MAC address.
Definition: meshHelper.h:265
MeshVersion::Patch
uint16_t Patch
Patch part of version. It's value can be between 0-31.
Definition: meshHelper.h:315
MeshVersion::operator<=
bool operator<=(const MeshVersion &val)
Definition: meshHelper.h:401
MeshVersion::Minor
uint16_t Minor
Minor part of version. It's value can be between 0-31.
Definition: meshHelper.h:320
MPS_Waiting
@ MPS_Waiting
Definition: meshHelper.h:198
MeshVersion::operator=
void operator=(const uint16_t &val)
Definition: meshHelper.h:383
TextAlignment
TextAlignment
Enumeration for text alignment in alignText() function.
Definition: meshHelper.h:115
MeshVersion::operator<=
bool operator<=(const uint16_t &val)
Definition: meshHelper.h:400
MeshVersion::operator>=
bool operator>=(const uint16_t &val)
Definition: meshHelper.h:397
MeshMAC::octet2
uint8_t octet2
Second octet of MeshMAC address.
Definition: meshHelper.h:221
_itoa_s
char * _itoa_s(int32_t value, char *buffer, size_t size, int radix)
MeshMAC::MeshMAC
MeshMAC()=default
Default constructor.
MeshMAC::digitToHex
static char digitToHex(uint8_t digit, bool low=true)
Converts one HEX digit (0-15) to HEX character (0-F).
Definition: meshHelper.cpp:140
MeshVersion
Structure that represents MeshNet version. Real size of this class is always 2 bytes.
Definition: meshHelper.h:310
MeshMAC::octet4
uint8_t octet4
Fourth octet of MeshMAC address.
Definition: meshHelper.h:211
InlinePrintable::printLine
virtual size_t printLine(OSTREAM *stream) const
Prints this data as row to table.
Definition: meshHelper.h:152