Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Linux / 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 implementatiuon of KVS for linux.
22  */
23
24 #include <platform/KeyValueStoreManager.h>
25
26 #include <algorithm>
27 #include <string.h>
28
29 #include <platform/Linux/CHIPLinuxStorage.h>
30 #include <support/CodeUtils.h>
31 #include <support/logging/CHIPLogging.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     size_t read_size;
43
44     // Copy data into value buffer
45     VerifyOrReturnError(value != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
46
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
49     // offset reads.
50     CHIP_ERROR err = mStorage.ReadValueBin(key, nullptr, 0, read_size);
51     if (err == CHIP_ERROR_KEY_NOT_FOUND)
52     {
53         return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
54     }
55     else if (err != CHIP_NO_ERROR)
56     {
57         return err;
58     }
59
60     uint8_t buf[read_size];
61     ReturnErrorOnFailure(mStorage.ReadValueBin(key, buf, read_size, read_size));
62
63     size_t copy_size = std::min(value_size, read_size - offset_bytes);
64     if (read_bytes_size != nullptr)
65     {
66         *read_bytes_size = copy_size;
67     }
68     ::memcpy(value, buf + offset_bytes, copy_size);
69
70     return CHIP_NO_ERROR;
71 }
72
73 CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
74 {
75     CHIP_ERROR err = CHIP_NO_ERROR;
76
77     err = mStorage.WriteValueBin(key, reinterpret_cast<const uint8_t *>(value), value_size);
78     SuccessOrExit(err);
79
80     // Commit the value to the persistent store.
81     err = mStorage.Commit();
82     SuccessOrExit(err);
83
84 exit:
85     return err;
86 }
87
88 CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
89 {
90     CHIP_ERROR err = CHIP_NO_ERROR;
91     err            = mStorage.ClearValue(key);
92
93     if (err == CHIP_ERROR_KEY_NOT_FOUND)
94     {
95         ExitNow(err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
96     }
97     SuccessOrExit(err);
98
99     // Commit the value to the persistent store.
100     err = mStorage.Commit();
101     SuccessOrExit(err);
102
103 exit:
104     return err;
105 }
106
107 } // namespace PersistedStorage
108 } // namespace DeviceLayer
109 } // namespace chip