CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
CCSDSPacket.h
Go to the documentation of this file.
1// Copyright 2025-2026 ExoSpaceLabs
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef CCSDS_PACKET_H
5#define CCSDS_PACKET_H
6
26#include <CCSDSResult.h>
27#include <vector>
28#include "CCSDSHeader.h"
29#include "CCSDSDataField.h"
30
35namespace CCSDS {
36
38 struct CRC16Config {
39 std::uint16_t polynomial = 0x1021;
40 std::uint16_t initialValue = 0xFFFF;
41 std::uint16_t finalXorValue = 0x0000;
42 };
43
44
62 class Packet {
63 public:
64 Packet() = default;
65
76
87 [[nodiscard]] ResultBool setPrimaryHeader(std::uint64_t data);
88
99 [[nodiscard]] ResultBool setPrimaryHeader(const std::vector<std::uint8_t> &data);
100
109 void setPrimaryHeader(const Header &header);
110
120 void setDataFieldHeader(const std::shared_ptr<SecondaryHeaderAbstract> &header);
121
130 template <typename T>
135
149 [[nodiscard]] ResultBool setDataFieldHeader(const std::vector<std::uint8_t> &data, const std::string &headerType);
150
165 [[nodiscard]] ResultBool setDataFieldHeader(const std::uint8_t *pData, size_t sizeData, const std::string &headerType);
166
178 [[nodiscard]] ResultBool setDataFieldHeader(const std::vector<std::uint8_t> &data);
179
192 [[nodiscard]] ResultBool setDataFieldHeader(const std::uint8_t *pData, size_t sizeData);
193
205 [[nodiscard]] ResultBool setApplicationData(const std::vector<std::uint8_t> &data);
206
219 [[nodiscard]] ResultBool setApplicationData(const std::uint8_t *pData, size_t sizeData);
220
231
233 [[nodiscard]] ResultBool setSequenceCount(std::uint16_t count);
234
244 void setDataFieldSize(std::uint16_t size);
245
250 void setUpdatePacketEnable(bool enable);
251
253 [[nodiscard]] ResultBool deserialize(const std::vector<std::uint8_t> &data);
254
256 [[nodiscard]] ResultBool deserialize(const std::vector<std::uint8_t> &data, const std::string& headerType, std::int32_t headerSize = -1);
257
259 [[nodiscard]] ResultBool deserialize(const std::vector<uint8_t> &data, std::uint16_t headerDataSizeBytes);
260
262 [[nodiscard]] ResultBool deserialize(const std::vector<uint8_t> &headerData, const std::vector<uint8_t> &data);
263
273 std::uint64_t getPrimaryHeader64bit();
274
279 std::uint16_t getFullPacketLength();
280
292 std::vector<uint8_t> serialize();
293
303 std::vector<uint8_t> getPrimaryHeaderBytes();
304
310 std::vector<uint8_t> getDataFieldHeaderBytes();
311
317 std::vector<uint8_t> getApplicationDataBytes();
318
324 std::vector<uint8_t> getFullDataFieldBytes();
325
334 std::vector<uint8_t> getCRCVectorBytes();
335
345 std::uint16_t getCRC();
346
348 std::uint16_t getDataFieldMaximumSize() const;
349
352
355
358
370 void setCrcConfig(const CRC16Config crcConfig) {m_CRC16Config = crcConfig;}
371
372
386 void setCrcConfig(const std::uint16_t polynomial, const std::uint16_t initialValue, const std::uint16_t finalXorValue) {m_CRC16Config = {polynomial, initialValue, finalXorValue};}
387
395 void update();
396
403 ResultBool loadFromConfigFile(const std::string &configPath);
404
411#ifndef CCSDS_MCU
412 ResultBool loadFromConfig(const Config &cfg);
413#endif
414
415 private:
418 std::uint16_t m_CRC16{};
420 bool m_updateStatus{false};
422 std::uint16_t m_sequenceCounter{0};
423 };
424}
425#endif // CCSDS_PACKET_H
#define FORWARD_RESULT(result)
Macro to return a result as-is (for functions returning Result<T>).
Represents the data field of a CCSDS packet.
ResultBool RegisterSecondaryHeader()
Registers a new header type with its creation function.
Manages the decomposition and manipulation of CCSDS primary headers.
Definition CCSDSHeader.h:83
Represents a CCSDS (Consultative Committee for Space Data Systems) packet.
Definition CCSDSPacket.h:62
ResultBool setDataFieldHeader(const std::vector< std::uint8_t > &data, const std::string &headerType)
Sets the data field header for the packet using a vector of bytes.
bool m_updateStatus
When setting data thus value should be set to false.
Packet()=default
ResultBool deserialize(const std::vector< uint8_t > &headerData, const std::vector< uint8_t > &data)
Deserializes a CCSDS packet using separate header and data vectors.
std::vector< uint8_t > getPrimaryHeaderBytes()
Retrieves the primary header of the packet as a vector of bytes.
void setCrcConfig(const std::uint16_t polynomial, const std::uint16_t initialValue, const std::uint16_t finalXorValue)
Sets the crc configuration of the crc calculation.
void update()
Updates Primary headers data field size.
std::vector< uint8_t > getCRCVectorBytes()
Retrieves the CRC-16 checksum as a vector of bytes.
void setUpdatePacketEnable(bool enable)
needs to be called as soon as possible, probably also from constructor.
ResultBool setSequenceCount(std::uint16_t count)
Sets the sequence count for the packet.
ResultBool setPrimaryHeader(const std::vector< std::uint8_t > &data)
Sets the primary header using the provided vector of std::uint8_tdata.
bool getDataFieldHeaderFlag()
@ returns the data field header flag
void setDataFieldHeader(const std::shared_ptr< SecondaryHeaderAbstract > &header)
Sets the data field header using the provided SecondaryHeaderAbstract derived header.
std::uint16_t m_sequenceCounter
Header m_primaryHeader
6 bytes / 48 bits / 12 hex
std::uint64_t getPrimaryHeader64bit()
Retrieves the primary header of the packet.
DataField m_dataField
variable
ResultBool loadFromConfig(const Config &cfg)
Loads a packet from a configuration object, including secondary header if present.
std::uint16_t getCRC()
Computes and retrieves the CRC-16 checksum of the packet.
std::uint16_t getDataFieldMaximumSize() const
returns the maximum data field size
void setSequenceFlags(ESequenceFlag flags)
Sets the sequence flags for the packet's primary header.
std::vector< uint8_t > getFullDataFieldBytes()
Retrieves the full data field data.
std::vector< uint8_t > serialize()
Retrieves the full packet as a vector of bytes.
ResultBool loadFromConfigFile(const std::string &configPath)
Loads a packet from a configuration file, including secondary header if present.
ResultBool setApplicationData(const std::vector< std::uint8_t > &data)
Sets the application data for the packet.
std::uint16_t m_CRC16
Cyclic Redundancy check 16 bits.
std::vector< uint8_t > getDataFieldHeaderBytes()
Retrieves the secondary header data from the data field.
void setPrimaryHeader(PrimaryHeader data)
Sets the primary header using the provided PrimaryHeader object.
ResultBool deserialize(const std::vector< std::uint8_t > &data)
Deserializes a vector of bytes into a CCSDS packet.
std::uint16_t getFullPacketLength()
Retrieves the current size of the CCSDS Packet.
ResultBool RegisterSecondaryHeader()
Registers a new header type with the data field secondary header creation function.
Header & getPrimaryHeader()
returns the CCSDS packet's Primary Header.
ResultBool setDataFieldHeader(const std::vector< std::uint8_t > &data)
Sets the data field header using the provided vector of bytes.
std::vector< uint8_t > getApplicationDataBytes()
Retrieves the application data from the data field.
DataField & getDataField()
returns the CCSDS packet's DataField.
bool m_enableUpdatePacket
Enables primary header and secondary header update.
ResultBool deserialize(const std::vector< uint8_t > &data, std::uint16_t headerDataSizeBytes)
Deserializes a CCSDS packet using a vector and a header data size.
void setDataFieldSize(std::uint16_t size)
Sets the maximum data packet size for the CCSDS DataField.
CRC16Config m_CRC16Config
structure holding configuration of crc calculation.
void setCrcConfig(const CRC16Config crcConfig)
Sets the crc configuration of the crc calculation.
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:85
Parses and stores config values from custom file format.
Definition CCSDSConfig.h:14
Contains definitions and classes for handling CCSDS headers.
ESequenceFlag
Represents the sequence flags used in CCSDS telemetry transfer frames.
Definition CCSDSHeader.h:22
Configuration structure for CRC16 calculation parameters.
Definition CCSDSPacket.h:38
std::uint16_t initialValue
Definition CCSDSPacket.h:40
std::uint16_t polynomial
Definition CCSDSPacket.h:39
std::uint16_t finalXorValue
Definition CCSDSPacket.h:41
Represents the primary header of a CCSDS packet.
Definition CCSDSHeader.h:44