mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 15:38:11 +01:00
Fix MDNS for ESP8266 devices (#2571)
Co-authored-by: Maurice Makaay <account-github@makaay.nl> Co-authored-by: Otto winter <otto@otto-winter.com> Co-authored-by: Maurice Makaay <mmakaay1@xs4all.net>
This commit is contained in:
parent
b91ee4847f
commit
332c9e891b
3 changed files with 21 additions and 5 deletions
|
@ -23,7 +23,7 @@ std::vector<MDNSService> MDNSComponent::compile_services_() {
|
||||||
#ifdef USE_API
|
#ifdef USE_API
|
||||||
if (api::global_api_server != nullptr) {
|
if (api::global_api_server != nullptr) {
|
||||||
MDNSService service{};
|
MDNSService service{};
|
||||||
service.service_type = "esphomelib";
|
service.service_type = "_esphomelib";
|
||||||
service.proto = "_tcp";
|
service.proto = "_tcp";
|
||||||
service.port = api::global_api_server->get_port();
|
service.port = api::global_api_server->get_port();
|
||||||
service.txt_records.push_back({"version", ESPHOME_VERSION});
|
service.txt_records.push_back({"version", ESPHOME_VERSION});
|
||||||
|
@ -57,7 +57,7 @@ std::vector<MDNSService> MDNSComponent::compile_services_() {
|
||||||
#ifdef USE_PROMETHEUS
|
#ifdef USE_PROMETHEUS
|
||||||
{
|
{
|
||||||
MDNSService service{};
|
MDNSService service{};
|
||||||
service.service_type = "prometheus-http";
|
service.service_type = "_prometheus-http";
|
||||||
service.proto = "_tcp";
|
service.proto = "_tcp";
|
||||||
service.port = WEBSERVER_PORT;
|
service.port = WEBSERVER_PORT;
|
||||||
res.push_back(service);
|
res.push_back(service);
|
||||||
|
@ -68,7 +68,7 @@ std::vector<MDNSService> MDNSComponent::compile_services_() {
|
||||||
// Publish "http" service if not using native API
|
// Publish "http" service if not using native API
|
||||||
// This is just to have *some* mDNS service so that .local resolution works
|
// This is just to have *some* mDNS service so that .local resolution works
|
||||||
MDNSService service{};
|
MDNSService service{};
|
||||||
service.service_type = "http";
|
service.service_type = "_http";
|
||||||
service.proto = "_tcp";
|
service.proto = "_tcp";
|
||||||
service.port = WEBSERVER_PORT;
|
service.port = WEBSERVER_PORT;
|
||||||
service.txt_records.push_back({"version", ESPHOME_VERSION});
|
service.txt_records.push_back({"version", ESPHOME_VERSION});
|
||||||
|
|
|
@ -13,7 +13,11 @@ struct MDNSTXTRecord {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MDNSService {
|
struct MDNSService {
|
||||||
|
// service name _including_ underscore character prefix
|
||||||
|
// as defined in RFC6763 Section 7
|
||||||
std::string service_type;
|
std::string service_type;
|
||||||
|
// second label indicating protocol _including_ underscore character prefix
|
||||||
|
// as defined in RFC6763 Section 7, like "_tcp" or "_udp"
|
||||||
std::string proto;
|
std::string proto;
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
std::vector<MDNSTXTRecord> txt_records;
|
std::vector<MDNSTXTRecord> txt_records;
|
||||||
|
|
|
@ -17,9 +17,21 @@ void MDNSComponent::setup() {
|
||||||
|
|
||||||
auto services = compile_services_();
|
auto services = compile_services_();
|
||||||
for (const auto &service : services) {
|
for (const auto &service : services) {
|
||||||
MDNS.addService(service.service_type.c_str(), service.proto.c_str(), service.port);
|
// Strip the leading underscore from the proto and service_type. While it is
|
||||||
|
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
||||||
|
// expects the underscore to be there, the ESP8266 implementation always adds
|
||||||
|
// the underscore itself.
|
||||||
|
auto proto = service.proto.c_str();
|
||||||
|
while (*proto == '_') {
|
||||||
|
proto++;
|
||||||
|
}
|
||||||
|
auto service_type = service.service_type.c_str();
|
||||||
|
while (*service_type == '_') {
|
||||||
|
service_type++;
|
||||||
|
}
|
||||||
|
MDNS.addService(service_type, proto, service.port);
|
||||||
for (const auto &record : service.txt_records) {
|
for (const auto &record : service.txt_records) {
|
||||||
MDNS.addServiceTxt(service.service_type.c_str(), service.proto.c_str(), record.key.c_str(), record.value.c_str());
|
MDNS.addServiceTxt(service_type, proto, record.key.c_str(), record.value.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue