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