Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. /*
  2. MDNS MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. // -----------------------------------------------------------------------------
  6. // mDNS Server
  7. // -----------------------------------------------------------------------------
  8. #include "mdns.h"
  9. #include "mqtt.h"
  10. #include "utils.h"
  11. #if MDNS_SERVER_SUPPORT
  12. #include <ESP8266mDNS.h>
  13. #if MQTT_SUPPORT
  14. void _mdnsFindMQTT() {
  15. int count = MDNS.queryService("mqtt", "tcp");
  16. DEBUG_MSG_P(PSTR("[MQTT] MQTT brokers found: %d\n"), count);
  17. for (int i=0; i<count; i++) {
  18. DEBUG_MSG_P(PSTR("[MQTT] Broker at %s:%d\n"), MDNS.IP(i).toString().c_str(), MDNS.port(i));
  19. mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i));
  20. }
  21. }
  22. #endif
  23. void _mdnsServerStart() {
  24. if (MDNS.begin(getSetting("hostname", getIdentifier()))) {
  25. DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
  26. } else {
  27. DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
  28. }
  29. }
  30. // -----------------------------------------------------------------------------
  31. void mdnsServerSetup() {
  32. bool done { false };
  33. #if WEB_SUPPORT
  34. {
  35. MDNS.addService("http", "tcp", getSetting("webPort", static_cast<uint16_t>(WEB_PORT)));
  36. done = true;
  37. }
  38. #endif
  39. #if TELNET_SUPPORT
  40. {
  41. MDNS.addService("telnet", "tcp", TELNET_PORT);
  42. done = true;
  43. }
  44. #endif
  45. #if OTA_ARDUINOOTA_SUPPORT
  46. {
  47. MDNS.addServiceTxt("arduino", "tcp", "app_name", APP_NAME);
  48. MDNS.addServiceTxt("arduino", "tcp", "app_version", getVersion());
  49. MDNS.addServiceTxt("arduino", "tcp", "build_date", buildTime());
  50. MDNS.addServiceTxt("arduino", "tcp", "mac", WiFi.macAddress());
  51. MDNS.addServiceTxt("arduino", "tcp", "target_board", getBoardName());
  52. MDNS.addServiceTxt("arduino", "tcp", "mem_size",
  53. String(static_cast<int>(ESP.getFlashChipRealSize() / 1024), 10));
  54. MDNS.addServiceTxt("arduino", "tcp", "sdk_size",
  55. String(static_cast<int>(ESP.getFlashChipSize() / 1024), 10));
  56. MDNS.addServiceTxt("arduino", "tcp", "free_space",
  57. String(static_cast<int>(ESP.getFreeSketchSpace() / 1024), 10));
  58. done = true;
  59. }
  60. #endif
  61. if (!done) {
  62. return;
  63. }
  64. // 2.7.4 and older require MDNS.begin() when interface is UP
  65. // 3.0.0 and newer only need to do MDNS.begin() once at setup()
  66. // (TODO: this is techically a constexpr, but not in 2.7.4 :/)
  67. const static bool OldCore { esp8266::coreVersionNumeric() <= 20704000 };
  68. wifiRegister([](wifi::Event event) {
  69. if (event == wifi::Event::StationConnected) {
  70. #if MQTT_SUPPORT
  71. _mdnsFindMQTT();
  72. #endif
  73. } else if (OldCore && (event == wifi::Event::Mode)) {
  74. _mdnsServerStart();
  75. }
  76. });
  77. if (!OldCore) {
  78. _mdnsServerStart();
  79. }
  80. }
  81. #endif // MDNS_SERVER_SUPPORT