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:
Vendored
+10
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||||
|
// for the documentation about the extensions.json format
|
||||||
|
"recommendations": [
|
||||||
|
"platformio.platformio-ide"
|
||||||
|
],
|
||||||
|
"unwantedRecommendations": [
|
||||||
|
"ms-vscode.cpptools-extension-pack"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,25 +1,32 @@
|
|||||||
// File: include/net_mdns.h
|
// net_mdns.h (Ergänzungen)
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stdint.h>
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
#include "mdns.h"
|
#include "mdns.h"
|
||||||
|
|
||||||
|
// bestehende APIs bleiben...
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *service; // z.B. "_myapp"
|
||||||
|
const char *proto; // z.B. "_tcp"
|
||||||
|
int enforce_port; // -1 oder 0 = egal
|
||||||
|
|
||||||
|
const char *fallback_service; // z.B. "_http"
|
||||||
|
const char *fallback_proto; // z.B. "_tcp"
|
||||||
|
int fallback_port; // z.B. 8086
|
||||||
|
|
||||||
|
const char *txt_key; // optional
|
||||||
|
const char *txt_value; // optional
|
||||||
|
|
||||||
|
uint32_t query_timeout_ms; // z.B. 2000
|
||||||
|
uint32_t retry_interval_ms; // z.B. 60000
|
||||||
|
int max_results; // z.B. 5
|
||||||
|
|
||||||
|
const char *instance_prefix; // z.B. "myapp-"
|
||||||
|
bool require_instance_prefix; // true/false
|
||||||
|
} net_mdns_discover_cfg_t;
|
||||||
|
|
||||||
|
esp_err_t net_mdns_autodiscover_start(const net_mdns_discover_cfg_t *cfg);
|
||||||
|
void net_mdns_autodiscover_stop(void);
|
||||||
esp_err_t net_mdns_autostart_init(const char* hostname, const char* instance_name);
|
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);
|
|
||||||
|
|||||||
@@ -1,19 +1,317 @@
|
|||||||
|
// File: firmware/components/net_mdns/src/net_mdns.c
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_err.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_netif.h"
|
#include "esp_netif.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
|
||||||
#include "mdns.h"
|
#include "mdns.h"
|
||||||
#include "net_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_hostname[33] = {0};
|
||||||
static char g_instance[33] = {0};
|
static char g_instance[33] = {0};
|
||||||
|
|
||||||
static volatile bool g_mdns_running = false;
|
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,
|
esp_err_t net_mdns_autodiscover_and_store(const char *service, const char *proto,
|
||||||
int enforce_port,
|
int enforce_port,
|
||||||
@@ -21,8 +319,10 @@ esp_err_t net_mdns_autodiscover_and_store(const char *service, const char *proto
|
|||||||
uint32_t timeout_ms)
|
uint32_t timeout_ms)
|
||||||
{
|
{
|
||||||
char url[96] = {0};
|
char url[96] = {0};
|
||||||
|
|
||||||
bool ok = net_mdns_find_service_url(service, proto, enforce_port,
|
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));
|
url, sizeof(url));
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
ESP_LOGW(TAG, "Auto-Discover: kein Treffer für %s.%s", service, proto);
|
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;
|
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,
|
bool net_mdns_find_service_url(const char *service, const char *proto,
|
||||||
int enforce_port, const char *txt_key, const char *txt_value,
|
int enforce_port, const char *txt_key, const char *txt_value,
|
||||||
uint32_t timeout_ms, int max_results,
|
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 (!service || !proto || !url_out || url_out_size < 16) return false;
|
||||||
|
|
||||||
if (!g_mdns_running) {
|
if (!g_mdns_running) {
|
||||||
ESP_LOGW(TAG, "mDNS not running yet; waiting...");
|
ESP_LOGW(TAG, "mDNS not running yet; waiting...");
|
||||||
if (!net_mdns_wait_started(3000)) {
|
if (!net_mdns_wait_started(3000)) {
|
||||||
@@ -146,7 +363,9 @@ bool net_mdns_find_service_url(const char* service, const char* proto,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool found = false;
|
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;
|
bool txt_ok = true;
|
||||||
if (txt_key && txt_value) {
|
if (txt_key && txt_value) {
|
||||||
txt_ok = false;
|
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 &&
|
if (r->txt[i].key && r->txt[i].value &&
|
||||||
strcmp(r->txt[i].key, txt_key) == 0 &&
|
strcmp(r->txt[i].key, txt_key) == 0 &&
|
||||||
strcmp(r->txt[i].value, txt_value) == 0) {
|
strcmp(r->txt[i].value, txt_value) == 0) {
|
||||||
txt_ok = true; break;
|
txt_ok = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!txt_ok) continue;
|
if (!txt_ok) continue;
|
||||||
|
|
||||||
if (r->instance_name && strncmp(r->instance_name, "myapp-", 5) != 0) {
|
// Optional: Instance prefix filter (nur wenn konfiguriert)
|
||||||
continue; // falsche Instanz, weitersuchen
|
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;
|
if (a->addr.type != ESP_IPADDR_TYPE_V4) continue;
|
||||||
|
|
||||||
uint16_t port = r->port ? r->port : 80;
|
uint16_t port = r->port ? r->port : 80;
|
||||||
if (enforce_port > 0 && port != (uint16_t)enforce_port) port = (uint16_t)enforce_port;
|
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);
|
ESP_LOGI(TAG, "mDNS URL: %s", url_out);
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mdns_query_results_free(results);
|
mdns_query_results_free(results);
|
||||||
return found;
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// File: firmware/components/sys_status/include/sys_status.h
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Field: firmware/components/sys_status/src/sys_status.c
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/event_groups.h"
|
#include "freertos/event_groups.h"
|
||||||
#include "esp_check.h"
|
#include "esp_check.h"
|
||||||
|
|||||||
+54
-1
@@ -45,6 +45,10 @@
|
|||||||
#include "dns_captive.h"
|
#include "dns_captive.h"
|
||||||
#include "http_portal.h"
|
#include "http_portal.h"
|
||||||
|
|
||||||
|
#include "net_mdns.h"
|
||||||
|
|
||||||
|
#include "esp_timer.h"
|
||||||
|
|
||||||
// --- Callback-Implementierungen für das Portal ---
|
// --- Callback-Implementierungen für das Portal ---
|
||||||
|
|
||||||
static bool app_load_sta_creds(char **ssid, char **pass)
|
static bool app_load_sta_creds(char **ssid, char **pass)
|
||||||
@@ -150,6 +154,26 @@ static lv_indev_t *lvgl_touch_indev = NULL;
|
|||||||
/* --- Button Handle global halten, falls benötigt --- */
|
/* --- Button Handle global halten, falls benötigt --- */
|
||||||
static button_handle_t g_btn = NULL;
|
static button_handle_t g_btn = NULL;
|
||||||
|
|
||||||
|
static void app_update_mdns_influx_status(void)
|
||||||
|
{
|
||||||
|
char *url = NULL;
|
||||||
|
|
||||||
|
// Ich kenne deine exakte storage-API nicht – bitte eine vorhandene Funktion nutzen:
|
||||||
|
// Beispiele: storage_load_service_url(&url) oder storage_get_service_url(&url)
|
||||||
|
// -> Nimm die, die bei dir existiert.
|
||||||
|
bool ok = storage_load_service_url(&url); // <- ggf. Namen anpassen
|
||||||
|
|
||||||
|
if (ok && url && url[0]) {
|
||||||
|
sys_status_set_mdns_ok(true);
|
||||||
|
sys_status_set_influx_ok(true);
|
||||||
|
} else {
|
||||||
|
sys_status_set_mdns_ok(false);
|
||||||
|
sys_status_set_influx_ok(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url) free(url);
|
||||||
|
}
|
||||||
|
|
||||||
/* Vorwärtsdeklarationen */
|
/* Vorwärtsdeklarationen */
|
||||||
void lv_fs_fatfs_init(void);
|
void lv_fs_fatfs_init(void);
|
||||||
static esp_err_t app_lvgl_init(void);
|
static esp_err_t app_lvgl_init(void);
|
||||||
@@ -223,6 +247,9 @@ static esp_err_t app_lvgl_init(void)
|
|||||||
|
|
||||||
static void ui_tick_cb(lv_timer_t *t) {
|
static void ui_tick_cb(lv_timer_t *t) {
|
||||||
(void)t;
|
(void)t;
|
||||||
|
|
||||||
|
app_update_mdns_influx_status(); // <<< NEU
|
||||||
|
|
||||||
if (lvgl_port_lock(0)) {
|
if (lvgl_port_lock(0)) {
|
||||||
ui_update_status_icons();
|
ui_update_status_icons();
|
||||||
tick_screen(0);
|
tick_screen(0);
|
||||||
@@ -335,6 +362,7 @@ static void button_init(void)
|
|||||||
/* ==================== main ==================== */
|
/* ==================== main ==================== */
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
esp_log_level_set("storage", ESP_LOG_NONE);
|
esp_log_level_set("storage", ESP_LOG_NONE);
|
||||||
|
|
||||||
esp_log_level_set("*", ESP_LOG_NONE);
|
esp_log_level_set("*", ESP_LOG_NONE);
|
||||||
@@ -343,7 +371,7 @@ void app_main(void)
|
|||||||
sys_status_set_wifi_ok(false);
|
sys_status_set_wifi_ok(false);
|
||||||
sys_status_set_influx_ok(false);
|
sys_status_set_influx_ok(false);
|
||||||
sys_status_set_mdns_ok(false);
|
sys_status_set_mdns_ok(false);
|
||||||
|
*/
|
||||||
// NVS zuerst
|
// NVS zuerst
|
||||||
esp_err_t ret = nvs_flash_init();
|
esp_err_t ret = nvs_flash_init();
|
||||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
@@ -393,6 +421,31 @@ void app_main(void)
|
|||||||
// WiFi AP+STA starten
|
// WiFi AP+STA starten
|
||||||
wifi_mgr_start();
|
wifi_mgr_start();
|
||||||
|
|
||||||
|
// --- mDNS: Autostart (startet bei STA_GOT_IP automatisch) ---
|
||||||
|
ESP_ERROR_CHECK(net_mdns_autostart_init(NULL, NULL));
|
||||||
|
|
||||||
|
// --- mDNS: Service-Discovery mit Retry (ersetzt mdns_discover_task in wifi_mgr) ---
|
||||||
|
net_mdns_discover_cfg_t mdns_cfg = {
|
||||||
|
.service = "_influxdb2",
|
||||||
|
.proto = "_tcp",
|
||||||
|
.enforce_port = 8086,
|
||||||
|
|
||||||
|
.fallback_service = "_http",
|
||||||
|
.fallback_proto = "_tcp",
|
||||||
|
.fallback_port = 8086,
|
||||||
|
|
||||||
|
.txt_key = NULL,
|
||||||
|
.txt_value = NULL,
|
||||||
|
|
||||||
|
.query_timeout_ms = 2000,
|
||||||
|
.retry_interval_ms = 60000,
|
||||||
|
.max_results = 5,
|
||||||
|
|
||||||
|
.instance_prefix = NULL,
|
||||||
|
.require_instance_prefix = false
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(net_mdns_autodiscover_start(&mdns_cfg));
|
||||||
|
|
||||||
// AP-Netif an DNS weitergeben
|
// AP-Netif an DNS weitergeben
|
||||||
dns_captive_set_ap_netif(wifi_mgr_get_ap_netif());
|
dns_captive_set_ap_netif(wifi_mgr_get_ap_netif());
|
||||||
// Captive-DNS starten
|
// Captive-DNS starten
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
// File: src/net_influx.c
|
// File: src/net_influx.c
|
||||||
#include "storage.h"
|
|
||||||
#include "net_influx.h"
|
|
||||||
#include "esp_http_client.h"
|
#include "esp_http_client.h"
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
@@ -10,6 +8,8 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "sys_status.h"
|
#include "sys_status.h"
|
||||||
|
#include "storage.h"
|
||||||
|
#include "net_influx.h"
|
||||||
|
|
||||||
static const char* TAG = "net_influx";
|
static const char* TAG = "net_influx";
|
||||||
|
|
||||||
|
|||||||
@@ -232,8 +232,6 @@ static void wifi_event_handler(void* arg, esp_event_base_t base, int32_t id, voi
|
|||||||
ESP_LOGI(TAG, "STA got IP: " IPSTR, IP2STR(&e->ip_info.ip));
|
ESP_LOGI(TAG, "STA got IP: " IPSTR, IP2STR(&e->ip_info.ip));
|
||||||
xEventGroupSetBits(wifi_events, EVT_STA_GOTIP);
|
xEventGroupSetBits(wifi_events, EVT_STA_GOTIP);
|
||||||
|
|
||||||
xTaskCreate(mdns_discover_task, "mdns_discover", 3072, NULL, 4, NULL);
|
|
||||||
|
|
||||||
if (ap_off_timer) {
|
if (ap_off_timer) {
|
||||||
if (esp_timer_is_active(ap_off_timer)) esp_timer_stop(ap_off_timer);
|
if (esp_timer_is_active(ap_off_timer)) esp_timer_stop(ap_off_timer);
|
||||||
ESP_ERROR_CHECK(esp_timer_start_once(ap_off_timer, 10 * 1000 * 1000)); // 10 s
|
ESP_ERROR_CHECK(esp_timer_start_once(ap_off_timer, 10 * 1000 * 1000)); // 10 s
|
||||||
@@ -353,31 +351,3 @@ static void ap_set_dhcp_dns_to_self(void) {
|
|||||||
&ip.ip, sizeof(ip.ip));
|
&ip.ip, sizeof(ip.ip));
|
||||||
esp_netif_dhcps_start(netif_ap);
|
esp_netif_dhcps_start(netif_ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Task für mDNS-Discover ---
|
|
||||||
static void mdns_discover_task(void *arg) {
|
|
||||||
// Start/ensure mDNS läuft
|
|
||||||
(void)net_mdns_autostart_init(NULL, NULL);
|
|
||||||
|
|
||||||
const TickType_t kDelay = pdMS_TO_TICKS(60000); // 60 s
|
|
||||||
for (;;) {
|
|
||||||
// Erst offizieller Service, dann Fallback auf HTTP:8086
|
|
||||||
esp_err_t err = net_mdns_autodiscover_and_store("_myapp", "_tcp", -1,
|
|
||||||
NULL, NULL, 2000);
|
|
||||||
if (err != ESP_OK) {
|
|
||||||
err = net_mdns_autodiscover_and_store("_http", "_tcp", 8086,
|
|
||||||
NULL, NULL, 2000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (err == ESP_OK) {
|
|
||||||
sys_status_set_mdns_ok(true);
|
|
||||||
ESP_LOGI(TAG, "mDNS: Service gefunden und gespeichert. Beende Re-Try-Task.");
|
|
||||||
vTaskDelete(NULL); // fertig, keine weiteren Versuche
|
|
||||||
} else {
|
|
||||||
sys_status_set_mdns_ok(false);
|
|
||||||
ESP_LOGW(TAG, "mDNS: nichts gefunden – neuer Versuch in 60 s");
|
|
||||||
vTaskDelay(kDelay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user