Refactor: net_mdns in separate component ausgelagert
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"src/net_mdns.c"
|
||||
INCLUDE_DIRS
|
||||
"include"
|
||||
REQUIRES
|
||||
mdns
|
||||
esp_netif
|
||||
esp_wifi
|
||||
storage
|
||||
)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// File: include/net_mdns.h
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "esp_err.h"
|
||||
#include "mdns.h"
|
||||
|
||||
esp_err_t net_mdns_autostart_init(const char* hostname, const char* instance_name);
|
||||
bool net_mdns_is_running(void);
|
||||
bool net_mdns_wait_started(uint32_t timeout_ms);
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
void net_mdns_stop(void);
|
||||
|
||||
// --- NEU: Auto-Discover + NVS persistieren ---
|
||||
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);
|
||||
@@ -0,0 +1,180 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "esp_log.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"
|
||||
|
||||
static const char* TAG = "net_mdns";
|
||||
static char g_hostname[33] = {0};
|
||||
static char g_instance[33] = {0};
|
||||
static volatile bool g_mdns_running = false;
|
||||
|
||||
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)
|
||||
{
|
||||
char url[96] = {0};
|
||||
bool ok = net_mdns_find_service_url(service, proto, enforce_port,
|
||||
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);
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
esp_err_t err = storage_save_service_url(url);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Auto-Discover: URL '%s' in NVS gespeichert.", url);
|
||||
}
|
||||
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,
|
||||
uint32_t timeout_ms, int max_results,
|
||||
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)) {
|
||||
ESP_LOGW(TAG, "mDNS still not running; abort query");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
return false;
|
||||
}
|
||||
if (!results) {
|
||||
ESP_LOGI(TAG, "mDNS query OK but no results for %s.%s", service, proto);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
for (mdns_result_t* r = results; r && !found; r = r->next) {
|
||||
bool txt_ok = true;
|
||||
if (txt_key && txt_value) {
|
||||
txt_ok = false;
|
||||
for (int i = 0; i < r->txt_count; ++i) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!txt_ok) continue;
|
||||
|
||||
if (r->instance_name && strncmp(r->instance_name, "myapp-", 5) != 0) {
|
||||
continue; // falsche Instanz, weitersuchen
|
||||
}
|
||||
|
||||
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);
|
||||
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(); }
|
||||
Reference in New Issue
Block a user