• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "nvs_partition_manager.hpp"
15 #include "nvs_handle.hpp"
16 #include "nvs_handle_simple.hpp"
17 #include "nvs_handle_locked.hpp"
18 #include "nvs_platform.hpp"
19 
20 namespace nvs {
21 
open_nvs_handle_from_partition(const char * partition_name,const char * ns_name,nvs_open_mode_t open_mode,esp_err_t * err)22 std::unique_ptr<NVSHandle> open_nvs_handle_from_partition(const char *partition_name,
23         const char *ns_name,
24         nvs_open_mode_t open_mode,
25         esp_err_t *err)
26 {
27     if (partition_name == nullptr || ns_name == nullptr) {
28         if (err) {
29             *err = ESP_ERR_INVALID_ARG;
30         }
31         return nullptr;
32     }
33 
34     Lock lock;
35 
36     NVSHandleSimple *handle_simple;
37     esp_err_t result = nvs::NVSPartitionManager::get_instance()->
38             open_handle(partition_name, ns_name, open_mode, &handle_simple);
39 
40     if (err) {
41         *err = result;
42     }
43 
44     if (result != ESP_OK) {
45         return nullptr;
46     }
47 
48     NVSHandleLocked *locked_handle = new (nothrow) NVSHandleLocked(handle_simple);
49 
50     if (!locked_handle) {
51         if (err) {
52             *err = ESP_ERR_NO_MEM;
53         }
54         delete handle_simple;
55         return nullptr;
56     }
57 
58     return std::unique_ptr<NVSHandleLocked>(locked_handle);
59 }
60 
open_nvs_handle(const char * ns_name,nvs_open_mode_t open_mode,esp_err_t * err)61 std::unique_ptr<NVSHandle> open_nvs_handle(const char *ns_name,
62         nvs_open_mode_t open_mode,
63         esp_err_t *err)
64 {
65     return open_nvs_handle_from_partition(NVS_DEFAULT_PART_NAME, ns_name, open_mode, err);
66 }
67 
68 } // namespace nvs
69