62 {
63 std::string appName = "ccsds_decoder";
64
65 std::unordered_map<std::string, std::string> allowed;
66 allowed.insert({"h", "help"});
67 allowed.insert({"v", "verbose"});
68 allowed.insert({"i", "input"});
69 allowed.insert({"o", "output"});
70 allowed.insert({"c", "config"});
71
72 allowed.insert({"tv", "version_number"});
73 allowed.insert({"tt", "type"});
74 allowed.insert({"ta", "apid"});
75 allowed.insert({"th", "secondary_header"});
76 allowed.insert({"ts", "segmented"});
77
78 std::unordered_map<std::string, std::string> args;
79 args.insert({"verbose", "false"});
80 args.insert({"help", "false"});
81
82 const auto start = std::chrono::high_resolution_clock::now();
84 std::cerr << "[ Error " << exp.error().code() << " ]: "<< exp.error().message() << std::endl ;
85 return exp.error().code();
86 }
87
88
89
90
91 if (args["help"] == "true") {
93 return 0;
94 }
95 bool verbose{args["verbose"] == "true"};
96
97 if (args.find("input") == args.end()) {
98 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Input file must be specified" << std::endl;
101 }
102
104 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Input \"" << args[
"input"] <<
"\" does not exist" << std::endl;
106 }
107 const std::string input{args["input"]};
108 const std::string output{args["output"]};
109
110 if (output.empty()) {
111 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Output file must be specified" << std::endl;
114 }
115
116 if (args.find("config") == args.end()) {
117 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Config file must be specified" << std::endl;
120 }
121
123 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Config \"" << args[
"config"] <<
"\" does not exist" << std::endl;
125 }
126 const std::string configFile{args["config"]};
127
128
129 customConsole(appName,
"reading CCSDS configuration file: " + configFile);
131 {
132 if (
auto exp = cfg.
load(configFile); !exp.has_value()) {
133 std::cerr << "[ Error " << exp.error().code() << " ]: "<< exp.error().message() << std::endl ;
134 return exp.error().code();
135 }
136 }
137
139 uint8_t versionNumber;
140 uint8_t type;
141 uint8_t APID;
142 uint8_t dataFieldHeaderFlag;
143 uint16_t sequenceCount;
145 uint16_t dataFieldSize;
146 bool segmented;
147 bool syncPatternEnable;
148 bool validationEnable;
149 uint32_t syncPattern;
150 if (args.find("version_number") == args.end()) {
151 if (!cfg.
isKey(
"ccsds_version_number")) {
153 << std::endl;
155 }
157 }else {
158 versionNumber = std::stoi(args["version_number"]);
159 }
160
161 if (args.find("type") == args.end()) {
162 if (!cfg.
isKey(
"ccsds_type")) {
163 std::cerr <<
"[ Error " <<
CONFIG_MISSING_PARAMETER <<
" ]: " <<
"Config: Missing bool field: ccsds_type" << std::endl;
165 }
167 }else {
168 type = args["type"] == "true";
169 }
170
171 if (args.find("secondary_header") == args.end()) {
172 if (!cfg.
isKey(
"ccsds_data_field_header_flag")) {
173 std::cerr <<
"[ Error " <<
CONFIG_MISSING_PARAMETER <<
" ]: " <<
"Config: Missing bool field: ccsds_data_field_header_flag" << std::endl;
175 }
177 }else {
178 dataFieldHeaderFlag = args["secondary_header"] == "true";
179 }
180
181 if (args.find("apid") == args.end()) {
182 if (!cfg.
isKey(
"ccsds_APID")) {
183 std::cerr <<
"[ Error " <<
CONFIG_MISSING_PARAMETER <<
" ]: " <<
"Config: Missing int field: ccsds_APID" << std::endl;
185 }
187 }else {
188 APID = std::stoi(args["apid"]);
189 }
190
191 if (args.find("segmented") == args.end()) {
192 if (!cfg.
isKey(
"ccsds_segmented")) {
193 std::cerr <<
"[ Error " <<
CONFIG_MISSING_PARAMETER <<
" ]: " <<
"Config: Missing bool field: ccsds_segmented" << std::endl;
195 }
197 }else {
198 segmented = args["segmented"] == "true";
199 }
200
201 if (!cfg.
isKey(
"validation_enable")) {
202 std::cerr <<
"[ Error " <<
CONFIG_MISSING_PARAMETER <<
" ]: " <<
"Config: Missing bool field: validation_enable" << std::endl;
204 }
205
206 if (!cfg.
isKey(
"data_field_size")) {
207 std::cerr <<
"[ Error " <<
CONFIG_MISSING_PARAMETER <<
" ]: " <<
"Config: Missing int field: data_field_size" << std::endl;
209 }
210
211 if (!cfg.
isKey(
"sync_pattern_enable")) {
212 std::cerr <<
"[ Error " <<
CONFIG_MISSING_PARAMETER <<
" ]: " <<
"Config: Missing int field: sync_pattern_enable" << std::endl;
214 }
215
219
220 {
221 if (
auto exp = cfg.
get<
int>(
"sync_pattern"); exp.has_value()) {
222 syncPattern = exp.value();
223 }
224 }
225
227 if (segmented) {
228 sequenceCount = 1;
230 }else {
231 sequenceCount = 0;
233 }
234
241
244
246 manager.setDatFieldSize(dataFieldSize);
247 manager.setSyncPatternEnable(syncPatternEnable);
248 if (syncPatternEnable && cfg.
isKey(
"sync_pattern")) {
249 manager.setSyncPattern(syncPattern);
250 }
251 std::vector<uint8_t> inputBytes;
252
255 std::cerr << "[ Error " << exp.error().code() << " ]: "<< exp.error().message() << std::endl ;
256 return exp.error().code();
257 }else {
258 inputBytes = exp.value();
259 if (!segmented && inputBytes.size() > dataFieldSize){
260 std::cerr <<
"[ Error " <<
INVALID_INPUT_DATA <<
" ]: "<<
"Input data is too big for unsegmented packets, data "
261 << inputBytes.size() << " must be less than defined data packet length of " << dataFieldSize << std::endl ;
263 }
264 }
265 customConsole(appName,
"deserializing CCSDS packets from file");
266 if (const auto exp = manager.load(inputBytes); !exp.has_value()) {
267 std::cerr << "[ Error " << exp.error().code() << " ]: "<< exp.error().message() << std::endl ;
268 return exp.error().code();
269 }
270 if (verbose)
customConsole(appName,
"printing loaded packets data to screen:");
272
273 customConsole(appName,
"retrieving Application data from CCSDS packets");
274 manager.setAutoValidateEnable(validationEnable);
275 std::vector<uint8_t> outputData;
276 if (const auto exp = manager.getApplicationDataBuffer(); !exp.has_value()) {
277 std::cerr << "[ Error " << exp.error().code() << " ]: "<< exp.error().message() << std::endl ;
278 return exp.error().code();
279 }else {
280 outputData = exp.value();
281 }
282
285 std::cerr << "[ Error " << exp.error().code() << " ]: "<< exp.error().message() << std::endl ;
286 return exp.error().code();
287 }
288
289 const auto end = std::chrono::high_resolution_clock::now();
290 const auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
291 customConsole(appName,
"execution time: " + std::to_string(duration.count()) +
" [us]");
293 return 0;
294}
#define ASSIGN_OR_PRINT(var, result)
Macro to assign a result value or print an error message.
bool fileExists(const std::string &fileName)
filesystem check fore file existence prepared for both windows and linux.
CCSDS::ResultBuffer readBinaryFile(const std::string &filename)
Read a specified binary file and return its contents as a buffer.
CCSDS::ResultBool writeBinaryFile(const std::vector< 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.
Manages CCSDS packets and their templates.
Represents a CCSDS (Consultative Committee for Space Data Systems) packet.
void setPrimaryHeader(PrimaryHeader data)
Sets the primary header using the provided PrimaryHeader object.
bool has_value() const
Checks if the result contains a valid value.
Parses and stores config values from custom file format.
bool isKey(const std::string &key) const
CCSDS::ResultBool load(const std::string &filename)
Load config file.
CCSDS::Result< T > get(const std::string &key) const
Get value by key and type.
void printHelpDecoder()
This is the source file that holds the execution logic of ccsds_encoder binary file.
void customConsole(const std::string &appName, const std::string &message, const std::string &logLevel="INFO")
@ CONFIG_MISSING_PARAMETER
@ ARG_PARSE_ERROR
Error Parsing argument.
CCSDS::ResultBool parseArguments(int argc, char *argv[], std::unordered_map< std::string, std::string > &allowedMap, std::unordered_map< std::string, std::string > &outArgs)
This is the source file that holds the execution logic of ccsds_encoder binary file.
ESequenceFlag
Represents the sequence flags used in CCSDS telemetry transfer frames.
@ UNSEGMENTED
11 Complete packet in a single frame.
@ FIRST_SEGMENT
01 First segment of a new packet.