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

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

#include "pal_log.h"
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdatomic.h>
#include "esp_random.h"
#include "rom/crc.h"

Go to the source code of this file.

Data Structures

union  category_t
 Type-safe union for the category field of a packet header. More...
 
union  type_t
 Type-safe union for the type field of a packet header. More...
 
struct  header_t
 Decoded representation of the packet header fields. More...
 

Macros

#define PACKET_BYTES   (111)
 Total packet size in bytes.
 
#define PAYLOAD_BYTES   (48)
 Size of the payload field in bytes.
 
#define PKT_NO_LOCATION   (-1)
 Sentinel value returned by pkt_decode() when no packet was recovered from the decoding pool.
 

Typedefs

typedef struct packet packet_t
 Opaque packet handle.
 
typedef struct pkt_enc_pool pkt_enc_pool_t
 Opaque encoder pool handle.
 
typedef struct pkt_dec_pool pkt_dec_pool_t
 Opaque decoder pool handle.
 

Enumerations

enum  domain_t : uint8_t { DOMAIN_TM = 0x00 , DOMAIN_TC = 0x01 , DOMAIN_TEST = 0x02 }
 Top-level message domain. More...
 
enum  tm_category_t : uint8_t { TM_SDATA = 0x00 , TM_BDATA = 0x01 , TM_EVENT = 0x02 }
 Categories within the Telemetry domain. More...
 
enum  tm_sdata_type_t : uint8_t { TM_SDATA_SENSORS = 0x00 , TM_SDATA_STATUS = 0x01 }
 Types within the Telemetry / Small data category. More...
 
enum  tm_bdata_type_t : uint8_t { TM_BDATA_IMAGE = 0x00 , TM_BDATA_VIDEO = 0x01 }
 Types within the Telemetry / Big data category. More...
 
enum  tm_event_type_t : uint8_t { TM_EVENT_LOG = 0x00 , TM_EVENT_ERROR = 0x01 , TM_EVENT_CRASH = 0x02 }
 Types within the Telemetry / Event category. More...
 
enum  tc_category_t : uint8_t { TC_CTRL = 0x00 , TC_REQ = 0x01 }
 Categories within the Telecommand domain. More...
 
enum  tc_req_type_t : uint8_t {
  TC_REQ_SENSORS = 0x00 , TC_REQ_STATUS = 0x01 , TC_REQ_IMAGE = 0x02 , TC_REQ_VIDEO = 0x03 ,
  TC_REQ_LOG = 0x04 , TC_REQ_ERROR = 0x05 , TC_REQ_CRASH = 0x06
}
 Types within the Telecommand / Request category. More...
 
enum  tc_ctrl_type_t : uint8_t { TC_CTRL_REBOOT = 0x00 }
 Types within the Telecommand / Control category. More...
 
enum  test_category_t : uint8_t { TEST_WIMG = 0x00 , TEST_FRM = 0x01 , TEST_MSG = 0x02 }
 Types within the Test domain. More...
 
enum  pkt_err_t {
  PKT_OK = 0 , PKT_FAIL , PKT_ERR_INVALID_ARG , PKT_ERR_SYNC ,
  PKT_ERR_CRC
}
 Return codes for packet utility functions. More...
 

Functions

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.
 

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