diff --git a/src/AutoConnectCredential.cpp b/src/AutoConnectCredential.cpp index d7202fe..dd47af1 100644 --- a/src/AutoConnectCredential.cpp +++ b/src/AutoConnectCredential.cpp @@ -2,8 +2,8 @@ * AutoConnectCredential class dispatcher. * @file AutoConnectCredential.cpp * @author hieromon@gmail.com - * @version 1.0.0 - * @date 2019-08-15 + * @version 1.0.2 + * @date 2019-09-16 * @copyright MIT license. */ @@ -520,7 +520,7 @@ bool AutoConnectCredential::_del(const char* ssid, const bool commit) { uint8_t AutoConnectCredential::_import(void) { uint8_t cn = 0; if (_pref->begin(AC_CREDENTIAL_NVSNAME, true)) { - size_t psz = _pref->getBytesLength(AC_CREDENTIAL_NVSKEY); + size_t psz = AutoConnectUtil::getPrefBytesLength(_pref.get(), AC_CREDENTIAL_NVSKEY); if (psz) { uint8_t* credtPool = (uint8_t*)malloc(psz); if (credtPool) { diff --git a/src/AutoConnectCredential.h b/src/AutoConnectCredential.h index 306a4dc..545145c 100644 --- a/src/AutoConnectCredential.h +++ b/src/AutoConnectCredential.h @@ -2,8 +2,8 @@ * Declaration of AutoConnectCredential class. * @file AutoConnectCredential.h * @author hieromon@gmail.com - * @version 1.0.0 - * @date 2019-08-15 + * @version 1.0.2 + * @date 2019-09-16 * @copyright MIT license. */ @@ -40,6 +40,7 @@ uint8_t bssid[6]; wifi_fast_scan_threshold_t threshold; }; #endif +#include "AutoConnectDefs.h" /** * Credential storage area offset specifier in EEPROM. @@ -105,12 +106,38 @@ class AutoConnectCredential : public AutoConnectCredentialBase { #else // #pragma message "AutoConnectCredential applies the Preferences" +#include #include #include +#include #define AC_CREDENTIAL_NVSNAME AC_IDENTIFIER #define AC_CREDENTIAL_NVSKEY AC_CREDENTIAL_NVSNAME +namespace AutoConnectUtil { +AC_HAS_FUNC(getBytesLength); + +template +typename std::enable_if::value, size_t>::type getPrefBytesLength(T* pref, const char* key) { + return pref->getBytesLength(key); +} + +template +typename std::enable_if::value, size_t>::type getPrefBytesLength(T* pref, const char* key) { + AC_UNUSED(pref); + uint32_t handle; + size_t len; + esp_err_t err = nvs_open(AC_CREDENTIAL_NVSNAME, NVS_READONLY, &handle); + if (err) + len = 0; + else { + (void)nvs_get_blob(handle, key, NULL, &len); + nvs_close(handle); + } + return len; +} +} + /** AutoConnectCredential class using Preferences for ESP32 */ class AutoConnectCredential : public AutoConnectCredentialBase { public: