9#include <unordered_map>
22 std::cout << std::endl <<
23 "▐▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▌\n"
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"
38 std::cout <<
"Usage: ccsds_encoder [OPTIONS] - take a file and packets it into ccsds packets and saves it to a binary file." << std::endl;
39 std::cout <<
"Mandatory parameters:" << std::endl;
40 std::cout <<
" -i or --input <filename> : input file to be encoded" << std::endl;;
41 std::cout <<
" -o or --output <filename> : Generated output file" << std::endl;;
42 std::cout <<
" -c or --config <filename> : Configuration file" << std::endl;
43 std::cout << std::endl;
44 std::cout <<
"Optionals:" << std::endl;
45 std::cout <<
" -h or --help : Show this help and message" << std::endl;
46 std::cout <<
" -v or --verbose : Show generated packets information" << std::endl;
47 std::cout << std::endl;
48 std::cout <<
"Note : the template CCSDS packet is defined in the configuration file" << std::endl;
49 std::cout <<
" This should follow the guide lines provided in the link below." << std::endl;
50 std::cout << std::endl;
51 std::cout <<
"For further information please visit: https://github.com/ExoSpaceLabs/CCSDSPack" << std::endl;
54int main(
const int argc,
char* argv[]) {
55 std::string appName =
"ccsds_encoder";
57 std::unordered_map<std::string, std::string> allowed;
58 allowed.insert({
"h",
"help"});
59 allowed.insert({
"v",
"verbose"});
60 allowed.insert({
"i",
"input"});
61 allowed.insert({
"o",
"output"});
62 allowed.insert({
"c",
"config"});
64 const std::set<std::string> booleanArgs{
"verbose",
"help"};
66 std::unordered_map<std::string, std::string> args;
67 args.insert({
"verbose",
"false"});
68 args.insert({
"help",
"false"});
70 const auto start = std::chrono::high_resolution_clock::now();
72 std::cerr <<
"[ Error " << res.error().code() <<
" ]: "<< res.error().message() << std::endl ;
73 return res.error().code();
79 if (args[
"help"] ==
"true") {
83 bool verbose{args[
"verbose"] ==
"true"};
85 if (args.find(
"input") == args.end()) {
86 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Input file must be specified" << std::endl;
92 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Input \"" << args[
"input"] <<
"\" does not exist" << std::endl;
95 const std::string input{args[
"input"]};
96 const std::string output{args[
"output"]};
99 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Output file must be specified" << std::endl;
104 if (args.find(
"config") == args.end()) {
105 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Config file must be specified" << std::endl;
111 std::cerr <<
"[ Error " <<
ARG_PARSE_ERROR <<
" ]: " <<
"Config \"" << args[
"config"] <<
"\" does not exist" << std::endl;
114 const std::string configFile{args[
"config"]};
117 customConsole(appName,
"reading CCSDS configuration file: " + configFile);
120 if (
auto res = cfg.
load(configFile); !res.has_value()) {
121 std::cerr <<
"[ Error " << res.error().code() <<
" ]: "<< res.error().message() << std::endl ;
122 return res.error().code();
128 if (cfg.
isKey(
"data_field_size")) {
129 std::uint16_t dataFieldSize;
134 if (cfg.
isKey(
"sync_pattern_enable")) {
135 bool syncPatternEnable;
138 if (syncPatternEnable && cfg.
isKey(
"sync_pattern")) {
139 std::uint32_t syncPattern;
148 std::cerr <<
"[ Error " << res.error().code() <<
" ]: "<< res.error().message() << std::endl ;
149 return res.error().code();
152 std::vector<std::uint8_t> inputBytes;
156 std::cerr <<
"[ Error " << res.error().code() <<
" ]: "<< res.error().message() << std::endl ;
157 return res.error().code();
159 inputBytes = res.value();
161 std::cerr <<
"[ Error " <<
INVALID_INPUT_DATA <<
" ]: "<<
"Input data is too big for unsegmented packets, data "
162 << inputBytes.size() <<
" must be less than defined data packet length of " << manager.
getDataFieldSize() << std::endl ;
167 customConsole(appName,
"generating CCSDS packets using input data");
169 std::cerr <<
"[ Error " << res.error().code() <<
" ]: "<< res.error().message() << std::endl ;
170 return res.error().code();
172 if (verbose)
customConsole(appName,
"printing data to screen:");
179 std::cerr <<
"[ Error " << res.error().code() <<
" ]: "<< res.error().message() << std::endl ;
180 return res.error().code();
183 const auto end = std::chrono::high_resolution_clock::now();
184 const auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
185 customConsole(appName,
"execution time: " + std::to_string(duration.count()) +
" [us]");
#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.
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.
Packet getTemplate()
Retrieves the packet template.
ResultBool loadTemplateConfig(const Config &cfg)
Loads a template packet from a configuration object.
std::vector< std::uint8_t > getPacketsBuffer() const
Retrieves a buffer containing all the stored packets sequentially.
void setDataFieldSize(std::uint16_t size)
Sets the size of the data field.
ResultBool setApplicationData(const std::vector< std::uint8_t > &data)
Sets the application data for the packet.
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.
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 printHelp()
This is the source file that holds the execution logic of ccsds_encoder binary file.
int main(const int argc, char *argv[])
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...
@ ARG_PARSE_ERROR
Error Parsing argument.
@ UNSEGMENTED
11 Complete packet in a single frame.