Skip to content

Commit

Permalink
[v1.3 cherry-pick] cherrypick #32747 and #33206 (#33438)
Browse files Browse the repository at this point in the history
* esp32: service type should be case-insensitive for endpoint filter of mdns packets (#33206)

* 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 <[email protected]>

---------

Co-authored-by: Boris Zbarsky <[email protected]>

---------

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
wqx6 and bzbarsky-apple committed May 14, 2024
1 parent c76613c commit 1dc089c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 18 additions & 17 deletions src/platform/ESP32/ESP32EndpointQueueFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class ESP32EndpointQueueFilter : public EndpointQueueFilter
}
// Drop the mDNS packets which don't contain 'matter' or '<device-hostname>'.
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;
}
Expand Down Expand Up @@ -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<uint8_t>('a' + hostNameLowerCase[i] - 'A');
return true;
}
}
return PayloadContains(payload, ByteSpan(mHostNameBuffer)) || PayloadContains(payload, ByteSpan(hostNameLowerCase));
return false;
}

static bool IsValidMdnsHostName(const chip::CharSpan & hostName)
Expand Down

0 comments on commit 1dc089c

Please sign in to comment.