Tasmota/tasmota/tasmota_support/support_network.ino

136 lines
4.0 KiB
Arduino
Raw Normal View History

2020-06-15 21:10:49 +01:00
/*
support_network.ino - Network support for Tasmota
2021-01-01 12:44:04 +00:00
Copyright (C) 2021 Theo Arends
2020-06-15 21:10:49 +01:00
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) {
2021-06-11 17:14:12 +01:00
// static uint8_t mdns_delayed_start = Settings->param[P_MDNS_DELAYED_START];
2020-10-30 11:29:48 +00:00
2021-06-11 17:14:12 +01:00
if (Settings->flag3.mdns_enabled) { // SetOption55 - Control mDNS service
2020-06-15 21:10:49 +01:00
if (!Mdns.begun) {
2020-10-30 11:29:48 +00:00
// if (mdns_delayed_start) {
2021-01-23 15:26:23 +00:00
// AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_ATTEMPTING_CONNECTION));
2020-10-30 11:29:48 +00:00
// mdns_delayed_start--;
// } else {
2021-06-11 17:14:12 +01:00
// mdns_delayed_start = Settings->param[P_MDNS_DELAYED_START];
MDNS.end(); // close existing or MDNS.begin will fail
2020-10-30 11:29:48 +00:00
Mdns.begun = (uint8_t)MDNS.begin(TasmotaGlobal.hostname);
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS "%s"), (Mdns.begun) ? PSTR(D_INITIALIZED) : PSTR(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
2021-01-23 15:26:23 +00:00
AddLog(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
2021-01-09 04:10:44 +00:00
SettingsUpdateText(SET_MQTT_HOST, MDNS.hostname(i).c_str());
2021-06-11 17:14:12 +01:00
Settings->mqtt_port = MDNS.port(i);
2020-06-15 21:10:49 +01:00
2021-06-11 17:14:12 +01:00
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_MQTT_SERVICE_FOUND " %s," D_PORT " %d"), 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
}
}
#ifdef ESP8266 //Not needed with esp32 mdns
2020-06-15 21:10:49 +01:00
void MdnsUpdate(void) {
if (2 == Mdns.begun) {
MDNS.update(); // this is basically passpacket like a webserver
// being called in main loop so no logging
2021-01-23 15:26:23 +00:00
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_MDNS "MDNS.update"));
2020-06-15 21:10:49 +01:00
}
}
#endif // ESP8266
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
}
2021-04-11 12:29:33 +01:00
String NetworkUniqueId(void) {
String unique_id = WiFi.macAddress();
unique_id.replace(":", ""); // Full 12 chars MAC address as ID
return unique_id;
}