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.

135 lines
3.5 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(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", "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. #ifdef APP_BUILD_FLAGS
  56. //MDNS.addServiceTxt("arduino", "tcp", "build_flags", APP_BUILD_FLAGS);
  57. #endif
  58. wifiRegister([](justwifi_messages_t code, char * parameter) {
  59. if (code == MESSAGE_CONNECTED) {
  60. _mdnsServerStart();
  61. #if MQTT_SUPPORT
  62. _mdnsFindMQTT();
  63. #endif // MQTT_SUPPORT
  64. }
  65. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  66. _mdnsServerStart();
  67. }
  68. });
  69. }
  70. #endif // MDNS_SERVER_SUPPORT
  71. // -----------------------------------------------------------------------------
  72. // mDNS Client
  73. // -----------------------------------------------------------------------------
  74. #if MDNS_CLIENT_SUPPORT
  75. #include <WiFiUdp.h>
  76. #include <mDNSResolver.h>
  77. using namespace mDNSResolver;
  78. WiFiUDP _mdns_udp;
  79. Resolver _mdns_resolver(_mdns_udp);
  80. String mdnsResolve(char * name) {
  81. if (strlen(name) == 0) return String();
  82. if (WiFi.status() != WL_CONNECTED) return String();
  83. _mdns_resolver.setLocalIP(WiFi.localIP());
  84. IPAddress ip = _mdns_resolver.search(name);
  85. if (ip == INADDR_NONE) return String(name);
  86. DEBUG_MSG_P(PSTR("[MDNS] '%s' resolved to '%s'\n"), name, ip.toString().c_str());
  87. return ip.toString();
  88. }
  89. String mdnsResolve(String name) {
  90. return mdnsResolve((char *) name.c_str());
  91. }
  92. void mdnsClientSetup() {
  93. // Register loop
  94. espurnaRegisterLoop(mdnsClientLoop);
  95. }
  96. void mdnsClientLoop() {
  97. _mdns_resolver.loop();
  98. }
  99. #endif // MDNS_CLIENT_SUPPORT