Tasmota/tasmota/support_network.ino

126 lines
3.7 KiB
Arduino
Raw Normal View History

2020-06-15 21:10:49 +01:00
/*
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) {
2020-10-30 11:29:48 +00:00
// static uint8_t mdns_delayed_start = Settings.param[P_MDNS_DELAYED_START];
2020-06-15 21:10:49 +01:00
if (Settings.flag3.mdns_enabled) { // SetOption55 - Control mDNS service
if (!Mdns.begun) {
2020-10-30 11:29:48 +00:00
// 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(TasmotaGlobal.hostname);
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS "%s"), (Mdns.begun) ? D_INITIALIZED : D_FAILED);
2020-10-30 11:29:48 +00:00
// }
2020-06-15 21:10:49 +01:00
}
}
}
#ifdef MQTT_HOST_DISCOVERY
void MqttDiscoverServer(void)
{
if (!Mdns.begun) { return; }
int n = MDNS.queryService("mqtt", "tcp"); // Search for mqtt service
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_QUERY_DONE " %d"), n);
2020-06-15 21:10:49 +01:00
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_P(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);
2020-06-15 21:10:49 +01:00
}
}
#endif // MQTT_HOST_DISCOVERY
#ifdef WEBSERVER_ADVERTISE
void MdnsAddServiceHttp(void) {
if (1 == Mdns.begun) {
Mdns.begun = 2;
MDNS.addService("http", "tcp", WEB_PORT);
MDNS.addServiceTxt("http", "tcp", "devicetype", "tasmota");
2020-06-15 21:10:49 +01:00
}
}
void MdnsUpdate(void) {
if (2 == Mdns.begun) {
MDNS.update();
AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_MDNS "MDNS.update"));
2020-06-15 21:10:49 +01:00
}
}
#endif // WEBSERVER_ADVERTISE
#endif // USE_DISCOVERY
/*********************************************************************************************\
* Global network parameters
\*********************************************************************************************/
char* NetworkHostname(void) {
#ifdef ESP32
#ifdef USE_ETHERNET
2020-10-30 11:29:48 +00:00
if (!TasmotaGlobal.global_state.eth_down) {
2020-06-15 21:10:49 +01:00
return EthernetHostname();
}
#endif
#endif
2020-10-30 11:29:48 +00:00
return TasmotaGlobal.hostname;
2020-06-15 21:10:49 +01:00
}
IPAddress NetworkAddress(void) {
#ifdef ESP32
#ifdef USE_ETHERNET
2020-10-30 11:29:48 +00:00
if (!TasmotaGlobal.global_state.eth_down) {
2020-07-15 09:50:54 +01:00
return EthernetLocalIP();
2020-06-15 21:10:49 +01:00
}
#endif
#endif
2020-07-15 09:50:54 +01:00
return WiFi.localIP();
2020-06-15 21:10:49 +01:00
}
String NetworkMacAddress(void) {
#ifdef ESP32
#ifdef USE_ETHERNET
2020-10-30 11:29:48 +00:00
if (!TasmotaGlobal.global_state.eth_down) {
2020-06-15 21:10:49 +01:00
return EthernetMacAddress();
}
#endif
#endif
2020-07-15 09:50:54 +01:00
return WiFi.macAddress();
2020-06-15 21:10:49 +01:00
}