ESP32-P4 OBC Firmware
ESP-IDF firmware for Plant-B CubeSat OBC
 
Loading...
Searching...
No Matches
packet_utils.c File Reference

Plant-B application layer protocol - packet encoding and decoding. More...

#include "packet_utils.h"

Data Structures

struct  packet
 
struct  tx_strip_pkt
 
struct  pkt_enc_pool
 
struct  rx_strip_pkt
 
struct  pkt_dec_pool
 

Macros

#define U16_BYTES   (2)
 
#define XOR_BYTES   (53)
 
#define POOL_DEPTH   (32)
 
#define SYNC8   (0xAA)
 
#define ALL_KNOWN_MASK   (0xFFFFFFFF)
 
#define XOR_LOCATIONS   { 0, 3, 7, 15, 31 }
 
#define XOR_GROUP_MASK   (0x80008088)
 
#define ONLY_N03_UNKNOWN_MASK   (XOR_GROUP_MASK & ~(1U << (3)))
 
#define ONLY_N07_UNKNOWN_MASK   (XOR_GROUP_MASK & ~(1U << (7)))
 
#define ONLY_N15_UNKNOWN_MASK   (XOR_GROUP_MASK & ~(1U << (15)))
 
#define ONLY_N31_UNKNOWN_MASK   (XOR_GROUP_MASK & ~(1U << (31)))
 
#define RAND_THRESHOLD   (5)
 
#define BYTES_STR_SIZE(n)
 
#define PKT_CHECK(cond, tag, msg)
 

Functions

static const char * domain_to_str (domain_t domain)
 
static const char * category_to_str (domain_t domain, category_t category)
 
static const char * type_to_str (domain_t domain, category_t category, type_t type)
 
static void bytes_to_str (const uint8_t *restrict buffer, size_t sz, char *restrict out, size_t out_sz)
 
static void enc_pool_update_slot (pkt_enc_pool_t *restrict pool, const packet_t *restrict pkt)
 
static const struct tx_strip_pktenc_pool_get_slot_n (const pkt_enc_pool_t *restrict pool, uint8_t n)
 
static void dec_pool_update_slot (pkt_dec_pool_t *restrict pool, const packet_t *restrict pkt)
 
static void dec_pool_mark_known (pkt_dec_pool_t *restrict pool, const packet_t *restrict pkt)
 
static bool dec_pool_has_unknown_pkt (const pkt_dec_pool_t *restrict pool)
 
static const struct rx_strip_pktdec_pool_get_slot_n (const pkt_dec_pool_t *restrict pool, uint16_t n)
 
static int8_t dec_pool_try_recovery (pkt_dec_pool_t *restrict pool)
 
static void pkt_set_xor_part (const pkt_enc_pool_t *restrict pool, packet_t *restrict pkt)
 
static void pkt_set_sync8 (packet_t *restrict pkt)
 
static uint8_t pkt_get_sync8 (const packet_t *restrict pkt)
 
static bool pkt_sync8_is_valid (const packet_t *restrict pkt)
 
static uint16_t pkt_compute_crc16_ccitt_false (const packet_t *restrict pkt)
 
static void pkt_set_crc16 (packet_t *restrict pkt)
 
static uint16_t pkt_get_crc16 (const packet_t *restrict pkt)
 
static bool pkt_crc16_is_valid (const packet_t *restrict pkt)
 
packet_tpkt_create (void)
 Allocate and initialize a packet, with each byte set to 0.
 
void pkt_destroy (packet_t *restrict pkt)
 Free a packet allocated by pkt_create().
 
pkt_enc_pool_tenc_pool_create (void)
 Allocate and initialize an encoder pool.
 
void enc_pool_destroy (pkt_enc_pool_t *restrict pool)
 Free an encoder pool allocated by enc_pool_create().
 
pkt_dec_pool_tdec_pool_create (void)
 Allocate and initialize a decoder pool.
 
void dec_pool_destroy (pkt_dec_pool_t *restrict pool)
 Free a decoder pool allocated by dec_pool_create().
 
pkt_err_t pkt_encode (packet_t *restrict pkt, pkt_enc_pool_t *restrict pool)
 Finalize and encode a packet for transmission.
 
pkt_err_t pkt_decode (const packet_t *restrict pkt, pkt_dec_pool_t *restrict pool, int8_t *restrict relative_loc)
 Validate and decode a received packet.
 
pkt_err_t dec_pool_get_pkt_from_relative_loc (const pkt_dec_pool_t *restrict pool, int8_t relative_loc, packet_t *restrict pkt)
 Reconstruct a recovered packet from the decoder pool.
 
pkt_err_t pkt_get_id (const packet_t *restrict pkt, uint16_t *id)
 Read the packet identifier.
 
pkt_err_t header_sensor_config (header_t *restrict header)
 Configure a header for a current sensor measurement packet.
 
pkt_err_t header_old_sensor_config (header_t *restrict header, uint16_t old_id)
 Configure a header for a retransmitted (old) sensor measurement packet.
 
pkt_err_t pkt_get_header (const packet_t *restrict pkt, header_t *header)
 Unpack the header fields from a raw packet.
 
pkt_err_t pkt_set_header (packet_t *restrict pkt, const header_t *restrict header)
 Pack header fields into a raw packet.
 
pkt_err_t pkt_get_payload (const packet_t *restrict pkt, uint8_t *payload, size_t len)
 Copy the payload field out of a packet.
 
pkt_err_t pkt_set_payload (packet_t *restrict pkt, const uint8_t *restrict payload, size_t len)
 Copy data into the payload field of a packet.
 
bool pkt_is_chunked (const packet_t *restrict pkt)
 Check whether a packet carries chunked data.
 
pkt_err_t pkt_log (const packet_t *restrict pkt)
 Log a human-readable dump of a packet to the PAL logger.
 
pkt_err_t pkt_test_random_set_sync8 (packet_t *restrict pkt, bool *failed)
 Randomly corrupt the SYNC8 field for testing purposes.
 
pkt_err_t pkt_test_random_set_crc16 (packet_t *restrict pkt)
 Randomly corrupt the CRC16 field for testing purposes.
 

Variables

static const char * TAG = "pkt_utils"
 
static _Atomic uint16_t id = 0
 

Detailed Description

Plant-B application layer protocol - packet encoding and decoding.

Author
Adrien Chevrier

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

Version
0.1
Date
2026-02-05

Data Structure Documentation

◆ packet

struct packet
Data Fields
uint8_t crc16[U16_BYTES]
uint8_t dct
uint8_t id[U16_BYTES]
uint8_t index[U16_BYTES]
uint8_t payload[PAYLOAD_BYTES]
uint8_t sync8
uint8_t total[U16_BYTES]
uint8_t xor_part[XOR_BYTES]

◆ tx_strip_pkt

struct tx_strip_pkt
Data Fields
uint8_t dct
uint8_t index[U16_BYTES]
uint8_t payload[PAYLOAD_BYTES]
uint8_t total[U16_BYTES]

◆ pkt_enc_pool

struct pkt_enc_pool
Data Fields
struct tx_strip_pkt * pkt
struct tx_strip_pkt pool[POOL_DEPTH]

◆ rx_strip_pkt

struct rx_strip_pkt
Data Fields
uint8_t dct
uint8_t index[U16_BYTES]
uint8_t payload[PAYLOAD_BYTES]
uint8_t total[U16_BYTES]
uint8_t xor_part[XOR_BYTES]

◆ pkt_dec_pool

struct pkt_dec_pool
Data Fields
uint32_t known
struct rx_strip_pkt * pkt
struct rx_strip_pkt pool[POOL_DEPTH]

Macro Definition Documentation

◆ ALL_KNOWN_MASK

#define ALL_KNOWN_MASK   (0xFFFFFFFF)

◆ BYTES_STR_SIZE

#define BYTES_STR_SIZE ( n)
Value:
((n) * 3 + 1)

◆ ONLY_N03_UNKNOWN_MASK

#define ONLY_N03_UNKNOWN_MASK   (XOR_GROUP_MASK & ~(1U << (3)))

◆ ONLY_N07_UNKNOWN_MASK

#define ONLY_N07_UNKNOWN_MASK   (XOR_GROUP_MASK & ~(1U << (7)))

◆ ONLY_N15_UNKNOWN_MASK

#define ONLY_N15_UNKNOWN_MASK   (XOR_GROUP_MASK & ~(1U << (15)))

◆ ONLY_N31_UNKNOWN_MASK

#define ONLY_N31_UNKNOWN_MASK   (XOR_GROUP_MASK & ~(1U << (31)))

◆ PKT_CHECK

#define PKT_CHECK ( cond,
tag,
msg )
Value:
do { \
if (!(cond)) { \
PAL_LOGE(tag, msg); \
} \
} while (0)
@ PKT_ERR_INVALID_ARG
One or more arguments are invalid or NULL.
Definition packet_utils.h:276

◆ POOL_DEPTH

#define POOL_DEPTH   (32)

◆ RAND_THRESHOLD

#define RAND_THRESHOLD   (5)

◆ SYNC8

#define SYNC8   (0xAA)

◆ U16_BYTES

#define U16_BYTES   (2)

◆ XOR_BYTES

#define XOR_BYTES   (53)

◆ XOR_GROUP_MASK

#define XOR_GROUP_MASK   (0x80008088)

◆ XOR_LOCATIONS

#define XOR_LOCATIONS   { 0, 3, 7, 15, 31 }

Function Documentation

◆ bytes_to_str()

static void bytes_to_str ( const uint8_t *restrict buffer,
size_t sz,
char *restrict out,
size_t out_sz )
static

◆ category_to_str()

static const char * category_to_str ( domain_t domain,
category_t category )
static

◆ dec_pool_get_slot_n()

static const struct rx_strip_pkt * dec_pool_get_slot_n ( const pkt_dec_pool_t *restrict pool,
uint16_t n )
inlinestatic

◆ dec_pool_has_unknown_pkt()

static bool dec_pool_has_unknown_pkt ( const pkt_dec_pool_t *restrict pool)
inlinestatic

◆ dec_pool_mark_known()

static void dec_pool_mark_known ( pkt_dec_pool_t *restrict pool,
const packet_t *restrict pkt )
inlinestatic

◆ dec_pool_try_recovery()

static int8_t dec_pool_try_recovery ( pkt_dec_pool_t *restrict pool)
static

◆ dec_pool_update_slot()

static void dec_pool_update_slot ( pkt_dec_pool_t *restrict pool,
const packet_t *restrict pkt )
inlinestatic

◆ domain_to_str()

static const char * domain_to_str ( domain_t domain)
static

◆ enc_pool_get_slot_n()

static const struct tx_strip_pkt * enc_pool_get_slot_n ( const pkt_enc_pool_t *restrict pool,
uint8_t n )
inlinestatic

◆ enc_pool_update_slot()

static void enc_pool_update_slot ( pkt_enc_pool_t *restrict pool,
const packet_t *restrict pkt )
inlinestatic

◆ pkt_compute_crc16_ccitt_false()

static uint16_t pkt_compute_crc16_ccitt_false ( const packet_t *restrict pkt)
inlinestatic

◆ pkt_crc16_is_valid()

static bool pkt_crc16_is_valid ( const packet_t *restrict pkt)
inlinestatic

◆ pkt_get_crc16()

static uint16_t pkt_get_crc16 ( const packet_t *restrict pkt)
inlinestatic

◆ pkt_get_sync8()

static uint8_t pkt_get_sync8 ( const packet_t *restrict pkt)
inlinestatic

◆ pkt_set_crc16()

static void pkt_set_crc16 ( packet_t *restrict pkt)
inlinestatic

◆ pkt_set_sync8()

static void pkt_set_sync8 ( packet_t *restrict pkt)
inlinestatic

◆ pkt_set_xor_part()

static void pkt_set_xor_part ( const pkt_enc_pool_t *restrict pool,
packet_t *restrict pkt )
static

◆ pkt_sync8_is_valid()

static bool pkt_sync8_is_valid ( const packet_t *restrict pkt)
inlinestatic

◆ type_to_str()

static const char * type_to_str ( domain_t domain,
category_t category,
type_t type )
static

Variable Documentation

◆ id

_Atomic uint16_t id = 0
static

◆ TAG

const char* TAG = "pkt_utils"
static