ESP32-P4 OBC Firmware
ESP-IDF firmware for Plant-B CubeSat OBC
 
Loading...
Searching...
No Matches
packet_utils.h
Go to the documentation of this file.
1
24
25#ifndef __PACKET_UTILS_H__
26#define __PACKET_UTILS_H__
27
28#include "pal_log.h"
29#include <stdint.h>
30#include <stdio.h>
31#include <stdbool.h>
32#include <stdatomic.h>
33#include "esp_random.h"
34#include "rom/crc.h"
35
63
65#define PACKET_BYTES (111)
66
68#define PAYLOAD_BYTES (48)
69
74#define PKT_NO_LOCATION (-1)
75
76#ifdef __cplusplus
77extern "C" {
78#endif
79
86typedef enum : uint8_t {
87 DOMAIN_TM = 0x00,
88 DOMAIN_TC = 0x01,
90} domain_t;
91
98typedef enum : uint8_t {
99 TM_SDATA = 0x00,
100 TM_BDATA = 0x01,
101 TM_EVENT = 0x02
103
110typedef enum : uint8_t {
114
123typedef enum : uint8_t {
127
136typedef enum : uint8_t {
141
148typedef enum : uint8_t {
149 TC_CTRL = 0x00,
150 TC_REQ = 0x01
152
159typedef enum : uint8_t {
164 TC_REQ_LOG = 0x04,
168
177typedef enum : uint8_t {
180
188typedef enum : uint8_t {
189 TEST_WIMG = 0x00,
190 TEST_FRM = 0x01,
191 TEST_MSG = 0x02
193
201typedef struct packet packet_t;
202
211
221
236
253
268
280
286packet_t* pkt_create(void);
287
294void pkt_destroy(packet_t* restrict pkt);
295
306
313void enc_pool_destroy(pkt_enc_pool_t* restrict pool);
314
325
332void dec_pool_destroy(pkt_dec_pool_t* restrict pool);
333
352 packet_t* restrict pkt,
353 pkt_enc_pool_t* restrict pool
354);
355
378 const packet_t* restrict pkt,
379 pkt_dec_pool_t* restrict pool,
380 int8_t* restrict relative_loc
381);
382
401 const pkt_dec_pool_t* restrict pool,
402 int8_t relative_loc,
403 packet_t* restrict pkt
404);
405
416 const packet_t* restrict pkt,
417 uint16_t* id
418);
419
431pkt_err_t header_sensor_config(header_t* restrict header);
432
451pkt_err_t header_old_sensor_config(header_t* restrict header, uint16_t old_id);
452
466 const packet_t* restrict pkt,
467 header_t* header
468);
469
483 packet_t* restrict pkt,
484 const header_t* restrict header
485);
486
500 const packet_t* restrict pkt,
501 uint8_t* payload,
502 size_t len
503);
504
519 packet_t* restrict pkt,
520 const uint8_t* restrict payload,
521 size_t len
522);
523
535bool pkt_is_chunked(const packet_t* restrict pkt);
536
548pkt_err_t pkt_log(const packet_t* restrict pkt);
549
565pkt_err_t pkt_test_random_set_sync8(packet_t* restrict pkt, bool* failed);
566
581
582#ifdef __cplusplus
583}
584#endif
585 // end of packet_utils group
587
588#endif // __PACKET_UTILS_H__
589
uint8_t u8
Raw byte value for DCT packing.
Definition packet_utils.h:234
tc_ctrl_type_t ctrl
Type interpreted as a TC control type.
Definition packet_utils.h:250
tm_category_t tm
Category interpreted as a TM category.
Definition packet_utils.h:231
tc_req_type_t req
Type interpreted as a TC request type.
Definition packet_utils.h:249
tm_bdata_type_t bdata
Type interpreted as a TM big data type.
Definition packet_utils.h:247
tm_event_type_t event
Type interpreted as a TM event type.
Definition packet_utils.h:248
uint16_t total
Total number of chunks minus 1.
Definition packet_utils.h:266
tm_sdata_type_t sdata
Type interpreted as a TM small data type.
Definition packet_utils.h:246
uint8_t u8
Raw byte value for DCT packing.
Definition packet_utils.h:251
category_t category
Message category (bits 5–4 of DCT).
Definition packet_utils.h:263
type_t type
Message type (bits 3–0 of DCT).
Definition packet_utils.h:264
uint16_t index
Chunk sequence number (0-based).
Definition packet_utils.h:265
tc_category_t tc
Category interpreted as a TC category.
Definition packet_utils.h:232
test_category_t test
Category interpreted as a Test type.
Definition packet_utils.h:233
domain_t domain
Message domain (bits 7–6 of DCT).
Definition packet_utils.h:262
tm_category_t
Categories within the Telemetry domain.
Definition packet_utils.h:98
struct packet packet_t
Opaque packet handle.
Definition packet_utils.h:201
void dec_pool_destroy(pkt_dec_pool_t *restrict pool)
Free a decoder pool allocated by dec_pool_create().
Definition packet_utils.c:587
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.
Definition packet_utils.c:710
pkt_err_t
Return codes for packet utility functions.
Definition packet_utils.h:273
pkt_err_t pkt_set_header(packet_t *restrict pkt, const header_t *restrict header)
Pack header fields into a raw packet.
Definition packet_utils.c:737
tm_sdata_type_t
Types within the Telemetry / Small data category.
Definition packet_utils.h:110
pkt_err_t header_sensor_config(header_t *restrict header)
Configure a header for a current sensor measurement packet.
Definition packet_utils.c:699
tc_category_t
Categories within the Telecommand domain.
Definition packet_utils.h:148
pkt_err_t pkt_get_id(const packet_t *restrict pkt, uint16_t *id)
Read the packet identifier.
Definition packet_utils.c:691
domain_t
Top-level message domain.
Definition packet_utils.h:86
pkt_err_t pkt_log(const packet_t *restrict pkt)
Log a human-readable dump of a packet to the PAL logger.
Definition packet_utils.c:795
void pkt_destroy(packet_t *restrict pkt)
Free a packet allocated by pkt_create().
Definition packet_utils.c:546
pkt_enc_pool_t * enc_pool_create(void)
Allocate and initialize an encoder pool.
Definition packet_utils.c:555
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.
Definition packet_utils.c:756
struct pkt_enc_pool pkt_enc_pool_t
Opaque encoder pool handle.
Definition packet_utils.h:210
packet_t * pkt_create(void)
Allocate and initialize a packet, with each byte set to 0.
Definition packet_utils.c:537
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.
Definition packet_utils.c:664
pkt_err_t pkt_test_random_set_sync8(packet_t *restrict pkt, bool *failed)
Randomly corrupt the SYNC8 field for testing purposes.
Definition packet_utils.c:845
pkt_dec_pool_t * dec_pool_create(void)
Allocate and initialize a decoder pool.
Definition packet_utils.c:576
struct pkt_dec_pool pkt_dec_pool_t
Opaque decoder pool handle.
Definition packet_utils.h:220
pkt_err_t pkt_test_random_set_crc16(packet_t *restrict pkt)
Randomly corrupt the CRC16 field for testing purposes.
Definition packet_utils.c:859
tc_req_type_t
Types within the Telecommand / Request category.
Definition packet_utils.h:159
test_category_t
Types within the Test domain.
Definition packet_utils.h:188
tc_ctrl_type_t
Types within the Telecommand / Control category.
Definition packet_utils.h:177
tm_bdata_type_t
Types within the Telemetry / Big data category.
Definition packet_utils.h:123
tm_event_type_t
Types within the Telemetry / Event category.
Definition packet_utils.h:136
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.
Definition packet_utils.c:625
void enc_pool_destroy(pkt_enc_pool_t *restrict pool)
Free an encoder pool allocated by enc_pool_create().
Definition packet_utils.c:566
pkt_err_t pkt_encode(packet_t *restrict pkt, pkt_enc_pool_t *restrict pool)
Finalize and encode a packet for transmission.
Definition packet_utils.c:597
pkt_err_t pkt_get_header(const packet_t *restrict pkt, header_t *header)
Unpack the header fields from a raw packet.
Definition packet_utils.c:721
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.
Definition packet_utils.c:771
bool pkt_is_chunked(const packet_t *restrict pkt)
Check whether a packet carries chunked data.
Definition packet_utils.c:790
@ TM_EVENT
Events (logs, errors, crashes).
Definition packet_utils.h:101
@ TM_BDATA
Big data (images, video streams).
Definition packet_utils.h:100
@ TM_SDATA
Small data (sensor measurements, system status).
Definition packet_utils.h:99
@ PKT_OK
Operation completed successfully.
Definition packet_utils.h:274
@ PKT_ERR_SYNC
SYNC8 word mismatch: packet boundary error.
Definition packet_utils.h:277
@ PKT_ERR_INVALID_ARG
One or more arguments are invalid or NULL.
Definition packet_utils.h:276
@ PKT_FAIL
Generic failure.
Definition packet_utils.h:275
@ PKT_ERR_CRC
CRC16 mismatch: packet is corrupted.
Definition packet_utils.h:278
@ TM_SDATA_STATUS
System status payload.
Definition packet_utils.h:112
@ TM_SDATA_SENSORS
Sensor measurements payload.
Definition packet_utils.h:111
@ TC_CTRL
Control commands (e.g. reboot).
Definition packet_utils.h:149
@ TC_REQ
Data or status requests.
Definition packet_utils.h:150
@ DOMAIN_TM
Telemetry domain (CubeSat to Ground).
Definition packet_utils.h:87
@ DOMAIN_TC
Telecommand domain (Ground to CubeSat).
Definition packet_utils.h:88
@ DOMAIN_TEST
Test domain (Ground to CubeSat, for tests only).
Definition packet_utils.h:89
@ TC_REQ_IMAGE
Request image transmission.
Definition packet_utils.h:162
@ TC_REQ_SENSORS
Request current sensor measurements.
Definition packet_utils.h:160
@ TC_REQ_LOG
Request log retrieval.
Definition packet_utils.h:164
@ TC_REQ_ERROR
Request error list.
Definition packet_utils.h:165
@ TC_REQ_CRASH
Request last crash information.
Definition packet_utils.h:166
@ TC_REQ_STATUS
Request current system status.
Definition packet_utils.h:161
@ TC_REQ_VIDEO
Request video transmission.
Definition packet_utils.h:163
@ TEST_WIMG
Write an image to OBC memory.
Definition packet_utils.h:189
@ TEST_FRM
Remove a file from OBC file system.
Definition packet_utils.h:190
@ TEST_MSG
Send a text message.
Definition packet_utils.h:191
@ TC_CTRL_REBOOT
Force a system reboot.
Definition packet_utils.h:178
@ TM_BDATA_IMAGE
Image data (JPEG).
Definition packet_utils.h:124
@ TM_BDATA_VIDEO
Video stream (H.264).
Definition packet_utils.h:125
@ TM_EVENT_CRASH
Critical failure / crash report.
Definition packet_utils.h:139
@ TM_EVENT_ERROR
Non-critical error.
Definition packet_utils.h:138
@ TM_EVENT_LOG
Normal event log.
Definition packet_utils.h:137
Decoded representation of the packet header fields.
Definition packet_utils.h:261
Type-safe union for the category field of a packet header.
Definition packet_utils.h:230
Type-safe union for the type field of a packet header.
Definition packet_utils.h:245
Definition packet_utils.c:83
Definition packet_utils.c:129
Definition packet_utils.c:107
Platform-agnostic logging macros.