| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /*
- * @file dataframe.h
- * @brief DataFrame interface
- *
- * @date: 31. 12. 2022
- * @author: juraj
- */
- #ifndef __DATAFRAME_H__
- #define __DATAFRAME_H__
- #include "stdint.h"
- /** Character for preamble - 1st byte, if is needed */
- #define HEADER_CHAR1 0xCC
- /** Character for preamble - 2nd byte, if is needed */
- #define HEADER_CHAR2 0xDD
- #include <locale.h>
- #include <string.h>
- /**
- * Definition of DatFrame type.
- * Can be simple frame, or frame with some preamble.
- */
- typedef enum {
- /** Dataframe with plain content. There is no header of preamble in frame*/
- TYPE_PLAIN,
- /** Dataframe contain first byte as a preamble of packet */
- TYPE_HEADER_1B,
- /** Dataframe contain first 2 bytes as a preamble of packet */
- TYPE_HEADER_2B,
- /** Dataframe with plain content. There is no header of preamble in frame, neither packet size in first byte*/
- TYPE_RAW,
- } DataframeType_t;
- /**
- * Can globally set the CRC computation for dataframe.
- */
- typedef enum {
- /** DataFrame does not contain any CRC */
- CRC_OFF,
- /** DataFrame contain CRC byte as last byte of packet*/
- CRC_ON,
- } DataframeCrc_t;
- /**
- * Internal representation of error state.
- */
- typedef enum {
- /** There is no error in dataframe create. */
- ERROR_NONE,
- /** The dataframe has small capacity */
- ERROR_OVERFLOW,
- } DataframeError_t;
- /**
- * @brief Class for representing data frame.
- */
- class DataFrame {
- private:
- uint8_t *_frame;
- uint16_t _length;
- uint16_t _capacity;
- DataframeType_t _type;
- DataframeCrc_t _crcUse;
- DataframeError_t _error;
- bool _raw_data;
- public:
- /**
- * Constructor.
- * @param frame pointer to existing byte array
- * @param size size of buffer `frame`
- */
- DataFrame(uint8_t *frame, uint16_t size, DataframeType_t type, DataframeCrc_t);
- /**
- * @brief Initialize packet
- * - set the header of packet
- * - reserver 1st byte to length of frame
- */
- void Init(void);
- /**
- * @brief Add one byte to data frame.
- * @return true, if it is success
- */
- bool AddUint8(uint8_t d);
- /**
- * @brief add 2 Bytes length variable to dataframe
- * @param d variable added to data frame
- * @return true, if it is success
- */
- bool AddUint16(uint16_t d);
- /**
- * @brief add 4 Bytes long variable to dataframe
- * @param d variable added to data frame
- * @return true, if it is success
- */
- bool AddUint32(uint32_t);
- bool AddArray(uint8_t *data, uint16_t length);
- /**
- * @brief add one byte signed variable to data frame
- * @param d variable added to data frame
- * @return true, if it is success
- */
- bool AddInt8(int8_t d);
- /**
- * @brief add 2 bytes length signed variable to data frame
- * @param d variable added to data frame
- * @return true, if it is success
- */
- bool AddInt16(int16_t);
- /**
- * @brief add 4 bytes length signed variable to data frame
- * @param d variable added to data frame
- * @return true, if it is success
- */
- bool AddInt32(int32_t);
- /**
- * @brief add float value to data frame.
- * Float value is encoded to 4 bytes according IEEE 754
- * @param d variable added to data frame
- * @return true, if it is success
- */
- bool AddFloat(float);
- /**
- * @brief Commit the creation of the packet.
- * - add to second position (frame[1]) length of packet
- * - compute CRC of packet
- * @return real length of packet (including CRC byte)
- */
- uint8_t Commit(void);
- /**
- * @brief Return pointer to data frame.
- * @return resulting packet
- */
- uint8_t *GetFrame(void);
- /**
- * @brief Return error status from data processing
- * @return 0 - no error, 1 - internal buffer overflow. The buffer have to be
- * increased in user code.
- */
- DataframeError_t getError(void);
- uint16_t GetLength(void);
- bool IsEmpty(void);
- void FromArray(uint8_t *data, uint16_t size);
- void AddHeader(DataframeType_t type);
- };
- #ifdef __cplusplus
- extern "C" {
- #endif
- uint8_t crc8(uint8_t crc, uint8_t Size, uint8_t *Buffer);
- #ifdef __cplusplus
- }
- #endif
- #endif /* DATAFRAME_H_ */
|