CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
CCSDSHeader.cpp
Go to the documentation of this file.
1#include "CCSDSHeader.h"
2#include "CCSDSUtils.h"
3
4CCSDS::ResultBool CCSDS::Header::deserialize(const std::vector<uint8_t> &data) {
5 RET_IF_ERR_MSG(data.size() != 6, ErrorCode::INVALID_HEADER_DATA, "Invalid Header Data provided: size != 6");
6
7 uint64_t headerData = 0;
8 for (int i = 0; i < 6; ++i) {
9 headerData |= static_cast<uint64_t>(data[i]) << (40 - i * 8); // Combine MSB to LSB
10 }
11 FORWARD_RESULT(setData(headerData));
12 return true;
13}
14
17 "Input data exceeds expected bit size for version or size.");
18 // Decompose data using mask and shifts
19 m_dataLength = (data & 0xFFFF); // last 16 bits
20 m_packetSequenceControl = (data >> 16) & 0xFFFF; // middle 16 bits
21 m_packetIdentificationAndVersion = (data >> 32); // first 16 bits
22
23 // decompose packet identifier
24 m_versionNumber = (m_packetIdentificationAndVersion >> 13); // First 3 bits for version
25 m_type = (m_packetIdentificationAndVersion >> 12) & 0x1; // Next 1 bit
26 m_dataFieldHeaderFlag = (m_packetIdentificationAndVersion >> 11) & 0x1; // Next 1 bit
27 m_APID = (m_packetIdentificationAndVersion & 0x07FF); // Last 11 bits
28
29 // decompose sequence control
30 m_sequenceFlags = (m_packetSequenceControl >> 14); // first 2 bits
31 m_sequenceCount = (m_packetSequenceControl & 0x3FFF); // Last 14 bits.
32 return true;
33}
34
35std::vector<uint8_t> CCSDS::Header::serialize() {
36 m_packetSequenceControl = (static_cast<uint16_t>(m_sequenceFlags) << 14) | m_sequenceCount;
37 m_packetIdentificationAndVersion = (static_cast<uint16_t>(m_versionNumber) << 13) | (m_type << 12) | static_cast<
38 uint16_t>((m_dataFieldHeaderFlag) << 11) | m_APID;
39
40 std::vector data{
41 static_cast<unsigned char>(m_packetIdentificationAndVersion >> 8),
42 static_cast<unsigned char>(m_packetIdentificationAndVersion & 0xFF),
43 static_cast<unsigned char>(m_packetSequenceControl >> 8),
44 static_cast<unsigned char>(m_packetSequenceControl & 0xFF),
45 static_cast<unsigned char>(m_dataLength >> 8),
46 static_cast<unsigned char>(m_dataLength & 0xFF),
47 };
48 return data;
49}
50
52 m_versionNumber = data.versionNumber & 0x0007;
53 m_type = data.type & 0x0001;
54 m_dataFieldHeaderFlag = data.dataFieldHeaderFlag & 0x0001;
55 m_APID = data.APID & 0x07FF;
56 m_sequenceFlags = data.sequenceFlags & 0x0003;
57 m_sequenceCount = data.sequenceCount & 0x3FFF;
58 m_dataLength = data.dataLength;
59
60 m_packetSequenceControl = (static_cast<uint16_t>(m_sequenceFlags) << 14) | m_sequenceCount;
61 m_packetIdentificationAndVersion = (static_cast<uint16_t>(m_versionNumber) << 13) | (m_type << 12) | static_cast<
62 uint16_t>((m_dataFieldHeaderFlag) << 11) | m_APID;
63}
#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>).
ResultBool deserialize(const std::vector< uint8_t > &data)
Sets the header data from a 64-bit integer representation.
std::vector< uint8_t > serialize()
decomposes the Primary header class and returns it as a vector of bytes.
ResultBool setData(const uint64_t &data)
Sets the header data from a 64-bit integer representation.
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:81
@ INVALID_HEADER_DATA
Header data is invalid.
Definition CCSDSResult.h:24
Represents the primary header of a CCSDS packet.
Definition CCSDSHeader.h:41
uint8_t type
1 bit second of packet identification
Definition CCSDSHeader.h:44
uint8_t dataFieldHeaderFlag
1 bit third of packet identification
Definition CCSDSHeader.h:45
uint16_t sequenceCount
14 bit last of sequence control
Definition CCSDSHeader.h:50
uint8_t sequenceFlags
2 bit first of sequence control
Definition CCSDSHeader.h:49
uint8_t versionNumber
3 bit first of packet identification
Definition CCSDSHeader.h:43
uint16_t dataLength
16 bits
Definition CCSDSHeader.h:53
uint16_t APID
11 bit last of packet identification
Definition CCSDSHeader.h:46