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,
42 CHIP_ERROR err = CHIP_NO_ERROR;
46 // On linux read first without a buffer which returns the size, and then
47 // use a local buffer to read the entire object, which allows partial and
49 err = mStorage.ReadValueBin(key, nullptr, 0, read_size);
50 uint8_t buf[read_size];
51 if (err == CHIP_ERROR_KEY_NOT_FOUND)
53 ExitNow(err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
55 err = mStorage.ReadValueBin(key, buf, read_size, read_size);
58 // Copy data into value buffer
61 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
63 copy_size = std::min(value_size, read_size - offset_bytes);
66 *read_bytes_size = copy_size;
68 ::memcpy(value, buf + offset_bytes, copy_size);
74 CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
76 CHIP_ERROR err = CHIP_NO_ERROR;
78 err = mStorage.WriteValueBin(key, reinterpret_cast<const uint8_t *>(value), value_size);
81 // Commit the value to the persistent store.
82 err = mStorage.Commit();
89 CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
91 CHIP_ERROR err = CHIP_NO_ERROR;
92 err = mStorage.ClearValue(key);
94 if (err == CHIP_ERROR_KEY_NOT_FOUND)
96 ExitNow(err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
100 // Commit the value to the persistent store.
101 err = mStorage.Commit();
108 } // namespace PersistedStorage
109 } // namespace DeviceLayer