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