Refactoring: Module entkoppelt und Aufrufe über main zentralisiert

wifi_mgr vollständig von http_portal und dns_captive entkoppelt
- Neues Callback-Interface in http_portal implementiert
- Callback-Initialisierung in main.c hinzugefügt (WiFi-Creds, Device-Info)
- dns_captive in eigenes Modul ausgelagert (dns_captive.h ergänzt)
- Alte Vorwärtsdeklarationen entfernt und öffentliche API in wifi_mgr.h ergänzt
- Startreihenfolge der Module neu strukturiert (Orchestrierung in main.c)
- Projektabhängigkeiten vereinfacht und Zyklus zwischen Modulen aufgelöst
This commit is contained in:
2025-12-10 22:14:55 +01:00
parent 6225906851
commit b7f6325dea
7 changed files with 123 additions and 60 deletions
-10
View File
@@ -1,10 +0,0 @@
{
// 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
View File
@@ -1,3 +1,4 @@
// File: dns_captive.c
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
+15 -11
View File
@@ -14,9 +14,7 @@
#include "http_portal.h"
#include "storage.h"
// Diese beiden werden vom WiFi-Manager bereitgestellt (in wifi_mgr.c definiert)
bool wifi_mgr_load_sta_creds(char **out_ssid, char **out_pass);
void wifi_mgr_save_sta_creds_and_reconnect(const char* ssid, const char* pass);
static portal_http_callbacks_t s_cb;
static void __attribute__((unused)) log_one_hdr(httpd_req_t *req, const char* key) {
size_t len = httpd_req_get_hdr_value_len(req, key);
@@ -34,9 +32,6 @@ static void __attribute__((unused)) log_one_hdr(httpd_req_t *req, const char* ke
}
}
// Geräteeigenschaften speichern (in wifi_mgr.c implementieren)
void wifi_mgr_save_device_info(const char* devicename, const char* token, const char* bucket, const char* org);
static const char *TAG = "portal_http";
static httpd_handle_t s_http = NULL;
@@ -460,16 +455,21 @@ static esp_err_t save_post(httpd_req_t *req) {
log_resp_basic("save_post", "200 OK", "text/html; charset=utf-8", (int)strlen(saved_html));
httpd_resp_send(req, saved_html, HTTPD_RESP_USE_STRLEN);
// Geräteeigenschaften speichern
wifi_mgr_save_device_info(
// Geräteeigenschaften speichern (über Callback)
if (s_cb.save_device_info) {
s_cb.save_device_info(
dev,
(token && token[0]) ? token : NULL, // NULL = nicht geändert
(bucket && bucket[0]) ? bucket : NULL,
(org && org[0]) ? org : NULL
);
}
// STA-Creds speichern & reconnect (über Callback)
if (s_cb.save_sta_creds_and_reconnect) {
s_cb.save_sta_creds_and_reconnect(ssid, pass);
}
// STA-Creds speichern & reconnect
wifi_mgr_save_sta_creds_and_reconnect(ssid, pass);
s_force_captive = false; // Captive beenden; OS darf 204 sehen
// Cleanup (Erfolgsweg)
@@ -653,8 +653,12 @@ static httpd_handle_t start_http(void) {
esp_err_t portal_http_start(EventGroupHandle_t wifi_events,
EventBits_t bit_gotip,
EventBits_t bit_fail,
const char* ap_ssid)
const char* ap_ssid,
const portal_http_callbacks_t *cb)
{
if (s_http) return ESP_OK;
if (!cb) return ESP_ERR_INVALID_ARG;
s_cb = *cb;
if (s_http) return ESP_OK;
s_wifi_events = wifi_events;
s_BIT_GOTIP = bit_gotip;
+17 -8
View File
@@ -1,18 +1,27 @@
// http_portal.h
#pragma once
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_err.h"
#include "esp_http_server.h"
// Startet den HTTP-Captive-Portal Server.
// - wifi_events: EventGroup, die vom WiFi-Manager gepflegt wird
// - bit_gotip: Bitmaske für "STA hat IP"
// - bit_fail: Bitmaske für "STA Verbindung fehlgeschlagen"
// - ap_ssid: SSID des SoftAP (für Anzeige im HTML)
// Callbacks, die das Portal nach außen benutzt
typedef struct {
bool (*load_sta_creds)(char **ssid, char **pass); // optional, wenn du es später brauchst
void (*save_sta_creds_and_reconnect)(const char *ssid, const char *pass);
void (*save_device_info)(
const char *devicename,
const char *token,
const char *bucket,
const char *org
);
} portal_http_callbacks_t;
// HTTP-Portal starten / stoppen
esp_err_t portal_http_start(EventGroupHandle_t wifi_events,
EventBits_t bit_gotip,
EventBits_t bit_fail,
const char* ap_ssid);
const char* ap_ssid,
const portal_http_callbacks_t *cb);
// Stoppt den HTTP-Server (optional)
void portal_http_stop(void);
+57 -15
View File
@@ -5,7 +5,6 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "wifi_mgr.h"
#include "sen5x_app.h"
#include "storage.h"
@@ -42,7 +41,32 @@
#include "images.h"
#include "screens.h" // <-- Zugriff auf objects.* und tick_screen()
// Entfernt: extern lv_obj_t *ui_wifi/influx/mdns;
#include "wifi_mgr.h"
#include "dns_captive.h"
#include "http_portal.h"
// --- Callback-Implementierungen für das Portal ---
static bool app_load_sta_creds(char **ssid, char **pass)
{
// Wenn du das Portal später mal /scan oder Default-SSID aus NVS ziehen lassen willst
return storage_load_wifi_creds(ssid, pass);
}
static void app_save_sta_creds_and_reconnect(const char *ssid, const char *pass)
{
wifi_mgr_save_sta_creds_and_reconnect(ssid, pass);
}
static void app_save_device_info(
const char *devicename,
const char *token,
const char *bucket,
const char *org)
{
storage_save_device_name(devicename);
storage_save_influx_softap(org, bucket, token);
}
// ---------- Status-Icons updaten: nutzt die in screens.c erzeugten Objekte ----------
static void ui_update_status_icons(void)
@@ -308,14 +332,10 @@ static void button_init(void)
// Beim Loslassen nach langem Drücken -> wirklich resetten
iot_button_register_cb(g_btn, BUTTON_LONG_PRESS_UP, NULL, reset_button_cb, NULL);
}
/* ==================== app_main (unverändert, plus Button-Reset) ==================== */
/* ==================== main ==================== */
void app_main(void)
{
/*
esp_log_level_set("storage", ESP_LOG_NONE);
*/
esp_log_level_set("*", ESP_LOG_NONE);
@@ -324,7 +344,7 @@ void app_main(void)
sys_status_set_influx_ok(false);
sys_status_set_mdns_ok(false);
// 0) NVS zuerst
// NVS zuerst
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
@@ -332,21 +352,21 @@ void app_main(void)
}
ESP_ERROR_CHECK(ret);
// --- Storage/WiFi wie gehabt ---
// --- Storage
ESP_ERROR_CHECK(storage_init());
storage_debug_dump();
// WiFi Manager initialisieren
wifi_mgr_init();
wifi_mgr_start();
ESP_ERROR_CHECK(i2cdev_init());
// 1) I2C-Bus initialisieren (bsp_i2c.h)
// I2C-Bus initialisieren (bsp_i2c.h)
i2c_master_bus_handle_t i2c_bus_handle = bsp_i2c_init();
// 2) Diesen Bus von i2cdev "adoptieren", damit SEN5x denselben Bus nutzt
// Diesen Bus von i2cdev "adoptieren", damit SEN5x denselben Bus nutzt
ESP_ERROR_CHECK(i2cdev_adopt_bus(0, i2c_bus_handle, 18, 19)); // Port 0, SDA=18, SCL=19
// 3) Restliche Peripherie
// Restliche Peripherie
bsp_spi_init();
bsp_battery_init();
// bsp_qmi8658_init(i2c_bus_handle);
@@ -370,7 +390,29 @@ void app_main(void)
lv_timer_t *ui_timer = lv_timer_create(ui_tick_cb, 1000, NULL);
(void)ui_timer;
// 4) SEN5x
// WiFi AP+STA starten
wifi_mgr_start();
// AP-Netif an DNS weitergeben
dns_captive_set_ap_netif(wifi_mgr_get_ap_netif());
// Captive-DNS starten
ESP_ERROR_CHECK(dns_captive_start());
portal_http_callbacks_t cb = {
.load_sta_creds = app_load_sta_creds,
.save_sta_creds_and_reconnect= app_save_sta_creds_and_reconnect,
.save_device_info = app_save_device_info
};
ESP_ERROR_CHECK(portal_http_start(
wifi_mgr_get_event_group(),
wifi_mgr_evt_sta_got_ip(),
wifi_mgr_evt_sta_fail(),
"nanoPSX",
&cb
));
// SEN5x
const sen5x_i2c_cfg_t cfg = {
.addr = 0x69,
.port = 0,
@@ -382,7 +424,7 @@ void app_main(void)
};
ESP_ERROR_CHECK(sen5x_app_init(&cfg));
// 5) Task starten
// Task starten
xTaskCreate(sen5x_app_task, "sen5x_task", 4096, NULL, 5, NULL);
for (;;) vTaskDelay(pdMS_TO_TICKS(1000));
+15 -11
View File
@@ -23,9 +23,7 @@
#include "net_mdns.h"
#include "sys_status.h"
// Captive-DNS ist in http_portal.c implementiert:
void dns_captive_start(void);
void dns_captive_stop(void);
#include "dns_captive.h"
// ---------- Konfig ----------
#define SOFTAP_SSID "nanoPSX"
@@ -304,8 +302,6 @@ static void wifi_start_ap_sta(void) {
// ---------- Öffentliche API ----------
void wifi_mgr_init(void) {
// NVS (neu über storage)
ESP_ERROR_CHECK(storage_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
@@ -326,16 +322,24 @@ void wifi_mgr_init(void) {
}
void wifi_mgr_start(void) {
// AP+STA hochfahren
wifi_start_ap_sta();
ESP_LOGI(TAG, "WiFi AP+STA gestartet.");
}
// HTTP-Portal starten (erzwingt Captive bis Save, s. http_portal.c)
ESP_ERROR_CHECK(portal_http_start(wifi_events, EVT_STA_GOTIP, EVT_STA_FAIL, SOFTAP_SSID));
EventGroupHandle_t wifi_mgr_get_event_group(void) {
return wifi_events;
}
// DNS/Captive starten
(void)dns_captive_start();
EventBits_t wifi_mgr_evt_sta_got_ip(void) {
return EVT_STA_GOTIP;
}
ESP_LOGI(TAG, "Bereit. Verbinde dich mit '%s' und öffne beliebige Seite – das Portal erscheint.", SOFTAP_SSID);
EventBits_t wifi_mgr_evt_sta_fail(void) {
return EVT_STA_FAIL;
}
esp_netif_t *wifi_mgr_get_ap_netif(void) {
return netif_ap;
}
// ---------- Hilfsfunktionen ----------
+13
View File
@@ -10,6 +10,19 @@ void wifi_mgr_init(void);
// Startet AP+STA, HTTP-Server und DNS-Server, und stößt ggf. eine STA-Verbindung an.
void wifi_mgr_start(void);
// vorhandene Funktionen, die du schon hattest
bool wifi_mgr_load_sta_creds(char **out_ssid, char **out_pass);
void wifi_mgr_save_sta_creds_and_reconnect(const char* ssid, const char* pass);
void wifi_mgr_save_device_info(const char* devicename,
const char* token,
const char* bucket,
const char* org);
// NEU: Getter für main.c
EventGroupHandle_t wifi_mgr_get_event_group(void);
EventBits_t wifi_mgr_evt_sta_got_ip(void);
EventBits_t wifi_mgr_evt_sta_fail(void);
esp_netif_t *wifi_mgr_get_ap_netif(void);
#ifdef __cplusplus
}