From 512d7962246e0628f6cd2c0a41cd4294605430d3 Mon Sep 17 00:00:00 2001 From: konradK Date: Sun, 8 Jan 2017 21:18:24 +0100 Subject: [PATCH] Update mqtthandler.cpp --- src/ebusd/mqtthandler.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ebusd/mqtthandler.cpp b/src/ebusd/mqtthandler.cpp index ba54c12a..94d00ba2 100644 --- a/src/ebusd/mqtthandler.cpp +++ b/src/ebusd/mqtthandler.cpp @@ -27,12 +27,14 @@ using namespace std; /** the definition of the MQTT arguments. */ static const struct argp_option g_mqtt_argp_options[] = { {NULL, 0, NULL, 0, "MQTT options:", 1 }, - {"mqttport", 1, "PORT", 0, "Connect to MQTT broker on PORT (usually 1883), 0 to disable [0]", 0 }, - {"mqtttopic", 2, "TOPIC", 0, "Use MQTT TOPIC (prefix before /%circuit/%name or complete format) [ebusd]", 0 }, + {"mqtthost", 1, "PORT", 0, "Connect to MQTT broker on HOST [localhsot]", 0 }, + {"mqttport", 2, "PORT", 0, "Connect to MQTT broker on PORT (usually 1883), 0 to disable [0]", 0 }, + {"mqtttopic", 3, "TOPIC", 0, "Use MQTT TOPIC (prefix before /%circuit/%name or complete format) [ebusd]", 0 }, {NULL, 0, NULL, 0, NULL, 0 }, }; +static const char* g_host = "localhost"; //!< MQTT Host to use [localhost] static uint16_t g_port = 0; //!< optional port of MQTT broker, 0 to disable [0] static const char* g_topic = PACKAGE; //!< MQTT topic to use (prefix if without wildcards) [ebusd] @@ -48,7 +50,15 @@ static error_t mqtt_parse_opt(int key, char *arg, struct argp_state *state) result_t result = RESULT_OK; switch (key) { - case 1: // --mqttport=1883 + case 1: // --mqtthost=localhost + if (arg == NULL || arg[0] == 0 || arg[0]=='/' || arg[strlen(arg)-1]=='/') { + argp_error(state, "invalid mqtthost"); + return EINVAL; + } + g_host = arg; + break; + + case 2: // --mqttport=1883 g_port = (uint16_t)parseInt(arg, 10, 1, 65535, result); if (result != RESULT_OK) { argp_error(state, "invalid mqttport"); @@ -56,7 +66,7 @@ static error_t mqtt_parse_opt(int key, char *arg, struct argp_state *state) } break; - case 2: // --mqtttopic=ebusd + case 3: // --mqtttopic=ebusd if (arg == NULL || arg[0] == 0 || arg[0]=='/' || arg[strlen(arg)-1]=='/') { argp_error(state, "invalid mqtttopic"); return EINVAL; @@ -207,7 +217,7 @@ MqttHandler::MqttHandler(BusHandler* busHandler, MessageMap* messages) true, #endif willTopic.c_str(), (uint32_t)len, (uint8_t*)(willData.c_str()), 0, true); - if (mosquitto_connect(m_mosquitto, "localhost", g_port, 60 + if (mosquitto_connect(m_mosquitto, g_host, g_port, 60 #if (LIBMOSQUITTO_MAJOR<1) , true #endif