17 m_templatePacket.setUpdatePacketEnable(
false);
18 m_templatePacket = std::move(packet);
19 m_validator.setTemplatePacket(m_templatePacket);
20 m_validator.configure(
true,
true,
true);
21 m_validateEnable =
true;
22 m_templateIsSet =
true;
30 m_templatePacket = std::move(templatePacket);
31 m_templateIsSet =
true;
39 m_templatePacket = std::move(templatePacket);
40 m_templateIsSet =
true;
45 m_templatePacket.setDataFieldSize(size);
49 return m_templatePacket.getDataFieldMaximumSize();
56 const auto maxBytesPerPacket = m_templatePacket.getDataFieldMaximumSize();
57 const auto dataBytesSize = data.size();
59 if (!m_packets.empty()) {
64 auto remainderBytes =
static_cast<std::int32_t
>(dataBytesSize);
66 while (i < dataBytesSize) {
67 Packet newPacket = m_templatePacket;
68 std::vector<std::uint8_t> tmp;
69 if (remainderBytes > maxBytesPerPacket) {
70 tmp.insert(tmp.end(), data.begin() + i, data.begin() + i + maxBytesPerPacket);
71 remainderBytes -= maxBytesPerPacket;
78 i += maxBytesPerPacket;
83 tmp.insert(tmp.end(), data.begin() + i, data.begin() + i + remainderBytes);
92 m_packets.push_back(std::move(newPacket));
99 m_updateEnable = enable;
100 for (
auto &packet: m_packets) {
101 packet.setUpdatePacketEnable(enable);
106 m_validateEnable = enable;
110 auto data = m_templatePacket.serialize();
117 "Cannot get packet, index is out of bounds");
118 if (m_validateEnable) {
119 m_packets[index].update();
120 const std::string errorMessage =
"Validation failure for packet at index " + std::to_string(index);
124 return m_packets[index].serialize();
128 std::vector<std::uint8_t> buffer;
129 for (
auto packet : m_packets) {
130 if (m_syncPattEnable) {
131 buffer.push_back(m_syncPattern >> 24 & 0xff);
132 buffer.push_back(m_syncPattern >> 16 & 0xff);
133 buffer.push_back(m_syncPattern >> 8 & 0xff);
134 buffer.push_back(m_syncPattern & 0xff);
136 std::vector<std::uint8_t> packetBuffer = packet.serialize();
137 buffer.insert(buffer.end(), packetBuffer.begin(), packetBuffer.end());
144 std::vector<std::uint8_t> data;
146 for (std::uint32_t index = 0; index < m_packets.size(); index++) {
147 if (m_validateEnable) {
148 const std::string errorMessage =
"Validation failure for packet at index" + std::to_string(index);
152 auto applicationData = m_packets[index].getApplicationDataBytes();
153 data.insert(data.end(), applicationData.begin(), applicationData.end());
160 "Cannot get Application data, index is out of bounds");
161 return m_packets[index].getApplicationDataBytes();
165 return m_packets.size();
174 if (m_validateEnable && !m_updateEnable) {
175 if (!m_templateIsSet) {
176 m_validator.configure(
true,
true,
false);
181 m_packets.push_back(std::move(packet));
195 for (
const auto& packet: packets) {
203 std::uint32_t offset{0};
204 while (offset < packetsBuffer.size()) {
205 std::vector<std::uint8_t> headerData;
206 if (m_syncPattEnable) {
207 const std::uint32_t value = (
static_cast<std::uint32_t
>(packetsBuffer[offset]) << 24) |
208 (
static_cast<std::uint32_t
>(packetsBuffer[offset+1]) << 16) |
209 (
static_cast<std::uint32_t
>(packetsBuffer[offset+2]) << 8) |
210 (
static_cast<std::uint32_t
>(packetsBuffer[offset+3]));
215 copy_n(packetsBuffer.begin() + offset, 6, std::back_inserter(headerData));
220 std::vector<std::uint8_t>packetData;
222 copy_n(packetsBuffer.begin() + offset, packetSize, std::back_inserter(packetData));
224 offset += packetSize;
230 std::vector<std::uint8_t> buffer;
246 std::vector<std::uint8_t> buffer;
252 return Error{
INVALID_DATA,
"Cannot load template, invalid file provided [supported extensions [.bin, .cfg]]"};
260 m_templateIsSet =
false;
261 m_templatePacket= {};
#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>).
#define ASSIGN_CP(var, result)
Macro to assign a result value to a variable or return an error by copy.
bool stringEndsWith(const std::string &str, const std::string &suffix)
Tests if str ends with suffix.
CCSDS::ResultBuffer readBinaryFile(const std::string &filename)
Read a specified binary file and return its contents as a buffer.
CCSDS::ResultBool writeBinaryFile(const std::vector< std::uint8_t > &data, const std::string &filename)
This function takes in a buffer of data and a file name.
Represents an error with both an error code and a message.
ResultBool loadTemplateConfigFile(const std::string &configPath)
Loads a template packet from a configuration file.
ResultBool setPacketTemplate(Packet packet)
Sets a new packet template.
std::vector< Packet > getPackets()
Retrieves all stored packets.
ResultBuffer getPacketTemplate()
Retrieves the packet template in serialized form.
void setSyncPatternEnable(bool enable)
enable sync pattern utilization both in serialization, deserialization, read and write.
std::uint16_t getDataFieldSize() const
retrieves the set data field size (this includes the secondary header if present)
ResultBool readTemplate(const std::string &filename)
Load a template packet from a binary or configuration file.
void setSyncPattern(std::uint32_t syncPattern)
set sync pattern that should indicate the start of a CCSDS packet.
ResultBool load(const std::vector< Packet > &packets)
Load a vector of packets.
ResultBool addPacket(Packet packet)
Adds a new packet to the list.
std::uint32_t getSyncPattern() const
returns the currently set sync pattern.
std::uint16_t getTotalPackets() const
Retrieves the total number of packets managed.
ResultBuffer getApplicationDataBuffer()
Retrieves the application data from the packets.
std::uint32_t m_syncPattern
ResultBool loadTemplateConfig(const Config &cfg)
Loads a template packet from a configuration object.
ResultBool read(const std::string &binaryFile)
Load a packet or a series of packets from a binary file.
void clearPackets()
Clears the packets and sets the counter to 0.
std::vector< std::uint8_t > getPacketsBuffer() const
Retrieves a buffer containing all the stored packets sequentially.
ResultBool addPacketFromBuffer(const std::vector< std::uint8_t > &packetBuffer)
Adds a new packet to the list.
void setAutoUpdateEnable(bool enable)
Enables or disables automatic updates for packets.
void setDataFieldSize(std::uint16_t size)
Sets the size of the data field.
bool getSyncPatternEnable() const
returns the current settings of the sync pattern enable
ResultBool setApplicationData(const std::vector< std::uint8_t > &data)
Sets the application data for the packet.
void clear()
Clears the manager, removes all packets and template.
void setAutoValidateEnable(bool enable)
Enables or disables automatic validation of packets.
ResultBuffer getApplicationDataBufferAtIndex(std::uint16_t index)
Retrieves the application data from a packet at the given index.
ResultBool write(const std::string &binaryFile) const
Write a packet or a series of packets to a binary file.
ResultBuffer getPacketBufferAtIndex(std::uint16_t index)
Retrieves a packet at the specified index.
Represents a CCSDS (Consultative Committee for Space Data Systems) packet.
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 loadFromConfig(const Config &cfg)
Loads a packet from a configuration object, including secondary header if present.
void setSequenceFlags(ESequenceFlag flags)
Sets the sequence flags for the packet's primary header.
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.
ResultBool deserialize(const std::vector< std::uint8_t > &data)
Deserializes a vector of bytes into a CCSDS packet.
Encapsulates a result that can hold either a value or an Error.
Parses and stores config values from custom file format.
@ NO_DATA
No data available.
@ INVALID_DATA
Data is invalid.
@ INVALID_HEADER_DATA
Header data is invalid.
@ TEMPLATE_SET_FAILURE
Failed to set Template Packet.
@ VALIDATION_FAILURE
Validation Failure.
@ LAST_SEGMENT
10 Last segment of a multi-frame packet.
@ UNSEGMENTED
11 Complete packet in a single frame.
@ CONTINUING_SEGMENT
00 Intermediate segment of a packet.
@ FIRST_SEGMENT
01 First segment of a new packet.