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.

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