adding some regex program options to handle eui64 addresses

This commit is contained in:
Dominik Kuhn 2022-09-13 20:45:36 +01:00
parent ad4689edbb
commit e149188ae4
5 changed files with 122 additions and 2 deletions

View File

@ -22,7 +22,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options(-Wall -Wextra -pedantic -lstdc++) add_compile_options(-Wall -Wextra -pedantic -lstdc++)
add_definitions(-DCFG_DEBUG -DCFG_eu868 -DCFG_sx1276_radio -DDEBUG_LMIC -DDEBUG_HAL -DDEBUG_RADIO) add_definitions(-DCFG_DEBUG -DCFG_eu868 -DCFG_sx1276_radio -DDEBUG_LMIC -DDEBUG_HAL -DDEBUG_RADIO)
add_executable(mqtt2LoRaWAN main.cpp MQTTDataStreamer.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 ./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 target_include_directories (mqtt2LoRaWAN PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}"

3
POHelperClasses.cpp Normal file
View File

@ -0,0 +1,3 @@
#include "POHelperClasses.hpp"

30
POHelperClasses.hpp Normal file
View File

@ -0,0 +1,30 @@
#ifndef ProgramOptions_HELPERCLASSES_HPP
#define ProgramOptions_HELPERCLASSES_HPP
#include <cstdint>
typedef union {
uint8_t e8[8]; /* lower 64-bit address */
uint8_t e32[2];
} eui64_t;
/* Define a completely non-sensical class. */
class magic_number {
public:
magic_number(int n) : n(n) {}
int n;
};
class appeui {
public:
appeui(eui64_t application_eui64) : application_eui64{application_eui64} {}
eui64_t application_eui64;
};
#endif

1
default.conf Normal file
View File

@ -0,0 +1 @@
APPEUI = 70B3D57ED005538D

View File

@ -3,9 +3,11 @@
#include <filesystem> #include <filesystem>
#include <string> #include <string>
#include "POHelperClasses.hpp"
#include "MQTTDataStreamer.hpp" #include "MQTTDataStreamer.hpp"
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/regex.hpp>
namespace po = boost::program_options; namespace po = boost::program_options;
extern "C" extern "C"
@ -144,6 +146,69 @@ 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::cout << "regex funktioniert .... " << boost::regex_replace(unfilterd_string, re, "" ) << std::endl;
// v = boost::any(appeui( boost::lexical_cast<eui.e32>(match[1]) ));
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
////////////////////////////////////////////////// //////////////////////////////////////////////////
// MAIN - INITIALIZATION AND STARTUP // MAIN - INITIALIZATION AND STARTUP
////////////////////////////////////////////////// //////////////////////////////////////////////////
@ -167,9 +232,12 @@ int main(int argc, char *argv[])
// set options allowed by the command line // set options allowed by the command line
po::options_description command_line_options("Allowed options"); po::options_description command_line_options("Allowed options");
command_line_options.add_options() ("help", "produce help message") command_line_options.add_options() ("help", "produce help message")
("version,v", "print the version number")
("hostname,h", po::value<string>()->default_value("localhost"), "Hostname") ("hostname,h", po::value<string>()->default_value("localhost"), "Hostname")
("port,p", po::value<int>()->default_value(1883), "Port") ("port,p", po::value<int>()->default_value(1883), "Port")
("config,c", po::value<std::string>()->default_value("default.conf"), "configuration file"); ("config,c", po::value<std::string>()->default_value("default.conf"), "configuration file")
("magic,m", po::value<magic_number>(), "magic value (in NNN-NNN format)")
("eui", po::value<appeui>(), "APPEU");
// set options allowed in config file // set options allowed in config file
po::options_description config_file_options; po::options_description config_file_options;
@ -200,6 +268,24 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
if (variable_map.count("version")) {
cout << "Version 1.\n";
return 0;
}
if (variable_map.count("magic")) {
cout << "The magic is \"" << variable_map["magic"].as<magic_number>().n << "\"\n";
return 0;
}
if (variable_map.count("eui"))
{
// std::cout << "APPEUI: " << variable_map["eui"].as<appeui>().application_eui64.e32 << std::endl;
return 0;
}
if (variable_map.count("APPEUI")) if (variable_map.count("APPEUI"))
{ {
std::cout << "APPEUI: " << variable_map["APPEUI"].as<string>() << std::endl; std::cout << "APPEUI: " << variable_map["APPEUI"].as<string>() << std::endl;