Skip to content

Commit

Permalink
feat(nvs): Add erase key and erase namespace methods
Browse files Browse the repository at this point in the history
* Update `espp::NvsHandle` to support `erase(std::error_code&)` and `erase(std::string_view key, std::error_code&)` methods
* Update `espp::Nvs` to support `erase(namespace, key)`, and `erase(namespace)` methods
* Update example to test new methods
  • Loading branch information
finger563 committed Nov 26, 2024
1 parent c40f12f commit 3d78ff2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
39 changes: 38 additions & 1 deletion components/nvs/example/main/nvs_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extern "C" void app_main(void) {
if (ec) {
fmt::print("Error: {}\n", ec.message());
} else {
fmt::print("String = {}\n", str);
fmt::print("String = '{}'\n", str);
}
ec.clear();

Expand All @@ -74,9 +74,46 @@ extern "C" void app_main(void) {
}
ec.clear();

// test getting the string value again
nvs.get_var("system", "string", str, ec);
if (ec) {
fmt::print("Error: {}\n", ec.message());
} else {
fmt::print("String = '{}'\n", str);
}
ec.clear();

// test erasing the string value
nvs.erase("system", "string", ec);
if (ec) {
fmt::print("Error: {}\n", ec.message());
} else {
fmt::print("String erased\n");
}
ec.clear();

// now test getting it again to ensure it was erased
nvs.get_var("system", "string", str, ec);
if (ec) {
fmt::print("Sucess, got expected error when reading erased value: {}\n", ec.message());
} else {
fmt::print("Failure, got unexpected success when reading erased value\n");
}
ec.clear();

counter++;

if (counter > 10) {
// test erasing the whole namespace
nvs.erase("system", ec);
if (ec) {
fmt::print("Error: {}\n", ec.message());
} else {
fmt::print("Namespace erased\n");
}
ec.clear();

// now erase the wole nvs partition
nvs.erase(ec);
nvs.init(ec);
counter = 0;
Expand Down
31 changes: 30 additions & 1 deletion components/nvs/include/nvs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ class Nvs : public BaseComponent {
}
}

/// @brief Erase a namespace from the NVS
/// @param[in] ns_name Namespace of the variable to erase
/// @param[out] ec Saves a std::error_code representing success or failure
bool erase(std::string_view ns_name, std::error_code &ec) {
NvsHandle handle(ns_name.data(), ec);
if (ec)
return false;

if (!handle.erase(ec))
return false;

return true;
}

/// @brief Erase a key from the NVS
/// @param[in] ns_name Namespace of the variable to erase
/// @param[in] key NVS Key of the variable to erase
/// @param[out] ec Saves a std::error_code representing success or failure
bool erase(std::string_view ns_name, std::string_view key, std::error_code &ec) {
NvsHandle handle(ns_name.data(), ec);
if (ec)
return false;

if (!handle.erase(key, ec))
return false;

return true;
}

/// @brief Save a variable in the NVS and commit
/// @param[in] ns_name Namespace of the variable to save
/// @param[in] key NVS Key of the variable to save
Expand Down Expand Up @@ -84,7 +113,7 @@ class Nvs : public BaseComponent {
template <typename T>
void get_or_set_var(std::string_view ns_name, std::string_view key, T &value, T default_value,
std::error_code &ec) {
get_var(ns_name.data(), key.data(), value, ec);
get_or_set_var(ns_name.data(), key.data(), value, default_value, ec);
}

/// @brief Save a variable in the NVS and commit
Expand Down
8 changes: 8 additions & 0 deletions components/nvs/include/nvs_errc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum class NvsErrc {
Init_NVS_Failed,
Erase_NVS_Failed,
Handle_Uninitialized,
Erase_NVS_Key_Failed,
Erase_NVS_Namespace_Failed,
};

struct NvsErrCategory : std::error_category {
Expand Down Expand Up @@ -60,6 +62,12 @@ std::string NvsErrCategory::message(int ev) const {
case NvsErrc::Handle_Uninitialized:
return "Handle not initialized";

case NvsErrc::Erase_NVS_Key_Failed:
return "Failed to erase NVS key";

case NvsErrc::Erase_NVS_Namespace_Failed:
return "Failed to erase NVS namespace";

default:
return "(unrecognized error)";
}
Expand Down
46 changes: 46 additions & 0 deletions components/nvs/include/nvs_handle_espp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,52 @@ class NvsHandle : public BaseComponent {
return;
}

/// @brief Erase a key from the NVS
/// @param[in] key NVS Key to erase
/// @param[out] ec Saves a std::error_code representing success or failure
/// @return true if successful, false otherwise
bool erase(std::string_view key, std::error_code &ec) { return erase(key.data(), ec); }

/// @brief Erase a key from the NVS
/// @param[in] key NVS Key to erase
/// @param[out] ec Saves a std::error_code representing success or failure
/// @return true if successful, false otherwise
bool erase(const char *key, std::error_code &ec) {
if (!handle_) {
logger_.error("NVS Handle not initialized!");
return false;
}

if (!check_key(key, ec))
return false;

esp_err_t err = handle_->erase_item(key);
if (err != ESP_OK) {
logger_.error("Error {} erasing key '{}' from NVS!", esp_err_to_name(err), key);
ec = make_error_code(NvsErrc::Erase_NVS_Key_Failed);
return false;
}
return true;
}

/// @brief Erase all keys from the NVS associated with the namespace / handle
/// @param[out] ec Saves a std::error_code representing success or failure
/// @return true if successful, false otherwise
bool erase(std::error_code &ec) {
if (!handle_) {
logger_.error("NVS Handle not initialized!");
return false;
}

esp_err_t err = handle_->erase_all();
if (err != ESP_OK) {
logger_.error("Error {} erasing all keys from NVS!", esp_err_to_name(err));
ec = make_error_code(NvsErrc::Erase_NVS_Namespace_Failed);
return false;
}
return true;
}

protected:
std::unique_ptr<nvs::NVSHandle> handle_;

Expand Down

0 comments on commit 3d78ff2

Please sign in to comment.