68#define SNAP_ERROR_NULL_FRAME (-1)
69#define SNAP_ERROR_NULL_BUFFER (-2)
70#define SNAP_ERROR_SHORT_BUFFER (-3)
71#define SNAP_ERROR_UNKNOWN_FORMAT (-4)
72#define SNAP_ERROR_FRAME_FORMAT (-5)
73#define SNAP_ERROR_SHORT_FRAME (-6)
74#define SNAP_ERROR_FIELD_TYPE (-7)
82#define SNAP_HDB2_DAB_MASK (0x03U)
83#define SNAP_HDB2_DAB_POS (6U)
85#define SNAP_HDB2_SAB_MASK (0x03U)
86#define SNAP_HDB2_SAB_POS (4U)
88#define SNAP_HDB2_PFB_MASK (0x03U)
89#define SNAP_HDB2_PFB_POS (2U)
91#define SNAP_HDB2_ACK_MASK (0x03U)
92#define SNAP_HDB2_ACK_POS (0U)
100#define SNAP_HDB1_CMD_MASK (0x01U)
101#define SNAP_HDB1_CMD_POS (7U)
103#define SNAP_HDB1_EDM_MASK (0x07U)
104#define SNAP_HDB1_EDM_POS (4U)
106#define SNAP_HDB1_NDB_MASK (0x0FU)
107#define SNAP_HDB1_NDB_POS (0U)
115#define SNAP_GET_BITS(integer, mask, pos) (((unsigned int)(integer) >> (pos)) & (mask))
117#define SNAP_HDB2(pByteArray) ((pByteArray)[SNAP_INDEX_HDB2])
118#define SNAP_HDB1(pByteArray) ((pByteArray)[SNAP_INDEX_HDB1])
120#define SNAP_HDB2_DAB(pByteArray) (SNAP_GET_BITS(SNAP_HDB2(pByteArray), SNAP_HDB2_DAB_MASK, SNAP_HDB2_DAB_POS))
121#define SNAP_HDB2_SAB(pByteArray) (SNAP_GET_BITS(SNAP_HDB2(pByteArray), SNAP_HDB2_SAB_MASK, SNAP_HDB2_SAB_POS))
122#define SNAP_HDB2_PFB(pByteArray) (SNAP_GET_BITS(SNAP_HDB2(pByteArray), SNAP_HDB2_PFB_MASK, SNAP_HDB2_PFB_POS))
123#define SNAP_HDB2_ACK(pByteArray) (SNAP_GET_BITS(SNAP_HDB2(pByteArray), SNAP_HDB2_ACK_MASK, SNAP_HDB2_ACK_POS))
125#define SNAP_HDB1_CMD(pByteArray) (SNAP_GET_BITS(SNAP_HDB1(pByteArray), SNAP_HDB1_CMD_MASK, SNAP_HDB1_CMD_POS))
126#define SNAP_HDB1_EDM(pByteArray) (SNAP_GET_BITS(SNAP_HDB1(pByteArray), SNAP_HDB1_EDM_MASK, SNAP_HDB1_EDM_POS))
127#define SNAP_HDB1_NDB(pByteArray) (SNAP_GET_BITS(SNAP_HDB1(pByteArray), SNAP_HDB1_NDB_MASK, SNAP_HDB1_NDB_POS))
135#define SNAP_SIZE_SYNC (1U)
136#define SNAP_SIZE_HDB2 (1U)
137#define SNAP_SIZE_HDB1 (1U)
138#define SNAP_SIZE_HEADER (SNAP_SIZE_HDB2 + SNAP_SIZE_HDB1)
139#define SNAP_SIZE_DATA(pByteArray) (snap_getDataSizeFromNdb(SNAP_HDB1_NDB(pByteArray)))
140#define SNAP_SIZE_HASH(pByteArray) (snap_getHashSizeFromEdm(SNAP_HDB1_EDM(pByteArray)))
141#define SNAP_MIN_SIZE_FRAME (SNAP_SIZE_SYNC + SNAP_SIZE_HEADER)
142#define SNAP_MAX_SIZE_FRAME (528U)
144#ifdef SNAP_SIZE_USER_HASH
145 #if (SNAP_SIZE_USER_HASH < 0) || (SNAP_SIZE_USER_HASH > 4)
146 #error Invalid user hash size! It must be an integer from 0 to 4 (bytes).
149 #define SNAP_SIZE_USER_HASH (0U)
158#define SNAP_INDEX_SYNC (0U)
159#define SNAP_INDEX_HDB2 (1U)
160#define SNAP_INDEX_HDB1 (2U)
161#define SNAP_INDEX_DAB (3U)
162#define SNAP_INDEX_SAB(pByteArray) (SNAP_INDEX_DAB + SNAP_HDB2_DAB(pByteArray))
163#define SNAP_INDEX_PFB(pByteArray) (SNAP_INDEX_SAB(pByteArray) + SNAP_HDB2_SAB(pByteArray))
164#define SNAP_INDEX_DATA(pByteArray) (SNAP_INDEX_PFB(pByteArray) + SNAP_HDB2_PFB(pByteArray))
165#define SNAP_INDEX_HASH(pByteArray) (SNAP_INDEX_DATA(pByteArray) + SNAP_SIZE_DATA(pByteArray))
173#define SNAP_BROADCAST_ADDRESS (0U)
174#define SNAP_SYNC (0x54U)
175#define SNAP_PADDING (0x00U)
189#define snap_getSync() (SNAP_SYNC)
190#define snap_getHdb2(pFrame) (SNAP_HDB2((pFrame)->buffer))
191#define snap_getHdb1(pFrame) (SNAP_HDB1((pFrame)->buffer))
192#define snap_getDab(pFrame) (SNAP_HDB2_DAB((pFrame)->buffer))
193#define snap_getSab(pFrame) (SNAP_HDB2_SAB((pFrame)->buffer))
194#define snap_getPfb(pFrame) (SNAP_HDB2_PFB((pFrame)->buffer))
195#define snap_getAck(pFrame) (SNAP_HDB2_ACK((pFrame)->buffer))
196#define snap_getCmd(pFrame) (SNAP_HDB1_CMD((pFrame)->buffer))
197#define snap_getEdm(pFrame) (SNAP_HDB1_EDM((pFrame)->buffer))
198#define snap_getNdb(pFrame) (SNAP_HDB1_NDB((pFrame)->buffer))
199#define snap_getHeader(pFrame, pHeader) (snap_getField(pFrame, pHeader, SNAP_FIELD_HEADER))
200#define snap_getDestAddress(pFrame, pDestAddr) (snap_getField(pFrame, pDestAddr, SNAP_FIELD_DEST_ADDRESS))
201#define snap_getSourceAddress(pFrame, pSourceAddr) (snap_getField(pFrame, pSourceAddr, SNAP_FIELD_SOURCE_ADDRESS))
202#define snap_getProtocolFlags(pFrame, pFlags) (snap_getField(pFrame, pFlags, SNAP_FIELD_PROTOCOL_FLAGS))
203#define snap_getData(pFrame, pData) (snap_getField(pFrame, pData, SNAP_FIELD_DATA))
204#define snap_getHash(pFrame, pHash) (snap_getField(pFrame, pHash, SNAP_FIELD_HASH))
205#define snap_getDataPtr(pFrame) ((pFrame)->buffer + SNAP_INDEX_DATA((pFrame)->buffer))
206#define snap_getBufferPtr(pFrame) ((pFrame)->buffer)
207#define snap_getStatus(pFrame) ((pFrame)->status)
215#define snap_getSyncIndex() (SNAP_INDEX_SYNC)
216#define snap_getHdb2Index() (SNAP_INDEX_HDB2)
217#define snap_getHdb1Index() (SNAP_INDEX_HDB1)
218#define snap_getHeaderIndex() (SNAP_INDEX_HDB2)
219#define snap_getDestAddrIndex() (SNAP_INDEX_DAB)
220#define snap_getSourceAddrIndex(pFrame) (SNAP_INDEX_SAB((pFrame)->buffer))
221#define snap_getProtFlagsIndex(pFrame) (SNAP_INDEX_PFB((pFrame)->buffer))
222#define snap_getDataIndex(pFrame) (SNAP_INDEX_DATA((pFrame)->buffer))
223#define snap_getHashIndex(pFrame) (SNAP_INDEX_HASH((pFrame)->buffer))
231#define snap_getSyncSize() (SNAP_SIZE_SYNC)
232#define snap_getHdb2Size() (SNAP_SIZE_HDB2)
233#define snap_getHdb1Size() (SNAP_SIZE_HDB1)
234#define snap_getHeaderSize() (SNAP_SIZE_HEADER)
235#define snap_getDestAddrSize(pFrame) (SNAP_HDB2_DAB((pFrame)->buffer))
236#define snap_getSourceAddrSize(pFrame) (SNAP_HDB2_SAB((pFrame)->buffer))
237#define snap_getProtFlagsSize(pFrame) (SNAP_HDB2_PFB((pFrame)->buffer))
238#define snap_getDataSize(pFrame) (SNAP_SIZE_DATA((pFrame)->buffer))
239#define snap_getHashSize(pFrame) (SNAP_SIZE_HASH((pFrame)->buffer))
240#define snap_getFrameSize(pFrame) ((pFrame)->size)
241#define snap_getBufferSize(pFrame) ((pFrame)->maxSize)
242#define snap_getFullFrameSize(pFrame) (SNAP_INDEX_HASH((pFrame)->buffer) + SNAP_SIZE_HASH((pFrame)->buffer))
int8_t snap_encapsulate(snap_frame_t *frame, snap_fields_t *fields)
Encapsulate a new frame into the buffer (if there is enough space). Update the frame status and size ...
Definition: snap.c:180
uint16_t snap_calculateCrc16(const uint8_t *data, const uint16_t size)
Calculate the 16-bit CRC of a byte array.
Definition: snap.c:599
uint16_t snap_getDataSizeFromNdb(const uint8_t ndb)
Get the number of data bytes based on the NDB bits of the HDB1 byte.
Definition: snap.c:472
void snap_reset(snap_frame_t *frame)
Reset the frame size and status. The other variables of the frame remain unchanged.
Definition: snap.c:102
int16_t snap_getField(const snap_frame_t *frame, void *fieldContent, const uint8_t fieldType)
Get the content of a selected frame field.
Definition: snap.c:284
uint16_t snap_removePaddingBytes(uint8_t *data, uint16_t size, const bool paddingAfter)
Remove the padding bytes (SNAP_PADDING) of a frame payload (if there are any).
Definition: snap.c:423
uint8_t snap_calculateCrc8(const uint8_t *data, const uint16_t size)
Calculate the 8-bit CRC of a byte array.
Definition: snap.c:526
uint32_t snap_calculateUserHash(const uint8_t *data, const uint16_t size)
Calculate the hash value of a byte array using a user-defined algorithm.
Definition: snap.c:768
uint32_t snap_calculateCrc32(const uint8_t *data, const uint16_t size)
Calculate the 32-bit CRC of a byte array.
Definition: snap.c:679
uint8_t snap_calculateChecksum8(const uint8_t *data, const uint16_t size)
Calculate the 8-bit checksum of a byte array.
Definition: snap.c:496
int16_t snap_init(snap_frame_t *frame, uint8_t *buffer, const uint16_t maxSize)
Initialize the frame structure.
Definition: snap.c:81
uint8_t snap_getHashSizeFromEdm(const uint8_t edm)
Get the hash value size of the frame based on the EDM bits of the HDB1 byte.
Definition: snap.c:484
int8_t snap_calculateHash(const snap_frame_t *frame, uint32_t *hash)
Select the error detection method based on the EDM bits and calculate the hash value of a frame.
Definition: snap.c:369
uint8_t snap_getNdbFromDataSize(const uint16_t dataSize)
Get the NDB bits of the HDB1 byte based on the number of data bytes (with or without padding bytes).
Definition: snap.c:455
int8_t snap_decode(snap_frame_t *frame, const uint8_t newByte)
Detect, decode, validate and store a frame, one byte at a time.
Definition: snap.c:119
snap_status_t
Values for the frame status. The frame structure will always be in one of these states.
Definition: snap.h:380
snap_hdb2_dab_t
Values for the DAB field of the HDB2 header byte. It defines the number of destination address bytes.
Definition: snap.h:279
snap_hdb1_edm_t
Values for the EDM field of the HDB1 header byte. It defines the error detection method used on the f...
Definition: snap.h:341
snap_fieldType_t
Values that identify each field of a frame.
Definition: snap.h:266
snap_hdb2_pfb_t
Values for the PFB field of the HDB2 header byte. It defines the number of protocol specific flags by...
Definition: snap.h:301
snap_hdb1_cmd_t
Values for the CMD field of the HDB1 header byte. It indicates whether the command mode is enabled or...
Definition: snap.h:331
snap_hdb2_ack_t
Values for the ACK field of the HDB2 header byte. It defines if the sending node requests an ACK/NACK...
Definition: snap.h:313
snap_hdb2_sab_t
Values for the SAB field of the HDB2 header byte. It defines the number of source address bytes.
Definition: snap.h:290
snap_hdb1_ndb_t
Values for the NDB field of the HDB1 header byte. It defines the number of data/payload bytes.
Definition: snap.h:357
@ SNAP_STATUS_VALID
Definition: snap.h:383
@ SNAP_STATUS_INCOMPLETE
Definition: snap.h:382
@ SNAP_STATUS_ERROR_OVERFLOW
Definition: snap.h:385
@ SNAP_STATUS_ERROR_HASH
Definition: snap.h:384
@ SNAP_STATUS_IDLE
Definition: snap.h:381
@ SNAP_HDB2_DAB_2BYTE_DEST_ADDRESS
Definition: snap.h:282
@ SNAP_HDB2_DAB_3BYTE_DEST_ADDRESS
Definition: snap.h:283
@ SNAP_HDB2_DAB_NO_DEST_ADDRESS
Definition: snap.h:280
@ SNAP_HDB2_DAB_1BYTE_DEST_ADDRESS
Definition: snap.h:281
@ SNAP_HDB1_EDM_USER_SPECIFIED
Definition: snap.h:349
@ SNAP_HDB1_EDM_8BIT_CHECKSUM
Definition: snap.h:344
@ SNAP_HDB1_EDM_3_RETRANSMISSION
Definition: snap.h:343
@ SNAP_HDB1_EDM_32BIT_CRC
Definition: snap.h:347
@ SNAP_HDB1_EDM_NO_ERROR_DETECTION
Definition: snap.h:342
@ SNAP_HDB1_EDM_16BIT_CRC
Definition: snap.h:346
@ SNAP_HDB1_EDM_8BIT_CRC
Definition: snap.h:345
@ SNAP_HDB1_EDM_FEC
Definition: snap.h:348
@ SNAP_FIELD_DATA
Definition: snap.h:271
@ SNAP_FIELD_SOURCE_ADDRESS
Definition: snap.h:269
@ SNAP_FIELD_HEADER
Definition: snap.h:267
@ SNAP_FIELD_HASH
Definition: snap.h:272
@ SNAP_FIELD_PROTOCOL_FLAGS
Definition: snap.h:270
@ SNAP_FIELD_DEST_ADDRESS
Definition: snap.h:268
@ SNAP_HDB2_PFB_1BYTE_PROTOCOL_FLAGS
Definition: snap.h:303
@ SNAP_HDB2_PFB_NO_PROTOCOL_FLAGS
Definition: snap.h:302
@ SNAP_HDB2_PFB_2BYTE_PROTOCOL_FLAGS
Definition: snap.h:304
@ SNAP_HDB2_PFB_3BYTE_PROTOCOL_FLAGS
Definition: snap.h:305
@ SNAP_HDB1_CMD_MODE_DISABLED
Definition: snap.h:332
@ SNAP_HDB1_CMD_MODE_ENABLED
Definition: snap.h:333
@ SNAP_HDB2_ACK_RESPONSE_ACK
Definition: snap.h:316
@ SNAP_HDB2_ACK_RESPONSE_NACK
Definition: snap.h:317
@ SNAP_HDB2_ACK_NOT_REQUESTED
Definition: snap.h:314
@ SNAP_HDB2_ACK_REQUESTED
Definition: snap.h:315
@ SNAP_HDB2_SAB_NO_SOURCE_ADDRESS
Definition: snap.h:291
@ SNAP_HDB2_SAB_2BYTE_SOURCE_ADDRESS
Definition: snap.h:293
@ SNAP_HDB2_SAB_1BYTE_SOURCE_ADDRESS
Definition: snap.h:292
@ SNAP_HDB2_SAB_3BYTE_SOURCE_ADDRESS
Definition: snap.h:294
@ SNAP_HDB1_NDB_2BYTE_DATA
Definition: snap.h:360
@ SNAP_HDB1_NDB_16BYTE_DATA
Definition: snap.h:367
@ SNAP_HDB1_NDB_3BYTE_DATA
Definition: snap.h:361
@ SNAP_HDB1_NDB_8BYTE_DATA
Definition: snap.h:366
@ SNAP_HDB1_NDB_5BYTE_DATA
Definition: snap.h:363
@ SNAP_HDB1_NDB_7BYTE_DATA
Definition: snap.h:365
@ SNAP_HDB1_NDB_6BYTE_DATA
Definition: snap.h:364
@ SNAP_HDB1_NDB_512BYTE_DATA
Definition: snap.h:372
@ SNAP_HDB1_NDB_1BYTE_DATA
Definition: snap.h:359
@ SNAP_HDB1_NDB_256BYTE_DATA
Definition: snap.h:371
@ SNAP_HDB1_NDB_USER_SPECIFIED
Definition: snap.h:373
@ SNAP_HDB1_NDB_128BYTE_DATA
Definition: snap.h:370
@ SNAP_HDB1_NDB_NO_DATA
Definition: snap.h:358
@ SNAP_HDB1_NDB_64BYTE_DATA
Definition: snap.h:369
@ SNAP_HDB1_NDB_4BYTE_DATA
Definition: snap.h:362
@ SNAP_HDB1_NDB_32BYTE_DATA
Definition: snap.h:368
Structure that stores every data necessary to build a new frame (i.e. every field value and size,...
Definition: snap.h:408
uint16_t dataSize
Number of bytes in the data array.
Definition: snap.h:414
bool paddingAfter
Position of the padding bytes in the payload (if there are any). true = padding after data,...
Definition: snap.h:415
snap_header_t header
Header bytes.
Definition: snap.h:413
uint32_t sourceAddress
Source address (up to 0xFFFFFF).
Definition: snap.h:411
uint32_t protocolFlags
Protocol specific flags (up to 0xFFFFFF).
Definition: snap.h:412
uint32_t destAddress
Destination address (up to 0xFFFFFF).
Definition: snap.h:410
uint8_t * data
Pointer to the array that holds the frame data/payload.
Definition: snap.h:409
This is the main structure of the library, used in frame decoding, encapsulation, and decapsulation.
Definition: snap.h:422
uint16_t maxSize
Maximum number of bytes that can be stored in the buffer.
Definition: snap.h:424
uint16_t size
Current size of the frame (it may be incomplete).
Definition: snap.h:425
int8_t status
Status of the frame, used primarily in the decoding process. It can assume any value from snap_status...
Definition: snap.h:426
uint8_t * buffer
Pointer to the array that stores all the bytes of the frame.
Definition: snap.h:423