Line data Source code
1 : /*
2 : * Copyright (c) 2021 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/util/persistence/DefaultAttributePersistenceProvider.h>
17 :
18 : #include <app/util/ember-strings.h>
19 : #include <lib/support/CodeUtils.h>
20 : #include <lib/support/DefaultStorageKeyAllocator.h>
21 : #include <lib/support/SafeInt.h>
22 :
23 : namespace chip {
24 : namespace app {
25 :
26 0 : CHIP_ERROR DefaultAttributePersistenceProvider::InternalReadValue(const StorageKeyName & aKey, EmberAfAttributeType aType,
27 : size_t aExpectedSize, MutableByteSpan & aValue)
28 : {
29 0 : ReturnErrorOnFailure(StorageDelegateWrapper::ReadValue(aKey, aValue));
30 0 : size_t size = aValue.size();
31 0 : if (emberAfIsStringAttributeType(aType))
32 : {
33 : // Ensure that we've read enough bytes that we are not ending up with
34 : // un-initialized memory. Should have read length + 1 (for the length
35 : // byte).
36 0 : VerifyOrReturnError(size >= 1 && size - 1 >= emberAfStringLength(aValue.data()), CHIP_ERROR_INCORRECT_STATE);
37 : }
38 0 : else if (emberAfIsLongStringAttributeType(aType))
39 : {
40 : // Ensure that we've read enough bytes that we are not ending up with
41 : // un-initialized memory. Should have read length + 2 (for the length
42 : // bytes).
43 0 : VerifyOrReturnError(size >= 2 && size - 2 >= emberAfLongStringLength(aValue.data()), CHIP_ERROR_INCORRECT_STATE);
44 : }
45 : else
46 : {
47 : // Ensure we got the expected number of bytes for all other types.
48 0 : VerifyOrReturnError(size == aExpectedSize, CHIP_ERROR_INVALID_ARGUMENT);
49 : }
50 0 : return CHIP_NO_ERROR;
51 : }
52 :
53 0 : CHIP_ERROR DefaultAttributePersistenceProvider::WriteValue(const ConcreteAttributePath & aPath, const ByteSpan & aValue)
54 : {
55 0 : return StorageDelegateWrapper::WriteValue(
56 0 : DefaultStorageKeyAllocator::AttributeValue(aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId), aValue);
57 : }
58 :
59 0 : CHIP_ERROR DefaultAttributePersistenceProvider::ReadValue(const ConcreteAttributePath & aPath,
60 : const EmberAfAttributeMetadata * aMetadata, MutableByteSpan & aValue)
61 : {
62 0 : return InternalReadValue(DefaultStorageKeyAllocator::AttributeValue(aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId),
63 0 : aMetadata->attributeType, aMetadata->size, aValue);
64 : }
65 :
66 : } // namespace app
67 : } // namespace chip
|