nvs to littfs wrapper

This commit is contained in:
gemu2015 2021-03-13 12:50:28 +01:00
parent 130b690d24
commit 189e612419

View File

@ -21,25 +21,169 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <esp_log.h>
#include <nvs_flash.h>
#include <string.h>
#include <FS.h>
#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 <LITTLEFS.h>
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
}