mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-21 09:46:31 +00:00
nvs to littfs wrapper
This commit is contained in:
parent
130b690d24
commit
189e612419
155
lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.c → lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.cpp
Normal file → Executable file
155
lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.c → lib/libesp32_div/ESP32-HomeKit/src/hap_platform_keystore.cpp
Normal file → Executable file
@ -21,25 +21,169 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <FS.h>
|
||||||
|
|
||||||
#define HAP_PLATFORM_DEF_NVS_PARTITION "nvs"
|
#define HAP_PLATFORM_DEF_NVS_PARTITION "nvs"
|
||||||
#define HAP_PLATFORM_DEF_FACTORY_NVS_PARTITION "factory_nvs"
|
#define HAP_PLATFORM_DEF_FACTORY_NVS_PARTITION "factory_nvs"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
static const char *TAG = "hap_platform_keystore";
|
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;
|
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;
|
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
|
#ifdef CONFIG_NVS_ENCRYPTION
|
||||||
int hap_platform_keystore_init_partition(const char *part_name, bool read_only)
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
#endif // USE_LITTLEFS
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user