Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / EFR32 / 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 key value storage implementation for EFR32
22  */
23
24 #include <platform/KeyValueStoreManager.h>
25
26 /* ignore GCC Wconversion warnings for pigweed */
27 #if defined(__GNUC__)
28 #pragma GCC diagnostic push
29 #pragma GCC diagnostic ignored "-Wconversion"
30 #endif
31
32 #include <pw_log/log.h>
33
34 #if defined(__GNUC__)
35 #pragma GCC diagnostic pop
36 #endif
37
38 namespace chip {
39 namespace DeviceLayer {
40 namespace PersistedStorage {
41
42 KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;
43
44 #if defined(CHIP_KVS_AVAILABLE) && CHIP_KVS_AVAILABLE
45
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
48 {
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);
51     if (read_bytes_size)
52     {
53         *read_bytes_size = status_and_size.size();
54     }
55     switch (status_and_size.status().code())
56     {
57     case pw::OkStatus().code():
58         return CHIP_NO_ERROR;
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;
69     default:
70         break;
71     }
72     return CHIP_ERROR_INTERNAL; // Unexpected KVS status.
73 }
74
75 CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
76 {
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())
80     {
81     case pw::OkStatus().code():
82         return CHIP_NO_ERROR;
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;
92     default:
93         break;
94     }
95     return CHIP_ERROR_INTERNAL; // Unexpected KVS status.
96 }
97
98 CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
99 {
100     assert(CHIP_KVS_AVAILABLE);
101     auto status = mKvs.Delete(key);
102     switch (status.code())
103     {
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;
116     default:
117         break;
118     }
119     return CHIP_ERROR_INTERNAL; // Unexpected KVS status.
120 }
121
122 CHIP_ERROR KeyValueStoreManagerImpl::ErasePartition()
123 {
124     assert(CHIP_KVS_AVAILABLE);
125     auto status = mKvsPartition.Erase();
126     switch (status.code())
127     {
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;
134     default:
135         break;
136     }
137     return CHIP_ERROR_INTERNAL; // Unexpected KVS status.
138 }
139 #endif // defined(CHIP_KVS_AVAILABLE) && CHIP_KVS_AVAILABLE
140
141 } // namespace PersistedStorage
142 } // namespace DeviceLayer
143 } // namespace chip