feat: enhance NVS and WiFi handlers for improved credential management and error handling
This commit is contained in:
@@ -160,6 +160,7 @@ esp_err_t WifiHandler::connect(const std::string& ssid, const std::string& passw
|
||||
this->current_ssid.clear();
|
||||
}
|
||||
this->current_ssid = ssid;
|
||||
this->current_password = password;
|
||||
|
||||
//
|
||||
wifi_config_t wifi_config = {};
|
||||
@@ -182,8 +183,8 @@ esp_err_t WifiHandler::connect(const std::string& ssid, const std::string& passw
|
||||
return err;
|
||||
}
|
||||
|
||||
// store credentials after successful connection attempt
|
||||
this->store_wifi_credentials(this->current_ssid, password);
|
||||
// Note: Credentials will be stored in the event handler after successful connection
|
||||
// to avoid storing credentials for failed connection attempts
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -305,6 +306,10 @@ void WifiHandler::wifi_event_handler(void* arg, esp_event_base_t event_base, int
|
||||
self->s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT
|
||||
);
|
||||
// Store credentials only after successful connection
|
||||
if (!self->current_ssid.empty() && !self->current_password.empty()) {
|
||||
self->store_wifi_credentials(self->current_ssid, self->current_password);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -328,7 +333,11 @@ void WifiHandler::store_wifi_credentials(const std::string& ssid, const std::str
|
||||
ESP_LOGE(TAG, "Failed to take credential mutex");
|
||||
return;
|
||||
}
|
||||
// store the password according to the JSON structure
|
||||
|
||||
// Store current SSID
|
||||
kvs->put(WIFI_SSID_KEY, ssid);
|
||||
|
||||
// Store the password according to the JSON structure
|
||||
std::string password_key_store = kvs->get(WIFI_PASSWORD_STORE_KEY);
|
||||
cJSON* json = nullptr;
|
||||
if (password_key_store.empty()) {
|
||||
@@ -348,17 +357,37 @@ void WifiHandler::store_wifi_credentials(const std::string& ssid, const std::str
|
||||
credentials = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(json, "credentials", credentials);
|
||||
}
|
||||
|
||||
// Limit stored credentials to prevent NVS overflow (keep max 10 SSIDs)
|
||||
int credential_count = cJSON_GetArraySize(credentials);
|
||||
if (credential_count >= 10) {
|
||||
ESP_LOGW(TAG, "Too many stored credentials (%d), clearing old ones", credential_count);
|
||||
// Keep only the current SSID's credentials, clear others
|
||||
cJSON_DeleteItemFromObject(credentials, ssid.c_str()); // Remove if exists
|
||||
cJSON* new_credentials = cJSON_CreateObject();
|
||||
cJSON_ReplaceItemInObject(json, "credentials", new_credentials);
|
||||
credentials = new_credentials;
|
||||
}
|
||||
|
||||
// Remove existing entry for this SSID to update it
|
||||
cJSON_DeleteItemFromObject(credentials, ssid.c_str());
|
||||
|
||||
// create SSID object
|
||||
cJSON* ssid_item = cJSON_CreateObject();
|
||||
// add password field
|
||||
cJSON_AddStringToObject(ssid_item, "password", password.c_str());
|
||||
// add SSID object to credentials
|
||||
cJSON_AddItemToObject(credentials, ssid.c_str(), ssid_item);
|
||||
|
||||
// store updated JSON string
|
||||
char* updated_json_str = cJSON_PrintUnformatted(json);
|
||||
if (updated_json_str) {
|
||||
esp_err_t err = ESP_OK;
|
||||
kvs->put(WIFI_PASSWORD_STORE_KEY, std::string(updated_json_str));
|
||||
// Note: Error handling is done in nvs_handler.cpp put() method
|
||||
cJSON_free(updated_json_str);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to serialize WiFi credentials JSON");
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user