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 EFR32
24 #include <platform/KeyValueStoreManager.h>
26 /* ignore GCC Wconversion warnings for pigweed */
28 #pragma GCC diagnostic push
29 #pragma GCC diagnostic ignored "-Wconversion"
32 #include <pw_log/log.h>
35 #pragma GCC diagnostic pop
39 namespace DeviceLayer {
40 namespace PersistedStorage {
42 KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;
44 #if defined(CHIP_KVS_AVAILABLE) && CHIP_KVS_AVAILABLE
46 CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size,
47 size_t offset_bytes) const
49 assert(CHIP_KVS_AVAILABLE);
50 auto status_and_size = mKvs.Get(key, std::span<std::byte>(reinterpret_cast<std::byte *>(value), value_size), offset_bytes);
53 *read_bytes_size = status_and_size.size();
55 switch (status_and_size.status().code())
57 case pw::OkStatus().code():
59 case pw::Status::NotFound().code():
60 return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
61 case pw::Status::DataLoss().code():
62 return CHIP_ERROR_INTEGRITY_CHECK_FAILED;
63 case pw::Status::ResourceExhausted().code():
64 return CHIP_ERROR_BUFFER_TOO_SMALL;
65 case pw::Status::FailedPrecondition().code():
66 return CHIP_ERROR_WELL_UNINITIALIZED;
67 case pw::Status::InvalidArgument().code():
68 return CHIP_ERROR_INVALID_ARGUMENT;
72 return CHIP_ERROR_INTERNAL; // Unexpected KVS status.
75 CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
77 assert(CHIP_KVS_AVAILABLE);
78 auto status = mKvs.Put(key, std::span<const std::byte>(reinterpret_cast<const std::byte *>(value), value_size));
79 switch (status.code())
81 case pw::OkStatus().code():
83 case pw::Status::DataLoss().code():
84 return CHIP_ERROR_INTEGRITY_CHECK_FAILED;
85 case pw::Status::ResourceExhausted().code():
86 case pw::Status::AlreadyExists().code():
87 return CHIP_ERROR_PERSISTED_STORAGE_FAILED;
88 case pw::Status::FailedPrecondition().code():
89 return CHIP_ERROR_WELL_UNINITIALIZED;
90 case pw::Status::InvalidArgument().code():
91 return CHIP_ERROR_INVALID_ARGUMENT;
95 return CHIP_ERROR_INTERNAL; // Unexpected KVS status.
98 CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
100 assert(CHIP_KVS_AVAILABLE);
101 auto status = mKvs.Delete(key);
102 switch (status.code())
104 case pw::OkStatus().code():
105 return CHIP_NO_ERROR;
106 case pw::Status::NotFound().code():
107 return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
108 case pw::Status::DataLoss().code():
109 return CHIP_ERROR_INTEGRITY_CHECK_FAILED;
110 case pw::Status::ResourceExhausted().code():
111 return CHIP_ERROR_PERSISTED_STORAGE_FAILED;
112 case pw::Status::FailedPrecondition().code():
113 return CHIP_ERROR_WELL_UNINITIALIZED;
114 case pw::Status::InvalidArgument().code():
115 return CHIP_ERROR_INVALID_ARGUMENT;
119 return CHIP_ERROR_INTERNAL; // Unexpected KVS status.
122 CHIP_ERROR KeyValueStoreManagerImpl::ErasePartition()
124 assert(CHIP_KVS_AVAILABLE);
125 auto status = mKvsPartition.Erase();
126 switch (status.code())
128 case pw::OkStatus().code():
129 return CHIP_NO_ERROR;
130 case pw::Status::DeadlineExceeded().code():
131 return CHIP_ERROR_TIMEOUT;
132 case pw::Status::PermissionDenied().code():
133 return CHIP_ERROR_ACCESS_DENIED;
137 return CHIP_ERROR_INTERNAL; // Unexpected KVS status.
139 #endif // defined(CHIP_KVS_AVAILABLE) && CHIP_KVS_AVAILABLE
141 } // namespace PersistedStorage
142 } // namespace DeviceLayer