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.

132 lines
3.4 KiB

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