Skip to content

Commit

Permalink
Type-safer NVS interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattsson committed Apr 4, 2024
1 parent 5bc5e20 commit b0a0959
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
SHELL:=/bin/bash

export IDF_TOOLS_PATH?=$(HOME)/.espressif-r7

ifeq ($(IDF_PATH),)

THIS_MK_FILE:=$(notdir $(lastword $(MAKEFILE_LIST)))
Expand Down
65 changes: 65 additions & 0 deletions components/modules/nvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ static int lnvs_set(lua_State *L)
}


// Lua: nvs.setstring(key, value)
static int lnvs_setstring(lua_State *L)
{
const char *key = luaL_checkstring(L, 1);
size_t len;
const char *blob = luaL_checklstring(L, 2, &len);
esp_err_t err = nvs_set_blob(handle, key, blob, len);

if (err == ESP_OK)
err = nvs_commit(handle);

return check_err(L, err);
}


//Lua: value = nvs.get(key)
static int lnvs_get(lua_State *L)
{
Expand Down Expand Up @@ -184,6 +199,32 @@ static int lnvs_get(lua_State *L)
}


//Lua: value = nvs.getstring(key)
static int lnvs_getstring(lua_State *L)
{
const char *key = luaL_checkstring(L, 1);
size_t needed_len;
esp_err_t err = nvs_get_blob(handle, key, NULL, &needed_len);
if (err == ESP_OK)
{
char *blob = luaM_malloc(L, needed_len);
size_t len = needed_len;
err = nvs_get_blob(handle, key, blob, &len);
if (err == ESP_OK)
lua_pushlstring(L, blob, len);
luaM_freemem(L, blob, needed_len);
if (err == ESP_OK)
return 1;
}
else if (err == ESP_ERR_NVS_NOT_FOUND) // Bernie doesn't want to pcall()
{
lua_pushnil(L);
return 1;
}
return check_err(L, err);
}


// Lua: nvs.remove(key)
static int lnvs_remove(lua_State *L)
{
Expand Down Expand Up @@ -219,6 +260,7 @@ static int lnvs_stats(lua_State *L)
return 3;
}


static int lnvs_makekey(lua_State *L)
{
char buf[32];
Expand All @@ -227,14 +269,37 @@ static int lnvs_makekey(lua_State *L)
return 1;
}


// Lua: nvs.forcestring(key)
static int lnvs_forcestring(lua_State *L)
{
const char *key = luaL_checkstring(L, 1);
uint64_t num;
esp_err_t err = nvs_get_u64(handle, key, &num);
if (err == ESP_OK)
{
lua_pushnumber(L, (lua_Number)num); // same nasty uint64_t->number hack
size_t len;
const char *blob = lua_tolstring(L, -1, &len);
check_err(L, nvs_erase_key(handle, key));
check_err(L, nvs_set_blob(handle, key, blob, len));
return check_err(L, nvs_commit(handle));
}
return 0;
}


LROT_BEGIN(nvs, NULL, 0)
LROT_FUNCENTRY( init, lnvs_init )
LROT_FUNCENTRY( set, lnvs_set )
LROT_FUNCENTRY( setstring, lnvs_setstring )
LROT_FUNCENTRY( get, lnvs_get )
LROT_FUNCENTRY( getstring, lnvs_getstring )
LROT_FUNCENTRY( remove, lnvs_remove )
LROT_FUNCENTRY( erase, lnvs_erase )
LROT_FUNCENTRY( stats, lnvs_stats )
LROT_FUNCENTRY( makekey, lnvs_makekey )
LROT_FUNCENTRY( forcestring, lnvs_forcestring )
LROT_END(nvs, NULL, 0)

NODEMCU_MODULE(NVS, "nvs", nvs, NULL);

0 comments on commit b0a0959

Please sign in to comment.