dataframe.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * @file dataframe.h
  3. * @brief DataFrame interface
  4. *
  5. * @date: 31. 12. 2022
  6. * @author: juraj
  7. */
  8. #ifndef __DATAFRAME_H__
  9. #define __DATAFRAME_H__
  10. #include "stdint.h"
  11. /** Character for preamble - 1st byte, if is needed */
  12. #define HEADER_CHAR1 0xCC
  13. /** Character for preamble - 2nd byte, if is needed */
  14. #define HEADER_CHAR2 0xDD
  15. #include <locale.h>
  16. #include <string.h>
  17. /**
  18. * Definition of DatFrame type.
  19. * Can be simple frame, or frame with some preamble.
  20. */
  21. typedef enum {
  22. /** Dataframe with plain content. There is no header of preamble in frame*/
  23. TYPE_PLAIN,
  24. /** Dataframe contain first byte as a preamble of packet */
  25. TYPE_HEADER_1B,
  26. /** Dataframe contain first 2 bytes as a preamble of packet */
  27. TYPE_HEADER_2B,
  28. /** Dataframe with plain content. There is no header of preamble in frame, neither packet size in first byte*/
  29. TYPE_RAW,
  30. } DataframeType_t;
  31. /**
  32. * Can globally set the CRC computation for dataframe.
  33. */
  34. typedef enum {
  35. /** DataFrame does not contain any CRC */
  36. CRC_OFF,
  37. /** DataFrame contain CRC byte as last byte of packet*/
  38. CRC_ON,
  39. } DataframeCrc_t;
  40. /**
  41. * Internal representation of error state.
  42. */
  43. typedef enum {
  44. /** There is no error in dataframe create. */
  45. ERROR_NONE,
  46. /** The dataframe has small capacity */
  47. ERROR_OVERFLOW,
  48. } DataframeError_t;
  49. /**
  50. * @brief Class for representing data frame.
  51. */
  52. class DataFrame {
  53. private:
  54. uint8_t *_frame;
  55. uint16_t _length;
  56. uint16_t _capacity;
  57. DataframeType_t _type;
  58. DataframeCrc_t _crcUse;
  59. DataframeError_t _error;
  60. bool _raw_data;
  61. public:
  62. /**
  63. * Constructor.
  64. * @param frame pointer to existing byte array
  65. * @param size size of buffer `frame`
  66. */
  67. DataFrame(uint8_t *frame, uint16_t size, DataframeType_t type, DataframeCrc_t);
  68. /**
  69. * @brief Initialize packet
  70. * - set the header of packet
  71. * - reserver 1st byte to length of frame
  72. */
  73. void Init(void);
  74. /**
  75. * @brief Add one byte to data frame.
  76. * @return true, if it is success
  77. */
  78. bool AddUint8(uint8_t d);
  79. /**
  80. * @brief add 2 Bytes length variable to dataframe
  81. * @param d variable added to data frame
  82. * @return true, if it is success
  83. */
  84. bool AddUint16(uint16_t d);
  85. /**
  86. * @brief add 4 Bytes long variable to dataframe
  87. * @param d variable added to data frame
  88. * @return true, if it is success
  89. */
  90. bool AddUint32(uint32_t);
  91. bool AddArray(uint8_t *data, uint16_t length);
  92. /**
  93. * @brief add one byte signed variable to data frame
  94. * @param d variable added to data frame
  95. * @return true, if it is success
  96. */
  97. bool AddInt8(int8_t d);
  98. /**
  99. * @brief add 2 bytes length signed variable to data frame
  100. * @param d variable added to data frame
  101. * @return true, if it is success
  102. */
  103. bool AddInt16(int16_t);
  104. /**
  105. * @brief add 4 bytes length signed variable to data frame
  106. * @param d variable added to data frame
  107. * @return true, if it is success
  108. */
  109. bool AddInt32(int32_t);
  110. /**
  111. * @brief add float value to data frame.
  112. * Float value is encoded to 4 bytes according IEEE 754
  113. * @param d variable added to data frame
  114. * @return true, if it is success
  115. */
  116. bool AddFloat(float);
  117. /**
  118. * @brief Commit the creation of the packet.
  119. * - add to second position (frame[1]) length of packet
  120. * - compute CRC of packet
  121. * @return real length of packet (including CRC byte)
  122. */
  123. uint8_t Commit(void);
  124. /**
  125. * @brief Return pointer to data frame.
  126. * @return resulting packet
  127. */
  128. uint8_t *GetFrame(void);
  129. /**
  130. * @brief Return error status from data processing
  131. * @return 0 - no error, 1 - internal buffer overflow. The buffer have to be
  132. * increased in user code.
  133. */
  134. DataframeError_t getError(void);
  135. uint16_t GetLength(void);
  136. bool IsEmpty(void);
  137. void FromArray(uint8_t *data, uint16_t size);
  138. void AddHeader(DataframeType_t type);
  139. };
  140. #ifdef __cplusplus
  141. extern "C" {
  142. #endif
  143. uint8_t crc8(uint8_t crc, uint8_t Size, uint8_t *Buffer);
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif /* DATAFRAME_H_ */