nvs_cxx_api.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "nvs_partition_manager.hpp"
  14. #include "nvs_handle.hpp"
  15. #include "nvs_handle_simple.hpp"
  16. #include "nvs_handle_locked.hpp"
  17. #include "nvs_platform.hpp"
  18. namespace nvs {
  19. std::unique_ptr<NVSHandle> open_nvs_handle_from_partition(const char *partition_name,
  20. const char *ns_name,
  21. nvs_open_mode_t open_mode,
  22. esp_err_t *err)
  23. {
  24. if (partition_name == nullptr || ns_name == nullptr) {
  25. if (err) {
  26. *err = ESP_ERR_INVALID_ARG;
  27. }
  28. return nullptr;
  29. }
  30. esp_err_t lock_result = Lock::init();
  31. if (lock_result != ESP_OK) {
  32. if (err != nullptr) {
  33. *err = lock_result;
  34. }
  35. return nullptr;
  36. }
  37. Lock lock;
  38. NVSHandleSimple *handle_simple;
  39. esp_err_t result = nvs::NVSPartitionManager::get_instance()->
  40. open_handle(partition_name, ns_name, open_mode, &handle_simple);
  41. if (err) {
  42. *err = result;
  43. }
  44. if (result != ESP_OK) {
  45. return nullptr;
  46. }
  47. NVSHandleLocked *locked_handle = new (nothrow) NVSHandleLocked(handle_simple);
  48. if (!locked_handle) {
  49. if (err) {
  50. *err = ESP_ERR_NO_MEM;
  51. }
  52. delete handle_simple;
  53. return nullptr;
  54. }
  55. return std::unique_ptr<NVSHandleLocked>(locked_handle);
  56. }
  57. std::unique_ptr<NVSHandle> open_nvs_handle(const char *ns_name,
  58. nvs_open_mode_t open_mode,
  59. esp_err_t *err)
  60. {
  61. return open_nvs_handle_from_partition(NVS_DEFAULT_PART_NAME, ns_name, open_mode, err);
  62. }
  63. } // namespace nvs