Skip to content

Commit

Permalink
Rename src/util/message.cpp to src/util/strings.cpp and add appro…
Browse files Browse the repository at this point in the history
…priate headers (#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
  • Loading branch information
andy31415 authored and pull[bot] committed Mar 5, 2024
1 parent c4572b6 commit 3673607
Show file tree
Hide file tree
Showing 21 changed files with 199 additions and 162 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,10 @@ jobs:
--known-failure app/util/generic-callback-stubs.cpp \
--known-failure app/util/im-client-callbacks.h \
--known-failure app/util/MatterCallbacks.h \
--known-failure app/util/message.cpp \
--known-failure app/util/odd-sized-integers.h \
--known-failure app/util/util.cpp \
--known-failure app/util/util.h \
--known-failure app/WriteHandler.h \
--known-failure lib/core/CHIPVendorIdentifiers.hpp \
--known-failure platform/DeviceSafeQueue.cpp \
--known-failure platform/DeviceSafeQueue.h \
--known-failure platform/GLibTypeDeleter.h \
Expand Down
1 change: 1 addition & 0 deletions src/app/DefaultAttributePersistenceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <app/DefaultAttributePersistenceProvider.h>
#include <app/util/ember-strings.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <lib/support/SafeInt.h>
Expand Down
1 change: 0 additions & 1 deletion src/app/chip_data_model.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ function(chip_configure_data_model APP_TARGET)
${CHIP_APP_BASE_DIR}/util/DataModelHandler.cpp
${CHIP_APP_BASE_DIR}/util/ember-compatibility-functions.cpp
${CHIP_APP_BASE_DIR}/util/generic-callback-stubs.cpp
${CHIP_APP_BASE_DIR}/util/message.cpp
${CHIP_APP_BASE_DIR}/util/privilege-storage.cpp
${CHIP_APP_BASE_DIR}/util/util.cpp
${APP_GEN_FILES}
Expand Down
3 changes: 2 additions & 1 deletion src/app/chip_data_model.gni
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ template("chip_data_model") {
"${_app_root}/util/binding-table.cpp",
"${_app_root}/util/binding-table.h",
"${_app_root}/util/generic-callback-stubs.cpp",
"${_app_root}/util/message.cpp",
"${_app_root}/util/privilege-storage.cpp",
"${chip_root}/zzz_generated/app-common/app-common/zap-generated/attributes/Accessors.cpp",
]
Expand Down Expand Up @@ -418,8 +417,10 @@ template("chip_data_model") {
":${_data_model_name}_codegen",
":${_data_model_name}_zapgen",
"${chip_root}/src/app",
"${chip_root}/src/app/common:attribute-type",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/app/common:enums",
"${chip_root}/src/app/util:types",
"${chip_root}/src/controller",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support",
Expand Down
6 changes: 6 additions & 0 deletions src/app/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ config("includes") {
include_dirs = [ "${chip_root}/zzz_generated/app-common" ]
}

source_set("attribute-type") {
sources = [ "${chip_root}/zzz_generated/app-common/app-common/zap-generated/attribute-type.h" ]

public_configs = [ ":includes" ]
}

source_set("ids") {
sources = [
"${chip_root}/zzz_generated/app-common/app-common/zap-generated/ids/Attributes.h",
Expand Down
10 changes: 10 additions & 0 deletions src/app/util/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ import("${chip_root}/src/app/common_flags.gni")

source_set("types") {
sources = [
"attribute-metadata.cpp",
"attribute-metadata.h",
"basic-types.h",
"ember-strings.cpp",
"ember-strings.h",
"types_stub.h",
]

deps = [
"${chip_root}/src/app/common:attribute-type",
"${chip_root}/src/lib/core:encoding",
"${chip_root}/src/lib/core:types",
]
public_configs = [ "${chip_root}/src:includes" ]
}
13 changes: 0 additions & 13 deletions src/app/util/af.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,6 @@ uint16_t emberAfFixedEndpointCount(void);
*/
bool emberAfIsTypeSigned(EmberAfAttributeType dataType);

/*
* @brief Function that copies a ZCL string type into a buffer. The size
* parameter should indicate the maximum number of characters to copy to the
* destination buffer not including the length byte.
*/
void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size);
/*
* @brief Function that copies a ZCL long string into a buffer. The size
* parameter should indicate the maximum number of characters to copy to the
* destination buffer not including the length bytes.
*/
void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size);

/** @} END Attribute Storage */

/** @name Device Control */
Expand Down
29 changes: 29 additions & 0 deletions src/app/util/attribute-metadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <app/util/attribute-metadata.h>

#include <app-common/zap-generated/attribute-type.h>

bool emberAfIsStringAttributeType(EmberAfAttributeType attributeType)
{
return (attributeType == ZCL_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
}

bool emberAfIsLongStringAttributeType(EmberAfAttributeType attributeType)
{
return (attributeType == ZCL_LONG_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE);
}
11 changes: 0 additions & 11 deletions src/app/util/attribute-metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,3 @@ bool emberAfIsStringAttributeType(EmberAfAttributeType attributeType);

/** @brief Returns true if the given attribute type is a long string. */
bool emberAfIsLongStringAttributeType(EmberAfAttributeType attributeType);

/*
* @brief Function that determines the length of a zigbee Cluster Library string
* (where the first byte is assumed to be the length).
*/
uint8_t emberAfStringLength(const uint8_t * buffer);
/*
* @brief Function that determines the length of a zigbee Cluster Library long string.
* (where the first two bytes are assumed to be the length).
*/
uint16_t emberAfLongStringLength(const uint8_t * buffer);
11 changes: 1 addition & 10 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <app/reporting/reporting.h>
#include <app/util/af.h>
#include <app/util/config.h>
#include <app/util/ember-strings.h>
#include <app/util/generic-callbacks.h>
#include <lib/core/CHIPConfig.h>
#include <lib/support/CodeUtils.h>
Expand Down Expand Up @@ -342,16 +343,6 @@ bool emberAfEndpointIndexIsEnabled(uint16_t index)
return (emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isEnabled));
}

bool emberAfIsStringAttributeType(EmberAfAttributeType attributeType)
{
return (attributeType == ZCL_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
}

bool emberAfIsLongStringAttributeType(EmberAfAttributeType attributeType)
{
return (attributeType == ZCL_LONG_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE);
}

bool emberAfIsThisDataTypeAListType(EmberAfAttributeType dataType)
{
return dataType == ZCL_ARRAY_ATTRIBUTE_TYPE;
Expand Down
83 changes: 83 additions & 0 deletions src/app/util/ember-strings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <app/util/ember-strings.h>

#include <lib/core/CHIPEncoding.h>

using namespace chip;

uint8_t emberAfStringLength(const uint8_t * buffer)
{
// The first byte specifies the length of the string. A length of 0xFF means
// the string is invalid and there is no character data.
return (buffer[0] == 0xFF ? 0 : buffer[0]);
}

uint16_t emberAfLongStringLength(const uint8_t * buffer)
{
// The first two bytes specify the length of the long string. A length of
// 0xFFFF means the string is invalid and there is no character data.
uint16_t length = Encoding::LittleEndian::Get16(buffer);
return (length == 0xFFFF ? 0 : length);
}

void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size)
{
if (src == nullptr)
{
dest[0] = 0; // Zero out the length of string
}
else if (src[0] == 0xFF)
{
dest[0] = src[0];
}
else
{
uint8_t length = emberAfStringLength(src);
if (size < length)
{
// Since we have checked that size < length, size must be able to fit into the type of length.
length = static_cast<decltype(length)>(size);
}
memmove(dest + 1, src + 1, length);
dest[0] = length;
}
}

void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size)
{
if (src == nullptr)
{
dest[0] = dest[1] = 0; // Zero out the length of string
}
else if ((src[0] == 0xFF) && (src[1] == 0xFF))
{
dest[0] = 0xFF;
dest[1] = 0xFF;
}
else
{
uint16_t length = emberAfLongStringLength(src);
if (size < length)
{
// Since we have checked that size < length, size must be able to fit into the type of length.
length = static_cast<decltype(length)>(size);
}
memmove(dest + 2, src + 2, length);
Encoding::LittleEndian::Put16(dest, length);
}
}
45 changes: 45 additions & 0 deletions src/app/util/ember-strings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <cstddef>
#include <cstdint>

/*
* @brief Function that determines the length of a zigbee Cluster Library string
* (where the first byte is assumed to be the length).
*/
uint8_t emberAfStringLength(const uint8_t * buffer);
/*
* @brief Function that determines the length of a zigbee Cluster Library long string.
* (where the first two bytes are assumed to be the length).
*/
uint16_t emberAfLongStringLength(const uint8_t * buffer);

/*
* @brief Function that copies a ZCL string type into a buffer. The size
* parameter should indicate the maximum number of characters to copy to the
* destination buffer not including the length byte.
*/
void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size);

/*
* @brief Function that copies a ZCL long string into a buffer. The size
* parameter should indicate the maximum number of characters to copy to the
* destination buffer not including the length bytes.
*/
void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size);
37 changes: 0 additions & 37 deletions src/app/util/message.cpp

This file was deleted.

1 change: 1 addition & 0 deletions src/app/util/mock/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ source_set("mock_ember") {

public_deps = [
"${chip_root}/src/app",
"${chip_root}/src/app/common:attribute-type",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support",
"${chip_root}/src/lib/support:testing_nlunit",
Expand Down
29 changes: 0 additions & 29 deletions src/app/util/mock/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,35 +234,6 @@ bool emberAfEndpointIndexIsEnabled(uint16_t index)
return index < GetMockNodeConfig().endpoints.size();
}

// This duplication of basic utilities is really unfortunate, but we can't link
// to the normal attribute-storage.cpp because we redefine some of its symbols
// above.
bool emberAfIsStringAttributeType(EmberAfAttributeType attributeType)
{
return (attributeType == ZCL_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
}

bool emberAfIsLongStringAttributeType(EmberAfAttributeType attributeType)
{
return (attributeType == ZCL_LONG_OCTET_STRING_ATTRIBUTE_TYPE || attributeType == ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE);
}

// And we don't have a good way to link to message.cpp either.
uint8_t emberAfStringLength(const uint8_t * buffer)
{
// The first byte specifies the length of the string. A length of 0xFF means
// the string is invalid and there is no character data.
return (buffer[0] == 0xFF ? 0 : buffer[0]);
}

uint16_t emberAfLongStringLength(const uint8_t * buffer)
{
// The first two bytes specify the length of the long string. A length of
// 0xFFFF means the string is invalid and there is no character data.
uint16_t length = Encoding::LittleEndian::Get16(buffer);
return (length == 0xFFFF ? 0 : length);
}

// This will find the first server that has the clusterId given from the index of endpoint.
bool emberAfContainsServerFromIndex(uint16_t index, ClusterId clusterId)
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/util/types_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#include <string.h> // For mem* functions.

#include <app/util/basic-types.h>
#include <lib/core/NodeId.h>
#include <lib/core/Optional.h>

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

/**
Expand Down
Loading

0 comments on commit 3673607

Please sign in to comment.