Skip to content

Commit

Permalink
cm_networking, network: Remove Arduino-MDNS dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
borg42 committed Sep 24, 2024
1 parent 55db2f1 commit 5a4cc29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
11 changes: 5 additions & 6 deletions software/src/modules/cm_networking/cm_mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

#include "cm_networking.h"

#include <Arduino.h>
#include <ESPmDNS.h>
#include <lwip/ip_addr.h>
#include <lwip/opt.h>
#include <lwip/dns.h>
#include <cstring>
#include <TFJson.h>

#include "mdns.h"
#include "event_log_prefix.h"
#include "module_dependencies.h"
#include "tools.h"
Expand Down Expand Up @@ -61,16 +60,16 @@ void CMNetworking::register_urls()
if (!network.config.get("enable_mdns")->asBool())
return;

MDNS.addService("tf-warp-cm", "udp", 34127);
MDNS.addServiceTxt("tf-warp-cm", "udp", "version", MACRO_VALUE_TO_STRING(CM_PACKET_MAGIC) "." MACRO_VALUE_TO_STRING(CM_STATE_VERSION));
mdns_service_add(NULL, "_tf-warp-cm", "_udp", 34127, NULL, 0);
mdns_service_txt_item_set("_tf-warp-cm", "_udp", "version", MACRO_VALUE_TO_STRING(CM_PACKET_MAGIC) "." MACRO_VALUE_TO_STRING(CM_STATE_VERSION));
task_scheduler.scheduleWithFixedDelay([](){
#if MODULE_DEVICE_NAME_AVAILABLE()
// Keep "display_name" updated because it can be changed at runtime without clicking "Save".
MDNS.addServiceTxt("tf-warp-cm", "udp", "display_name", device_name.display_name.get("display_name")->asString());
mdns_service_txt_item_set("_tf-warp-cm", "_udp", "display_name", device_name.display_name.get("display_name")->asEphemeralCStr());
#endif

// Keep "enabled" updated because it is retrieved from the EVSE.
MDNS.addServiceTxt("tf-warp-cm", "udp", "enabled", evse_common.get_management_enabled() ? "true" : "false");
mdns_service_txt_item_set("_tf-warp-cm", "_udp", "enabled", evse_common.get_management_enabled() ? "true" : "false");
}, 0, 10000);
#endif
}
Expand Down
21 changes: 15 additions & 6 deletions software/src/modules/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include "network.h"

#include <sdkconfig.h>
#include <ESPmDNS.h>

#include "mdns.h"
#include "event_log_prefix.h"
#include "module_dependencies.h"
#include "build.h"
Expand Down Expand Up @@ -50,15 +50,24 @@ void Network::register_urls()
{
api.addPersistentConfig("network/config", &config);

if (!config.get("enable_mdns")->asBool())
if (!config.get("enable_mdns")->asBool()) {
return;
}

if (!MDNS.begin(config.get("hostname")->asEphemeralCStr())) {
logger.printfln("Error setting up mDNS responder!");
if (mdns_init() != ESP_OK) {
logger.printfln("Error initializing mDNS responder");
} else {
logger.printfln("mDNS responder started");
String hostname = config.get("hostname")->asString();
hostname.toLowerCase();

if(mdns_hostname_set(hostname.c_str()) != ESP_OK) {
logger.printfln("Error initializing mDNS hostname");
} else {
logger.printfln("mDNS responder started");
}
}
MDNS.addService("http", "tcp", 80);

mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);

#if MODULE_DEBUG_AVAILABLE()
debug.register_task("mdns", CONFIG_MDNS_TASK_STACK_SIZE);
Expand Down

0 comments on commit 5a4cc29

Please sign in to comment.