Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / util / attribute-size.cpp
1 /**
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *
20  *    Copyright (c) 2020 Silicon Labs
21  *
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
25  *
26  *        http://www.apache.org/licenses/LICENSE-2.0
27  *
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.
33  */
34 /***************************************************************************/
35 /**
36  * @file
37  * @brief Contains storage and function for retrieving
38  *attribute size.
39  *******************************************************************************
40  ******************************************************************************/
41
42 #include "af.h"
43
44 #include "gen/attribute-type.h"
45
46 static const uint8_t attributeSizes[] = {
47 #include "gen/attribute-size.h"
48 };
49
50 uint8_t emberAfGetDataSize(uint8_t dataType)
51 {
52     for (unsigned i = 0; (i + 1) < sizeof(attributeSizes); i += 2)
53     {
54         if (attributeSizes[i] == dataType)
55         {
56             return attributeSizes[i + 1];
57         }
58     }
59
60     return 0;
61 }
62
63 uint16_t emberAfAttributeValueSize(EmberAfAttributeType dataType, const uint8_t * buffer)
64 {
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,
68     // return zero.
69     //
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))
75     {
76         if (buffer != 0)
77         {
78             if (emberAfIsStringAttributeType(dataType))
79             {
80                 // size is string length plus 1-byte length prefix
81                 dataSize = static_cast<uint16_t>(static_cast<uint16_t>(emberAfStringLength(buffer)) + 1u);
82             }
83             else
84             {
85                 // size is long string length plus 2-byte length prefix
86                 dataSize = static_cast<uint16_t>(emberAfLongStringLength(buffer) + 2u);
87             }
88         }
89     }
90     else
91     {
92         dataSize = (uint16_t) emberAfGetDataSize(dataType);
93     }
94
95     return dataSize;
96 }