18 const std::vector<std::uint8_t> &data,
const std::uint16_t polynomial,
const std::uint16_t initialValue,
19 const std::uint16_t finalXorValue) {
20 std::uint16_t crc = initialValue;
22 for (
const auto &
byte: data) {
23 crc ^=
static_cast<std::uint16_t
>(byte) << 8;
24 for (std::int32_t i = 0; i < 8; ++i) {
28 crc = (crc << 1) ^ polynomial;
35 return crc ^ finalXorValue;
39 return str.size() >= suffix.size() &&
40 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
44 std::string binaryString;
46 const std::int32_t paddedBits = ((bits + 3) / 4) * 4;
48 for (std::int32_t i = paddedBits - 1; i >= 0; --i) {
49 binaryString += ((value >> i) & 1) ?
'1' :
'0';
52 if (i % 4 == 0 && i != 0) {
62 for (std::int32_t i = num - 1; i >= 0; --i) {
69void printBufferData(
const std::vector<std::uint8_t> &buffer,
const std::int32_t limitBytes) {
71 if (buffer.size() > limitBytes) {
72 for (
size_t i= 0 ; i < static_cast<int>(limitBytes / 2); i++) {
73 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(buffer[i]) <<
" ";
76 for (
size_t i = buffer.size() -
static_cast<int>(limitBytes / 2) ; i < buffer.size(); i++) {
77 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(buffer[i]) <<
" ";
80 for (
const unsigned char i: buffer) {
81 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(i) <<
" ";
84 std::cout <<
"]" << std::endl;
90 const std::uint16_t maxSize = (applicationData.size() > dataFieldHeader.size())
91 ? applicationData.size()
92 : dataFieldHeader.size();
94 std::cout <<
" [CCSDS DATA] Data Field Length : " << applicationData.size() + dataFieldHeader.size() <<
" bytes" << std::endl;
97 :
"False") <<
" ]" << std::endl;
98 if (!dataFieldHeader.empty()) {
99 std::cout <<
" [CCSDS DATA] Secondary Header [Hex] : " <<
getBitsSpaces(
100 (maxSize -
static_cast<std::uint16_t
>(dataFieldHeader.size())) * 4);
104 std::cout <<
" [CCSDS DATA] Application Data [Hex] : ";
106 std::cout << std::endl;
110 std::cout <<
" [CCSDS HEADER] Full Primary Header [Hex] : [ " <<
getBitsSpaces(17 - 12) <<
"0x" << std::hex <<
112 std::cout << std::endl;
114 std::cout <<
" [CCSDS HEADER] Version Number : [ " <<
getBitsSpaces(19 - 4) <<
116 std::cout <<
" - [Dec] : "<< std::dec << static_cast<int>( header.
getVersionNumber()) << std::endl;
118 std::cout <<
" [CCSDS HEADER] Type : [ " <<
getBitsSpaces(19 - 4) <<
120 std::cout <<
" - [Dec] : "<< std::dec << static_cast<int>( header.
getType()) << std::endl;
126 std::cout <<
" [CCSDS HEADER] APID : [ " <<
getBitsSpaces(17 - 12) <<
128 std::cout <<
" - [Dec] : " << std::dec << header.
getAPID() << std::endl;
130 std::cout <<
" [CCSDS HEADER] Sequence Flags : [ " <<
getBitsSpaces(19 - 4) <<
132 std::cout <<
" - : ";
135 std::cout <<
"CONTINUING_SEGMENT";
138 std::cout <<
"FIRST_SEGMENT";
141 std::cout <<
"LAST_SEGMENT";
144 std::cout <<
"UNSEGMENTED";
149 std::cout << std::endl;
153 std::cout <<
" - [Dec] : "<< std::dec << header.
getSequenceCount() << std::endl;
157 std::cout <<
" - [Dec] : "<< std::dec << header.
getDataLength() << std::endl;
159 std::cout << std::endl;
174 std::cout <<
"[ CCSDSPack ] CRC-16 [Hex] : [ " <<
"0x" << std::hex << packet.
getCRC() <<
" ]";
175 std::cout <<
" - [Dec] : "<< std::dec << packet.
getCRC() << std::endl;
185 std::cout <<
"[ CCSDS Manager ] Number of Packets : " << manager.
getTotalPackets() << std::endl;
186 std::cout <<
"[ CCSDS Manager ] Sync Pattern Enabled : " << (manager.
getSyncPatternEnable() ?
"True" :
"False") << std::endl;
187 std::cout <<
"[ CCSDS Manager ] Sync Pattern : 0x" << std::hex << manager.
getSyncPattern() << std::dec << std::endl;
190 std::cout <<
"[ CCSDS Manager ] Template : " << std::endl ;
193 std::int32_t idx = 1;
195 std::cout <<
"__________________________________________________________________________________________________________________" << std::endl;
196 std::cout <<
"[ CCSDS Manager ] Printing Packet [ " << idx <<
" ]:" << std::endl;
197 std::cout <<
"[ CCSDS Manager ] Packet Length : " << packet.getFullPacketLength() <<
" bytes" << std::endl;
198 std::cout <<
"[ CCSDS Manager ] Data ";
208 std::ofstream out(filename, std::ios::binary);
213 out.write(
reinterpret_cast<const char*
>(data.data()),
static_cast<std::streamsize
>(data.size()));
222 std::ifstream in(filename, std::ios::binary | std::ios::ate);
226 const std::streamsize size = in.tellg();
227 in.seekg(0, std::ios::beg);
230 std::vector<std::uint8_t> data(size);
231 in.read(
reinterpret_cast<char*
>(data.data()), size);
239 if ( res.has_value()) {
#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< std::uint8_t > &data, const std::uint16_t polynomial, const std::uint16_t initialValue, const std::uint16_t finalXorValue)
Computes the CRC-16 checksum for a given data vector with configurable parameters.
bool fileExists(const std::string &fileName)
filesystem check fore file existence prepared for both windows and linux.
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 getBinaryString(const std::uint32_t value, const std::int32_t bits)
Converts a given value to its binary representation as a string, with spaces every 4 bits.
void printData(CCSDS::DataField dataField)
Prints the data field details, including the secondary header and application data.
std::string getBitsSpaces(const std::int32_t num)
Generates a string of spaces for formatting binary outputs.
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.
void printBufferData(const std::vector< std::uint8_t > &buffer, const std::int32_t limitBytes)
Prints to console the HEX data from the bytes vector.
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.
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.
bool getDataFieldHeaderFlag() const
retrieves true if a known secondary header has been set
std::vector< std::uint8_t > getDataFieldHeaderBytes()
Retrieves the secondary header data as a vector of bytes.
std::vector< std::uint8_t > getApplicationData()
Retrieves the application data from the data field.
Manages CCSDS packets and their templates.
std::vector< Packet > & getPacketsReference()
Returns a reference to the packets vector.
std::uint32_t getSyncPattern() const
returns the currently set sync pattern.
std::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.
std::uint16_t getCRC()
Computes and retrieves the CRC-16 checksum of the packet.
DataField & getDataField()
returns the CCSDS packet's DataField.
Encapsulates a result that can hold either a value or an Error.
@ FILE_READ_ERROR
Reading from file failure.
@ FILE_WRITE_ERROR
Writing to file failure.