nvs_partition.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 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 <cstdlib>
  15. #include "nvs_partition.hpp"
  16. namespace nvs {
  17. NVSPartition::NVSPartition(const esp_partition_t* partition)
  18. : mESPPartition(partition)
  19. {
  20. // ensure the class is in a valid state
  21. if (partition == nullptr) {
  22. std::abort();
  23. }
  24. }
  25. const char *NVSPartition::get_partition_name()
  26. {
  27. return mESPPartition->label;
  28. }
  29. esp_err_t NVSPartition::read_raw(size_t src_offset, void* dst, size_t size)
  30. {
  31. return esp_partition_read_raw(mESPPartition, src_offset, dst, size);
  32. }
  33. esp_err_t NVSPartition::read(size_t src_offset, void* dst, size_t size)
  34. {
  35. if (size % ESP_ENCRYPT_BLOCK_SIZE != 0) {
  36. return ESP_ERR_INVALID_ARG;
  37. }
  38. return esp_partition_read(mESPPartition, src_offset, dst, size);
  39. }
  40. esp_err_t NVSPartition::write_raw(size_t dst_offset, const void* src, size_t size)
  41. {
  42. return esp_partition_write_raw(mESPPartition, dst_offset, src, size);
  43. }
  44. esp_err_t NVSPartition::write(size_t dst_offset, const void* src, size_t size)
  45. {
  46. if (size % ESP_ENCRYPT_BLOCK_SIZE != 0) {
  47. return ESP_ERR_INVALID_ARG;
  48. }
  49. return esp_partition_write(mESPPartition, dst_offset, src, size);
  50. }
  51. esp_err_t NVSPartition::erase_range(size_t dst_offset, size_t size)
  52. {
  53. return esp_partition_erase_range(mESPPartition, dst_offset, size);
  54. }
  55. uint32_t NVSPartition::get_address()
  56. {
  57. return mESPPartition->address;
  58. }
  59. uint32_t NVSPartition::get_size()
  60. {
  61. return mESPPartition->size;
  62. }
  63. } // nvs