create a CmdlineHelper class and move the overloaded validation functions

This commit is contained in:
Dominik Kuhn 2022-10-07 15:57:14 +01:00
parent a7b057abd1
commit 87bb8539b3
6 changed files with 181 additions and 130 deletions

View File

@ -22,7 +22,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options(-Wall -Wextra -pedantic -lstdc++)
add_definitions(-DCFG_DEBUG -DCFG_eu868 -DCFG_sx1276_radio -DDEBUG_LMIC -DDEBUG_HAL -DDEBUG_RADIO)
add_executable(mqtt2LoRaWAN main.cpp MQTTDataStreamer.cpp POHelperClasses.cpp ./lmic/lmic.c ./lmic/aes.c ./lmic/radio.c ./lmic/oslmic.c ./lora_gps_hat/debug.c ./lora_gps_hat/gpio.c ./lora_gps_hat/hal.c )
add_executable(mqtt2LoRaWAN main.cpp MQTTDataStreamer.cpp POHelperClasses.cpp POCmdlineHelperClasses.cpp ./lmic/lmic.c ./lmic/aes.c ./lmic/radio.c ./lmic/oslmic.c ./lora_gps_hat/debug.c ./lora_gps_hat/gpio.c ./lora_gps_hat/hal.c )
target_include_directories (mqtt2LoRaWAN PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"

View File

@ -0,0 +1,23 @@
#include <string>
#include "POHelperClasses.hpp"
#include "POCmdlineHelperClasses.hpp"
namespace po = boost::program_options;
void po_cmdline_helper::init(boost::program_options::options_description cmdline_desc){
cmdline_desc.add_options() ("help", "produce help message")
("version,v", "print the version number")
("hostname,h", po::value<std::string>()->default_value("localhost"), "Hostname")
("port,p", po::value<int>()->default_value(1883), "Port")
("config,c", po::value<std::string>(), "configuration file")
("magic,m", po::value<magic_number>(), "magic value (in NNN-NNN format)")
("appeui", po::value<appeui>(), "APPEUI")
("deveui", po::value<deveui>(), "DEVEUI")
("devkey", po::value<devkey>(), "DEVKEY");
}

View File

@ -0,0 +1,17 @@
#ifndef ProgramOptions_CmdLine_HelperClasses_HPP
#define ProgramOptions_CmdLine_HelperClasses_HPP
#include <boost/program_options.hpp>
class po_cmdline_helper {
public:
void init(boost::program_options::options_description cmdline_desc);
};
#endif

View File

@ -1,9 +1,13 @@
#include <iostream>
#include <cstring>
#include "POHelperClasses.hpp"
#include <boost/regex.hpp>
#include <boost/algorithm/hex.hpp>
appeui::appeui(std::string appeui_hexstring){
std::copy(appeui_hexstring.begin(), appeui_hexstring.end(), application_eui64.e8);
@ -17,8 +21,135 @@ deveui::deveui(std::string deveui_hexstring){
}
devkey::devkey(std::string devkey_hexstring){
std::copy(devkey_hexstring.begin(), devkey_hexstring.end(), device_key);
}
/* Overload the 'validate' function for the user-defined class.
It makes sure that value is of form XXX-XXX
where X are digits and converts the second group to an integer.
This has no practical meaning, meant only to show how
regex can be used to validate values.
*/
void validate(boost::any& v,
const std::vector<std::string>& values,
magic_number*, int)
{
static boost::regex r("\\d\\d\\d-(\\d\\d\\d)");
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = validators::get_single_string(values);
// Do regex match and convert the interesting part to
// int.
boost::smatch match;
if (regex_match(s, match, r)) {
v = boost::any(magic_number(boost::lexical_cast<int>(match[1])));
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
void validate(boost::any& v,
const std::vector<std::string>& values,
appeui*, int)
{
static boost::regex r("^([[:xdigit:]]{2}[:.-]?){7}[[:xdigit:]]{2}$");
boost::regex re("[:.-]");
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = validators::get_single_string(values);
// Do regex match and convert the interesting part to
// int.
boost::smatch match;
if (regex_match(s, match, r)) {
std::string unfilterd_string = match[0];
std::string filterd_string = boost::regex_replace( unfilterd_string, re, "" );
std::cout << "regex funktioniert .... " << filterd_string << std::endl;
std::cout << "boost algorithm ... " << boost::algorithm::unhex(filterd_string) << std::endl;
v = boost::any( appeui( boost::algorithm::unhex(filterd_string) ) );
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
void validate(boost::any& v,
const std::vector<std::string>& values,
deveui*, int)
{
static boost::regex r("^([[:xdigit:]]{2}[:.-]?){7}[[:xdigit:]]{2}$");
boost::regex re("[:.-]");
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = validators::get_single_string(values);
// Do regex match and convert the interesting part to
// int.
boost::smatch match;
if (regex_match(s, match, r)) {
std::string unfilterd_string = match[0];
std::string filterd_string = boost::regex_replace( unfilterd_string, re, "" );
std::cout << "regex funktioniert .... " << filterd_string << std::endl;
std::cout << "boost algorithm ... " << boost::algorithm::unhex(filterd_string) << std::endl;
v = boost::any( deveui( boost::algorithm::unhex(filterd_string) ) );
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
void validate(boost::any& v,
const std::vector<std::string>& values,
devkey*, int)
{
static boost::regex r("^([[:xdigit:]]{2}[:.-]?){15}[[:xdigit:]]{2}$");
boost::regex re("[:.-]");
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = validators::get_single_string(values);
// Do regex match and convert the interesting part to
// int.
boost::smatch match;
if (regex_match(s, match, r)) {
std::string unfilterd_string = match[0];
std::string filterd_string = boost::regex_replace( unfilterd_string, re, "" );
std::cout << "regex funktioniert .... " << filterd_string << std::endl;
std::cout << "boost algorithm ... " << boost::algorithm::unhex(filterd_string) << std::endl;
v = boost::any( devkey( boost::algorithm::unhex(filterd_string) ) );
} else {
throw validation_error(validation_error::invalid_option_value);
}
}

View File

@ -4,6 +4,7 @@
#include <string>
#include <cstdint>
#include <boost/program_options.hpp>
typedef union {
uint8_t e8[8]; /* lower 64-bit address */
@ -31,7 +32,6 @@ public:
};
class deveui {
public:
@ -40,6 +40,7 @@ public:
};
class devkey {
public:
@ -48,5 +49,10 @@ public:
};
void validate(boost::any& v, const std::vector<std::string>& values, magic_number*, int);
void validate(boost::any& v, const std::vector<std::string>& values, appeui*, int);
void validate(boost::any& v, const std::vector<std::string>& values, deveui*, int);
void validate(boost::any& v, const std::vector<std::string>& values, devkey*, int);
#endif

126
main.cpp
View File

@ -148,132 +148,6 @@ public:
};
/* Overload the 'validate' function for the user-defined class.
It makes sure that value is of form XXX-XXX
where X are digits and converts the second group to an integer.
This has no practical meaning, meant only to show how
regex can be used to validate values.
*/
void validate(boost::any& v,
const std::vector<std::string>& values,
magic_number*, int)
{
static boost::regex r("\\d\\d\\d-(\\d\\d\\d)");
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = validators::get_single_string(values);
// Do regex match and convert the interesting part to
// int.
boost::smatch match;
if (regex_match(s, match, r)) {
v = boost::any(magic_number(boost::lexical_cast<int>(match[1])));
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
void validate(boost::any& v,
const std::vector<std::string>& values,
appeui*, int)
{
static boost::regex r("^([[:xdigit:]]{2}[:.-]?){7}[[:xdigit:]]{2}$");
boost::regex re("[:.-]");
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = validators::get_single_string(values);
// Do regex match and convert the interesting part to
// int.
boost::smatch match;
if (regex_match(s, match, r)) {
std::string unfilterd_string = match[0];
std::string filterd_string = boost::regex_replace( unfilterd_string, re, "" );
std::cout << "regex funktioniert .... " << filterd_string << std::endl;
std::cout << "boost algorithm ... " << boost::algorithm::unhex(filterd_string) << std::endl;
v = boost::any( appeui( boost::algorithm::unhex(filterd_string) ) );
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
void validate(boost::any& v,
const std::vector<std::string>& values,
deveui*, int)
{
static boost::regex r("^([[:xdigit:]]{2}[:.-]?){7}[[:xdigit:]]{2}$");
boost::regex re("[:.-]");
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = validators::get_single_string(values);
// Do regex match and convert the interesting part to
// int.
boost::smatch match;
if (regex_match(s, match, r)) {
std::string unfilterd_string = match[0];
std::string filterd_string = boost::regex_replace( unfilterd_string, re, "" );
std::cout << "regex funktioniert .... " << filterd_string << std::endl;
std::cout << "boost algorithm ... " << boost::algorithm::unhex(filterd_string) << std::endl;
v = boost::any( deveui( boost::algorithm::unhex(filterd_string) ) );
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
void validate(boost::any& v,
const std::vector<std::string>& values,
devkey*, int)
{
static boost::regex r("^([[:xdigit:]]{2}[:.-]?){15}[[:xdigit:]]{2}$");
boost::regex re("[:.-]");
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = validators::get_single_string(values);
// Do regex match and convert the interesting part to
// int.
boost::smatch match;
if (regex_match(s, match, r)) {
std::string unfilterd_string = match[0];
std::string filterd_string = boost::regex_replace( unfilterd_string, re, "" );
std::cout << "regex funktioniert .... " << filterd_string << std::endl;
std::cout << "boost algorithm ... " << boost::algorithm::unhex(filterd_string) << std::endl;
v = boost::any( devkey( boost::algorithm::unhex(filterd_string) ) );
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
std::string mapper(std::string env_var)