layer2_structs: use the new HAM-64 address structure

This commit is contained in:
Thomas Kolb 2024-07-01 21:25:49 +02:00
parent 4bb4017623
commit f17d5649fc
2 changed files with 16 additions and 16 deletions

View file

@ -6,8 +6,8 @@
size_t layer2_encode_packet_header(const layer2_packet_header_t *header, uint8_t *encoded)
{
assert(header->src_addr_len < 4);
assert(header->dst_addr_len < 4);
assert(header->src_addr.length <= 4 && header->src_addr.length != 0);
assert(header->dst_addr.length <= 4 && header->dst_addr.length != 0);
assert(header->tx_seq_nr < 16);
assert(header->rx_seq_nr < 16);
@ -16,18 +16,18 @@ size_t layer2_encode_packet_header(const layer2_packet_header_t *header, uint8_t
encoded[0] =
((uint8_t)header->msg_type & 0x7) << 5
| ((uint8_t)header->tx_request & 0x1) << 4
| (header->src_addr_len & 0x3) << 2
| (header->dst_addr_len & 0x3) << 0;
| ((header->src_addr.length-1) & 0x3) << 2
| ((header->dst_addr.length-1) & 0x3) << 0;
encoded[1] =
(header->tx_seq_nr & 0xF) << 4
| (header->rx_seq_nr & 0xF) << 0;
memcpy(encoded + encoded_size, header->src_addr, 2 * header->src_addr_len);
encoded_size += 2 * header->src_addr_len;
memcpy(encoded + encoded_size, header->src_addr.addr, 2 * header->src_addr.length);
encoded_size += 2 * header->src_addr.length;
memcpy(encoded + encoded_size, header->dst_addr, 2 * header->dst_addr_len);
encoded_size += 2 * header->dst_addr_len;
memcpy(encoded + encoded_size, header->dst_addr.addr, 2 * header->dst_addr.length);
encoded_size += 2 * header->dst_addr.length;
return encoded_size;
}
@ -37,14 +37,14 @@ bool layer2_decode_packet_header(const uint8_t *encoded, layer2_packet_header_t
{
header->msg_type = (encoded[0] >> 5) & 0x7;
header->tx_request = (encoded[0] & 0x10) != 0;
header->src_addr_len = (encoded[0] >> 2) & 0x3;
header->dst_addr_len = (encoded[0] >> 0) & 0x3;
header->src_addr.length = (encoded[0] >> 2) & 0x3 + 1;
header->dst_addr.length = (encoded[0] >> 0) & 0x3 + 1;
header->tx_seq_nr = (encoded[1] >> 4) & 0xF;
header->tx_seq_nr = (encoded[1] >> 0) & 0xF;
memcpy(header->src_addr, encoded + 2, 2 * header->src_addr_len);
memcpy(header->dst_addr, encoded + 2 + 2 * header->src_addr_len, 2 * header->dst_addr_len);
memcpy(header->src_addr.addr, encoded + 2, 2 * header->src_addr.length);
memcpy(header->dst_addr.addr, encoded + 2 + 2 * header->src_addr.length, 2 * header->dst_addr.length);
return true;
}

View file

@ -5,6 +5,8 @@
#include <stddef.h>
#include <stdbool.h>
#include "layer2/ham64.h"
/* Common Link-layer Header */
typedef enum {
@ -16,14 +18,12 @@ typedef enum {
typedef struct layer2_packet_header_s {
layer2_message_type_t msg_type; //!< message type
bool tx_request; //!< transmission request (marks the end of the burst)
uint8_t src_addr_len; //!< source address length (in 16-bit words)
uint8_t dst_addr_len; //!< destination address length (in 16-bit words)
uint8_t tx_seq_nr; //!< sequence number of this packet
uint8_t rx_seq_nr; //!< sequence number expected next from he other side
uint8_t src_addr[8]; //!< source HAM-64 address
uint8_t dst_addr[8]; //!< destination HAM-64 address
ham64_t src_addr; //!< source HAM-64 address
ham64_t dst_addr; //!< destination HAM-64 address
} layer2_packet_header_t;
/*!\brief Encode a layer2 packet header to the transmitted form.