From 880bbe357d5c232de81a5cd7db288c26151fc9ef Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 31 Mar 2019 17:57:28 +0200 Subject: [PATCH] Change String to char Change String to char --- sonoff/support.ino | 13 +++++++++++-- sonoff/xdrv_01_webserver.ino | 12 +----------- sonoff/xdrv_02_mqtt.ino | 12 ++++++++++++ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/sonoff/support.ino b/sonoff/support.ino index e571e6ec1..d101185a6 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -370,6 +370,14 @@ uint8_t Shortcut(const char* str) return result; } +bool ValidIpAddress(const char* str) +{ + const char* p = str; + + while (*p && ( (*p == '.') || (*p >= '0') || (*p <= '9') )) { p++; } + return (*p == '\0'); +} + bool ParseIp(uint32_t* addr, const char* str) { uint8_t *part = (uint8_t*)addr; @@ -1247,8 +1255,9 @@ void Syslog(void) // Destroys log_data char syslog_preamble[64]; // Hostname + Id - if (syslog_host_hash != GetHash(Settings.syslog_host, strlen(Settings.syslog_host))) { - syslog_host_hash = GetHash(Settings.syslog_host, strlen(Settings.syslog_host)); + uint32_t current_hash = GetHash(Settings.syslog_host, strlen(Settings.syslog_host)); + if (syslog_host_hash != current_hash) { + syslog_host_hash = current_hash; WiFi.hostByName(Settings.syslog_host, syslog_host_addr); // If sleep enabled this might result in exception so try to do it once using hash } if (PortUdp.beginPacket(syslog_host_addr, Settings.syslog_port)) { diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index 44be997a6..76cdae534 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -2148,7 +2148,7 @@ void HandleNotFound(void) /* Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */ bool CaptivePortal(void) { - if ((WifiIsInManagerMode()) && !ValidIpAddress(WebServer->hostHeader())) { + if ((WifiIsInManagerMode()) && !ValidIpAddress(WebServer->hostHeader().c_str())) { AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_REDIRECTED)); WebServer->sendHeader(F("Location"), String("http://") + WebServer->client().localIP().toString(), true); @@ -2159,16 +2159,6 @@ bool CaptivePortal(void) return false; } -/** Is this an IP? */ -bool ValidIpAddress(String str) -{ - for (uint16_t i = 0; i < str.length(); i++) { - int c = str.charAt(i); - if (c != '.' && (c < '0' || c > '9')) { return false; } - } - return true; -} - /*********************************************************************************************/ String UrlEncode(const String& text) diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 5720b9a7d..b7daa1f34 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -37,6 +37,9 @@ const char kMqttCommands[] PROGMEM = D_CMND_MQTTUSER "|" D_CMND_MQTTPASSWORD "|" D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|" D_CMND_PUBLISH "|" D_CMND_BUTTONTOPIC "|" D_CMND_SWITCHTOPIC "|" D_CMND_BUTTONRETAIN "|" D_CMND_SWITCHRETAIN "|" D_CMND_POWERRETAIN "|" D_CMND_SENSORRETAIN ; +IPAddress mqtt_host_addr; // MQTT host IP address +uint32_t mqtt_host_hash = 0; // MQTT host name hash + uint16_t mqtt_connect_count = 0; // MQTT re-connect count uint16_t mqtt_retry_counter = 1; // MQTT connection retry counter uint8_t mqtt_initial_connection_state = 2; // MQTT connection messages state @@ -449,6 +452,15 @@ void MqttReconnect(void) MqttClient.setCallback(MqttDataHandler); MqttClient.setServer(Settings.mqtt_host, Settings.mqtt_port); +/* + // Skip MQTT host DNS lookup if not needed + uint32_t current_hash = GetHash(Settings.mqtt_host, strlen(Settings.mqtt_host)); + if (mqtt_host_hash != current_hash) { + mqtt_host_hash = current_hash; + WiFi.hostByName(Settings.mqtt_host, mqtt_host_addr); // Skips DNS lookup if mqtt_host is IP address string as from mDns + } + MqttClient.setServer(mqtt_host_addr, Settings.mqtt_port); +*/ if (MqttClient.connect(mqtt_client, mqtt_user, mqtt_pwd, stopic, 1, true, mqtt_data)) { MqttConnected(); } else {