3 * Copyright (c) 2021 Project CHIP Authors
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Platform-specific key value storage implementation for ESP32
24 #include <platform/KeyValueStoreManager.h>
30 #include "nvs_flash.h"
31 #include <support/CodeUtils.h>
34 namespace DeviceLayer {
35 namespace PersistedStorage {
37 KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;
39 CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size,
42 CHIP_ERROR err = CHIP_NO_ERROR;
44 bool needClose = false;
48 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
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);
57 err = nvs_open(kNamespace, NVS_READONLY, &handle);
61 err = nvs_get_blob(handle, key, value, &value_size);
65 *read_bytes_size = value_size;
76 CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
78 CHIP_ERROR err = CHIP_NO_ERROR;
80 bool needClose = false;
84 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
86 err = nvs_open(kNamespace, NVS_READWRITE, &handle);
90 err = nvs_set_blob(handle, key, value, value_size);
93 // Commit the value to the persistent store.
94 err = nvs_commit(handle);
106 CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
108 CHIP_ERROR err = CHIP_NO_ERROR;
110 bool needClose = false;
112 err = nvs_open(kNamespace, NVS_READWRITE, &handle);
116 err = nvs_erase_key(handle, key);
119 // Commit the value to the persistent store.
120 err = nvs_commit(handle);
132 } // namespace PersistedStorage
133 } // namespace DeviceLayer