PreferencesManager

Non-volatile storage management

Manages persistent storage using ESP32 NVS. More...

#include <PreferencesManager.h>

Public Member Functions

void init ()
 Initialize the preferences manager. More...
 
bool hasWiFiCredentials ()
 Check if WiFi credentials are stored. More...
 
void saveWiFiCredentials (const String &ssid, const String &password)
 Save WiFi credentials to NVS. More...
 
String getWiFiSSID ()
 Retrieve stored WiFi SSID. More...
 
String getWiFiPassword ()
 Retrieve stored WiFi password. More...
 
bool beginBatch (bool readOnly=false)
 Begin batch operation for multiple read/write operations. More...
 
void endBatch ()
 End batch operation and close NVS namespace. More...
 
bool isBatchOpen () const
 Check if batch operation is currently open. More...
 
bool putString (const char *key, const String &value)
 Save string during batch operation. More...
 
String getString (const char *key, const String &defaultValue="")
 Retrieve string during batch operation. More...
 
bool save (const char *key, const String &value)
 Save a string value to NVS. More...
 
String get (const char *key)
 Retrieve a string value from NVS. More...
 
void clear ()
 Clear all preferences in current namespace. More...
 
void clearAll ()
 Clear all stored preferences. More...
 

Detailed Description

Manages persistent storage using ESP32 NVS.

Handles device configuration, WiFi credentials, and generic key-value storage with thread-safe operations using FreeRTOS mutexes.

Features:

  • Automatic initialization with default device settings
  • Thread-safe WiFi credential management
  • Batch operations for improved performance
  • Generic key-value storage interface
  • Safe clear/reset operations
Note
All operations are thread-safe and protected by mutex

Member Function Documentation

◆ beginBatch()

bool CloudMouse::Prefs::PreferencesManager::beginBatch ( bool  readOnly = false)

Begin batch operation for multiple read/write operations.

Parameters
readOnlyIf true, opens in read-only mode (default: false)
Returns
True if batch was successfully opened

Keeps NVS namespace open for multiple operations to improve performance. Supports nested calls with depth tracking. Automatically retries on failure.

Note
Must be followed by endBatch() to release resources
Parameters
readOnlyIf true, opens in read-only mode
Returns
True if batch was successfully opened

Keeps NVS namespace open for multiple operations to improve performance. Supports nested calls with depth tracking. Automatically retries on failure.

◆ clear()

void CloudMouse::Prefs::PreferencesManager::clear ( )

Clear all preferences in current namespace.

Removes all stored key-value pairs and reinitializes namespace to ensure clean state.

◆ clearAll()

void CloudMouse::Prefs::PreferencesManager::clearAll ( )

Clear all stored preferences.

Complete wipe of all preferences data across all namespaces.

Complete wipe of all preferences data.

◆ endBatch()

void CloudMouse::Prefs::PreferencesManager::endBatch ( )

End batch operation and close NVS namespace.

Handles nested calls by tracking depth. Only closes namespace when depth reaches zero.

◆ get()

String CloudMouse::Prefs::PreferencesManager::get ( const char *  key)

Retrieve a string value from NVS.

Parameters
keyStorage key
Returns
Stored value or empty string if not found

◆ getString()

String CloudMouse::Prefs::PreferencesManager::getString ( const char *  key,
const String &  defaultValue = "" 
)

Retrieve string during batch operation.

Parameters
keyStorage key
defaultValueValue to return if key not found (default: empty string)
Returns
Stored value or defaultValue if not found

If batch is not open, falls back to normal get().

Parameters
keyStorage key
defaultValueValue to return if key not found
Returns
Stored value or defaultValue if not found

If batch is not open, falls back to normal get().

◆ getWiFiPassword()

String CloudMouse::Prefs::PreferencesManager::getWiFiPassword ( )

Retrieve stored WiFi password.

Returns
WiFi password or empty string if not set

◆ getWiFiSSID()

String CloudMouse::Prefs::PreferencesManager::getWiFiSSID ( )

Retrieve stored WiFi SSID.

Returns
WiFi SSID or empty string if not set

◆ hasWiFiCredentials()

bool CloudMouse::Prefs::PreferencesManager::hasWiFiCredentials ( )

Check if WiFi credentials are stored.

Returns
True if both SSID and password are present

◆ init()

void CloudMouse::Prefs::PreferencesManager::init ( )

Initialize the preferences manager.

Creates mutex for thread-safe access and sets default values for device settings if not already present.

Creates mutex for thread-safe NVS access and initializes device settings with default values if not already present.

◆ isBatchOpen()

bool CloudMouse::Prefs::PreferencesManager::isBatchOpen ( ) const
inline

Check if batch operation is currently open.

Returns
True if batch is open

◆ putString()

bool CloudMouse::Prefs::PreferencesManager::putString ( const char *  key,
const String &  value 
)

Save string during batch operation.

Parameters
keyStorage key
valueString value to store
Returns
True if save was successful

If batch is not open, falls back to normal save().

◆ save()

bool CloudMouse::Prefs::PreferencesManager::save ( const char *  key,
const String &  value 
)

Save a string value to NVS.

Parameters
keyStorage key
valueString value to store
Returns
True if save was successful

◆ saveWiFiCredentials()

void CloudMouse::Prefs::PreferencesManager::saveWiFiCredentials ( const String &  ssid,
const String &  password 
)

Save WiFi credentials to NVS.

Parameters
ssidWiFi network SSID
passwordWiFi network password