1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2021 by Thorsten von Eicken
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <string.h>
28 
29 #include "py/runtime.h"
30 #include "py/mperrno.h"
31 #include "mphalport.h"
32 #include "modesp32.h"
33 #include "nvs_flash.h"
34 #include "nvs.h"
35 
36 // This file implements the NVS (Non-Volatile Storage) class in the esp32 module.
37 // It provides simple access to the NVS feature provided by ESP-IDF.
38 
39 // NVS python object that represents an NVS namespace.
40 typedef struct _esp32_nvs_obj_t {
41     mp_obj_base_t base;
42     nvs_handle_t namespace;
43 } esp32_nvs_obj_t;
44 
45 // *esp32_nvs_new allocates a python NVS object given a handle to an esp-idf namespace C obj.
esp32_nvs_new(nvs_handle_t namespace)46 STATIC esp32_nvs_obj_t *esp32_nvs_new(nvs_handle_t namespace) {
47     esp32_nvs_obj_t *self = m_new_obj(esp32_nvs_obj_t);
48     self->base.type = &esp32_nvs_type;
49     self->namespace = namespace;
50     return self;
51 }
52 
53 // esp32_nvs_print prints an NVS object, unfortunately it doesn't seem possible to extract the
54 // namespace string or anything else from the opaque handle provided by esp-idf.
esp32_nvs_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)55 STATIC void esp32_nvs_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
56     // esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in);
57     mp_printf(print, "<NVS namespace>");
58 }
59 
60 // esp32_nvs_make_new constructs a handle to an NVS namespace.
esp32_nvs_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * all_args)61 STATIC mp_obj_t esp32_nvs_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
62     // Check args
63     mp_arg_check_num(n_args, n_kw, 1, 1, false);
64 
65     // Get requested nvs namespace
66     const char *ns_name = mp_obj_str_get_str(all_args[0]);
67     nvs_handle_t namespace;
68     check_esp_err(nvs_open(ns_name, NVS_READWRITE, &namespace));
69     return MP_OBJ_FROM_PTR(esp32_nvs_new(namespace));
70 }
71 
72 // esp32_nvs_set_i32 sets a 32-bit integer value
esp32_nvs_set_i32(mp_obj_t self_in,mp_obj_t key_in,mp_obj_t value_in)73 STATIC mp_obj_t esp32_nvs_set_i32(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) {
74     esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in);
75     const char *key = mp_obj_str_get_str(key_in);
76     int32_t value = mp_obj_get_int(value_in);
77     check_esp_err(nvs_set_i32(self->namespace, key, value));
78     return mp_const_none;
79 }
80 STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32_nvs_set_i32_obj, esp32_nvs_set_i32);
81 
82 // esp32_nvs_get_i32 reads a 32-bit integer value
esp32_nvs_get_i32(mp_obj_t self_in,mp_obj_t key_in)83 STATIC mp_obj_t esp32_nvs_get_i32(mp_obj_t self_in, mp_obj_t key_in) {
84     esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in);
85     const char *key = mp_obj_str_get_str(key_in);
86     int32_t value;
87     check_esp_err(nvs_get_i32(self->namespace, key, &value));
88     return mp_obj_new_int(value);
89 }
90 STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32_nvs_get_i32_obj, esp32_nvs_get_i32);
91 
92 // esp32_nvs_set_blob writes a buffer object into a binary blob value.
esp32_nvs_set_blob(mp_obj_t self_in,mp_obj_t key_in,mp_obj_t value_in)93 STATIC mp_obj_t esp32_nvs_set_blob(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) {
94     esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in);
95     const char *key = mp_obj_str_get_str(key_in);
96     mp_buffer_info_t value;
97     mp_get_buffer_raise(value_in, &value, MP_BUFFER_READ);
98     check_esp_err(nvs_set_blob(self->namespace, key, value.buf, value.len));
99     return mp_const_none;
100 }
101 STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32_nvs_set_blob_obj, esp32_nvs_set_blob);
102 
103 // esp32_nvs_get_blob reads a binary blob value into a bytearray. Returns actual length.
esp32_nvs_get_blob(mp_obj_t self_in,mp_obj_t key_in,mp_obj_t value_in)104 STATIC mp_obj_t esp32_nvs_get_blob(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) {
105     esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in);
106     const char *key = mp_obj_str_get_str(key_in);
107     // get buffer to be filled
108     mp_buffer_info_t value;
109     mp_get_buffer_raise(value_in, &value, MP_BUFFER_WRITE);
110     size_t length = value.len;
111     // fill the buffer with the value, will raise an esp-idf error if the length of
112     // the provided buffer (bytearray) is too small
113     check_esp_err(nvs_get_blob(self->namespace, key, value.buf, &length));
114     return MP_OBJ_NEW_SMALL_INT(length);
115 }
116 STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32_nvs_get_blob_obj, esp32_nvs_get_blob);
117 
118 // esp32_nvs_erase_key erases one key.
esp32_nvs_erase_key(mp_obj_t self_in,mp_obj_t key_in)119 STATIC mp_obj_t esp32_nvs_erase_key(mp_obj_t self_in, mp_obj_t key_in) {
120     esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in);
121     const char *key = mp_obj_str_get_str(key_in);
122     check_esp_err(nvs_erase_key(self->namespace, key));
123     return mp_const_none;
124 }
125 STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32_nvs_erase_key_obj, esp32_nvs_erase_key);
126 
127 // esp32_nvs_commit commits any changes to flash.
esp32_nvs_commit(mp_obj_t self_in)128 STATIC mp_obj_t esp32_nvs_commit(mp_obj_t self_in) {
129     esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in);
130     check_esp_err(nvs_commit(self->namespace));
131     return mp_const_none;
132 }
133 STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_nvs_commit_obj, esp32_nvs_commit);
134 
135 STATIC const mp_rom_map_elem_t esp32_nvs_locals_dict_table[] = {
136     { MP_ROM_QSTR(MP_QSTR_get_i32), MP_ROM_PTR(&esp32_nvs_get_i32_obj) },
137     { MP_ROM_QSTR(MP_QSTR_set_i32), MP_ROM_PTR(&esp32_nvs_set_i32_obj) },
138     { MP_ROM_QSTR(MP_QSTR_get_blob), MP_ROM_PTR(&esp32_nvs_get_blob_obj) },
139     { MP_ROM_QSTR(MP_QSTR_set_blob), MP_ROM_PTR(&esp32_nvs_set_blob_obj) },
140     { MP_ROM_QSTR(MP_QSTR_erase_key), MP_ROM_PTR(&esp32_nvs_erase_key_obj) },
141     { MP_ROM_QSTR(MP_QSTR_commit), MP_ROM_PTR(&esp32_nvs_commit_obj) },
142 };
143 STATIC MP_DEFINE_CONST_DICT(esp32_nvs_locals_dict, esp32_nvs_locals_dict_table);
144 
145 const mp_obj_type_t esp32_nvs_type = {
146     { &mp_type_type },
147     .name = MP_QSTR_NVS,
148     .print = esp32_nvs_print,
149     .make_new = esp32_nvs_make_new,
150     .locals_dict = (mp_obj_dict_t *)&esp32_nvs_locals_dict,
151 };
152