21 const std::vector<std::uint8_t> &data,
const std::uint16_t polynomial,
const std::uint16_t initialValue,
22 const std::uint16_t finalXorValue) {
23 std::uint16_t crc = initialValue;
25 for (
const auto &
byte: data) {
26 crc ^=
static_cast<std::uint16_t
>(byte) << 8;
27 for (std::int32_t i = 0; i < 8; ++i) {
31 crc = (crc << 1) ^ polynomial;
38 return crc ^ finalXorValue;
42 return str.size() >= suffix.size() &&
43 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
47 std::string binaryString;
49 const std::int32_t paddedBits = ((bits + 3) / 4) * 4;
51 for (std::int32_t i = paddedBits - 1; i >= 0; --i) {
52 binaryString += ((value >> i) & 1) ?
'1' :
'0';
55 if (i % 4 == 0 && i != 0) {
65 for (std::int32_t i = num - 1; i >= 0; --i) {
72void printBufferData(
const std::vector<std::uint8_t> &buffer,
const std::int32_t limitBytes) {
74 if (buffer.size() > limitBytes) {
75 for (
size_t i= 0 ; i < static_cast<int>(limitBytes / 2); i++) {
76 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(buffer[i]) <<
" ";
79 for (
size_t i = buffer.size() -
static_cast<int>(limitBytes / 2) ; i < buffer.size(); i++) {
80 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(buffer[i]) <<
" ";
83 for (
const unsigned char i: buffer) {
84 std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(i) <<
" ";
87 std::cout <<
"]" << std::endl;
93 const std::uint16_t maxSize = (applicationData.size() > dataFieldHeader.size())
94 ? applicationData.size()
95 : dataFieldHeader.size();
97 std::cout <<
" [CCSDS DATA] Data Field Length : " << applicationData.size() + dataFieldHeader.size() <<
" bytes" << std::endl;
100 :
"False") <<
" ]" << std::endl;
101 if (!dataFieldHeader.empty()) {
102 std::cout <<
" [CCSDS DATA] Secondary Header [Hex] : " <<
getBitsSpaces(
103 (maxSize -
static_cast<std::uint16_t
>(dataFieldHeader.size())) * 4);
107 std::cout <<
" [CCSDS DATA] Application Data [Hex] : ";
109 std::cout << std::endl;
113 std::cout <<
" [CCSDS HEADER] Full Primary Header [Hex] : [ " <<
getBitsSpaces(17 - 12) <<
"0x" << std::hex <<
115 std::cout << std::endl;
117 std::cout <<
" [CCSDS HEADER] Version Number : [ " <<
getBitsSpaces(19 - 4) <<
119 std::cout <<
" - [Dec] : "<< std::dec << static_cast<int>( header.
getVersionNumber()) << std::endl;
121 std::cout <<
" [CCSDS HEADER] Type : [ " <<
getBitsSpaces(19 - 4) <<
123 std::cout <<
" - [Dec] : "<< std::dec << static_cast<int>( header.
getType()) << std::endl;
129 std::cout <<
" [CCSDS HEADER] APID : [ " <<
getBitsSpaces(17 - 12) <<
131 std::cout <<
" - [Dec] : " << std::dec << header.
getAPID() << std::endl;
133 std::cout <<
" [CCSDS HEADER] Sequence Flags : [ " <<
getBitsSpaces(19 - 4) <<
135 std::cout <<
" - : ";
138 std::cout <<
"CONTINUING_SEGMENT";
141 std::cout <<
"FIRST_SEGMENT";
144 std::cout <<
"LAST_SEGMENT";
147 std::cout <<
"UNSEGMENTED";
152 std::cout << std::endl;
156 std::cout <<
" - [Dec] : "<< std::dec << header.
getSequenceCount() << std::endl;
160 std::cout <<
" - [Dec] : "<< std::dec << header.
getDataLength() << std::endl;
162 std::cout << std::endl;
177 std::cout <<
"[ CCSDSPack ] CRC-16 [Hex] : [ " <<
"0x" << std::hex << packet.
getCRC() <<
" ]";
178 std::cout <<
" - [Dec] : "<< std::dec << packet.
getCRC() << std::endl;
188 std::cout <<
"[ CCSDS Manager ] Number of Packets : " << manager.
getTotalPackets() << std::endl;
189 std::cout <<
"[ CCSDS Manager ] Sync Pattern Enabled : " << (manager.
getSyncPatternEnable() ?
"True" :
"False") << std::endl;
190 std::cout <<
"[ CCSDS Manager ] Sync Pattern : 0x" << std::hex << manager.
getSyncPattern() << std::dec << std::endl;
193 std::cout <<
"[ CCSDS Manager ] Template : " << std::endl ;
196 std::int32_t idx = 1;
198 std::cout <<
"__________________________________________________________________________________________________________________" << std::endl;
199 std::cout <<
"[ CCSDS Manager ] Printing Packet [ " << idx <<
" ]:" << std::endl;
200 std::cout <<
"[ CCSDS Manager ] Packet Length : " << packet.getFullPacketLength() <<
" bytes" << std::endl;
201 std::cout <<
"[ CCSDS Manager ] Data ";
211 std::ofstream out(filename, std::ios::binary);
216 out.write(
reinterpret_cast<const char*
>(data.data()),
static_cast<std::streamsize
>(data.size()));
225 std::ifstream in(filename, std::ios::binary | std::ios::ate);
229 const std::streamsize size = in.tellg();
230 in.seekg(0, std::ios::beg);
233 std::vector<std::uint8_t> data(size);
234 in.read(
reinterpret_cast<char*
>(data.data()), size);
242 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.