Compare commits

...

5 Commits

7 changed files with 285 additions and 144 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

View File

@ -1,3 +1,3 @@
APPEUI = 4523000000000101
DEVEUI = 70B3D57ED005538D
APPEUI = 0101000000002345
DEVEUI = 8D5305D07ED5B370
DEVKEY = 4499B4D3DB0207E3D61A066ECB26FCA2

220
main.cpp
View File

@ -4,6 +4,7 @@
#include <string>
#include "POHelperClasses.hpp"
#include "POCmdlineHelperClasses.hpp"
#include "MQTTDataStreamer.hpp"
#include <boost/program_options.hpp>
@ -37,13 +38,13 @@ const auto TIMEOUT = std::chrono::seconds(1);
//////////////////////////////////////////////////
// application router ID (LSBF)
static const u1_t APPEUI[8] = {0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x23, 0x45};
static u1_t APPEUI[8];
// unique device ID (LSBF)
static const u1_t DEVEUI[8] = {0x8D, 0x53, 0x05, 0xD0, 0x7E, 0xD5, 0xB3, 0x70};
static u1_t DEVEUI[8];
// device-specific AES key (derived from device EUI)
static const u1_t DEVKEY[16] = {0x44, 0x99, 0xB4, 0xD3, 0xDB, 0x02, 0x07, 0xE3, 0xD6, 0x1A, 0x06, 0x6E, 0xCB, 0x26, 0xFC, 0xA2};
static u1_t DEVKEY[16];
//////////////////////////////////////////////////
// LMIC APPLICATION CALLBACKS
@ -148,131 +149,18 @@ 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)
std::string mapper(std::string env_var)
{
static boost::regex r("\\d\\d\\d-(\\d\\d\\d)");
// ensure the env_var is all caps
std::transform(env_var.begin(), env_var.end(), env_var.begin(), ::toupper);
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);
}
if (env_var == "PATH") return "path";
if (env_var == "EXAMPLE_VERBOSE") return "verbosity";
if (env_var == "HOSTNAME") return "hostname";
if (env_var == "HOME") return "home";
return "";
}
@ -297,18 +185,56 @@ int main(int argc, char *argv[])
{
osjob_t initjob;
std::string hostname;
po_cmdline_helper po_cmdline_inst;
boost::program_options::options_description desc_env;
desc_env.add_options() ("path", "the execution path")
("home", "the home directory of the executing user")
("verbosity", po::value<std::string>()->default_value("INFO"), "set verbosity: DEBUG, INFO, WARN, ERROR, FATAL")
("hostname", boost::program_options::value<std::string>(&hostname));
boost::program_options::variables_map vm_env;
boost::program_options::store(boost::program_options::parse_environment(desc_env, boost::function1<std::string, std::string>(mapper)), vm_env);
boost::program_options::notify(vm_env);
if (vm_env.count("home"))
{
std::cout << "home path: " << vm_env["home"].as<std::string>() << std::endl;
}
if (vm_env.count("path"))
{
std::cout << "First 75 chars of the system path: \n";
std::cout << vm_env["path"].as<std::string>().substr(0, 75) << std::endl;
}
std::cout << "Verbosity: " << vm_env["verbosity"].as<std::string>() << std::endl;
if ( vm_env.count("hostname"))
{
std::cout << "hostname: " << vm_env["hostname"].as<std::string>() << std::endl; // correct value of HOSTNAME environent variable
}
// set options allowed by the command line
po::options_description command_line_options("Allowed options");
po_cmdline_inst.init( &command_line_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>(), "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");
*/
// set options allowed in config file
po::options_description config_file_options;
config_file_options.add_options() ("APPEUI", po::value<appeui>(), "APPEUI")
@ -320,7 +246,23 @@ int main(int argc, char *argv[])
po::store(po::parse_command_line(argc, argv, command_line_options), variable_map);
po::notify(variable_map);
auto config_file = variable_map.at("config").as<std::string>();
std::string config_file;
if( variable_map.count("config") ){
config_file = variable_map.at("config").as<std::string>();
}else if( vm_env.count("home") ){
if(std::filesystem::exists( vm_env["home"].as<std::string>() + "/.config/mqtt2LoRaWAN/default.conf" )){
config_file = vm_env["home"].as<std::string>() + "/.config/mqtt2LoRaWAN/default.conf";
}else if(std::filesystem::exists("/etc/mqtt2LoRaWAN/default.conf")){
config_file = "/etc/mqtt2LoRaWAN/default.conf";
}
}else{
if(std::filesystem::exists("/etc/mqtt2LoRaWAN/default.conf")){
config_file = "/etc/mqtt2LoRaWAN/default.conf";
}
}
std::ifstream ifs(config_file.c_str());
if (!ifs) {
@ -358,6 +300,11 @@ int main(int argc, char *argv[])
std::printf("%#02x", variable_map["DEVEUI"].as<deveui>().device_eui64.e8[i] );
}
std::cout << std::endl;
std::memcpy(DEVEUI, variable_map["DEVEUI"].as<deveui>().device_eui64.e8, 8);
}else{
std::cout << "No DEVEUI found in the config file. Add a 8 bytes long EUI to config file or specify an EUI as an option via the command line." << std::endl;
return 1;
}
@ -368,6 +315,23 @@ int main(int argc, char *argv[])
std::printf("%#02x", variable_map["APPEUI"].as<appeui>().application_eui64.e8[i] );
}
std::cout << std::endl;
std::memcpy(APPEUI, variable_map["APPEUI"].as<appeui>().application_eui64.e8, 8);
}else{
std::cout << "No APPEUI found in the config file. Add a 8 bytes long EUI to config file or specify an EUI as an option via the command line." << std::endl;
return 1;
}
if (variable_map.count("DEVKEY"))
{
std::cout << "DEVKEY: " << std::endl;
for( int i = 0; i < 16; i++ ){
std::printf("%#02x", variable_map["DEVKEY"].as<devkey>().device_key[i] );
}
std::cout << std::endl;
std::memcpy(DEVKEY, variable_map["DEVKEY"].as<devkey>().device_key, 16);
}else{
std::cout << "No DEVKEY found in the config file. Add a 16 bytes long key to config file or specify a key as an option via the command line." << std::endl;
return 1;
}