Skip to content

Commit

Permalink
ESP32/NVS: use put rather than set
Browse files Browse the repository at this point in the history
Right now there are `fetch` and `get` function, `put` name matches better
current naming.

Signed-off-by: Davide Bettio <[email protected]>
  • Loading branch information
bettio committed Aug 8, 2023
1 parent 2025e40 commit bef89a3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
8 changes: 4 additions & 4 deletions doc/src/network-programming-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ If set in NVS storage, you may remove the corresponding `ssid` and `psk` paramet

You can set these credentials once, as follows:

esp:nvs_set_binary(atomvm, sta_ssid, <<"myssid">>).
esp:nvs_set_binary(atomvm, sta_psk, <<"mypsk">>).
esp:nvs_put_binary(atomvm, sta_ssid, <<"myssid">>).
esp:nvs_put_binary(atomvm, sta_psk, <<"mypsk">>).

or

esp:nvs_set_binary(atomvm, ap_ssid, <<"myssid">>).
esp:nvs_set_binary(atomvm, ap_psk, <<"mypsk">>).
esp:nvs_put_binary(atomvm, ap_ssid, <<"myssid">>).
esp:nvs_put_binary(atomvm, ap_psk, <<"mypsk">>).

With these settings, you can run ESP programs that initialize the network without configuring your SSID and PSK explicitly in source code.

Expand Down
20 changes: 18 additions & 2 deletions libs/eavmlib/src/esp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
nvs_fetch_binary/2,
nvs_get_binary/1, nvs_get_binary/2, nvs_get_binary/3,
nvs_set_binary/2, nvs_set_binary/3,
nvs_put_binary/3,
nvs_erase_key/1, nvs_erase_key/2,
nvs_erase_all/0, nvs_erase_all/1,
nvs_reformat/0,
Expand Down Expand Up @@ -196,7 +197,7 @@ nvs_get_binary(Namespace, Key, Default) when
end.

%%-----------------------------------------------------------------------------
%% @doc Equivalent to nvs_set_binary(?ATOMVM_NVS_NS, Key, Value).
%% @doc (Deprecated) Equivalent to nvs_set_binary(?ATOMVM_NVS_NS, Key, Value).
%% @end
%%-----------------------------------------------------------------------------
-spec nvs_set_binary(Key :: atom(), Value :: binary()) -> ok.
Expand All @@ -208,13 +209,28 @@ nvs_set_binary(Key, Value) when is_atom(Key) andalso is_binary(Value) ->
%% @param Key NVS key
%% @param Value binary value
%% @returns ok
%% @doc Set an binary value associated with a key. If a value exists
%% @doc (Deprecated) set an binary value associated with a key. If a value exists
%% for the specified key, it is over-written.
%% @end
%%-----------------------------------------------------------------------------
-spec nvs_set_binary(Namespace :: atom(), Key :: atom(), Value :: binary()) -> ok.
nvs_set_binary(Namespace, Key, Value) when
is_atom(Namespace) andalso is_atom(Key) andalso is_binary(Value)
->
nvs_put_binary(Namespace, Key, Value).

%%-----------------------------------------------------------------------------
%% @param Namespace NVS namespace
%% @param Key NVS key
%% @param Value binary value
%% @returns ok
%% @doc Set an binary value associated with a key. If a value exists
%% for the specified key, it is over-written.
%% @end
%%-----------------------------------------------------------------------------
-spec nvs_put_binary(Namespace :: atom(), Key :: atom(), Value :: binary()) -> ok.
nvs_put_binary(Namespace, Key, Value) when
is_atom(Namespace) andalso is_atom(Key) andalso is_binary(Value)
->
erlang:nif_error(undefined).

Expand Down
10 changes: 5 additions & 5 deletions src/platforms/esp32/components/avm_builtins/nvs_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static term nif_esp_nvs_get_binary(Context *ctx, int argc, term argv[])
}
}

static term nif_esp_nvs_set_binary(Context *ctx, int argc, term argv[])
static term nif_esp_nvs_put_binary(Context *ctx, int argc, term argv[])
{
UNUSED(argc);
VALIDATE_VALUE(argv[0], term_is_atom);
Expand Down Expand Up @@ -260,9 +260,9 @@ static const struct Nif esp_nvs_get_binary_nif = {
.base.type = NIFFunctionType,
.nif_ptr = nif_esp_nvs_get_binary
};
static const struct Nif esp_nvs_set_binary_nif = {
static const struct Nif esp_nvs_put_binary_nif = {
.base.type = NIFFunctionType,
.nif_ptr = nif_esp_nvs_set_binary
.nif_ptr = nif_esp_nvs_put_binary
};
static const struct Nif esp_nvs_erase_key_nif = {
.base.type = NIFFunctionType,
Expand Down Expand Up @@ -294,9 +294,9 @@ const struct Nif *nvs_nif_get_nif(const char *nifname)
TRACE("Resolved platform nif %s ...\n", nifname);
return &esp_nvs_get_binary_nif;
}
if (strcmp("esp:nvs_set_binary/3", nifname) == 0) {
if (strcmp("esp:nvs_put_binary/3", nifname) == 0) {
TRACE("Resolved platform nif %s ...\n", nifname);
return &esp_nvs_set_binary_nif;
return &esp_nvs_put_binary_nif;
}
if (strcmp("esp:nvs_erase_key/2", nifname) == 0) {
TRACE("Resolved platform nif %s ...\n", nifname);
Expand Down

0 comments on commit bef89a3

Please sign in to comment.