From 04137839f0768099e2444c339aeea15dcc5ff870 Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Sat, 4 May 2024 00:09:07 +0800 Subject: [PATCH 1/2] esp32: service type should be case-insensitive for endpoint filter of mdns packets (#33206) --- src/platform/ESP32/ESP32EndpointQueueFilter.h | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/platform/ESP32/ESP32EndpointQueueFilter.h b/src/platform/ESP32/ESP32EndpointQueueFilter.h index 031ea29b64d798..aa283d9b7fd770 100644 --- a/src/platform/ESP32/ESP32EndpointQueueFilter.h +++ b/src/platform/ESP32/ESP32EndpointQueueFilter.h @@ -44,7 +44,8 @@ class ESP32EndpointQueueFilter : public EndpointQueueFilter } // Drop the mDNS packets which don't contain 'matter' or ''. const uint8_t matterBytes[] = { 'm', 'a', 't', 't', 'e', 'r' }; - if (PayloadContains(pktPayload, ByteSpan(matterBytes)) || PayloadContainsHostNameCaseInsensitive(pktPayload)) + if (PayloadContainsCaseInsensitive(pktPayload, ByteSpan(matterBytes)) || + PayloadContainsCaseInsensitive(pktPayload, ByteSpan(mHostNameBuffer))) { return FilterOutcome::kAllowPacket; } @@ -79,34 +80,34 @@ class ESP32EndpointQueueFilter : public EndpointQueueFilter return false; } - static bool PayloadContains(const chip::System::PacketBufferHandle & payload, const chip::ByteSpan & byteSpan) + static bool BytesCaseInsensitiveCompare(const uint8_t * buf1, const uint8_t * buf2, size_t size) { - if (payload->HasChainedBuffer() || payload->TotalLength() < byteSpan.size()) - { - return false; - } - for (size_t i = 0; i <= payload->TotalLength() - byteSpan.size(); ++i) + for (size_t i = 0; i < size; ++i) { - if (memcmp(payload->Start() + i, byteSpan.data(), byteSpan.size()) == 0) + uint8_t byte1 = (buf1[i] >= 'A' && buf1[i] <= 'Z') ? buf1[i] - 'A' + 'a' : buf1[i]; + uint8_t byte2 = (buf2[i] >= 'A' && buf2[i] <= 'Z') ? buf2[i] - 'A' + 'a' : buf2[i]; + if (byte1 != byte2) { - return true; + return false; } } - return false; + return true; } - bool PayloadContainsHostNameCaseInsensitive(const chip::System::PacketBufferHandle & payload) + static bool PayloadContainsCaseInsensitive(const chip::System::PacketBufferHandle & payload, const chip::ByteSpan & byteSpan) { - uint8_t hostNameLowerCase[12]; - memcpy(hostNameLowerCase, mHostNameBuffer, sizeof(mHostNameBuffer)); - for (size_t i = 0; i < sizeof(hostNameLowerCase); ++i) + if (payload->HasChainedBuffer() || payload->TotalLength() < byteSpan.size()) + { + return false; + } + for (size_t i = 0; i <= payload->TotalLength() - byteSpan.size(); ++i) { - if (hostNameLowerCase[i] <= 'F' && hostNameLowerCase[i] >= 'A') + if (BytesCaseInsensitiveCompare(payload->Start() + i, byteSpan.data(), byteSpan.size())) { - hostNameLowerCase[i] = static_cast('a' + hostNameLowerCase[i] - 'A'); + return true; } } - return PayloadContains(payload, ByteSpan(mHostNameBuffer)) || PayloadContains(payload, ByteSpan(hostNameLowerCase)); + return false; } static bool IsValidMdnsHostName(const chip::CharSpan & hostName) From e07953d8a1af689c3f6e559e8fec70d10947cfc9 Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Thu, 11 Apr 2024 12:13:15 +0800 Subject: [PATCH 2/2] Add checks for localization-configuration cluster and time-format-localization cluster init callback (#32747) * Add checks for localization-configuration cluster and time-format-localization cluster * Update src/app/clusters/time-format-localization-server/time-format-localization-server.cpp Co-authored-by: Boris Zbarsky --------- Co-authored-by: Boris Zbarsky --- .../localization-configuration-server.cpp | 2 +- .../time-format-localization-server.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/app/clusters/localization-configuration-server/localization-configuration-server.cpp b/src/app/clusters/localization-configuration-server/localization-configuration-server.cpp index 88283325c7c380..5f059e73b9a1d3 100644 --- a/src/app/clusters/localization-configuration-server/localization-configuration-server.cpp +++ b/src/app/clusters/localization-configuration-server/localization-configuration-server.cpp @@ -208,7 +208,7 @@ void emberAfLocalizationConfigurationClusterServerInitCallback(EndpointId endpoi it->Release(); - if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) + if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND && validLocaleCached) { // If initial value is not one of the allowed values, write the valid value it. status = ActiveLocale::Set(endpoint, validLocale); diff --git a/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp b/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp index 08e90207760603..ff38290c21f8d4 100644 --- a/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp +++ b/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp @@ -78,6 +78,12 @@ class AutoReleaseIterator TimeFormatLocalizationAttrAccess gAttrAccess; +bool HasFeature(EndpointId endpoint, Feature feature) +{ + uint32_t featureMap; + return FeatureMap::Get(endpoint, &featureMap) == Status::Success ? (featureMap & to_underlying(feature)) : false; +} + CHIP_ERROR TimeFormatLocalizationAttrAccess::ReadSupportedCalendarTypes(AttributeValueEncoder & aEncoder) { DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider(); @@ -202,6 +208,10 @@ Protocols::InteractionModel::Status MatterTimeFormatLocalizationClusterServerPre void emberAfTimeFormatLocalizationClusterServerInitCallback(EndpointId endpoint) { + if (!HasFeature(endpoint, Feature::kCalendarFormat)) + { + return; + } CalendarTypeEnum calendarType; CalendarTypeEnum validType; Status status = ActiveCalendarType::Get(endpoint, &calendarType);