From a0903516c1de1c237101f50c4f28680a4688b0e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Obrembski?= Date: Sun, 15 Nov 2020 14:51:23 +0100 Subject: [PATCH 1/3] Add MY_MQTT_ETH_INIT_DELAY to configuration MY_MQTT_ETH_INIT_DELAY configuration field is useful for adjusting Ethernet Chip initialisation delay time by library user. --- MyConfig.h | 12 ++++++++++++ core/MyGatewayTransportMQTTClient.cpp | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/MyConfig.h b/MyConfig.h index 201dc04fd..21ac622c5 100755 --- a/MyConfig.h +++ b/MyConfig.h @@ -1551,6 +1551,17 @@ */ //#define MY_MQTT_CLIENT_KEY +/** + * @def MY_MQTT_ETH_INIT_DELAY + * @brief Set a delay for Ethernet initialisation. + * + * This define is useful if you want to change default Ethernet chip initialisation + * delay time to other value. By default, it is 1000ms + * Example: @code #define MY_MQTT_ETH_INIT_DELAY 1 @endcode + */ +#ifndef MY_MQTT_ETH_INIT_DELAY +#define MY_MQTT_ETH_INIT_DELAY 1000 +#endif /** * @def MY_IP_ADDRESS * @brief Static ip address of gateway. If not defined, DHCP will be used. @@ -2323,6 +2334,7 @@ #define MY_MQTT_CA_CERT #define MY_MQTT_CLIENT_CERT #define MY_MQTT_CLIENT_KEY +#define MY_MQTT_ETH_INIT_DELAY #define MY_SIGNAL_REPORT_ENABLED // general #define MY_WITH_LEDS_BLINKING_INVERSE diff --git a/core/MyGatewayTransportMQTTClient.cpp b/core/MyGatewayTransportMQTTClient.cpp index bd5ab5f58..86c699801 100755 --- a/core/MyGatewayTransportMQTTClient.cpp +++ b/core/MyGatewayTransportMQTTClient.cpp @@ -194,7 +194,7 @@ bool gatewayTransportConnect(void) Ethernet.localIP()[0], Ethernet.localIP()[1], Ethernet.localIP()[2], Ethernet.localIP()[3]); // give the Ethernet interface a second to initialize - delay(1000); + delay(MY_MQTT_ETH_INIT_DELAY); #endif return true; } From c326e38b000aa3d53df8e4b9f6cd1aad79ce4cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Obrembski?= Date: Sun, 15 Nov 2020 14:52:30 +0100 Subject: [PATCH 2/3] Add MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT to config MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT is useful for adjusting default MQTT TCP/IP connection timeout by library user. --- MyConfig.h | 16 ++++++++++++++++ core/MyGatewayTransportMQTTClient.cpp | 9 ++++++++- keywords.txt | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/MyConfig.h b/MyConfig.h index 21ac622c5..94d3115d6 100755 --- a/MyConfig.h +++ b/MyConfig.h @@ -1562,6 +1562,21 @@ #ifndef MY_MQTT_ETH_INIT_DELAY #define MY_MQTT_ETH_INIT_DELAY 1000 #endif +/** + * @def MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT + * @brief Set a MQTT broker socket connection timeout time. + * + * This define is useful if you want to change default MQTT TCP/IP broker + * connection timeout. By default, it is 1000ms. + * + * Note that this is not supported in ESP8266 and ESP32 platforms, sorry. + * + * Example: @code #define MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT 1000 @endcode + */ +#ifndef MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT +#define MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT 1000 +#endif + /** * @def MY_IP_ADDRESS * @brief Static ip address of gateway. If not defined, DHCP will be used. @@ -2335,6 +2350,7 @@ #define MY_MQTT_CLIENT_CERT #define MY_MQTT_CLIENT_KEY #define MY_MQTT_ETH_INIT_DELAY +#define MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT #define MY_SIGNAL_REPORT_ENABLED // general #define MY_WITH_LEDS_BLINKING_INVERSE diff --git a/core/MyGatewayTransportMQTTClient.cpp b/core/MyGatewayTransportMQTTClient.cpp index 86c699801..c40119b24 100755 --- a/core/MyGatewayTransportMQTTClient.cpp +++ b/core/MyGatewayTransportMQTTClient.cpp @@ -159,7 +159,11 @@ bool reconnectMQTT(void) return true; } +#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32) delay(1000); +#else + delay(MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT); +#endif GATEWAY_DEBUG(PSTR("!GWT:RMQ:FAIL\n")); return false; } @@ -247,7 +251,10 @@ bool gatewayTransportInit(void) #else _MQTT_client.setServer(MY_CONTROLLER_URL_ADDRESS, MY_PORT); #endif /* End of MY_CONTROLLER_IP_ADDRESS */ - + // ESP platform doesn't support connection timeout +#if !defined(MY_GATEWAY_ESP8266) && !defined(MY_GATEWAY_ESP32) + _MQTT_ethClient.setConnectionTimeout(MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT); +#endif _MQTT_client.setCallback(incomingMQTT); #if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32) diff --git a/keywords.txt b/keywords.txt index d63941799..fce987843 100755 --- a/keywords.txt +++ b/keywords.txt @@ -258,6 +258,8 @@ MY_MQTT_PASSWORD LITERAL1 MY_MQTT_PUBLISH_TOPIC_PREFIX LITERAL1 MY_MQTT_SUBSCRIBE_TOPIC_PREFIX LITERAL1 MY_MQTT_USER LITERAL1 +MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT LITERAL1 +MY_MQTT_ETH_INIT_DELAY LITERAL1 MY_W5100_SPI_EN LITERAL1 MY_WIFI_SSID LITERAL1 MY_WIFI_BSSID LITERAL1 From c9130b917fa8df39f595182cb0314579aa5ff315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Obrembski?= Date: Tue, 17 Nov 2020 11:23:43 +0100 Subject: [PATCH 3/3] Added setConnectionTimeout to EthernetClient for Linux --- .../Linux/drivers/core/EthernetClient.cpp | 11 +++++++++-- hal/architecture/Linux/drivers/core/EthernetClient.h | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/hal/architecture/Linux/drivers/core/EthernetClient.cpp b/hal/architecture/Linux/drivers/core/EthernetClient.cpp index a75884e4d..02f7a55d0 100644 --- a/hal/architecture/Linux/drivers/core/EthernetClient.cpp +++ b/hal/architecture/Linux/drivers/core/EthernetClient.cpp @@ -33,11 +33,11 @@ #include #include "log.h" -EthernetClient::EthernetClient() : _sock(-1) +EthernetClient::EthernetClient() : _sock(-1), socketTimeout(1000) { } -EthernetClient::EthernetClient(int sock) : _sock(sock) +EthernetClient::EthernetClient(int sock) : _sock(sock), socketTimeout(1000) { } @@ -89,6 +89,13 @@ int EthernetClient::connect(const char* host, uint16_t port) continue; } + // Sets the socket timeout + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = socketTimeout * 1000000; + setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); + setsockopt(_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)); + break; } diff --git a/hal/architecture/Linux/drivers/core/EthernetClient.h b/hal/architecture/Linux/drivers/core/EthernetClient.h index 126d12264..59533f931 100644 --- a/hal/architecture/Linux/drivers/core/EthernetClient.h +++ b/hal/architecture/Linux/drivers/core/EthernetClient.h @@ -200,12 +200,21 @@ class EthernetClient : public Client { return !this->operator==(rhs); }; + /** + * @brief Set socket timeout. + * + */ + void setConnectionTimeout(uint16_t timeoutInMilis) + { + socketTimeout = timeoutInMilis; + }; friend class EthernetServer; private: int _sock; //!< @brief Network socket file descriptor. IPAddress _srcip; //!< @brief Local ip to bind to. + uint16_t socketTimeout; //!< @brief Socket timeout in miliseconds. }; #endif