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