adding some regex program options to handle eui64 addresses
This commit is contained in:
parent
ad4689edbb
commit
e149188ae4
@ -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 ./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
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
|
3
POHelperClasses.cpp
Normal file
3
POHelperClasses.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include "POHelperClasses.hpp"
|
||||
|
||||
|
30
POHelperClasses.hpp
Normal file
30
POHelperClasses.hpp
Normal 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
1
default.conf
Normal file
@ -0,0 +1 @@
|
||||
APPEUI = 70B3D57ED005538D
|
88
main.cpp
88
main.cpp
@ -3,9 +3,11 @@
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#include "POHelperClasses.hpp"
|
||||
#include "MQTTDataStreamer.hpp"
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
namespace po = boost::program_options;
|
||||
|
||||
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
|
||||
//////////////////////////////////////////////////
|
||||
@ -167,9 +232,12 @@ int main(int argc, char *argv[])
|
||||
// set options allowed by the command line
|
||||
po::options_description command_line_options("Allowed options");
|
||||
command_line_options.add_options() ("help", "produce help message")
|
||||
("version,v", "print the version number")
|
||||
("hostname,h", po::value<string>()->default_value("localhost"), "Hostname")
|
||||
("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
|
||||
po::options_description config_file_options;
|
||||
@ -200,6 +268,24 @@ int main(int argc, char *argv[])
|
||||
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"))
|
||||
{
|
||||
std::cout << "APPEUI: " << variable_map["APPEUI"].as<string>() << std::endl;
|
||||
|
Loading…
Reference in New Issue
Block a user