CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
PusServices.cpp
Go to the documentation of this file.
1// Copyright 2025-2026 ExoSpaceLabs
2// SPDX-License-Identifier: Apache-2.0
3
4
5#include "PusServices.h"
6#include "CCSDSDataField.h"
7#include "CCSDSUtils.h"
8
9CCSDS::ResultBool PusA::deserialize(const std::vector<std::uint8_t> &data) {
11 "PUS-A header not correct size (size != 6 bytes)");
12
13 m_version = data[0] & 0x7;
14 m_serviceType = data[1];
15 m_serviceSubType = data[2];
16 m_sourceID = data[3];
17 m_dataLength = data[4] << 8 | data[5];
18 return true;
19}
20
21std::vector<std::uint8_t> PusA::serialize() const {
22 std::vector data{
23 static_cast<std::uint8_t>(m_version & 0x7),
27 static_cast<std::uint8_t>(m_dataLength >> 8 & 0xFF),
28 static_cast<std::uint8_t>(m_dataLength & 0xFF),
29 };
30
31 return data;
32}
33
37
38CCSDS::ResultBool PusB::deserialize(const std::vector<std::uint8_t> &data) {
40 "PUS-B header not correct size (size != 8 bytes)");
41 m_version = data[0] & 0x7;
42 m_serviceType = data[1];
43 m_serviceSubType = data[2];
44 m_sourceID = data[3];
45 m_eventID = data[4] << 8 | data[5];
46 m_dataLength = data[6] << 8 | data[7];
47 return true;
48}
49
50std::vector<std::uint8_t> PusB::serialize() const {
51 std::vector data{
52 static_cast<std::uint8_t>(m_version & 0x7),
56 static_cast<std::uint8_t>(m_eventID >> 8 & 0xFF),
57 static_cast<std::uint8_t>(m_eventID & 0xFF),
58 static_cast<std::uint8_t>(m_dataLength >> 8 & 0xFF),
59 static_cast<std::uint8_t>(m_dataLength & 0xFF),
60 };
61
62 return data;
63}
64
68
69
70CCSDS::ResultBool PusC::deserialize(const std::vector<std::uint8_t> &data) {
72 "PUS-C header not correct size (size <= 6 bytes)");
73 m_version = data[0] & 0x7;
74 m_serviceType = data[1];
75 m_serviceSubType = data[2];
76 m_sourceID = data[3];
77 m_dataLength = data[data.size()-2] << 8 | data[data.size()-1];
78 if (data.size() > 6) {
79 m_timeCode.assign(data.begin() + 4, data.end()-2);
80 }
81 return true;
82}
83
84std::vector<std::uint8_t> PusC::serialize() const {
85 std::vector<std::uint8_t> data;
86 data.reserve(4 + m_timeCode.size() + 2);
87 data.push_back(static_cast<std::uint8_t>(m_version & 0x7));
88 data.push_back(m_serviceType);
89 data.push_back(m_serviceSubType);
90 data.push_back(m_sourceID);
91
92 if (!m_timeCode.empty()) {
93 const auto* p = m_timeCode.data();
94 const auto n = m_timeCode.size();
95 data.insert(data.end(), p, p + n);
96 }
97 data.push_back(m_dataLength >> 8 & 0xFF);
98 data.push_back(m_dataLength & 0xFF);
99
100 return data;
101}
102
105}
106
107#ifndef CCSDS_MCU
108
110 std::uint8_t version = 0;
111 std::uint8_t serviceType = 0;
112 std::uint8_t serviceSubType = 0;
113 std::uint8_t sourceId = 0;
114
115 RET_IF_ERR_MSG(!cfg.isKey("pus_version"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_version");
116 RET_IF_ERR_MSG(!cfg.isKey("pus_service_type"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_service_type");
117 RET_IF_ERR_MSG(!cfg.isKey("pus_service_sub_type"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_service_sub_type");
118 RET_IF_ERR_MSG(!cfg.isKey("pus_source_id"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_source_id");
119
120 ASSIGN_OR_PRINT(version, cfg.get<int>("pus_version"));
121 ASSIGN_OR_PRINT(serviceType, cfg.get<int>("pus_service_type"));
122 ASSIGN_OR_PRINT(serviceSubType,cfg.get< int>("pus_service_sub_type"));
123 ASSIGN_OR_PRINT(sourceId,cfg.get<int>("pus_source_id"));
124
125 m_version = version & 0x7;
126 m_serviceType = serviceType & 0xFF;
127 m_serviceSubType = serviceSubType & 0xFF;
128 m_sourceID = sourceId & 0xFF;
129
130 return true;
131}
132
134 std::uint8_t version = 0;
135 std::uint8_t serviceType = 0;
136 std::uint8_t serviceSubType = 0;
137 std::uint8_t sourceId = 0;
138 std::uint16_t eventId = 0;
139
140 RET_IF_ERR_MSG(!cfg.isKey("pus_version"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_version");
141 RET_IF_ERR_MSG(!cfg.isKey("pus_service_type"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_service_type");
142 RET_IF_ERR_MSG(!cfg.isKey("pus_service_sub_type"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_service_sub_type");
143 RET_IF_ERR_MSG(!cfg.isKey("pus_source_id"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_source_id");
144 RET_IF_ERR_MSG(!cfg.isKey("pus_event_id"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_event_id");
145
146 ASSIGN_OR_PRINT(version, cfg.get<int>("pus_version"));
147 ASSIGN_OR_PRINT(serviceType, cfg.get<int>("pus_service_type"));
148 ASSIGN_OR_PRINT(serviceSubType,cfg.get< int>("pus_service_sub_type"));
149 ASSIGN_OR_PRINT(sourceId,cfg.get<int>("pus_source_id"));
150 ASSIGN_OR_PRINT(eventId,cfg.get<int>("pus_event_id"));
151
152 m_version = version & 0x7;
153 m_serviceType = serviceType & 0xFF;
154 m_serviceSubType = serviceSubType & 0xFF;
155 m_sourceID = sourceId & 0xFF;
156 m_eventID = eventId & 0xFFFF;
157
158 return true;
159}
160
162 std::uint8_t version = 0;
163 std::uint8_t serviceType = 0;
164 std::uint8_t serviceSubType = 0;
165 std::uint8_t sourceId = 0;
166 std::vector<std::uint8_t> timeCode{};
167
168 RET_IF_ERR_MSG(!cfg.isKey("pus_version"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_version");
169 RET_IF_ERR_MSG(!cfg.isKey("pus_service_type"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_service_type");
170 RET_IF_ERR_MSG(!cfg.isKey("pus_service_sub_type"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_service_sub_type");
171 RET_IF_ERR_MSG(!cfg.isKey("pus_source_id"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_source_id");
172 RET_IF_ERR_MSG(!cfg.isKey("pus_time_code"), CCSDS::ErrorCode::CONFIG_FILE_ERROR,"Config: Missing string field: pus_time_code");
173
174 ASSIGN_OR_PRINT(version, cfg.get<int>("pus_version"));
175 ASSIGN_OR_PRINT(serviceType, cfg.get<int>("pus_service_type"));
176 ASSIGN_OR_PRINT(serviceSubType,cfg.get< int>("pus_service_sub_type"));
177 ASSIGN_OR_PRINT(sourceId,cfg.get<int>("pus_source_id"));
178 ASSIGN_OR_PRINT(timeCode,cfg.get<std::vector<std::uint8_t>>("pus_time_code"));
179
180 m_version = version & 0x7;
181 m_serviceType = serviceType & 0xFF;
182 m_serviceSubType = serviceSubType & 0xFF;
183 m_sourceID = sourceId & 0xFF;
184 m_timeCode = timeCode;
185
186 return true;
187}
188
189#endif
#define RET_IF_ERR_MSG(condition, errorCode, message)
Macro to return an error with an error message if a condition is met.
#define ASSIGN_OR_PRINT(var, result)
Macro to assign a result value or print an error message.
Represents the data field of a CCSDS packet.
std::uint16_t getApplicationDataBytesSize() const
Retrieves the size of the application data stored in the data field.
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:85
Parses and stores config values from custom file format.
Definition CCSDSConfig.h:14
bool isKey(const std::string &key) const
CCSDS::Result< T > get(const std::string &key) const
Get value by key and type.
Definition CCSDSConfig.h:23
std::uint8_t m_serviceType
Definition PusServices.h:65
std::uint8_t m_serviceSubType
Definition PusServices.h:66
std::uint8_t m_sourceID
Definition PusServices.h:67
std::vector< std::uint8_t > serialize() const override
Retrieves the serialized representation of the header.
void update(CCSDS::DataField *dataField) override
Defines how the packet secondary header is updated using the data field as reference.
const std::uint16_t m_size
Definition PusServices.h:71
CCSDS::ResultBool loadFromConfig(const ::Config &cfg) override
std::uint8_t m_version
Definition PusServices.h:64
CCSDS::ResultBool deserialize(const std::vector< std::uint8_t > &data) override
takes a buffer if data (vector std::uint8) and creates the header
std::uint16_t m_dataLength
Definition PusServices.h:68
std::uint8_t m_serviceSubType
CCSDS::ResultBool deserialize(const std::vector< std::uint8_t > &data) override
takes a buffer if data (vector std::uint8) and creates the header
std::vector< std::uint8_t > serialize() const override
Retrieves the serialized representation of the header.
std::uint8_t m_serviceType
std::uint8_t m_sourceID
std::uint16_t m_eventID
std::uint16_t m_dataLength
std::uint8_t m_version
const std::uint16_t m_size
void update(CCSDS::DataField *dataField) override
Defines how the packet secondary header is updated using the data field as reference.
CCSDS::ResultBool loadFromConfig(const ::Config &cfg) override
const std::uint16_t m_size
CCSDS::ResultBool deserialize(const std::vector< std::uint8_t > &data) override
takes a buffer if data (vector std::uint8) and creates the header
std::vector< std::uint8_t > m_timeCode
std::uint8_t m_serviceType
std::uint8_t m_sourceID
std::vector< std::uint8_t > serialize() const override
Retrieves the serialized representation of the header.
CCSDS::ResultBool loadFromConfig(const ::Config &cfg) override
std::uint16_t m_dataLength
std::uint8_t m_serviceSubType
void update(CCSDS::DataField *dataField) override
Defines how the packet secondary header is updated using the data field as reference.
std::uint8_t m_version
@ CONFIG_FILE_ERROR
Configuration file error.
Definition CCSDSResult.h:37
@ INVALID_SECONDARY_HEADER_DATA
Secondary header data is invalid.
Definition CCSDSResult.h:29