l2/packet_structs: add function to encode a complete packet
This commit is contained in:
parent
8c79185df6
commit
0345dc589b
|
@ -8,9 +8,13 @@
|
|||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <liquid/liquid.h>
|
||||
|
||||
#define LOGGER_MODULE_NAME "l2ps"
|
||||
#include "logger.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "packet_structs.h"
|
||||
|
||||
|
||||
|
@ -129,3 +133,36 @@ void layer2_dump_packet_header(int level, const layer2_packet_header_t *header)
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
size_t layer2_encode_packet(
|
||||
const layer2_packet_header_t *header,
|
||||
uint8_t *payload, size_t payload_len,
|
||||
uint8_t *data_out, size_t data_out_len)
|
||||
{
|
||||
unsigned int crc_size = crc_sizeof_key(PAYLOAD_CRC_SCHEME);
|
||||
|
||||
if(data_out_len < LAYER2_PACKET_HEADER_ENCODED_SIZE_MAX) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t packet_size = layer2_encode_packet_header(header, data_out);
|
||||
|
||||
if(data_out_len < packet_size + crc_size + payload_len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// add the payload data
|
||||
if(payload) {
|
||||
memcpy(data_out + packet_size, payload, payload_len);
|
||||
}
|
||||
|
||||
packet_size += payload_len;
|
||||
|
||||
// calculate CRC of everything and append it to the packet
|
||||
crc_append_key(PAYLOAD_CRC_SCHEME, data_out, packet_size);
|
||||
|
||||
packet_size += crc_size;
|
||||
|
||||
return packet_size;
|
||||
}
|
||||
|
|
|
@ -74,6 +74,19 @@ bool layer2_decode_packet_header(const uint8_t *encoded, size_t encoded_len, lay
|
|||
*/
|
||||
void layer2_dump_packet_header(int level, const layer2_packet_header_t *header);
|
||||
|
||||
/*!\brief Encode a complete packet (with header and CRC)
|
||||
* \param header The header structure to encode.
|
||||
* \param payload The payload data to encode.
|
||||
* \param payload_len The length of the payload data.
|
||||
* \param data_out The buffer for the encoded packet data.
|
||||
* \param data_out_len The size of the output buffer.
|
||||
* \returns The number of bytes written to the output buffer or 0 if not enough space was available.
|
||||
*/
|
||||
size_t layer2_encode_packet(
|
||||
const layer2_packet_header_t *header,
|
||||
uint8_t *payload, size_t payload_len,
|
||||
uint8_t *data_out, size_t data_out_len);
|
||||
|
||||
/*!\brief Get a string representation of the given message type.
|
||||
*/
|
||||
const char* layer2_msg_type_to_string(layer2_message_type_t type);
|
||||
|
|
Loading…
Reference in a new issue