3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Copyright (c) 2020 Silicon Labs
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
26 * http://www.apache.org/licenses/LICENSE-2.0
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 /***************************************************************************/
37 * @brief Contains storage and function for retrieving
39 *******************************************************************************
40 ******************************************************************************/
44 #include "gen/attribute-type.h"
46 static const uint8_t attributeSizes[] = {
47 #include "gen/attribute-size.h"
50 uint8_t emberAfGetDataSize(uint8_t dataType)
52 for (unsigned i = 0; (i + 1) < sizeof(attributeSizes); i += 2)
54 if (attributeSizes[i] == dataType)
56 return attributeSizes[i + 1];
63 uint16_t emberAfAttributeValueSize(EmberAfAttributeType dataType, const uint8_t * buffer)
65 // If the dataType is a string or long string, refer to the buffer for the
66 // string's length prefix; size is string length plus number of prefix bytes.
67 // If non-string, determine size from dataType. If dataType is unrecognized,
70 // Note: A non-empty long string has max length 0xFFFE, and adding 2 for its
71 // length prefix would roll a uint16_t back to zero. Choosing not to
72 // expand return type to uint32_t just to accommodate that one case.
73 uint16_t dataSize = 0;
74 if (emberAfIsThisDataTypeAStringType(dataType))
78 if (emberAfIsStringAttributeType(dataType))
80 // size is string length plus 1-byte length prefix
81 dataSize = static_cast<uint16_t>(static_cast<uint16_t>(emberAfStringLength(buffer)) + 1u);
85 // size is long string length plus 2-byte length prefix
86 dataSize = static_cast<uint16_t>(emberAfLongStringLength(buffer) + 2u);
92 dataSize = (uint16_t) emberAfGetDataSize(dataType);