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 implementatiuon of KVS for linux.
24 #include <platform/KeyValueStoreManager.h>
29 #include <platform/Linux/CHIPLinuxStorage.h>
30 #include <support/CodeUtils.h>
31 #include <support/logging/CHIPLogging.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,
44 // Copy data into value buffer
45 VerifyOrReturnError(value != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
47 // On linux read first without a buffer which returns the size, and then
48 // use a local buffer to read the entire object, which allows partial and
50 CHIP_ERROR err = mStorage.ReadValueBin(key, nullptr, 0, read_size);
51 if (err == CHIP_ERROR_KEY_NOT_FOUND)
53 return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
55 else if (err != CHIP_NO_ERROR)
60 uint8_t buf[read_size];
61 ReturnErrorOnFailure(mStorage.ReadValueBin(key, buf, read_size, read_size));
63 size_t copy_size = std::min(value_size, read_size - offset_bytes);
64 if (read_bytes_size != nullptr)
66 *read_bytes_size = copy_size;
68 ::memcpy(value, buf + offset_bytes, copy_size);
73 CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
75 CHIP_ERROR err = CHIP_NO_ERROR;
77 err = mStorage.WriteValueBin(key, reinterpret_cast<const uint8_t *>(value), value_size);
80 // Commit the value to the persistent store.
81 err = mStorage.Commit();
88 CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
90 CHIP_ERROR err = CHIP_NO_ERROR;
91 err = mStorage.ClearValue(key);
93 if (err == CHIP_ERROR_KEY_NOT_FOUND)
95 ExitNow(err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
99 // Commit the value to the persistent store.
100 err = mStorage.Commit();
107 } // namespace PersistedStorage
108 } // namespace DeviceLayer