Skip to content

Commit 42727b1

Browse files
authored
Rename src/util/message.cpp to src/util/strings.cpp and add appropriate headers (project-chip#32379)
* Move things from message, update dependencies * Fix lint * Restyle * Remove wrongly copied comment * Add another dependency * Vendor identifiers is not orphaned anymore * Move strings to ember-strings to not have overlap in names * Add include for the cpp file into its own header * Standard ordering for attribute metadata cpp * Update darwin framework xcode file * Also move the copy string functions into ember strings. header af.h and implementation in util.cpp makes no sense
1 parent 2b1d7e1 commit 42727b1

File tree

21 files changed

+199
-162
lines changed

21 files changed

+199
-162
lines changed

.github/workflows/lint.yml

-2
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,10 @@ jobs:
123123
--known-failure app/util/generic-callback-stubs.cpp \
124124
--known-failure app/util/im-client-callbacks.h \
125125
--known-failure app/util/MatterCallbacks.h \
126-
--known-failure app/util/message.cpp \
127126
--known-failure app/util/odd-sized-integers.h \
128127
--known-failure app/util/util.cpp \
129128
--known-failure app/util/util.h \
130129
--known-failure app/WriteHandler.h \
131-
--known-failure lib/core/CHIPVendorIdentifiers.hpp \
132130
--known-failure platform/DeviceSafeQueue.cpp \
133131
--known-failure platform/DeviceSafeQueue.h \
134132
--known-failure platform/GLibTypeDeleter.h \

src/app/DefaultAttributePersistenceProvider.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include <app/DefaultAttributePersistenceProvider.h>
18+
#include <app/util/ember-strings.h>
1819
#include <lib/support/CodeUtils.h>
1920
#include <lib/support/DefaultStorageKeyAllocator.h>
2021
#include <lib/support/SafeInt.h>

src/app/chip_data_model.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ function(chip_configure_data_model APP_TARGET)
144144
${CHIP_APP_BASE_DIR}/util/DataModelHandler.cpp
145145
${CHIP_APP_BASE_DIR}/util/ember-compatibility-functions.cpp
146146
${CHIP_APP_BASE_DIR}/util/generic-callback-stubs.cpp
147-
${CHIP_APP_BASE_DIR}/util/message.cpp
148147
${CHIP_APP_BASE_DIR}/util/privilege-storage.cpp
149148
${CHIP_APP_BASE_DIR}/util/util.cpp
150149
${APP_GEN_FILES}

src/app/chip_data_model.gni

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ template("chip_data_model") {
215215
"${_app_root}/util/binding-table.cpp",
216216
"${_app_root}/util/binding-table.h",
217217
"${_app_root}/util/generic-callback-stubs.cpp",
218-
"${_app_root}/util/message.cpp",
219218
"${_app_root}/util/privilege-storage.cpp",
220219
"${chip_root}/zzz_generated/app-common/app-common/zap-generated/attributes/Accessors.cpp",
221220
]
@@ -418,8 +417,10 @@ template("chip_data_model") {
418417
":${_data_model_name}_codegen",
419418
":${_data_model_name}_zapgen",
420419
"${chip_root}/src/app",
420+
"${chip_root}/src/app/common:attribute-type",
421421
"${chip_root}/src/app/common:cluster-objects",
422422
"${chip_root}/src/app/common:enums",
423+
"${chip_root}/src/app/util:types",
423424
"${chip_root}/src/controller",
424425
"${chip_root}/src/lib/core",
425426
"${chip_root}/src/lib/support",

src/app/common/BUILD.gn

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ config("includes") {
1818
include_dirs = [ "${chip_root}/zzz_generated/app-common" ]
1919
}
2020

21+
source_set("attribute-type") {
22+
sources = [ "${chip_root}/zzz_generated/app-common/app-common/zap-generated/attribute-type.h" ]
23+
24+
public_configs = [ ":includes" ]
25+
}
26+
2127
source_set("ids") {
2228
sources = [
2329
"${chip_root}/zzz_generated/app-common/app-common/zap-generated/ids/Attributes.h",

src/app/util/BUILD.gn

+10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ import("${chip_root}/src/app/common_flags.gni")
1717

1818
source_set("types") {
1919
sources = [
20+
"attribute-metadata.cpp",
2021
"attribute-metadata.h",
2122
"basic-types.h",
23+
"ember-strings.cpp",
24+
"ember-strings.h",
2225
"types_stub.h",
2326
]
27+
28+
deps = [
29+
"${chip_root}/src/app/common:attribute-type",
30+
"${chip_root}/src/lib/core:encoding",
31+
"${chip_root}/src/lib/core:types",
32+
]
33+
public_configs = [ "${chip_root}/src:includes" ]
2434
}

src/app/util/af.h

-13
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,6 @@ uint16_t emberAfFixedEndpointCount(void);
194194
*/
195195
bool emberAfIsTypeSigned(EmberAfAttributeType dataType);
196196

197-
/*
198-
* @brief Function that copies a ZCL string type into a buffer. The size
199-
* parameter should indicate the maximum number of characters to copy to the
200-
* destination buffer not including the length byte.
201-
*/
202-
void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size);
203-
/*
204-
* @brief Function that copies a ZCL long string into a buffer. The size
205-
* parameter should indicate the maximum number of characters to copy to the
206-
* destination buffer not including the length bytes.
207-
*/
208-
void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size);
209-
210197
/** @} END Attribute Storage */
211198

212199
/** @name Device Control */

src/app/util/attribute-metadata.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
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+
#include <app/util/attribute-metadata.h>
18+
19+
#include <app-common/zap-generated/attribute-type.h>
20+
21+
bool emberAfIsStringAttributeType(EmberAfAttributeType attributeType)
22+
{
23+
return (attributeType == ZCL_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
24+
}
25+
26+
bool emberAfIsLongStringAttributeType(EmberAfAttributeType attributeType)
27+
{
28+
return (attributeType == ZCL_LONG_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE);
29+
}

src/app/util/attribute-metadata.h

-11
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,3 @@ bool emberAfIsStringAttributeType(EmberAfAttributeType attributeType);
198198

199199
/** @brief Returns true if the given attribute type is a long string. */
200200
bool emberAfIsLongStringAttributeType(EmberAfAttributeType attributeType);
201-
202-
/*
203-
* @brief Function that determines the length of a zigbee Cluster Library string
204-
* (where the first byte is assumed to be the length).
205-
*/
206-
uint8_t emberAfStringLength(const uint8_t * buffer);
207-
/*
208-
* @brief Function that determines the length of a zigbee Cluster Library long string.
209-
* (where the first two bytes are assumed to be the length).
210-
*/
211-
uint16_t emberAfLongStringLength(const uint8_t * buffer);

src/app/util/attribute-storage.cpp

+1-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <app/reporting/reporting.h>
2323
#include <app/util/af.h>
2424
#include <app/util/config.h>
25+
#include <app/util/ember-strings.h>
2526
#include <app/util/generic-callbacks.h>
2627
#include <lib/core/CHIPConfig.h>
2728
#include <lib/support/CodeUtils.h>
@@ -342,16 +343,6 @@ bool emberAfEndpointIndexIsEnabled(uint16_t index)
342343
return (emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isEnabled));
343344
}
344345

345-
bool emberAfIsStringAttributeType(EmberAfAttributeType attributeType)
346-
{
347-
return (attributeType == ZCL_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
348-
}
349-
350-
bool emberAfIsLongStringAttributeType(EmberAfAttributeType attributeType)
351-
{
352-
return (attributeType == ZCL_LONG_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE);
353-
}
354-
355346
bool emberAfIsThisDataTypeAListType(EmberAfAttributeType dataType)
356347
{
357348
return dataType == ZCL_ARRAY_ATTRIBUTE_TYPE;

src/app/util/ember-strings.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
#include <app/util/ember-strings.h>
18+
19+
#include <lib/core/CHIPEncoding.h>
20+
21+
using namespace chip;
22+
23+
uint8_t emberAfStringLength(const uint8_t * buffer)
24+
{
25+
// The first byte specifies the length of the string. A length of 0xFF means
26+
// the string is invalid and there is no character data.
27+
return (buffer[0] == 0xFF ? 0 : buffer[0]);
28+
}
29+
30+
uint16_t emberAfLongStringLength(const uint8_t * buffer)
31+
{
32+
// The first two bytes specify the length of the long string. A length of
33+
// 0xFFFF means the string is invalid and there is no character data.
34+
uint16_t length = Encoding::LittleEndian::Get16(buffer);
35+
return (length == 0xFFFF ? 0 : length);
36+
}
37+
38+
void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size)
39+
{
40+
if (src == nullptr)
41+
{
42+
dest[0] = 0; // Zero out the length of string
43+
}
44+
else if (src[0] == 0xFF)
45+
{
46+
dest[0] = src[0];
47+
}
48+
else
49+
{
50+
uint8_t length = emberAfStringLength(src);
51+
if (size < length)
52+
{
53+
// Since we have checked that size < length, size must be able to fit into the type of length.
54+
length = static_cast<decltype(length)>(size);
55+
}
56+
memmove(dest + 1, src + 1, length);
57+
dest[0] = length;
58+
}
59+
}
60+
61+
void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size)
62+
{
63+
if (src == nullptr)
64+
{
65+
dest[0] = dest[1] = 0; // Zero out the length of string
66+
}
67+
else if ((src[0] == 0xFF) && (src[1] == 0xFF))
68+
{
69+
dest[0] = 0xFF;
70+
dest[1] = 0xFF;
71+
}
72+
else
73+
{
74+
uint16_t length = emberAfLongStringLength(src);
75+
if (size < length)
76+
{
77+
// Since we have checked that size < length, size must be able to fit into the type of length.
78+
length = static_cast<decltype(length)>(size);
79+
}
80+
memmove(dest + 2, src + 2, length);
81+
Encoding::LittleEndian::Put16(dest, length);
82+
}
83+
}

src/app/util/ember-strings.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
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+
#pragma once
18+
19+
#include <cstddef>
20+
#include <cstdint>
21+
22+
/*
23+
* @brief Function that determines the length of a zigbee Cluster Library string
24+
* (where the first byte is assumed to be the length).
25+
*/
26+
uint8_t emberAfStringLength(const uint8_t * buffer);
27+
/*
28+
* @brief Function that determines the length of a zigbee Cluster Library long string.
29+
* (where the first two bytes are assumed to be the length).
30+
*/
31+
uint16_t emberAfLongStringLength(const uint8_t * buffer);
32+
33+
/*
34+
* @brief Function that copies a ZCL string type into a buffer. The size
35+
* parameter should indicate the maximum number of characters to copy to the
36+
* destination buffer not including the length byte.
37+
*/
38+
void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size);
39+
40+
/*
41+
* @brief Function that copies a ZCL long string into a buffer. The size
42+
* parameter should indicate the maximum number of characters to copy to the
43+
* destination buffer not including the length bytes.
44+
*/
45+
void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size);

src/app/util/message.cpp

-37
This file was deleted.

src/app/util/mock/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ source_set("mock_ember") {
2929

3030
public_deps = [
3131
"${chip_root}/src/app",
32+
"${chip_root}/src/app/common:attribute-type",
3233
"${chip_root}/src/lib/core",
3334
"${chip_root}/src/lib/support",
3435
"${chip_root}/src/lib/support:testing_nlunit",

src/app/util/mock/attribute-storage.cpp

-29
Original file line numberDiff line numberDiff line change
@@ -234,35 +234,6 @@ bool emberAfEndpointIndexIsEnabled(uint16_t index)
234234
return index < GetMockNodeConfig().endpoints.size();
235235
}
236236

237-
// This duplication of basic utilities is really unfortunate, but we can't link
238-
// to the normal attribute-storage.cpp because we redefine some of its symbols
239-
// above.
240-
bool emberAfIsStringAttributeType(EmberAfAttributeType attributeType)
241-
{
242-
return (attributeType == ZCL_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
243-
}
244-
245-
bool emberAfIsLongStringAttributeType(EmberAfAttributeType attributeType)
246-
{
247-
return (attributeType == ZCL_LONG_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE);
248-
}
249-
250-
// And we don't have a good way to link to message.cpp either.
251-
uint8_t emberAfStringLength(const uint8_t * buffer)
252-
{
253-
// The first byte specifies the length of the string. A length of 0xFF means
254-
// the string is invalid and there is no character data.
255-
return (buffer[0] == 0xFF ? 0 : buffer[0]);
256-
}
257-
258-
uint16_t emberAfLongStringLength(const uint8_t * buffer)
259-
{
260-
// The first two bytes specify the length of the long string. A length of
261-
// 0xFFFF means the string is invalid and there is no character data.
262-
uint16_t length = Encoding::LittleEndian::Get16(buffer);
263-
return (length == 0xFFFF ? 0 : length);
264-
}
265-
266237
// This will find the first server that has the clusterId given from the index of endpoint.
267238
bool emberAfContainsServerFromIndex(uint16_t index, ClusterId clusterId)
268239
{

src/app/util/types_stub.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
#include <string.h> // For mem* functions.
2121

2222
#include <app/util/basic-types.h>
23+
#include <lib/core/NodeId.h>
2324
#include <lib/core/Optional.h>
2425

25-
#include <transport/raw/MessageHeader.h>
2626
static_assert(sizeof(chip::NodeId) == sizeof(uint64_t), "Unexpected node if size");
2727

2828
/**

0 commit comments

Comments
 (0)