9 const auto dataFiledSize =
static_cast<uint16_t
>(dataField.size());
30 return m_dataField.getDataFieldAvailableBytesSize();
35 return m_primaryHeader.getDataFieldHeaderFlag();
39 std::vector<uint8_t> crc(2);
40 const auto crcVar = getCRC();
41 crc[0] = (crcVar >> 8) & 0xFF;
42 crc[1] = crcVar & 0xFF;
53 return m_primaryHeader;
58 return m_primaryHeader.getFullHeader();
63 return m_primaryHeader.serialize();
68 return m_dataField.getDataFieldHeaderBytes();
73 return m_dataField.getApplicationData();
77 return m_dataField.getFullDataFieldBytes();
81 auto header = getPrimaryHeaderBytes();
82 auto dataField = m_dataField.getFullDataFieldBytes();
83 const auto crc = getCRCVectorBytes();
85 std::vector<uint8_t> packet;
86 packet.reserve(header.size() + dataField.size() + crc.size());
87 packet.insert(packet.end(), header.begin(), header.end());
88 if (!getFullDataFieldBytes().empty()) {
89 packet.insert(packet.end(), dataField.begin(), dataField.end());
91 packet.insert(packet.end(), crc.begin(), crc.end());
98 "Cannot Deserialize Packet, Invalid Data provided data size must be at least 8 bytes");
100 std::vector<uint8_t> dataFieldVector;
101 std::copy(data.begin() + 6, data.end(), std::back_inserter(dataFieldVector));
103 FORWARD_RESULT(deserialize({data[0], data[1], data[2], data[3], data[4], data[5]}, dataFieldVector));
110 "Cannot Deserialize Packet, Invalid Data provided data size must be at least 8 bytes");
112 "Cannot Deserialize Packet, BufferHeader is not of defined size");
114 "Cannot Deserialize Packet, Unregistered Secondary header: " + headerType);
116 const auto secondaryHeader = m_dataField.getDataFieldHeaderFactory().create(headerType);
117 const auto headerDataSizeBytes = secondaryHeader->getSize();
119 std::vector<uint8_t> dataFieldHeaderVector;
120 std::copy_n(data.begin() + 6, headerDataSizeBytes , std::back_inserter(dataFieldHeaderVector));
121 FORWARD_RESULT( secondaryHeader->deserialize(dataFieldHeaderVector ));
122 setDataFieldHeader(secondaryHeader);
124 if (data.size() > (8 + headerDataSizeBytes)) {
125 std::vector<uint8_t> dataFieldVector;
127 if (data.size() > (9 + headerDataSizeBytes)) {
128 std::copy(data.begin() + 6 + headerDataSizeBytes, data.end(), std::back_inserter(dataFieldVector));
130 FORWARD_RESULT(deserialize({data[0], data[1], data[2], data[3], data[4], data[5]}, dataFieldVector));
138 "Cannot Serialize Packet, Invalid Data provided");
140 std::vector<uint8_t> secondaryHeader;
141 std::vector<uint8_t> dataFieldVector;
142 std::copy(data.begin() + 6, data.begin() + 6 + headerDataSizeBytes, std::back_inserter(secondaryHeader));
144 if (data.size() > (7 + headerDataSizeBytes)) {
145 std::copy(data.begin() + 6 + headerDataSizeBytes, data.end(), std::back_inserter(dataFieldVector));
147 FORWARD_RESULT(deserialize({data[0], data[1], data[2], data[3], data[4], data[5]}, dataFieldVector));
153 "Cannot Deserialize Packet, Invalid Header Data provided.");
155 m_sequenceCounter = m_primaryHeader.getSequenceCount();
158 "Cannot Deserialize Packet, Invalid Data provided, at least CRC is required.");
160 std::vector<uint8_t> dataCopy;
161 m_CRC16 = (data[data.size() - 2] << 8) + data.back();
163 if (data.size() == 2)
return true;
165 std::copy(data.begin(), data.end() - 2, std::back_inserter(dataCopy));
173 return 8 + m_dataField.getDataFieldUsedBytesSize();
178 m_sequenceCounter = m_primaryHeader.getSequenceCount();
179 m_updateStatus =
false;
185 m_sequenceCounter = m_primaryHeader.getSequenceCount();
186 m_updateStatus =
false;
191 m_primaryHeader = header;
196 m_sequenceCounter = m_primaryHeader.getSequenceCount();
197 m_updateStatus =
false;
201 m_dataField.setDataFieldHeader(header);
202 m_updateStatus =
false;
206 FORWARD_RESULT(m_dataField.setDataFieldHeader( data, headerType ));
207 m_updateStatus =
false;
212 const std::string &headerType) {
213 FORWARD_RESULT(m_dataField.setDataFieldHeader( pData, sizeData, headerType ));
214 m_updateStatus =
false;
220 m_updateStatus =
false;
226 m_updateStatus =
false;
232 m_updateStatus =
false;
238 m_updateStatus =
false;
243 m_primaryHeader.setSequenceFlags(flags);
244 m_updateStatus =
false;
249 "Unable to set non 0 value for UNSEGMENTED packet");
250 m_sequenceCounter = count;
251 m_updateStatus =
false;
256 m_dataField.setDataPacketSize(size);
260 m_enableUpdatePacket = enable;
261 m_dataField.setDataFieldHeaderAutoUpdateStatus(enable);
#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>).
uint16_t crc16(const std::vector< uint8_t > &data, uint16_t polynomial=0x1021, uint16_t initialValue=0xFFFF, uint16_t finalXorValue=0x0000)
Computes the CRC-16 checksum for a given data vector with configurable parameters.
Represents the data field of a CCSDS packet.
bool getDataFieldHeaderFlag() const
retrieves true if a known secondary header has been set
std::vector< uint8_t > getFullDataFieldBytes()
Retrieves the full data field by combining the data field header and application data.
uint16_t m_CRC16
Cyclic Redundancy check 16 bits.
ResultBool setSequenceCount(uint16_t count)
Sets the sequence count for the packet.
std::vector< uint8_t > serialize()
Retrieves the full packet as a vector of bytes.
bool m_updateStatus
When setting data thus value should be set to false.
ResultBool deserialize(const std::vector< uint8_t > &data)
Deserializes a vector of bytes into a CCSDS packet.
void update()
Updates Primary headers data field size.
ResultBool setApplicationData(const std::vector< uint8_t > &data)
Sets the application data for the packet.
void setUpdatePacketEnable(bool enable)
needs to be called as soon as possible, probably also from constructor.
void setDataFieldSize(uint16_t size)
Sets the maximum data packet size for the CCSDS DataField.
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.
Header m_primaryHeader
6 bytes / 48 bits / 12 hex
uint64_t getPrimaryHeader64bit()
Retrieves the primary header of the packet.
DataField m_dataField
variable
uint16_t getCRC()
Computes and retrieves the CRC-16 checksum of the packet.
void setSequenceFlags(ESequenceFlag flags)
Sets the sequence flags for the packet's primary header.
std::vector< uint8_t > getApplicationDataBytes()
Retrieves the application data from the data field.
std::vector< uint8_t > getFullDataFieldBytes()
Retrieves the full data field data.
void setPrimaryHeader(PrimaryHeader data)
Sets the primary header using the provided PrimaryHeader object.
std::vector< uint8_t > getCRCVectorBytes()
Retrieves the CRC-16 checksum as a vector of bytes.
uint16_t m_sequenceCounter
uint16_t getFullPacketLength()
Retrieves the current size of the CCSDS Packet.
CCSDS::Header & getPrimaryHeader()
returns the CCSDS packet's Primary Header.
uint16_t getDataFieldMaximumSize()
returns the maximum data field size
CCSDS::DataField & getDataField()
returns the CCSDS packet's DataField.
bool m_enableUpdatePacket
Enables primary header and secondary header update.
std::vector< uint8_t > getDataFieldHeaderBytes()
Retrieves the secondary header data from the data field.
std::vector< uint8_t > getPrimaryHeaderBytes()
Retrieves the primary header of the packet as a vector of bytes.
CRC16Config m_CRC16Config
structure holding configuration of crc calculation.
Encapsulates a result that can hold either a value or an Error.
ESequenceFlag
Represents the sequence flags used in CCSDS telemetry transfer frames.
@ UNSEGMENTED
11 Complete packet in a single frame.
@ INVALID_DATA
Data is invalid.
@ INVALID_HEADER_DATA
Header data is invalid.
@ INVALID_SECONDARY_HEADER_DATA
Secondary header data is invalid.