From 189e612419c5174d89cb103ffb29078f608b40eb Mon Sep 17 00:00:00 2001 From: gemu2015 Date: Sat, 13 Mar 2021 12:50:28 +0100 Subject: [PATCH] nvs to littfs wrapper --- ...m_keystore.c => hap_platform_keystore.cpp} | 155 +++++++++++++++++- 1 file changed, 151 insertions(+), 4 deletions(-) rename lib/libesp32_div/ESP32-HomeKit/src/{hap_platform_keystore.c => hap_platform_keystore.cpp} (65%) mode change 100644 => 100755 diff --git a/lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.c b/lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.cpp old mode 100644 new mode 100755 similarity index 65% rename from lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.c rename to lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.cpp index b82457ca5..db6c3e905 --- a/lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.c +++ b/lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.cpp @@ -21,25 +21,169 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ + + #include #include #include +#include #define HAP_PLATFORM_DEF_NVS_PARTITION "nvs" #define HAP_PLATFORM_DEF_FACTORY_NVS_PARTITION "factory_nvs" +extern "C" { + static const char *TAG = "hap_platform_keystore"; -char * hap_platform_keystore_get_nvs_partition_name() -{ +const char * hap_platform_keystore_get_nvs_partition_name() { return HAP_PLATFORM_DEF_NVS_PARTITION; } -char * hap_platform_keystore_get_factory_nvs_partition_name() -{ +const char * hap_platform_keystore_get_factory_nvs_partition_name() { return HAP_PLATFORM_DEF_FACTORY_NVS_PARTITION; } +#define HAP_USE_LITTLEFS + +#ifdef HAP_USE_LITTLEFS + +#include + +extern FS *ffsp; + +int hap_platform_keystore_init_partition(const char *part_name, bool read_only) { + return 0; +} + +int hap_platform_keystore_get(const char *part_name, const char *name_space, const char *key, uint8_t *val, size_t *val_size) { + char path[48]; + strcpy(path, "/"); + strcat(path, part_name); + + File fp = ffsp->open(path, "r"); + if (!fp) { + ffsp->mkdir(path); + return -1; + } + fp.close(); + + strcat(path, "/"); + strcat(path, name_space); + fp = ffsp->open(path, "r"); + if (!fp) { + ffsp->mkdir(path); + return -1; + } + fp.close(); + + strcat(path, "/"); + strcat(path, key); + fp = ffsp->open(path, "r"); + if (fp) { + fp.read(val, *val_size); + fp.close(); + } else { + *val_size = 0; + return -1; + } + return 0; +} + +int hap_platform_keystore_set(const char *part_name, const char *name_space, const char *key, const uint8_t *val, const size_t val_len) { + char path[48]; + strcpy(path, "/"); + strcat(path, part_name); + + File fp = ffsp->open(path, "r"); + if (!fp) { + ffsp->mkdir(path); + } + fp.close(); + + strcat(path, "/"); + strcat(path, name_space); + fp = ffsp->open(path, "r"); + if (!fp) { + ffsp->mkdir(path); + } + fp.close(); + + strcat(path, "/"); + strcat(path, key); + fp = ffsp->open(path, "w"); + if (fp) { + fp.write(val, val_len); + fp.close(); + } else { + return -1; + } + return 0; +} + +int hap_platform_keystore_delete(const char *part_name, const char *name_space, const char *key) { + char path[48]; + strcpy(path, "/"); + strcat(path, part_name); + strcat(path, "/"); + strcat(path, name_space); + strcat(path, "/"); + strcat(path, key); + ffsp->remove(path); + return 0; +} + +// should +int hap_platform_keystore_delete_namespace(const char *part_name, const char *name_space) { + char path[48]; + strcpy(path, "/"); + strcat(path, part_name); + strcat(path, "/"); + strcat(path, name_space); + File fp = ffsp->open(path, "r"); + if (fp.isDirectory()) { + while (true) { + File entry = fp.openNextFile(); + if (!entry) break; + char fp[48]; + strcpy(fp,path); + strcat(fp, "/"); + strcat(fp, entry.name()); + ffsp->remove(fp); + entry.close(); + } + } + return 0; +} + +// last resort only +int hap_platfrom_keystore_erase_partition(const char *part_name) { +// LITTLEFS.format(); +char path[48]; +strcpy(path, "/"); +strcat(path, part_name); +File fp = ffsp->open(path, "r"); +if (fp.isDirectory()) { + while (true) { + File entry = fp.openNextFile(); + if (!entry) break; + char fp[48]; + strcpy(fp,path); + strcat(fp, "/"); + strcat(fp, entry.name()); + if (entry.isDirectory()) { + hap_platform_keystore_delete_namespace(part_name, entry.name()); + ffsp->rmdir(fp); + } else { + ffsp->remove(fp); + } + entry.close(); + } +} + return 0; +} + +#else + #ifdef CONFIG_NVS_ENCRYPTION int hap_platform_keystore_init_partition(const char *part_name, bool read_only) { @@ -185,3 +329,6 @@ int hap_platfrom_keystore_erase_partition(const char *part_name) } return -1; } +#endif // USE_LITTLEFS + +}