Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / ESP32 / KeyValueStoreManagerImpl.cpp
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *    @file
21  *          Platform-specific key value storage implementation for ESP32
22  */
23
24 #include <platform/KeyValueStoreManager.h>
25
26 #include <algorithm>
27 #include <string.h>
28
29 #include "nvs.h"
30 #include "nvs_flash.h"
31 #include <support/CodeUtils.h>
32
33 namespace chip {
34 namespace DeviceLayer {
35 namespace PersistedStorage {
36
37 KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;
38
39 CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size,
40                                           size_t offset_bytes)
41 {
42     CHIP_ERROR err = CHIP_NO_ERROR;
43     nvs_handle handle;
44     bool needClose = false;
45
46     if (!value)
47     {
48         ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
49     }
50     if (offset_bytes > 0)
51     {
52         // Offset and partial reads are not supported in nvs, for now just return NOT_IMPLEMENTED. Support can be added in the
53         // future if this is needed.
54         ExitNow(err = CHIP_ERROR_NOT_IMPLEMENTED);
55     }
56
57     err = nvs_open(kNamespace, NVS_READONLY, &handle);
58     SuccessOrExit(err);
59     needClose = true;
60
61     err = nvs_get_blob(handle, key, value, &value_size);
62     SuccessOrExit(err);
63     if (read_bytes_size)
64     {
65         *read_bytes_size = value_size;
66     }
67
68 exit:
69     if (needClose)
70     {
71         nvs_close(handle);
72     }
73     return err;
74 }
75
76 CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
77 {
78     CHIP_ERROR err = CHIP_NO_ERROR;
79     nvs_handle handle;
80     bool needClose = false;
81
82     if (!value)
83     {
84         ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
85     }
86     err = nvs_open(kNamespace, NVS_READWRITE, &handle);
87     SuccessOrExit(err);
88     needClose = true;
89
90     err = nvs_set_blob(handle, key, value, value_size);
91     SuccessOrExit(err);
92
93     // Commit the value to the persistent store.
94     err = nvs_commit(handle);
95     SuccessOrExit(err);
96
97 exit:
98     if (needClose)
99     {
100         nvs_close(handle);
101     }
102
103     return err;
104 }
105
106 CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
107 {
108     CHIP_ERROR err = CHIP_NO_ERROR;
109     nvs_handle handle;
110     bool needClose = false;
111
112     err = nvs_open(kNamespace, NVS_READWRITE, &handle);
113     SuccessOrExit(err);
114     needClose = true;
115
116     err = nvs_erase_key(handle, key);
117     SuccessOrExit(err);
118
119     // Commit the value to the persistent store.
120     err = nvs_commit(handle);
121     SuccessOrExit(err);
122
123 exit:
124     if (needClose)
125     {
126         nvs_close(handle);
127     }
128
129     return err;
130 }
131
132 } // namespace PersistedStorage
133 } // namespace DeviceLayer
134 } // namespace chip