refactor: entkopple mDNS-Discovery, Status-Flags und WiFi-Manager
- net_mdns: Autostart + Discovery-Retry stabilisiert (Forward-Declarations, Task-Start bei GOT_IP) - sys_status: EventGroup zentralisiert; Statusbits nicht mehr aus net_mdns gesetzt - main: Integration von net_mdns Autostart/Discovery und UI-Status-Update vorbereitet - wifi_mgr: mDNS-Discovery-Logik entfernt/vereinheitlicht, damit keine doppelte Discovery läuft - net_influx: Anpassungen an neue Discovery/Storage-Semantik
This commit is contained in:
@@ -1,28 +1,328 @@
|
||||
// File: firmware/components/net_mdns/src/net_mdns.c
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_wifi.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "mdns.h"
|
||||
#include "net_mdns.h"
|
||||
#include "storage.h"
|
||||
#include "storage.h"
|
||||
|
||||
static const char* TAG = "net_mdns";
|
||||
static const char *TAG = "net_mdns";
|
||||
|
||||
// -------------------- Global State --------------------
|
||||
static char g_hostname[33] = {0};
|
||||
static char g_instance[33] = {0};
|
||||
|
||||
static volatile bool g_mdns_running = false;
|
||||
static volatile bool g_autostart_inited = false;
|
||||
|
||||
// Event handler instance handles (damit nicht mehrfach registriert wird)
|
||||
static esp_event_handler_instance_t g_ip_handler = NULL;
|
||||
static esp_event_handler_instance_t g_wifi_handler = NULL;
|
||||
|
||||
// Discovery task state
|
||||
static TaskHandle_t g_discover_task = NULL;
|
||||
|
||||
// Konfiguration für Discovery-Retry (interne Kopie)
|
||||
typedef struct {
|
||||
char service[32];
|
||||
char proto[8];
|
||||
int enforce_port;
|
||||
|
||||
char fallback_service[32];
|
||||
char fallback_proto[8];
|
||||
int fallback_port;
|
||||
|
||||
// optional TXT filter (können leer sein)
|
||||
char txt_key[32];
|
||||
char txt_value[64];
|
||||
|
||||
uint32_t query_timeout_ms;
|
||||
uint32_t retry_interval_ms;
|
||||
int max_results;
|
||||
|
||||
// optional: Instanzpräfix-Filter
|
||||
char instance_prefix[16]; // z.B. "myapp-"; leer = kein Filter
|
||||
bool require_instance_prefix;
|
||||
} discover_cfg_t;
|
||||
|
||||
static discover_cfg_t g_discover_cfg = {0};
|
||||
static volatile bool g_discover_enabled = false;
|
||||
|
||||
// -------------------- Forward Declarations (C needs this) --------------------
|
||||
static void mdns_start_if_needed(void);
|
||||
static void mdns_stop_if_running(void);
|
||||
static bool have_sta_ip(void);
|
||||
static void mdns_discover_task(void *arg);
|
||||
static void on_ip_event(void *arg, esp_event_base_t base, int32_t id, void *data);
|
||||
static void on_wifi_event(void *arg, esp_event_base_t base, int32_t id, void *data);
|
||||
|
||||
// These are public (declared in net_mdns.h), but used before their definition in this .c
|
||||
esp_err_t net_mdns_autodiscover_and_store(const char *service, const char *proto,
|
||||
int enforce_port,
|
||||
const char *txt_key, const char *txt_value,
|
||||
uint32_t timeout_ms);
|
||||
|
||||
bool net_mdns_find_service_url(const char *service, const char *proto,
|
||||
int enforce_port, const char *txt_key, const char *txt_value,
|
||||
uint32_t timeout_ms, int max_results,
|
||||
char *url_out, size_t url_out_size);
|
||||
|
||||
// -------------------- Internal Helpers --------------------
|
||||
static void mdns_start_if_needed(void)
|
||||
{
|
||||
if (g_mdns_running) return;
|
||||
|
||||
esp_err_t err = mdns_init();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "mdns_init failed: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_hostname[0]) mdns_hostname_set(g_hostname);
|
||||
if (g_instance[0]) mdns_instance_name_set(g_instance);
|
||||
|
||||
g_mdns_running = true;
|
||||
ESP_LOGI(TAG, "mDNS started (host=%s, inst=%s)", g_hostname, g_instance);
|
||||
}
|
||||
|
||||
static void mdns_stop_if_running(void)
|
||||
{
|
||||
if (!g_mdns_running) return;
|
||||
|
||||
mdns_free();
|
||||
g_mdns_running = false;
|
||||
ESP_LOGI(TAG, "mDNS stopped");
|
||||
}
|
||||
|
||||
static bool have_sta_ip(void)
|
||||
{
|
||||
esp_netif_t *sta = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
if (!sta) return false;
|
||||
|
||||
esp_netif_ip_info_t ip;
|
||||
if (esp_netif_get_ip_info(sta, &ip) != ESP_OK) return false;
|
||||
|
||||
return (ip.ip.addr != 0);
|
||||
}
|
||||
|
||||
// Platzhalter: sys_status wird bewusst NICHT in diesem Component gesetzt.
|
||||
static void set_mdns_ok(bool ok)
|
||||
{
|
||||
(void)ok;
|
||||
}
|
||||
|
||||
// -------------------- Discovery Task --------------------
|
||||
static void mdns_discover_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
|
||||
const uint32_t retry_ms = g_discover_cfg.retry_interval_ms ? g_discover_cfg.retry_interval_ms : 60000;
|
||||
const TickType_t retry_delay = pdMS_TO_TICKS(retry_ms);
|
||||
|
||||
ESP_LOGI(TAG,
|
||||
"mDNS discover task started: primary=%s.%s port=%d fallback=%s.%s port=%d retry=%u ms",
|
||||
g_discover_cfg.service, g_discover_cfg.proto, g_discover_cfg.enforce_port,
|
||||
g_discover_cfg.fallback_service, g_discover_cfg.fallback_proto, g_discover_cfg.fallback_port,
|
||||
(unsigned)retry_ms);
|
||||
|
||||
for (;;) {
|
||||
// Wenn Discovery deaktiviert wurde → sauber beenden
|
||||
if (!g_discover_enabled) {
|
||||
ESP_LOGI(TAG, "mDNS discover disabled; exiting task.");
|
||||
g_discover_task = NULL;
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
// Wenn STA nicht online ist → warten
|
||||
if (!have_sta_ip()) {
|
||||
set_mdns_ok(false);
|
||||
vTaskDelay(retry_delay);
|
||||
continue;
|
||||
}
|
||||
|
||||
// mDNS sicherstellen
|
||||
mdns_start_if_needed();
|
||||
if (!g_mdns_running) {
|
||||
set_mdns_ok(false);
|
||||
vTaskDelay(retry_delay);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 1) Primary service
|
||||
esp_err_t err = net_mdns_autodiscover_and_store(
|
||||
g_discover_cfg.service,
|
||||
g_discover_cfg.proto,
|
||||
g_discover_cfg.enforce_port,
|
||||
(g_discover_cfg.txt_key[0] ? g_discover_cfg.txt_key : NULL),
|
||||
(g_discover_cfg.txt_value[0] ? g_discover_cfg.txt_value : NULL),
|
||||
g_discover_cfg.query_timeout_ms ? g_discover_cfg.query_timeout_ms : 2000
|
||||
);
|
||||
|
||||
// 2) Fallback service (wenn primary nicht gefunden)
|
||||
if (err != ESP_OK && g_discover_cfg.fallback_service[0] && g_discover_cfg.fallback_proto[0]) {
|
||||
err = net_mdns_autodiscover_and_store(
|
||||
g_discover_cfg.fallback_service,
|
||||
g_discover_cfg.fallback_proto,
|
||||
g_discover_cfg.fallback_port,
|
||||
NULL,
|
||||
NULL,
|
||||
g_discover_cfg.query_timeout_ms ? g_discover_cfg.query_timeout_ms : 2000
|
||||
);
|
||||
}
|
||||
|
||||
if (err == ESP_OK) {
|
||||
set_mdns_ok(true);
|
||||
ESP_LOGI(TAG, "mDNS: service found & stored; stopping discover task.");
|
||||
g_discover_enabled = false; // Einmal finden reicht (wie zuvor in wifi_mgr)
|
||||
g_discover_task = NULL;
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
} else {
|
||||
set_mdns_ok(false);
|
||||
ESP_LOGW(TAG, "mDNS: no service found; retry in %u ms", (unsigned)retry_ms);
|
||||
vTaskDelay(retry_delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- Event Handlers --------------------
|
||||
static void on_ip_event(void *arg, esp_event_base_t base, int32_t id, void *data)
|
||||
{
|
||||
(void)arg;
|
||||
(void)data;
|
||||
|
||||
if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
|
||||
// mDNS starten
|
||||
mdns_start_if_needed();
|
||||
|
||||
// Optional: Discovery-Task starten, wenn enabled
|
||||
if (g_discover_enabled && g_discover_task == NULL) {
|
||||
BaseType_t ok = xTaskCreate(mdns_discover_task, "mdns_discover", 3072, NULL, 4, &g_discover_task);
|
||||
if (ok != pdPASS) {
|
||||
g_discover_task = NULL;
|
||||
ESP_LOGE(TAG, "Failed to create mdns_discover task");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void on_wifi_event(void *arg, esp_event_base_t base, int32_t id, void *data)
|
||||
{
|
||||
(void)arg;
|
||||
(void)data;
|
||||
|
||||
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
// Beim Disconnect mDNS stoppen
|
||||
mdns_stop_if_running();
|
||||
|
||||
// Status: nicht ok (No-op in diesem Component)
|
||||
set_mdns_ok(false);
|
||||
|
||||
// Discovery Task NICHT hart killen (kann in mdns_query hängen).
|
||||
// Bei nächstem GOT_IP kann sie erneut starten; oder sie schläft und merkt g_discover_enabled später.
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- Public API --------------------
|
||||
esp_err_t net_mdns_autostart_init(const char *hostname, const char *instance_name)
|
||||
{
|
||||
// idempotent: mehrfacher Aufruf ist ok, registriert aber nur einmal Handler
|
||||
if (hostname) {
|
||||
strncpy(g_hostname, hostname, sizeof(g_hostname) - 1);
|
||||
g_hostname[sizeof(g_hostname) - 1] = '\0';
|
||||
}
|
||||
if (instance_name) {
|
||||
strncpy(g_instance, instance_name, sizeof(g_instance) - 1);
|
||||
g_instance[sizeof(g_instance) - 1] = '\0';
|
||||
}
|
||||
|
||||
if (!g_autostart_inited) {
|
||||
esp_err_t e1 = esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
on_ip_event, NULL, &g_ip_handler);
|
||||
esp_err_t e2 = esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED,
|
||||
on_wifi_event, NULL, &g_wifi_handler);
|
||||
if (e1 != ESP_OK || e2 != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to register event handlers (ip=%s wifi=%s)",
|
||||
esp_err_to_name(e1), esp_err_to_name(e2));
|
||||
return (e1 != ESP_OK) ? e1 : e2;
|
||||
}
|
||||
g_autostart_inited = true;
|
||||
ESP_LOGI(TAG, "mDNS autostart initialized");
|
||||
} else {
|
||||
ESP_LOGD(TAG, "mDNS autostart already initialized");
|
||||
}
|
||||
|
||||
// Falls IP schon vorhanden → mDNS direkt starten (+ ggf. discovery task)
|
||||
if (have_sta_ip()) {
|
||||
mdns_start_if_needed();
|
||||
if (g_discover_enabled && g_discover_task == NULL) {
|
||||
BaseType_t ok = xTaskCreate(mdns_discover_task, "mdns_discover", 3072, NULL, 4, &g_discover_task);
|
||||
if (ok != pdPASS) {
|
||||
g_discover_task = NULL;
|
||||
ESP_LOGE(TAG, "Failed to create mdns_discover task");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool net_mdns_is_running(void)
|
||||
{
|
||||
return g_mdns_running;
|
||||
}
|
||||
|
||||
bool net_mdns_wait_started(uint32_t timeout_ms)
|
||||
{
|
||||
uint32_t waited = 0;
|
||||
const uint32_t step = 50;
|
||||
|
||||
while (!g_mdns_running && waited < timeout_ms) {
|
||||
vTaskDelay(pdMS_TO_TICKS(step));
|
||||
waited += step;
|
||||
}
|
||||
return g_mdns_running;
|
||||
}
|
||||
|
||||
esp_err_t net_mdns_add_service(const char *service, const char *proto,
|
||||
uint16_t port, mdns_txt_item_t *txt, size_t txt_count)
|
||||
{
|
||||
if (!g_mdns_running) {
|
||||
mdns_start_if_needed();
|
||||
if (!g_mdns_running) return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_err_t err = mdns_service_add(NULL, service, proto, port, txt, txt_count);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "mdns_service_add %s.%s:%u failed: %s",
|
||||
service, proto, (unsigned)port, esp_err_to_name(err));
|
||||
} else {
|
||||
ESP_LOGI(TAG, "mDNS service announced: %s.%s:%u", service, proto, (unsigned)port);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t net_mdns_autodiscover_and_store(const char *service, const char *proto,
|
||||
int enforce_port,
|
||||
const char *txt_key, const char *txt_value,
|
||||
uint32_t timeout_ms)
|
||||
int enforce_port,
|
||||
const char *txt_key, const char *txt_value,
|
||||
uint32_t timeout_ms)
|
||||
{
|
||||
char url[96] = {0};
|
||||
|
||||
bool ok = net_mdns_find_service_url(service, proto, enforce_port,
|
||||
txt_key, txt_value, timeout_ms, 5,
|
||||
txt_key, txt_value,
|
||||
timeout_ms, 5,
|
||||
url, sizeof(url));
|
||||
if (!ok) {
|
||||
ESP_LOGW(TAG, "Auto-Discover: kein Treffer für %s.%s", service, proto);
|
||||
@@ -36,96 +336,13 @@ esp_err_t net_mdns_autodiscover_and_store(const char *service, const char *proto
|
||||
return err;
|
||||
}
|
||||
|
||||
static void mdns_start_if_needed(void) {
|
||||
if (g_mdns_running) return;
|
||||
esp_err_t err = mdns_init();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "mdns_init failed: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
if (g_hostname[0]) mdns_hostname_set(g_hostname);
|
||||
if (g_instance[0]) mdns_instance_name_set(g_instance);
|
||||
g_mdns_running = true;
|
||||
ESP_LOGI(TAG, "mDNS started (host=%s, inst=%s)", g_hostname, g_instance);
|
||||
}
|
||||
|
||||
static void mdns_stop_if_running(void) {
|
||||
if (!g_mdns_running) return;
|
||||
mdns_free();
|
||||
g_mdns_running = false;
|
||||
ESP_LOGI(TAG, "mDNS stopped");
|
||||
}
|
||||
|
||||
static void on_ip_event(void* arg, esp_event_base_t base, int32_t id, void* data) {
|
||||
if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
|
||||
mdns_start_if_needed();
|
||||
}
|
||||
}
|
||||
|
||||
static void on_wifi_event(void* arg, esp_event_base_t base, int32_t id, void* data) {
|
||||
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
mdns_stop_if_running();
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t net_mdns_autostart_init(const char* hostname, const char* instance_name) {
|
||||
if (hostname) {
|
||||
strncpy(g_hostname, hostname, sizeof(g_hostname)-1);
|
||||
g_hostname[sizeof(g_hostname)-1] = '\0';
|
||||
}
|
||||
if (instance_name) {
|
||||
strncpy(g_instance, instance_name, sizeof(g_instance)-1);
|
||||
g_instance[sizeof(g_instance)-1] = '\0';
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, on_ip_event, NULL, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, on_wifi_event, NULL, NULL));
|
||||
|
||||
// Falls IP schon vorhanden:
|
||||
esp_netif_t* sta = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
if (sta) {
|
||||
esp_netif_ip_info_t ip;
|
||||
if (esp_netif_get_ip_info(sta, &ip) == ESP_OK && ip.ip.addr != 0) {
|
||||
mdns_start_if_needed();
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool net_mdns_is_running(void) {
|
||||
return g_mdns_running;
|
||||
}
|
||||
|
||||
bool net_mdns_wait_started(uint32_t timeout_ms) {
|
||||
uint32_t waited = 0;
|
||||
const uint32_t step = 50;
|
||||
while (!g_mdns_running && waited < timeout_ms) {
|
||||
vTaskDelay(pdMS_TO_TICKS(step));
|
||||
waited += step;
|
||||
}
|
||||
return g_mdns_running;
|
||||
}
|
||||
|
||||
esp_err_t net_mdns_add_service(const char* service, const char* proto,
|
||||
uint16_t port, mdns_txt_item_t* txt, size_t txt_count) {
|
||||
if (!g_mdns_running) {
|
||||
mdns_start_if_needed();
|
||||
if (!g_mdns_running) return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
esp_err_t err = mdns_service_add(NULL, service, proto, port, txt, txt_count);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "mdns_service_add %s.%s:%u failed: %s", service, proto, port, esp_err_to_name(err));
|
||||
} else {
|
||||
ESP_LOGI(TAG, "mDNS service announced: %s.%s:%u", service, proto, port);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
bool net_mdns_find_service_url(const char* service, const char* proto,
|
||||
int enforce_port, const char* txt_key, const char* txt_value,
|
||||
bool net_mdns_find_service_url(const char *service, const char *proto,
|
||||
int enforce_port, const char *txt_key, const char *txt_value,
|
||||
uint32_t timeout_ms, int max_results,
|
||||
char* url_out, size_t url_out_size) {
|
||||
char *url_out, size_t url_out_size)
|
||||
{
|
||||
if (!service || !proto || !url_out || url_out_size < 16) return false;
|
||||
|
||||
if (!g_mdns_running) {
|
||||
ESP_LOGW(TAG, "mDNS not running yet; waiting...");
|
||||
if (!net_mdns_wait_started(3000)) {
|
||||
@@ -134,7 +351,7 @@ bool net_mdns_find_service_url(const char* service, const char* proto,
|
||||
}
|
||||
}
|
||||
|
||||
mdns_result_t* results = NULL;
|
||||
mdns_result_t *results = NULL;
|
||||
esp_err_t err = mdns_query_ptr(service, proto, timeout_ms, max_results, &results);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "mdns_query_ptr error: %s", esp_err_to_name(err));
|
||||
@@ -146,7 +363,9 @@ bool net_mdns_find_service_url(const char* service, const char* proto,
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
for (mdns_result_t* r = results; r && !found; r = r->next) {
|
||||
|
||||
for (mdns_result_t *r = results; r && !found; r = r->next) {
|
||||
// TXT filter
|
||||
bool txt_ok = true;
|
||||
if (txt_key && txt_value) {
|
||||
txt_ok = false;
|
||||
@@ -154,27 +373,105 @@ bool net_mdns_find_service_url(const char* service, const char* proto,
|
||||
if (r->txt[i].key && r->txt[i].value &&
|
||||
strcmp(r->txt[i].key, txt_key) == 0 &&
|
||||
strcmp(r->txt[i].value, txt_value) == 0) {
|
||||
txt_ok = true; break;
|
||||
txt_ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!txt_ok) continue;
|
||||
|
||||
if (r->instance_name && strncmp(r->instance_name, "myapp-", 5) != 0) {
|
||||
continue; // falsche Instanz, weitersuchen
|
||||
// Optional: Instance prefix filter (nur wenn konfiguriert)
|
||||
if (g_discover_cfg.require_instance_prefix && g_discover_cfg.instance_prefix[0]) {
|
||||
size_t pfx_len = strlen(g_discover_cfg.instance_prefix);
|
||||
if (!r->instance_name || strncmp(r->instance_name, g_discover_cfg.instance_prefix, pfx_len) != 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
for (mdns_ip_addr_t* a = r->addr; a && !found; a = a->next) {
|
||||
for (mdns_ip_addr_t *a = r->addr; a && !found; a = a->next) {
|
||||
if (a->addr.type != ESP_IPADDR_TYPE_V4) continue;
|
||||
|
||||
uint16_t port = r->port ? r->port : 80;
|
||||
if (enforce_port > 0 && port != (uint16_t)enforce_port) port = (uint16_t)enforce_port;
|
||||
snprintf(url_out, url_out_size, "http://" IPSTR ":%u", IP2STR(&a->addr.u_addr.ip4), (unsigned)port);
|
||||
|
||||
snprintf(url_out, url_out_size, "http://" IPSTR ":%u",
|
||||
IP2STR(&a->addr.u_addr.ip4), (unsigned)port);
|
||||
|
||||
ESP_LOGI(TAG, "mDNS URL: %s", url_out);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
mdns_query_results_free(results);
|
||||
return found;
|
||||
}
|
||||
|
||||
void net_mdns_stop(void) { mdns_stop_if_running(); }
|
||||
void net_mdns_stop(void)
|
||||
{
|
||||
// Discovery stoppen (soft)
|
||||
g_discover_enabled = false;
|
||||
|
||||
// mDNS stoppen
|
||||
mdns_stop_if_running();
|
||||
set_mdns_ok(false);
|
||||
}
|
||||
|
||||
// -------------------- New Public: Discovery Retry --------------------
|
||||
esp_err_t net_mdns_autodiscover_start(const net_mdns_discover_cfg_t *cfg)
|
||||
{
|
||||
if (!cfg || !cfg->service || !cfg->proto) return ESP_ERR_INVALID_ARG;
|
||||
|
||||
// config übernehmen (interne Kopie)
|
||||
memset(&g_discover_cfg, 0, sizeof(g_discover_cfg));
|
||||
|
||||
strlcpy(g_discover_cfg.service, cfg->service, sizeof(g_discover_cfg.service));
|
||||
strlcpy(g_discover_cfg.proto, cfg->proto, sizeof(g_discover_cfg.proto));
|
||||
g_discover_cfg.enforce_port = cfg->enforce_port;
|
||||
|
||||
if (cfg->fallback_service && cfg->fallback_proto) {
|
||||
strlcpy(g_discover_cfg.fallback_service, cfg->fallback_service, sizeof(g_discover_cfg.fallback_service));
|
||||
strlcpy(g_discover_cfg.fallback_proto, cfg->fallback_proto, sizeof(g_discover_cfg.fallback_proto));
|
||||
g_discover_cfg.fallback_port = cfg->fallback_port;
|
||||
}
|
||||
|
||||
if (cfg->txt_key && cfg->txt_value) {
|
||||
strlcpy(g_discover_cfg.txt_key, cfg->txt_key, sizeof(g_discover_cfg.txt_key));
|
||||
strlcpy(g_discover_cfg.txt_value, cfg->txt_value, sizeof(g_discover_cfg.txt_value));
|
||||
}
|
||||
|
||||
g_discover_cfg.query_timeout_ms = (cfg->query_timeout_ms ? cfg->query_timeout_ms : 2000);
|
||||
g_discover_cfg.retry_interval_ms = (cfg->retry_interval_ms ? cfg->retry_interval_ms : 60000);
|
||||
g_discover_cfg.max_results = (cfg->max_results ? cfg->max_results : 5);
|
||||
|
||||
if (cfg->instance_prefix && cfg->instance_prefix[0]) {
|
||||
strlcpy(g_discover_cfg.instance_prefix, cfg->instance_prefix, sizeof(g_discover_cfg.instance_prefix));
|
||||
g_discover_cfg.require_instance_prefix = cfg->require_instance_prefix;
|
||||
} else {
|
||||
g_discover_cfg.require_instance_prefix = false;
|
||||
}
|
||||
|
||||
// enable
|
||||
g_discover_enabled = true;
|
||||
set_mdns_ok(false);
|
||||
|
||||
// Wenn wir schon IP haben: Task sofort starten, sonst wird er bei GOT_IP gestartet
|
||||
if (have_sta_ip()) {
|
||||
mdns_start_if_needed();
|
||||
if (g_discover_task == NULL) {
|
||||
BaseType_t ok = xTaskCreate(mdns_discover_task, "mdns_discover", 3072, NULL, 4, &g_discover_task);
|
||||
if (ok != pdPASS) {
|
||||
g_discover_task = NULL;
|
||||
ESP_LOGE(TAG, "Failed to create mdns_discover task");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void net_mdns_autodiscover_stop(void)
|
||||
{
|
||||
g_discover_enabled = false;
|
||||
// Task beendet sich selbst (kein hartes Kill)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user