diff --git a/POHelperClasses.cpp b/POHelperClasses.cpp index 6f850c7..662a415 100644 --- a/POHelperClasses.cpp +++ b/POHelperClasses.cpp @@ -1,3 +1,24 @@ + +#include + #include "POHelperClasses.hpp" + +appeui::appeui(std::string appeui_hexstring){ + std::copy(appeui_hexstring.begin(), appeui_hexstring.end(), application_eui64.e8); + // std::memcpy(&application_eui64, appeui_hexstring, 8); +} + + +deveui::deveui(std::string deveui_hexstring){ + std::copy(deveui_hexstring.begin(), deveui_hexstring.end(), device_eui64.e8); + // std::memcpy(&application_eui64, appeui_hexstring, 8); +} + + + + +devkey::devkey(std::string devkey_hexstring){ + std::copy(devkey_hexstring.begin(), devkey_hexstring.end(), device_key); +} diff --git a/POHelperClasses.hpp b/POHelperClasses.hpp index 45006ca..2ef2a40 100644 --- a/POHelperClasses.hpp +++ b/POHelperClasses.hpp @@ -1,15 +1,19 @@ #ifndef ProgramOptions_HELPERCLASSES_HPP #define ProgramOptions_HELPERCLASSES_HPP +#include #include typedef union { uint8_t e8[8]; /* lower 64-bit address */ - uint8_t e32[2]; + uint8_t e32[2]; } eui64_t; +typedef uint8_t devkey_t[16]; + + /* Define a completely non-sensical class. */ class magic_number { @@ -18,12 +22,30 @@ public: int n; }; + class appeui { public: - appeui(eui64_t application_eui64) : application_eui64{application_eui64} {} + appeui(std::string appeui_hexstring); eui64_t application_eui64; -}; +}; + + + +class deveui { + +public: + deveui(std::string deveui_hexstring); + eui64_t device_eui64; +}; + + +class devkey { + +public: + devkey(std::string devkey_hexstring); + devkey_t device_key; +}; diff --git a/default.conf b/default.conf index 4d5c407..bfc2337 100644 --- a/default.conf +++ b/default.conf @@ -1 +1,3 @@ -APPEUI = 70B3D57ED005538D \ No newline at end of file +APPEUI = 4523000000000101 +DEVEUI = 70B3D57ED005538D +DEVKEY = 4499B4D3DB0207E3D61A066ECB26FCA2 diff --git a/main.cpp b/main.cpp index c37814c..393485c 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,7 @@ #include #include +#include namespace po = boost::program_options; extern "C" @@ -174,7 +175,7 @@ void validate(boost::any& v, v = boost::any(magic_number(boost::lexical_cast(match[1]))); } else { throw validation_error(validation_error::invalid_option_value); - } + } } void validate(boost::any& v, @@ -198,15 +199,82 @@ void validate(boost::any& v, // 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(match[1]) )); + 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& 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& 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); + } } - ////////////////////////////////////////////////// @@ -237,11 +305,15 @@ int main(int argc, char *argv[]) ("port,p", po::value()->default_value(1883), "Port") ("config,c", po::value()->default_value("default.conf"), "configuration file") ("magic,m", po::value(), "magic value (in NNN-NNN format)") - ("eui", po::value(), "APPEU"); + ("appeui", po::value(), "APPEUI") + ("deveui", po::value(), "DEVEUI") + ("devkey", po::value(), "DEVKEY"); // set options allowed in config file po::options_description config_file_options; - config_file_options.add_options() ("APPEUI", po::value(), "APPEUI"); + config_file_options.add_options() ("APPEUI", po::value(), "APPEUI") + ("DEVEUI", po::value(), "DEVEUI") + ("DEVKEY", po::value(), "DEVKEY"); po::variables_map variable_map; @@ -279,20 +351,26 @@ int main(int argc, char *argv[]) } - if (variable_map.count("eui")) + if (variable_map.count("DEVEUI")) { - // std::cout << "APPEUI: " << variable_map["eui"].as().application_eui64.e32 << std::endl; - return 0; + std::cout << "DEVEUI: " << std::endl; + for( int i = 0; i < 8; i++ ){ + std::printf("%#02x", variable_map["DEVEUI"].as().device_eui64.e8[i] ); + } + std::cout << std::endl; } if (variable_map.count("APPEUI")) { - std::cout << "APPEUI: " << variable_map["APPEUI"].as() << std::endl; + std::cout << "APPEUI: " << std::endl; + for( int i = 0; i < 8; i++ ){ + std::printf("%#02x", variable_map["APPEUI"].as().application_eui64.e8[i] ); + } + std::cout << std::endl; } - std::cout << "Initializing and connecting for server '" << variable_map["hostname"].as() << "'..." << std::endl; std::vector> topics_to_handle;