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.

124 lines
3.2 KiB

  1. /*
  2. MDNS MODULE
  3. Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. // -----------------------------------------------------------------------------
  6. // mDNS Server
  7. // -----------------------------------------------------------------------------
  8. #if MDNS_SERVER_SUPPORT
  9. #include <ESP8266mDNS.h>
  10. #if MQTT_SUPPORT
  11. void _mdnsFindMQTT() {
  12. int count = MDNS.queryService("mqtt", "tcp");
  13. DEBUG_MSG_P(PSTR("[MQTT] MQTT brokers found: %d\n"), count);
  14. for (int i=0; i<count; i++) {
  15. DEBUG_MSG_P(PSTR("[MQTT] Broker at %s:%d\n"), MDNS.IP(i).toString().c_str(), MDNS.port(i));
  16. mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i));
  17. }
  18. }
  19. #endif
  20. void _mdnsServerStart() {
  21. if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
  22. DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
  23. } else {
  24. DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
  25. }
  26. }
  27. // -----------------------------------------------------------------------------
  28. void mdnsServerSetup() {
  29. #if WEB_SUPPORT
  30. MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt());
  31. #endif
  32. #if TELNET_SUPPORT
  33. MDNS.addService("telnet", "tcp", TELNET_PORT);
  34. #endif
  35. // Public ESPurna related txt for OTA discovery
  36. MDNS.addServiceTxt("arduino", "tcp", "app_name", APP_NAME);
  37. MDNS.addServiceTxt("arduino", "tcp", "app_version", APP_VERSION);
  38. MDNS.addServiceTxt("arduino", "tcp", "target_board", getBoardName());
  39. {
  40. char buffer[6];
  41. itoa(ESP.getFlashChipRealSize() / 1024, buffer, 10);
  42. MDNS.addServiceTxt("arduino", "tcp", "mem_size", (const char *) buffer);
  43. }
  44. {
  45. char buffer[6];
  46. itoa(ESP.getFlashChipSize() / 1024, buffer, 10);
  47. MDNS.addServiceTxt("arduino", "tcp", "sdk_size", (const char *) buffer);
  48. }
  49. {
  50. char buffer[6];
  51. itoa(ESP.getFreeSketchSpace(), buffer, 10);
  52. MDNS.addServiceTxt("arduino", "tcp", "free_space", (const char *) buffer);
  53. }
  54. wifiRegister([](justwifi_messages_t code, char * parameter) {
  55. if (code == MESSAGE_CONNECTED) {
  56. _mdnsServerStart();
  57. #if MQTT_SUPPORT
  58. _mdnsFindMQTT();
  59. #endif // MQTT_SUPPORT
  60. }
  61. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  62. _mdnsServerStart();
  63. }
  64. });
  65. }
  66. #endif // MDNS_SERVER_SUPPORT
  67. // -----------------------------------------------------------------------------
  68. // mDNS Client
  69. // -----------------------------------------------------------------------------
  70. #if MDNS_CLIENT_SUPPORT
  71. #include <WiFiUdp.h>
  72. #include <mDNSResolver.h>
  73. using namespace mDNSResolver;
  74. WiFiUDP _mdns_udp;
  75. Resolver _mdns_resolver(_mdns_udp);
  76. String mdnsResolve(char * name) {
  77. if (strlen(name) == 0) return String();
  78. if (WiFi.status() != WL_CONNECTED) return String();
  79. _mdns_resolver.setLocalIP(WiFi.localIP());
  80. IPAddress ip = _mdns_resolver.search(name);
  81. if (ip == INADDR_NONE) return String(name);
  82. DEBUG_MSG_P(PSTR("[MDNS] '%s' resolved to '%s'\n"), name, ip.toString().c_str());
  83. return ip.toString();
  84. }
  85. String mdnsResolve(String name) {
  86. return mdnsResolve((char *) name.c_str());
  87. }
  88. void mdnsClientLoop() {
  89. _mdns_resolver.loop();
  90. }
  91. #endif // MDNS_CLIENT_SUPPORT