Line data Source code
1 : /*
2 : * Copyright (c) 2024 Project CHIP Authors
3 : *
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at
7 : *
8 : * http://www.apache.org/licenses/LICENSE-2.0
9 : *
10 : * Unless required by applicable law or agreed to in writing, software
11 : * distributed under the License is distributed on an "AS IS" BASIS,
12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : * See the License for the specific language governing permissions and
14 : * limitations under the License.
15 : */
16 : #include <app/StorageDelegateWrapper.h>
17 :
18 : #include <lib/support/SafeInt.h>
19 :
20 : namespace chip {
21 : namespace app {
22 :
23 65 : CHIP_ERROR StorageDelegateWrapper::WriteValue(const StorageKeyName & aKey, const ByteSpan & aValue)
24 : {
25 65 : VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
26 :
27 : // TODO: we may want to have a small cache for values that change a lot, so
28 : // we only write them once a bunch of changes happen or on timer or
29 : // shutdown.
30 65 : if (!CanCastTo<uint16_t>(aValue.size()))
31 : {
32 0 : return CHIP_ERROR_BUFFER_TOO_SMALL;
33 : }
34 65 : return mStorage->SyncSetKeyValue(aKey.KeyName(), aValue.data(), static_cast<uint16_t>(aValue.size()));
35 : }
36 :
37 72 : CHIP_ERROR StorageDelegateWrapper::ReadValue(const StorageKeyName & aKey, MutableByteSpan & aValue)
38 : {
39 72 : VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
40 :
41 72 : uint16_t size = static_cast<uint16_t>(std::min(aValue.size(), static_cast<size_t>(UINT16_MAX)));
42 72 : ReturnErrorOnFailure(mStorage->SyncGetKeyValue(aKey.KeyName(), aValue.data(), size));
43 66 : aValue.reduce_size(size);
44 66 : return CHIP_NO_ERROR;
45 : }
46 :
47 : } // namespace app
48 : } // namespace chip
|