CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
CCSDSDataField.cpp
Go to the documentation of this file.
1#include "CCSDSDataField.h"
3
4#include <utility>
5
6std::vector<std::uint8_t> CCSDS::DataField::serialize() {
7 update();
8 const auto &dataFieldHeader = getDataFieldHeaderBytes();
9 std::vector<std::uint8_t> fullData{};
10 if (!dataFieldHeader.empty()) {
11 fullData.insert(fullData.end(), dataFieldHeader.begin(), dataFieldHeader.end());
12 }
13 fullData.insert(fullData.end(), m_applicationData.begin(), m_applicationData.end());
14 return fullData;
15}
16
17std::vector<std::uint8_t> CCSDS::DataField::getApplicationData() {
18 return m_applicationData;
19}
20
22 return m_dataPacketSize - getDataFieldUsedBytesSize();
23}
24
26 return m_dataPacketSize;
27}
28
30 return m_applicationData.size();
31}
32
33
35 if (!getDataFieldHeaderFlag()) {
36 return m_applicationData.size();
37 }
38 if (m_secondaryHeader != nullptr) {
39 return m_applicationData.size() + m_secondaryHeader->getSize();
40 }
41 return 0;
42}
43
44std::shared_ptr<CCSDS::SecondaryHeaderAbstract> CCSDS::DataField::getSecondaryHeader() {
45 update();
46 return m_secondaryHeader;
47}
48
50 if (!m_dataFieldHeaderUpdated && m_enableDataFieldUpdate) {
51 if (m_secondaryHeaderFactory.typeIsRegistered(m_dataFieldHeaderType)) {
52 m_secondaryHeader->update(this);
53 }
54 m_dataFieldHeaderUpdated = true;
55 }
56}
57
58CCSDS::ResultBool CCSDS::DataField::setApplicationData(const std::uint8_t *pData, const size_t &sizeData) {
59 RET_IF_ERR_MSG(!pData, ErrorCode::NULL_POINTER, "Application data is nullptr");
60 RET_IF_ERR_MSG(sizeData < 1, ErrorCode::INVALID_APPLICATION_DATA, "Application data size cannot be < 1");
61 RET_IF_ERR_MSG(sizeData > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_APPLICATION_DATA,
62 "Application data field exceeds available size");
63
64 if (!m_applicationData.empty()) {
65 printf( "[ CCSDS Data ] Warning: Data field is not empty, it has been overwritten.");
66 }
67 m_applicationData.assign(pData, pData + sizeData);
68 m_dataFieldHeaderUpdated = false;
69 return true;
70}
71
72CCSDS::ResultBool CCSDS::DataField::setApplicationData(const std::vector<std::uint8_t> &applicationData) {
73 RET_IF_ERR_MSG(applicationData.size() > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_APPLICATION_DATA,
74 "Application data field exceeds available size.");
75 m_applicationData = applicationData;
76 m_dataFieldHeaderUpdated = false;
77 return true;
78}
79
80CCSDS::ResultBool CCSDS::DataField::setDataFieldHeader(const std::uint8_t *pData, const size_t &sizeData) {
81 RET_IF_ERR_MSG(!pData, ErrorCode::NULL_POINTER, "Secondary header data is nullptr");
82 RET_IF_ERR_MSG(sizeData < 1, ErrorCode::INVALID_SECONDARY_HEADER_DATA, "Secondary header data size cannot be < 1");
83 RET_IF_ERR_MSG(sizeData > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
84 "Secondary header data exceeds available size.");
85
86 std::vector<std::uint8_t> data;
87 data.assign(pData, pData + sizeData);
88 FORWARD_RESULT( setDataFieldHeader(data) );
89 return true;
90}
91
92CCSDS::ResultBool CCSDS::DataField::setDataFieldHeader(const std::uint8_t *pData, const size_t &sizeData,
93 const std::string &pType) {
94 RET_IF_ERR_MSG(!pData, ErrorCode::NULL_POINTER, "Secondary header data is nullptr");
95
96 RET_IF_ERR_MSG(!m_secondaryHeaderFactory.typeIsRegistered(pType), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
97 "Secondary header type is not registered: " + pType);
98
99 if (m_secondaryHeaderFactory.typeIsRegistered(pType)) {
100 std::vector<std::uint8_t> data;
101 data.assign(pData, pData + sizeData);
102 FORWARD_RESULT(setDataFieldHeader(data,pType));
103 } else {
104 FORWARD_RESULT(setDataFieldHeader(pData, sizeData));
105 m_dataFieldHeaderType = pType;
106 }
107 m_dataFieldHeaderUpdated = false;
108 return true;
109}
110
111CCSDS::ResultBool CCSDS::DataField::setDataFieldHeader(const std::vector<std::uint8_t> &data,
112 const std::string &pType) {
113 RET_IF_ERR_MSG(data.size() > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
114 "Secondary header data exceeds available size");
115 RET_IF_ERR_MSG(!m_secondaryHeaderFactory.typeIsRegistered(pType), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
116 "Secondary header type is not registered: " + pType);
117
118 auto header = m_secondaryHeaderFactory.create(pType);
119
120 if (!header->variableLength) {
121 RET_IF_ERR_MSG(data.size() != header->getSize(), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
122 "Secondary header data size mismatch for type: " + pType);
123 }
124
125 m_secondaryHeader = std::move(header);
126 FORWARD_RESULT(m_secondaryHeader->deserialize(data));
127
128 m_dataFieldHeaderType = pType;
129
130 m_dataFieldHeaderUpdated = false;
131 return true;
132}
133
134CCSDS::ResultBool CCSDS::DataField::setDataFieldHeader(const std::vector<std::uint8_t> &dataFieldHeader) {
135 RET_IF_ERR_MSG(dataFieldHeader.size() > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
136 "Secondary header data exceeds available size");
137
138 BufferHeader header;
139 m_secondaryHeader = std::make_shared<BufferHeader>(dataFieldHeader);
140 FORWARD_RESULT( m_secondaryHeader->deserialize(dataFieldHeader) );
141
142 m_dataFieldHeaderType = m_secondaryHeader->getType(); ;
143 m_dataFieldHeaderUpdated = false;
144 return true;
145}
146
147#ifndef CCSDS_MCU
149 RET_IF_ERR_MSG(!cfg.isKey("secondary_header_type"), ErrorCode::CONFIG_FILE_ERROR,
150 "Config: Missing string field: secondary_header_type");
151 std::string type{};
152 ASSIGN_OR_PRINT(type, cfg.get<std::string>("secondary_header_type"));
153 RET_IF_ERR_MSG(!m_secondaryHeaderFactory.typeIsRegistered(type), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
154 "Secondary header type is not registered: " + type);
155
156 m_secondaryHeader = m_secondaryHeaderFactory.create(type);
158 "Failed to create secondary header of type: " + type);
159 m_secondaryHeader->loadFromConfig(cfg);
160 m_dataFieldHeaderType = m_secondaryHeader->getType();
161 return true;
162}
163#endif
164
165void CCSDS::DataField::setDataFieldHeader(std::shared_ptr<SecondaryHeaderAbstract> header) {
166 m_secondaryHeader = std::move(header);
167 m_dataFieldHeaderType = m_secondaryHeader->getType();
168 m_dataFieldHeaderUpdated = false;
169}
170
171void CCSDS::DataField::setDataPacketSize(const std::uint16_t &value) { m_dataPacketSize = value; }
172
173std::vector<std::uint8_t> CCSDS::DataField::getDataFieldHeaderBytes() {
174 update();
175 if (m_secondaryHeader) {
176 return m_secondaryHeader->serialize();
177 }
178 return {};
179}
180
181
182
#define RET_IF_ERR_MSG(condition, errorCode, message)
Macro to return an error with an error message if a condition is met.
#define ASSIGN_OR_PRINT(var, result)
Macro to assign a result value or print an error message.
#define FORWARD_RESULT(result)
Macro to return a result as-is (for functions returning Result<T>).
Represents a fixed secondary header used as a data buffer.
std::vector< std::uint8_t > serialize()
Retrieves the full data field by combining the data field header and application data.
std::shared_ptr< SecondaryHeaderAbstract > getSecondaryHeader()
retrieves the known PUS type
std::uint16_t getDataFieldUsedBytesSize() const
Retrieves the used size of the data field in bytes.
void setDataPacketSize(const std::uint16_t &value)
Sets the maximum data packet size for the CCSDS DataField.
ResultBool setDataFieldHeader(const std::uint8_t *pData, const size_t &sizeData)
Sets the secondary header data for the data field.
std::uint16_t getDataFieldAbsoluteBytesSize() const
Retrieves the absolute size of the data field in bytes.
void update()
Updates the data field header based on the current application data size.
std::vector< std::uint8_t > getDataFieldHeaderBytes()
Retrieves the secondary header data as a vector of bytes.
std::uint16_t getApplicationDataBytesSize() const
Retrieves the size of the application data stored in the data field.
std::vector< std::uint8_t > getApplicationData()
Retrieves the application data from the data field.
std::vector< std::uint8_t > m_applicationData
Application data buffer.
ResultBool setApplicationData(const std::vector< std::uint8_t > &applicationData)
Sets the application data using a vector of bytes.
std::uint16_t getDataFieldAvailableBytesSize() const
Retrieves the available size of the data field in bytes.
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:82
Parses and stores config values from custom file format.
Definition CCSDSConfig.h:11
bool isKey(const std::string &key) const
CCSDS::Result< T > get(const std::string &key) const
Get value by key and type.
Definition CCSDSConfig.h:20
@ INVALID_APPLICATION_DATA
Application data is invalid.
Definition CCSDSResult.h:27
@ CONFIG_FILE_ERROR
Configuration file error.
Definition CCSDSResult.h:34
@ INVALID_SECONDARY_HEADER_DATA
Secondary header data is invalid.
Definition CCSDSResult.h:26
@ NULL_POINTER
Null pointer encountered.
Definition CCSDSResult.h:28