Fix mdns related compile error

This commit is contained in:
Theo Arends 2020-06-15 22:10:49 +02:00
parent 129ff5d4ab
commit c1d0d20996
4 changed files with 135 additions and 109 deletions

128
tasmota/support_network.ino Normal file
View File

@ -0,0 +1,128 @@
/*
support_network.ino - Network support for Tasmota
Copyright (C) 2020 Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*********************************************************************************************\
* MDNS
\*********************************************************************************************/
struct {
uint8_t begun = 0; // mDNS active
} Mdns;
#ifdef USE_DISCOVERY
void StartMdns(void) {
if (Settings.flag3.mdns_enabled) { // SetOption55 - Control mDNS service
if (!Mdns.begun) {
// if (mdns_delayed_start) {
// AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_ATTEMPTING_CONNECTION));
// mdns_delayed_start--;
// } else {
// mdns_delayed_start = Settings.param[P_MDNS_DELAYED_START];
Mdns.begun = (uint8_t)MDNS.begin(my_hostname);
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS "%s"), (Mdns.begun) ? D_INITIALIZED : D_FAILED);
// }
}
}
}
#ifdef MQTT_HOST_DISCOVERY
void MqttDiscoverServer(void)
{
if (!Mdns.begun) { return; }
int n = MDNS.queryService("mqtt", "tcp"); // Search for mqtt service
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_QUERY_DONE " %d"), n);
if (n > 0) {
uint32_t i = 0; // If the hostname isn't set, use the first record found.
#ifdef MDNS_HOSTNAME
for (i = n; i > 0; i--) { // Search from last to first and use first if not found
if (!strcmp(MDNS.hostname(i).c_str(), MDNS_HOSTNAME)) {
break; // Stop at matching record
}
}
#endif // MDNS_HOSTNAME
SettingsUpdateText(SET_MQTT_HOST, MDNS.IP(i).toString().c_str());
Settings.mqtt_port = MDNS.port(i);
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_MQTT_SERVICE_FOUND " %s, " D_IP_ADDRESS " %s, " D_PORT " %d"), MDNS.hostname(i).c_str(), SettingsText(SET_MQTT_HOST), Settings.mqtt_port);
}
}
#endif // MQTT_HOST_DISCOVERY
#ifdef WEBSERVER_ADVERTISE
void MdnsAddServiceHttp(void) {
if (1 == Mdns.begun) {
Mdns.begun = 2;
MDNS.addService("http", "tcp", WEB_PORT);
}
}
void MdnsUpdate(void) {
if (2 == Mdns.begun) {
MDNS.update();
AddLog_P(LOG_LEVEL_DEBUG_MORE, D_LOG_MDNS, "MDNS.update");
}
}
#endif // WEBSERVER_ADVERTISE
#endif // USE_DISCOVERY
/*********************************************************************************************\
* Global network parameters
\*********************************************************************************************/
char* NetworkHostname(void) {
if (global_state.eth_down) {
return my_hostname;
}
#ifdef ESP32
#ifdef USE_ETHERNET
else {
return EthernetHostname();
}
#endif
#endif
}
IPAddress NetworkAddress(void) {
if (global_state.eth_down) {
return WiFi.localIP();
}
#ifdef ESP32
#ifdef USE_ETHERNET
else {
return EthernetLocalIP();
}
#endif
#endif
}
String NetworkMacAddress(void) {
if (global_state.eth_down) {
return WiFi.macAddress();
}
#ifdef ESP32
#ifdef USE_ETHERNET
else {
return EthernetMacAddress();
}
#endif
#endif
}

View File

@ -52,7 +52,6 @@ struct WIFI {
uint8_t status;
uint8_t config_type = 0;
uint8_t config_counter = 0;
uint8_t mdns_begun = 0; // mDNS active
uint8_t scan_state;
uint8_t bssid[6];
int8_t best_network_db;
@ -376,39 +375,6 @@ String WifiGetIPv6(void)
return "";
}
#ifdef USE_DISCOVERY
void StartMdns(void) {
if (Settings.flag3.mdns_enabled) { // SetOption55 - Control mDNS service
if (!Wifi.mdns_begun) {
// if (mdns_delayed_start) {
// AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_ATTEMPTING_CONNECTION));
// mdns_delayed_start--;
// } else {
// mdns_delayed_start = Settings.param[P_MDNS_DELAYED_START];
Wifi.mdns_begun = (uint8_t)MDNS.begin(my_hostname);
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS "%s"), (Wifi.mdns_begun) ? D_INITIALIZED : D_FAILED);
// }
}
}
}
#ifdef WEBSERVER_ADVERTISE
void MdnsAddServiceHttp(void) {
if (1 == Wifi.mdns_begun) {
Wifi.mdns_begun = 2;
MDNS.addService("http", "tcp", WEB_PORT);
}
}
void MdnsUpdate(void) {
if (2 == Wifi.mdns_begun) {
MDNS.update();
AddLog_P(LOG_LEVEL_DEBUG_MORE, D_LOG_MDNS, "MDNS.update");
}
}
#endif // WEBSERVER_ADVERTISE
#endif // USE_DISCOVERY
bool WifiCheckIPAddrStatus(void) // Return false for 169.254.x.x or fe80::/64
{
bool ip_global=false;
@ -569,7 +535,7 @@ void WifiCheck(uint8_t param)
}
} else {
WifiSetState(0);
Wifi.mdns_begun = 0;
Mdns.begun = 0;
}
}
}
@ -735,43 +701,3 @@ void wifiKeepAlive(void) {
}
}
#endif // ARDUINO_ESP8266_RELEASE_2_3_0
char* NetworkHostname(void) {
if (global_state.eth_down) {
return my_hostname;
}
#ifdef ESP32
#ifdef USE_ETHERNET
else {
return EthernetHostname();
}
#endif
#endif
}
IPAddress NetworkAddress(void) {
if (global_state.eth_down) {
return WiFi.localIP();
}
#ifdef ESP32
#ifdef USE_ETHERNET
else {
return EthernetLocalIP();
}
#endif
#endif
}
String NetworkMacAddress(void) {
if (global_state.eth_down) {
return WiFi.macAddress();
}
#ifdef ESP32
#ifdef USE_ETHERNET
else {
return EthernetMacAddress();
}
#endif
#endif
}

View File

@ -870,14 +870,14 @@ void StartWebserver(int type, IPAddress ipweb)
String ipv6_addr = WifiGetIPv6();
if (ipv6_addr!="") {
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_HTTP D_WEBSERVER_ACTIVE_ON " %s%s " D_WITH_IP_ADDRESS " %s and IPv6 global address %s "),
NetworkHostname(), (Wifi.mdns_begun) ? ".local" : "", ipweb.toString().c_str(), ipv6_addr.c_str());
NetworkHostname(), (Mdns.begun) ? ".local" : "", ipweb.toString().c_str(), ipv6_addr.c_str());
} else {
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_HTTP D_WEBSERVER_ACTIVE_ON " %s%s " D_WITH_IP_ADDRESS " %s"),
NetworkHostname(), (Wifi.mdns_begun) ? ".local" : "", ipweb.toString().c_str());
NetworkHostname(), (Mdns.begun) ? ".local" : "", ipweb.toString().c_str());
}
#else
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_HTTP D_WEBSERVER_ACTIVE_ON " %s%s " D_WITH_IP_ADDRESS " %s"),
NetworkHostname(), (Wifi.mdns_begun) ? ".local" : "", ipweb.toString().c_str());
NetworkHostname(), (Mdns.begun) ? ".local" : "", ipweb.toString().c_str());
#endif // LWIP_IPV6 = 1
rules_flag.http_init = 1;
}
@ -1158,7 +1158,7 @@ void WSContentSendStyle_P(const char* formatP, ...)
bool sip = (static_cast<uint32_t>(WiFi.softAPIP()) != 0);
WSContentSend_P(PSTR("<h4>%s%s (%s%s%s)</h4>"), // tasmota.local (192.168.2.12, 192.168.4.1)
NetworkHostname(),
(Wifi.mdns_begun) ? ".local" : "",
(Mdns.begun) ? ".local" : "",
(lip) ? WiFi.localIP().toString().c_str() : "",
(lip && sip) ? ", " : "",
(sip) ? WiFi.softAPIP().toString().c_str() : "");
@ -2480,7 +2480,7 @@ void HandleInformation(void)
#ifdef ESP32
#ifdef USE_ETHERNET
if (static_cast<uint32_t>(EthernetLocalIP()) != 0) {
WSContentSend_P(PSTR("}1" D_HOSTNAME "}2%s%s"), EthernetHostname(), (Wifi.mdns_begun) ? ".local" : "");
WSContentSend_P(PSTR("}1" D_HOSTNAME "}2%s%s"), EthernetHostname(), (Mdns.begun) ? ".local" : "");
WSContentSend_P(PSTR("}1" D_MAC_ADDRESS "}2%s"), EthernetMacAddress().c_str());
WSContentSend_P(PSTR("}1" D_IP_ADDRESS "}2%s"), EthernetLocalIP().toString().c_str());
}
@ -2489,7 +2489,7 @@ void HandleInformation(void)
if (Settings.flag4.network_wifi) {
int32_t rssi = WiFi.RSSI();
WSContentSend_P(PSTR("}1" D_AP "%d " D_SSID " (" D_RSSI ")}2%s (%d%%, %d dBm)"), Settings.sta_active +1, SettingsText(SET_STASSID1 + Settings.sta_active), WifiGetRssiAsQuality(rssi), rssi);
WSContentSend_P(PSTR("}1" D_HOSTNAME "}2%s%s"), my_hostname, (Wifi.mdns_begun) ? ".local" : "");
WSContentSend_P(PSTR("}1" D_HOSTNAME "}2%s%s"), my_hostname, (Mdns.begun) ? ".local" : "");
#if LWIP_IPV6
String ipv6_addr = WifiGetIPv6();
if (ipv6_addr != "") {

View File

@ -123,34 +123,6 @@ void MakeValidMqtt(uint32_t option, char* str)
}
}
#ifdef USE_DISCOVERY
#ifdef MQTT_HOST_DISCOVERY
void MqttDiscoverServer(void)
{
if (!Wifi.mdns_begun) { return; }
int n = MDNS.queryService("mqtt", "tcp"); // Search for mqtt service
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_QUERY_DONE " %d"), n);
if (n > 0) {
uint32_t i = 0; // If the hostname isn't set, use the first record found.
#ifdef MDNS_HOSTNAME
for (i = n; i > 0; i--) { // Search from last to first and use first if not found
if (!strcmp(MDNS.hostname(i).c_str(), MDNS_HOSTNAME)) {
break; // Stop at matching record
}
}
#endif // MDNS_HOSTNAME
SettingsUpdateText(SET_MQTT_HOST, MDNS.IP(i).toString().c_str());
Settings.mqtt_port = MDNS.port(i);
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_MQTT_SERVICE_FOUND " %s, " D_IP_ADDRESS " %s, " D_PORT " %d"), MDNS.hostname(i).c_str(), SettingsText(SET_MQTT_HOST), Settings.mqtt_port);
}
}
#endif // MQTT_HOST_DISCOVERY
#endif // USE_DISCOVERY
/*********************************************************************************************\
* MQTT driver specific code need to provide the following functions:
*