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.

133 lines
3.4 KiB

7 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. #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((char *) getSetting("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<uint16_t>("webPort", WEB_PORT));
  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", "build_date", buildTime());
  39. MDNS.addServiceTxt("arduino", "tcp", "mac", WiFi.macAddress());
  40. MDNS.addServiceTxt("arduino", "tcp", "target_board", getBoardName());
  41. {
  42. char buffer[6] = {0};
  43. itoa(ESP.getFlashChipRealSize() / 1024, buffer, 10);
  44. MDNS.addServiceTxt("arduino", "tcp", "mem_size", (const char *) buffer);
  45. }
  46. {
  47. char buffer[6] = {0};
  48. itoa(ESP.getFlashChipSize() / 1024, buffer, 10);
  49. MDNS.addServiceTxt("arduino", "tcp", "sdk_size", (const char *) buffer);
  50. }
  51. {
  52. char buffer[6] = {0};
  53. itoa(ESP.getFreeSketchSpace(), buffer, 10);
  54. MDNS.addServiceTxt("arduino", "tcp", "free_space", (const char *) buffer);
  55. }
  56. wifiRegister([](justwifi_messages_t code, char * parameter) {
  57. if (code == MESSAGE_CONNECTED) {
  58. _mdnsServerStart();
  59. #if MQTT_SUPPORT
  60. _mdnsFindMQTT();
  61. #endif // MQTT_SUPPORT
  62. }
  63. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  64. _mdnsServerStart();
  65. }
  66. });
  67. }
  68. #endif // MDNS_SERVER_SUPPORT
  69. // -----------------------------------------------------------------------------
  70. // mDNS Client
  71. // -----------------------------------------------------------------------------
  72. #if MDNS_CLIENT_SUPPORT
  73. #include <WiFiUdp.h>
  74. #include <mDNSResolver.h>
  75. using namespace mDNSResolver;
  76. WiFiUDP _mdns_udp;
  77. Resolver _mdns_resolver(_mdns_udp);
  78. String mdnsResolve(char * name) {
  79. if (strlen(name) == 0) return String();
  80. if (WiFi.status() != WL_CONNECTED) return String();
  81. _mdns_resolver.setLocalIP(WiFi.localIP());
  82. IPAddress ip = _mdns_resolver.search(name);
  83. if (ip == INADDR_NONE) return String(name);
  84. DEBUG_MSG_P(PSTR("[MDNS] '%s' resolved to '%s'\n"), name, ip.toString().c_str());
  85. return ip.toString();
  86. }
  87. String mdnsResolve(String name) {
  88. return mdnsResolve((char *) name.c_str());
  89. }
  90. void mdnsClientSetup() {
  91. // Register loop
  92. espurnaRegisterLoop(mdnsClientLoop);
  93. }
  94. void mdnsClientLoop() {
  95. _mdns_resolver.loop();
  96. }
  97. #endif // MDNS_CLIENT_SUPPORT