12 std::string binaryString;
14 const int paddedBits = ((bits + 3) / 4) * 4;
16 for (
int i = paddedBits - 1; i >= 0; --i) {
17 binaryString += ((value >> i) & 1) ?
'1' :
'0';
20 if (i % 4 == 0 && i != 0) {
30 for (
int i = num - 1; i >= 0; --i) {
39 if (buffer.size() > limitBytes) {
40 for (
size_t i= 0 ; i < static_cast<int>(limitBytes / 2); i++) {
41 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(buffer[i]) <<
" ";
44 for (
size_t i = buffer.size() -
static_cast<int>(limitBytes / 2) ; i < buffer.size(); i++) {
45 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(buffer[i]) <<
" ";
48 for (
const unsigned char i: buffer) {
49 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(i) <<
" ";
52 std::cout <<
"]" << std::endl;
58 const uint16_t maxSize = (applicationData.size() > dataFieldHeader.size())
59 ? applicationData.size()
60 : dataFieldHeader.size();
62 std::cout <<
" [CCSDS DATA] Data Field Length : " << applicationData.size() + dataFieldHeader.size() <<
" bytes" << std::endl;
65 :
"False") <<
" ]" << std::endl;
66 if (!dataFieldHeader.empty()) {
67 std::cout <<
" [CCSDS DATA] Secondary Header [Hex] : " <<
getBitsSpaces(
68 (maxSize -
static_cast<uint16_t
>(dataFieldHeader.size())) * 4);
72 std::cout <<
" [CCSDS DATA] Application Data [Hex] : ";
74 std::cout << std::endl;
78 std::cout <<
" [CCSDS HEADER] Full Primary Header [Hex] : [ " <<
getBitsSpaces(17 - 12) <<
"0x" << std::hex <<
80 std::cout << std::endl;
82 std::cout <<
" [CCSDS HEADER] Version Number : [ " <<
getBitsSpaces(19 - 4) <<
84 std::cout <<
" - [Dec] : "<< std::dec << static_cast<int>( header.
getVersionNumber()) << std::endl;
86 std::cout <<
" [CCSDS HEADER] Type : [ " <<
getBitsSpaces(19 - 4) <<
88 std::cout <<
" - [Dec] : "<< std::dec << static_cast<int>( header.
getType()) << std::endl;
90 std::cout <<
" [CCSDS HEADER] APID : [ " <<
getBitsSpaces(17 - 12) <<
92 std::cout <<
" - [Dec] : " << std::dec << header.
getAPID() << std::endl;
98 std::cout <<
" [CCSDS HEADER] Sequence Flags : [ " <<
getBitsSpaces(19 - 4) <<
100 std::cout <<
" - : ";
103 std::cout <<
"CONTINUING_SEGMENT";
106 std::cout <<
"FIRST_SEGMENT";
109 std::cout <<
"LAST_SEGMENT";
112 std::cout <<
"UNSEGMENTED";
117 std::cout << std::endl;
121 std::cout <<
" - [Dec] : "<< std::dec << header.
getSequenceCount() << std::endl;
125 std::cout <<
" - [Dec] : "<< std::dec << header.
getDataLength() << std::endl;
127 std::cout << std::endl;
142 std::cout <<
"[ CCSDSPack ] CRC-16 [Hex] : [ " <<
"0x" << std::hex << packet.
getCRC() <<
" ]";
143 std::cout <<
" - [Dec] : "<< std::dec << packet.
getCRC() << std::endl;
148 const std::vector<uint8_t> &data,
const uint16_t polynomial,
const uint16_t initialValue,
149 const uint16_t finalXorValue) {
150 uint16_t crc = initialValue;
152 for (
const auto &
byte: data) {
153 crc ^=
static_cast<uint16_t
>(byte) << 8;
154 for (
int i = 0; i < 8; ++i) {
158 crc = (crc << 1) ^ polynomial;
165 return crc ^ finalXorValue;
174 std::cout <<
"[ CCSDS Manager ] Number of Packets : " << manager.
getTotalPackets() << std::endl;
175 std::cout <<
"[ CCSDS Manager ] Sync Pattern Enabled : " << (manager.
getSyncPatternEnable() ?
"True" :
"False") << std::endl;
176 std::cout <<
"[ CCSDS Manager ] Sync Pattern : 0x" << std::hex << manager.
getSyncPattern() << std::dec << std::endl;
179 std::cout <<
"[ CCSDS Manager ] Template : " << std::endl ;
184 std::cout <<
"__________________________________________________________________________________________________________________" << std::endl;
185 std::cout <<
"[ CCSDS Manager ] Printing Packet [ " << idx <<
" ]:" << std::endl;
186 std::cout <<
"[ CCSDS Manager ] Packet Length : " << packet.getFullPacketLength() <<
" bytes" << std::endl;
187 std::cout <<
"[ CCSDS Manager ] Data ";
197 std::ofstream out(filename, std::ios::binary);
202 out.write(
reinterpret_cast<const char*
>(data.data()),
static_cast<std::streamsize
>(data.size()));
211 std::ifstream in(filename, std::ios::binary | std::ios::ate);
215 const std::streamsize size = in.tellg();
216 in.seekg(0, std::ios::beg);
219 std::vector<uint8_t> data(size);
220 in.read(
reinterpret_cast<char*
>(data.data()), size);
228 if ( exp.has_value()) {
235 return str.size() >= suffix.size() &&
236 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
247 if (
line.empty() ||
line.front() ==
'#')
continue;
252 if (type ==
"string") {
254 }
else if (type ==
"int") {
261 }
else if (type ==
"float") {
263 }
else if (type ==
"bool") {
265 }
else if (type ==
"bytes") {
295 if (!value.empty() && value.front() ==
'"' && value.back() ==
'"')
296 value = value.substr(1, value.size() - 2);
298 return {
key, type, value};
302 std::vector<uint8_t>
result;
309 while (std::getline(
ss,
token,
',')) {
315 if (
token.size() > 2 &&
token.substr(0, 2) ==
"0x") {
#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 fileExists(const std::string &fileName)
filesystem check fore file existence prepared for both windows and linux.
std::string getBinaryString(const uint32_t value, const int bits)
Converts a given value to its binary representation as a string, with spaces every 4 bits.
uint16_t crc16(const std::vector< uint8_t > &data, const uint16_t polynomial, const uint16_t initialValue, const uint16_t finalXorValue)
Computes the CRC-16 checksum for a given data vector with configurable parameters.
void printPacket(CCSDS::Packet &packet)
Prints to console a CCSDS Packets, breaking it down to Primary header and Data field.
bool stringEndsWith(const std::string &str, const std::string &suffix)
Tests if str ends with suffix.
CCSDS::ResultBool printPrimaryHeader(CCSDS::Packet &packet)
Prints to console the primary header of a provided CCSDS packet.
std::string getBitsSpaces(const int num)
Generates a string of spaces for formatting binary outputs.
void printData(CCSDS::DataField dataField)
Prints the data field details, including the secondary header and application data.
CCSDS::ResultBuffer readBinaryFile(const std::string &filename)
Read a specified binary file and return its contents as a buffer.
void printHeader(CCSDS::Header &header)
Prints the header fields and their binary or hexadecimal representations.
CCSDS::ResultBool writeBinaryFile(const std::vector< uint8_t > &data, const std::string &filename)
This function takes in a buffer of data and a file name.
void printBufferData(const std::vector< uint8_t > &buffer, const int limitBytes)
Prints to console the HEX data from the bytes vector.
void printPackets(CCSDS::Manager &manager)
Prints to console a series of CCSDS Packets contained in the manager.
void printDataField(CCSDS::Packet &packet)
Prints the data field and the CRC-16 checksum of the packet.
Represents the data field of a CCSDS packet.
std::vector< uint8_t > getDataFieldHeaderBytes()
Retrieves the secondary header data as a vector of bytes.
std::vector< uint8_t > getApplicationData()
Retrieves the application data from the data field.
bool getDataFieldHeaderFlag() const
retrieves true if a known secondary header has been set
Represents an error with both an error code and a message.
Manages CCSDS packets and their templates.
std::vector< Packet > & getPacketsReference()
Returns a reference to the packets vector.
uint32_t getSyncPattern() const
returns the currently set sync pattern.
uint16_t getTotalPackets() const
Retrieves the total number of packets managed.
Packet getTemplate()
Retrieves the packet template.
bool getSyncPatternEnable() const
returns the current settings of the sync pattern enable
Represents a CCSDS (Consultative Committee for Space Data Systems) packet.
uint16_t getCRC()
Computes and retrieves the CRC-16 checksum of the packet.
CCSDS::DataField & getDataField()
returns the CCSDS packet's DataField.
Encapsulates a result that can hold either a value or an Error.
std::variant< std::string, int, float, bool, std::vector< uint8_t > > ConfigValue
bool isKey(const std::string &key) const
static CCSDS::ResultBuffer parseBytes(const std::string &valueStr)
Parse string "[1,2,3]" into vector<uint8_t>
std::unordered_map< std::string, ConfigValue > values
CCSDS::ResultBool load(const std::string &filename)
Load config file.
CCSDS::Result< T > get(const std::string &key) const
Get value by key and type.
static std::tuple< std::string, std::string, std::string > parseLine(const std::string &line)
Parse a single line from config.
@ FILE_READ_ERROR
Reading from file failure.
@ FILE_WRITE_ERROR
Writing to file failure.