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#include "CCSDSUtils.h"
4#include <iostream>
5#include <utility>
6
8 update();
9 const auto &dataFieldHeader = getDataFieldHeaderBytes();
10 std::vector<uint8_t> fullData{};
11 if (!dataFieldHeader.empty()) {
12 fullData.insert(fullData.end(), dataFieldHeader.begin(), dataFieldHeader.end());
13 }
14 fullData.insert(fullData.end(), m_applicationData.begin(), m_applicationData.end());
15 return fullData;
16}
17
19 update();
20 return m_applicationData;
21}
22
24 update();
25 return m_dataPacketSize - getDataFieldUsedBytesSize();
26}
27
29 update();
30 return m_dataPacketSize;
31}
32
34 update();
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 return m_secondaryHeader;
46}
47
49 if (!m_dataFieldHeaderUpdated && m_enableDataFieldUpdate) {
50 if (m_secondaryHeaderFactory.typeIsRegistered(m_dataFieldHeaderType) && m_dataFieldHeaderType != "BufferHeader") {
51 m_secondaryHeader->setDataLength(m_applicationData.size());;
52 }
53 m_dataFieldHeaderUpdated = true;
54 }
55}
56
57CCSDS::ResultBool CCSDS::DataField::setApplicationData(const uint8_t *pData, const size_t &sizeData) {
58 RET_IF_ERR_MSG(!pData, ErrorCode::NULL_POINTER, "Application data is nullptr");
59 RET_IF_ERR_MSG(sizeData < 1, ErrorCode::INVALID_APPLICATION_DATA, "Application data size cannot be < 1");
60 RET_IF_ERR_MSG(sizeData > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_APPLICATION_DATA,
61 "Application data field exceeds available size");
62
63 if (!m_applicationData.empty()) {
64 std::cerr << "[ CCSDS Data ] Warning: Data field is not empty, it has been overwritten." << std::endl;
65 }
66 m_applicationData.assign(pData, pData + sizeData);
67 m_dataFieldHeaderUpdated = false;
68 return true;
69}
70
71CCSDS::ResultBool CCSDS::DataField::setApplicationData(const std::vector<uint8_t> &applicationData) {
72 RET_IF_ERR_MSG(applicationData.size() > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_APPLICATION_DATA,
73 "Application data field exceeds available size.");
74 m_applicationData = applicationData;
75 m_dataFieldHeaderUpdated = false;
76 return true;
77}
78
79CCSDS::ResultBool CCSDS::DataField::setDataFieldHeader(const uint8_t *pData, const size_t &sizeData) {
80 RET_IF_ERR_MSG(!pData, ErrorCode::NULL_POINTER, "Secondary header data is nullptr");
81 RET_IF_ERR_MSG(sizeData < 1, ErrorCode::INVALID_SECONDARY_HEADER_DATA, "Secondary header data size cannot be < 1");
82 RET_IF_ERR_MSG(sizeData > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
83 "Secondary header data exceeds available size.");
84
85 std::vector<uint8_t> data;
86 data.assign(pData, pData + sizeData);
87 FORWARD_RESULT( setDataFieldHeader(data) );
88 return true;
89}
90
91CCSDS::ResultBool CCSDS::DataField::setDataFieldHeader(const uint8_t *pData, const size_t &sizeData,
92 const std::string &pType) {
93 RET_IF_ERR_MSG(!pData, ErrorCode::NULL_POINTER, "Secondary header data is nullptr");
94
95 RET_IF_ERR_MSG(!m_secondaryHeaderFactory.typeIsRegistered(pType), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
96 "Secondary header type is not registered: " + pType);
97
98 if (m_secondaryHeaderFactory.typeIsRegistered(pType)) {
99 std::vector<uint8_t> data;
100 data.assign(pData, pData + sizeData);
101 FORWARD_RESULT(setDataFieldHeader(data,pType));
102 } else {
103 FORWARD_RESULT(setDataFieldHeader(pData, sizeData));
104 m_dataFieldHeaderType = pType;
105 }
106 m_dataFieldHeaderUpdated = false;
107 return true;
108}
109
111 const std::string &pType) {
112 RET_IF_ERR_MSG(data.size() > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
113 "Secondary header data exceeds available size");
114 RET_IF_ERR_MSG(!m_secondaryHeaderFactory.typeIsRegistered(pType), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
115 "Secondary header type is not registered: " + pType);
116
117 auto header = m_secondaryHeaderFactory.create(pType);
118
119 RET_IF_ERR_MSG(data.size() != header->getSize(), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
120 "Secondary header data size mismatch for type: " + pType);
121
122
123 m_secondaryHeader = std::move(header);
124 FORWARD_RESULT(m_secondaryHeader->deserialize(data));
125
126 m_dataFieldHeaderType = pType;
127
128 m_dataFieldHeaderUpdated = false;
129 return true;
130}
131
132CCSDS::ResultBool CCSDS::DataField::setDataFieldHeader(const std::vector<uint8_t> &dataFieldHeader) {
133 RET_IF_ERR_MSG(dataFieldHeader.size() > getDataFieldAvailableBytesSize(), ErrorCode::INVALID_SECONDARY_HEADER_DATA,
134 "Secondary header data exceeds available size");
135
136 BufferHeader header;
137 m_secondaryHeader = std::make_shared<BufferHeader>(dataFieldHeader);
138 FORWARD_RESULT( m_secondaryHeader->deserialize(dataFieldHeader) );
139
140 m_dataFieldHeaderType = m_secondaryHeader->getType(); ;
141 m_dataFieldHeaderUpdated = false;
142 return true;
143}
144
145void CCSDS::DataField::setDataFieldHeader(std::shared_ptr<SecondaryHeaderAbstract> header) {
146 m_secondaryHeader = std::move(header);
147 m_dataFieldHeaderType = m_secondaryHeader->getType();
148 m_dataFieldHeaderUpdated = false;
149}
150
151void CCSDS::DataField::setDataPacketSize(const uint16_t &value) { m_dataPacketSize = value; }
152
154 update();
155 if (m_secondaryHeader) {
156 return m_secondaryHeader->serialize();
157 }
158 return {};
159}
160
161
162
#define RET_IF_ERR_MSG(condition, errorCode, message)
Macro to return an error with an error message if a condition is met.
#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.
ResultBool setDataFieldHeader(const uint8_t *pData, const size_t &sizeData)
Sets the secondary header data for the data field.
uint16_t getDataFieldAvailableBytesSize()
Retrieves the available size of the data field in bytes.
uint16_t getDataFieldUsedBytesSize()
Retrieves the used size of the data field in bytes.
std::vector< uint8_t > m_applicationData
Application data buffer.
std::vector< uint8_t > getDataFieldHeaderBytes()
Retrieves the secondary header data as a vector of bytes.
std::shared_ptr< SecondaryHeaderAbstract > getSecondaryHeader()
retrieves the known PUS type
std::vector< uint8_t > getApplicationData()
Retrieves the application data from the data field.
ResultBool setApplicationData(const std::vector< uint8_t > &applicationData)
Sets the application data using a vector of bytes.
void update()
Updates the data field header based on the current application data size.
void setDataPacketSize(const uint16_t &value)
Sets the maximum data packet size for the CCSDS DataField.
std::vector< uint8_t > getFullDataFieldBytes()
Retrieves the full data field by combining the data field header and application data.
uint16_t getDataFieldAbsoluteBytesSize()
Retrieves the absolute size of the data field in bytes.
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:81
@ INVALID_APPLICATION_DATA
Application data is invalid.
Definition CCSDSResult.h:26
@ INVALID_SECONDARY_HEADER_DATA
Secondary header data is invalid.
Definition CCSDSResult.h:25
@ NULL_POINTER
Null pointer encountered.
Definition CCSDSResult.h:27