Gitea URL
All checks were successful
ESP32 Build & Release / build (push) Successful in 6m37s

This commit is contained in:
2026-09-12 14:35:09 +02:00
parent 3708674bbb
commit c272776d47
4 changed files with 162 additions and 127 deletions

View File

@@ -29,7 +29,7 @@ static EventGroupHandle_t s_wifi_group = NULL;
static int s_retry_count = 0;
static char s_sta_ip[20] = "No Wi-Fi";
static char s_active_ssid[WIFI_SSID_MAX_LEN + 1] = "Disconnected";
static char s_active_ssid[WIFI_SSID_MAX_LEN + 1] = {0};
static SemaphoreHandle_t s_wifi_mux = NULL;
static SemaphoreHandle_t s_net_lock = NULL;
@@ -62,10 +62,8 @@ int wifi_manager_get_saved_profiles(wifi_profile_t *out_profiles, int max_count)
size_t s_len = sizeof(out_profiles[loaded].ssid);
size_t p_len = sizeof(out_profiles[loaded].password);
if (nvs_get_str(h, key_s, out_profiles[loaded].ssid, &s_len) == ESP_OK) {
if (nvs_get_str(h, key_p, out_profiles[loaded].password, &p_len) != ESP_OK) {
out_profiles[loaded].password[0] = '\0';
}
if (nvs_get_str(h, key_s, out_profiles[loaded].ssid, &s_len) == ESP_OK &&
nvs_get_str(h, key_p, out_profiles[loaded].password, &p_len) == ESP_OK) {
loaded++;
}
}
@@ -76,43 +74,40 @@ int wifi_manager_get_saved_profiles(wifi_profile_t *out_profiles, int max_count)
esp_err_t wifi_manager_save_profile(const char *ssid, const char *password)
{
if (ssid == NULL || strlen(ssid) == 0) {
return ESP_ERR_INVALID_ARG;
}
if (ssid == NULL || strlen(ssid) == 0) return ESP_ERR_INVALID_ARG;
if (password == NULL) password = "";
wifi_profile_t profiles[WIFI_MAX_PROFILES];
int count = wifi_manager_get_saved_profiles(profiles, WIFI_MAX_PROFILES);
/* Check if already exists - update password if so */
int target_idx = -1;
for (int i = 0; i < count; i++) {
if (strcmp(profiles[i].ssid, ssid) == 0) {
target_idx = i;
break;
nvs_handle_t h;
esp_err_t err = nvs_open(NVS_WIFI_NS, NVS_READWRITE, &h);
if (err != ESP_OK) return err;
char key_p[16];
snprintf(key_p, sizeof(key_p), "p_%d", i);
nvs_set_str(h, key_p, password);
err = nvs_commit(h);
nvs_close(h);
ESP_LOGI(TAG, "Updated password for saved profile '%s'", ssid);
return err;
}
}
if (target_idx >= 0) {
strncpy(profiles[target_idx].password, password ? password : "", sizeof(profiles[target_idx].password) - 1);
profiles[target_idx].password[sizeof(profiles[target_idx].password) - 1] = '\0';
} else {
if (count < WIFI_MAX_PROFILES) {
target_idx = count;
count++;
} else {
/* Replace oldest (index 0), shift left */
for (int i = 0; i < WIFI_MAX_PROFILES - 1; i++) {
profiles[i] = profiles[i + 1];
}
target_idx = WIFI_MAX_PROFILES - 1;
if (count >= WIFI_MAX_PROFILES) {
for (int i = 0; i < WIFI_MAX_PROFILES - 1; i++) {
profiles[i] = profiles[i + 1];
}
strncpy(profiles[target_idx].ssid, ssid, sizeof(profiles[target_idx].ssid) - 1);
profiles[target_idx].ssid[sizeof(profiles[target_idx].ssid) - 1] = '\0';
strncpy(profiles[target_idx].password, password ? password : "", sizeof(profiles[target_idx].password) - 1);
profiles[target_idx].password[sizeof(profiles[target_idx].password) - 1] = '\0';
count = WIFI_MAX_PROFILES - 1;
}
/* Write back to NVS */
strncpy(profiles[count].ssid, ssid, sizeof(profiles[count].ssid) - 1);
profiles[count].ssid[sizeof(profiles[count].ssid) - 1] = '\0';
strncpy(profiles[count].password, password, sizeof(profiles[count].password) - 1);
profiles[count].password[sizeof(profiles[count].password) - 1] = '\0';
count++;
nvs_handle_t h;
esp_err_t err = nvs_open(NVS_WIFI_NS, NVS_READWRITE, &h);
if (err != ESP_OK) return err;
@@ -128,7 +123,7 @@ esp_err_t wifi_manager_save_profile(const char *ssid, const char *password)
err = nvs_commit(h);
nvs_close(h);
ESP_LOGI(TAG, "Saved profile '%s' to NVS (total saved: %d)", ssid, count);
ESP_LOGI(TAG, "Saved profile '%s' to NVS (total: %d)", ssid, count);
return err;
}
@@ -139,19 +134,17 @@ esp_err_t wifi_manager_delete_profile(const char *ssid)
wifi_profile_t profiles[WIFI_MAX_PROFILES];
int count = wifi_manager_get_saved_profiles(profiles, WIFI_MAX_PROFILES);
int found = -1;
int del_idx = -1;
for (int i = 0; i < count; i++) {
if (strcmp(profiles[i].ssid, ssid) == 0) {
found = i;
del_idx = i;
break;
}
}
if (found < 0) {
return ESP_ERR_NOT_FOUND;
}
/* Shift remaining */
for (int i = found; i < count - 1; i++) {
if (del_idx == -1) return ESP_ERR_NOT_FOUND;
for (int i = del_idx; i < count - 1; i++) {
profiles[i] = profiles[i + 1];
}
count--;
@@ -185,14 +178,13 @@ static void wifi_event_handler(void *arg, esp_event_base_t base, int32_t id, voi
(void)arg;
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START) {
ESP_LOGI(TAG, "station started, connecting to '%s'...", s_active_ssid);
esp_wifi_connect();
ESP_LOGI(TAG, "Wi-Fi station interface started");
} else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
strncpy(s_sta_ip, "No Wi-Fi", sizeof(s_sta_ip));
if (s_wifi_group) {
xEventGroupClearBits(s_wifi_group, WIFI_CONNECTED_BIT);
}
if (s_retry_count < WIFI_RETRY_MAX) {
if (s_active_ssid[0] != '\0' && s_retry_count < WIFI_RETRY_MAX) {
ESP_LOGW(TAG, "disconnected from '%s' (attempt %d), reconnecting...", s_active_ssid, s_retry_count);
esp_wifi_connect();
s_retry_count++;
@@ -221,7 +213,7 @@ const char *wifi_manager_get_ip(void)
const char *wifi_manager_get_connected_ssid(void)
{
if (!wifi_manager_connected()) {
if (!wifi_manager_connected() || s_active_ssid[0] == '\0') {
return "Disconnected";
}
return s_active_ssid;
@@ -380,6 +372,19 @@ esp_err_t wifi_manager_init(void)
esp_netif_set_hostname(sta_netif, "mtg-companion");
}
esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
if (ap_netif != NULL) {
esp_netif_set_hostname(ap_netif, "mtg-companion");
esp_netif_ip_info_t ip_info = {
.ip = { .addr = ESP_IP4TOADDR(192, 168, 4, 1) },
.netmask = { .addr = ESP_IP4TOADDR(255, 255, 255, 0) },
.gw = { .addr = ESP_IP4TOADDR(192, 168, 4, 1) },
};
esp_netif_dhcps_stop(ap_netif);
esp_netif_set_ip_info(ap_netif, &ip_info);
esp_netif_dhcps_start(ap_netif);
}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
@@ -400,6 +405,17 @@ esp_err_t wifi_manager_start(void)
const char *target_pass = NULL;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
wifi_config_t ap_cfg = {
.ap = {
.ssid_len = sizeof("MTG-Companion") - 1,
.max_connection = 4,
.authmode = WIFI_AUTH_OPEN,
},
};
strncpy((char *)ap_cfg.ap.ssid, "MTG-Companion", sizeof(ap_cfg.ap.ssid) - 1);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_cfg));
ESP_ERROR_CHECK(esp_wifi_start());
if (profile_count > 0) {
@@ -465,10 +481,16 @@ esp_err_t wifi_manager_start(void)
strncpy(s_active_ssid, target_ssid, sizeof(s_active_ssid) - 1);
s_active_ssid[sizeof(s_active_ssid) - 1] = '\0';
s_retry_count = 0;
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg));
ESP_LOGI(TAG, "Connecting to target '%s'...", target_ssid);
esp_wifi_connect();
esp_wifi_disconnect();
esp_err_t err = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_wifi_set_config failed: %s", esp_err_to_name(err));
} else {
ESP_LOGI(TAG, "Connecting to target '%s'...", target_ssid);
esp_wifi_connect();
}
} else {
ESP_LOGI(TAG, "No Wi-Fi networks configured yet. Device running in SoftAP setup mode.");
}