CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
exec_decoder.cpp
Go to the documentation of this file.
1// Copyright 2025-2026 ExoSpaceLabs
2// SPDX-License-Identifier: Apache-2.0
3
4
9#include <unordered_map>
10#include <string>
11#include <vector>
12#include <iostream>
13#include <chrono>
14#include <sstream>
15#include "CCSDSPack.h"
16#include "exec_utils.h"
17
18
20 // ascii art generated on https://www.asciiart.eu/text-to-ascii-art
21 // with ANSI SHADOW Font, with 80 and Block frame
22
23 std::cout << std::endl <<
24 "▐▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▌\n"
25 "▐ ██████╗ ██████╗███████╗██████╗ ███████╗ ▌\n"
26 "▐ ██╔════╝██╔════╝██╔════╝██╔══██╗██╔════╝ ▌\n"
27 "▐ ██║ ██║ ███████╗██║ ██║███████╗ ▌\n"
28 "▐ ██║ ██║ ╚════██║██║ ██║╚════██║ █▀█░█▀█░█▀▀░█░█░ ▌\n"
29 "▐ ╚██████╗╚██████╗███████║██████╔╝███████║ █▀▀░█▀█░█░░░█▀▄░ ▌\n"
30 "▐ ╚═════╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝ ▀░░░▀░▀░▀▀▀░▀░▀░ ▌\n"
31 "▐ ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗██████╗ ▌\n"
32 "▐ ██╔══██╗██╔════╝ ██╔════╝██╔═══██╗██╔══██╗██╔════╝██╔══██╗ ▌\n"
33 "▐ ██║ ██║█████╗ ██║ ██║ ██║██║ ██║█████╗ ██████╔╝ ▌\n"
34 "▐ ██║ ██║██╔══╝ ██║ ██║ ██║██║ ██║██╔══╝ ██╔══██╗ ▌\n"
35 "▐ ██████╔╝███████╗ ╚██████╗╚██████╔╝██████╔╝███████╗██║ ██║ ▌\n"
36 "▐ ╚═════╝ ╚══════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ▌\n"
37 "▐▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▌\n"
38 << std::endl;
39 std::cout << "Usage: ccsds_decoder [OPTIONS] - decode a ccsds binary file and generate a file from application data." << std::endl;
40 std::cout << "Mandatory parameters:" << std::endl;
41 std::cout << " -i or --input <filename> : input file to be encoded" << std::endl;;
42 std::cout << " -o or --output <filename> : Generated output file" << std::endl;;
43 std::cout << " -c or --config <filename> : Configuration file" << std::endl;
44 std::cout << std::endl;
45 std::cout << "Optionals:" << std::endl;
46 std::cout << " -h or --help : Show this help and message" << std::endl;
47 std::cout << " -v or --verbose : Show generated packets information" << std::endl;
48 std::cout << std::endl;
49 std::cout << "Note : the template CCSDS packet is defined in the configuration file" << std::endl;
50 std::cout << " This should follow the guide lines provided in the link below." << std::endl;
51 std::cout << std::endl;
52 std::cout << "For further information please visit: https://github.com/ExoSpaceLabs/CCSDSPack" << std::endl;
53}
54
55int main(const int argc, char* argv[]) {
56 std::string appName = "ccsds_decoder";
57
58 std::unordered_map<std::string, std::string> allowed;
59 allowed.insert({"h", "help"});
60 allowed.insert({"v", "verbose"});
61 allowed.insert({"i", "input"});
62 allowed.insert({"o", "output"});
63 allowed.insert({"c", "config"});
64
65 const std::set<std::string> booleanArgs{"verbose", "help"};
66
67 std::unordered_map<std::string, std::string> args;
68 args.insert({"verbose", "false"});
69 args.insert({"help", "false"});
70
71 const auto start = std::chrono::high_resolution_clock::now();
72 if (const auto res = parseArguments(argc, argv, allowed, args, booleanArgs); !res.has_value()) {
73 std::cerr << "[ Error " << res.error().code() << " ]: "<< res.error().message() << std::endl ;
74 return res.error().code();
75 }
76 // std::cout << "Parsed args:\n";
77 //for (const auto& [k, v] : args) {
78 // std::cout << " " << k << ": " << v << '\n';
79 //}
80 if (args["help"] == "true") {
82 return 0;
83 }
84 bool verbose{args["verbose"] == "true"};
85
86 if (args.find("input") == args.end()) {
87 std::cerr << "[ Error " << ARG_PARSE_ERROR << " ]: " << "Input file must be specified" << std::endl;
89 return ARG_PARSE_ERROR;
90 }
91
92 if (!fileExists(args["input"])) {
93 std::cerr << "[ Error " << ARG_PARSE_ERROR << " ]: " << "Input \"" << args["input"] << "\" does not exist" << std::endl;
94 return ARG_PARSE_ERROR;
95 }
96 const std::string input{args["input"]};
97 const std::string output{args["output"]};
98
99 if (output.empty()) {
100 std::cerr << "[ Error " << ARG_PARSE_ERROR << " ]: " << "Output file must be specified" << std::endl;
102 return ARG_PARSE_ERROR;
103 }
104
105 if (args.find("config") == args.end()) {
106 std::cerr << "[ Error " << ARG_PARSE_ERROR << " ]: " << "Config file must be specified" << std::endl;
108 return ARG_PARSE_ERROR;
109 }
110
111 if (!fileExists(args["config"])) {
112 std::cerr << "[ Error " << ARG_PARSE_ERROR << " ]: " << "Config \"" << args["config"] << "\" does not exist" << std::endl;
113 return ARG_PARSE_ERROR;
114 }
115 const std::string configFile{args["config"]};
116
117 // prepare template packet
118 customConsole(appName,"reading CCSDS configuration file: " + configFile);
119 Config cfg;
120 {
121 if (auto res = cfg.load(configFile); !res.has_value()) {
122 std::cerr << "[ Error " << res.error().code() << " ]: "<< res.error().message() << std::endl ;
123 return res.error().code();
124 }
125 }
126
127 CCSDS::Manager manager;
128
129 if (cfg.isKey("data_field_size")) {
130 std::uint16_t dataFieldSize;
131 ASSIGN_OR_PRINT(dataFieldSize, cfg.get<int>("data_field_size"));
132 manager.setDataFieldSize(dataFieldSize);
133
134 }
135
136 if (cfg.isKey("sync_pattern_enable")) {
137 bool syncPatternEnable;
138 ASSIGN_OR_PRINT(syncPatternEnable, cfg.get<bool>("sync_pattern_enable"));
139 manager.setSyncPatternEnable(syncPatternEnable);
140 if (syncPatternEnable && cfg.isKey("sync_pattern")) {
141 std::uint32_t syncPattern;
142 ASSIGN_OR_PRINT(syncPattern, cfg.get<int>("sync_pattern"));
143 manager.setSyncPattern(syncPattern);
144 }
145 }
146
147 bool validationEnable;
148 if (!cfg.isKey("validation_enable")) {
149 std::cerr << "[ Error " << CONFIG_MISSING_PARAMETER << " ]: " << "Config: Missing bool field: validation_enable" << std::endl;
151 }
152 ASSIGN_OR_PRINT(validationEnable, cfg.get<bool>("validation_enable"));
153 customConsole(appName,"creating CCSDS template packet");
154
155 if (const auto res = manager.loadTemplateConfig(cfg);!res.has_value()) {
156 std::cerr << "[ Error " << res.error().code() << " ]: "<< res.error().message() << std::endl ;
157 return res.error().code();
158 }
159
160 std::vector<std::uint8_t> inputBytes;
161 customConsole(appName,"reading data from " + input);
162
163 if (const auto res = readBinaryFile(input); !res.has_value()) {
164 std::cerr << "[ Error " << res.error().code() << " ]: "<< res.error().message() << std::endl ;
165 return res.error().code();
166 }else {
167 inputBytes = res.value();
168 if (manager.getTemplate().getPrimaryHeader().getSequenceFlags() == CCSDS::UNSEGMENTED && inputBytes.size() > manager.getDataFieldSize()){
169 std::cerr << "[ Error " << INVALID_INPUT_DATA << " ]: "<< "Input data is too big for unsegmented packets, data "
170 << inputBytes.size() << " must be less than defined data packet length of " << manager.getDataFieldSize() << std::endl ;
171 return INVALID_INPUT_DATA;
172 }
173 }
174 customConsole(appName, "deserializing CCSDS packets from file");
175 if (const auto res = manager.load(inputBytes); !res.has_value()) {
176 std::cerr << "[ Error " << res.error().code() << " ]: "<< res.error().message() << std::endl ;
177 return res.error().code();
178 }
179 if (verbose) customConsole(appName,"printing loaded packets data to screen:");
180 if (verbose) printPackets(manager);
181
182 customConsole(appName,"retrieving Application data from CCSDS packets");
183 manager.setAutoValidateEnable(validationEnable);
184 std::vector<std::uint8_t> outputData;
185 if (const auto res = manager.getApplicationDataBuffer(); !res.has_value()) {
186 std::cerr << "[ Error " << res.error().code() << " ]: "<< res.error().message() << std::endl ;
187 return res.error().code();
188 }else {
189 outputData = res.value();
190 }
191
192 customConsole(appName,"writing data to " + output);
193 if (const auto res = writeBinaryFile(outputData, output); !res.has_value()) {
194 std::cerr << "[ Error " << res.error().code() << " ]: "<< res.error().message() << std::endl ;
195 return res.error().code();
196 }
197
198 const auto end = std::chrono::high_resolution_clock::now();
199 const auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
200 customConsole(appName,"execution time: " + std::to_string(duration.count()) + " [us]");
201 customConsole(appName,"[ Exit code 0 ]");
202 return 0;
203}
#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< 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.
std::uint8_t getSequenceFlags() const
2 bits
Definition CCSDSHeader.h:91
Manages CCSDS packets and their templates.
void setSyncPatternEnable(bool enable)
enable sync pattern utilization both in serialization, deserialization, read and write.
std::uint16_t getDataFieldSize() const
retrieves the set data field size (this includes the secondary header if present)
void setSyncPattern(std::uint32_t syncPattern)
set sync pattern that should indicate the start of a CCSDS packet.
ResultBool load(const std::vector< Packet > &packets)
Load a vector of packets.
Packet getTemplate()
Retrieves the packet template.
ResultBuffer getApplicationDataBuffer()
Retrieves the application data from the packets.
ResultBool loadTemplateConfig(const Config &cfg)
Loads a template packet from a configuration object.
void setDataFieldSize(std::uint16_t size)
Sets the size of the data field.
void setAutoValidateEnable(bool enable)
Enables or disables automatic validation of packets.
Header & getPrimaryHeader()
returns the CCSDS packet's Primary Header.
bool has_value() const
Checks if the result contains a valid value.
Parses and stores config values from custom file format.
Definition CCSDSConfig.h:14
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.
Definition CCSDSConfig.h:23
int main(const int argc, char *argv[])
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")
Prints log to console adding various information about.
CCSDS::ResultBool parseArguments(int argc, char *argv[], std::unordered_map< std::string, std::string > &allowedMap, std::unordered_map< std::string, std::string > &outArgs, const std::set< std::string > &booleanArgs)
Parses the input arguments with defined values returns an unordered map with given keys and input val...
@ CONFIG_MISSING_PARAMETER
Definition exec_utils.h:12
@ ARG_PARSE_ERROR
Error Parsing argument.
Definition exec_utils.h:11
@ INVALID_INPUT_DATA
Definition exec_utils.h:13
@ UNSEGMENTED
11 Complete packet in a single frame.
Definition CCSDSHeader.h:26