Skip to content

Commit 5ec949a

Browse files
committed
moving to char span and addressing the review comments
1 parent 0d673da commit 5ec949a

File tree

3 files changed

+64
-33
lines changed

3 files changed

+64
-33
lines changed

src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/* this file behaves like a config.h, comes first */
1919
#include <platform/internal/CHIPDeviceLayerInternal.h>
2020

21+
#include <lib/support/BytesToHex.h>
2122
#include <lib/support/CodeUtils.h>
2223
#include <lib/support/logging/CHIPLogging.h>
2324
#include <platform/ConnectivityManager.h>
@@ -442,17 +443,23 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
442443
{
443444
sl_wfx_mac_address_t macaddr;
444445
wfx_get_wifi_mac_addr(SL_WFX_STA_INTERFACE, &macaddr);
445-
if (mEndpointQueueFilter.SetHostName(ByteSpan(macaddr.octet)) == CHIP_NO_ERROR)
446+
char macStr[12] = {0};
447+
if(Encoding::BytesToHex(macaddr.octet, sizeof(macaddr.octet), macStr, sizeof(macStr),Encoding::HexFlags::kUppercase) == CHIP_NO_ERROR)
446448
{
447-
chip::Inet::UDPEndPointImpl::SetQueueFilter(&mEndpointQueueFilter);
448-
} else {
449-
ChipLogError(DeviceLayer, "Failed to set host name filter");
450-
}
449+
if (mEndpointQueueFilter.SetHostName(CharSpan(macStr)) == CHIP_NO_ERROR)
450+
{
451+
chip::Inet::UDPEndPointImpl::SetQueueFilter(&mEndpointQueueFilter);
452+
} else {
453+
ChipLogError(DeviceLayer, "Failed to set host name filter");
454+
}
455+
}
456+
else
457+
{
458+
ChipLogError(DeviceLayer, "Failed to convert MAC address to string");
459+
}
451460
}
452461
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
453462

454-
(void) PlatformMgr().PostEvent(&event);
455-
456463
if (haveIPv4Conn != hadIPv4Conn)
457464
{
458465
ChipLogProgress(DeviceLayer, "%s Internet connectivity %s", "IPv4", (haveIPv4Conn) ? "ESTABLISHED" : "LOST");

src/platform/silabs/EndpointQueueFilter.cpp

+42-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
#include "EndpointQueueFilter.h"
22
#include <string.h>
3+
#include <lib/support/BytesToHex.h>
34
#include <support/CodeUtils.h>
45
#include <support/logging/CHIPLogging.h>
5-
#include <string_view>
6-
76
namespace chip {
87
namespace Inet {
98

109
using FilterOutcome = EndpointQueueFilter::FilterOutcome;
1110

1211
namespace {
1312

14-
bool IsValidMdnsHostName(uint8_t * hostName)
13+
bool IsValidMdnsHostName(const CharSpan & hostName)
1514
{
16-
for (size_t i = 0; i < sizeof(hostName); ++i)
15+
for (size_t i = 0; i < hostName.size(); ++i)
1716
{
18-
char ch_data = hostName[i];
17+
char ch_data = *(hostName.data() + i);
1918
if (!((ch_data >= '0' && ch_data <= '9') || (ch_data >= 'A' && ch_data <= 'F')))
2019
{
2120
return false;
@@ -24,16 +23,36 @@ bool IsValidMdnsHostName(uint8_t * hostName)
2423
return true;
2524
}
2625

27-
bool IsMdnsBroadcastPacket(const void * endpoint, const IPPacketInfo & pktInfo, const System::PacketBufferHandle & pktPayload)
26+
bool IsMdnsBroadcastPacket(const IPPacketInfo & pktInfo, const System::PacketBufferHandle & pktPayload)
2827
{
2928
// if the packet is not a broadcast packet to mDNS port, drop it.
3029
VerifyOrReturnValue(pktInfo.DestPort == 5353, false);
3130
#if INET_CONFIG_ENABLE_IPV4
3231
ip_addr_t mdnsIPv4BroadcastAddr = IPADDR4_INIT_BYTES(224, 0, 0, 251);
33-
VerifyOrReturnValue(pktInfo.DestAddress == Inet::IPAddress(mdnsIPv4BroadcastAddr), false);
32+
if(pktInfo.DestAddress == Inet::IPAddress(mdnsIPv4BroadcastAddr))
33+
{
34+
return true;
35+
}
3436
#endif
3537
ip_addr_t mdnsIPv6BroadcastAddr = IPADDR6_INIT_HOST(0xFF020000, 0, 0, 0xFB);
36-
VerifyOrReturnValue(pktInfo.DestAddress == Inet::IPAddress(mdnsIPv6BroadcastAddr), false);
38+
if(pktInfo.DestAddress == Inet::IPAddress(mdnsIPv6BroadcastAddr))
39+
{
40+
return true;
41+
}
42+
return false;
43+
}
44+
45+
static bool CaseInsensitiveCompare(const CharSpan & buffer1, const CharSpan & buffer2, size_t size)
46+
{
47+
for (size_t i = 0; i < size; ++i)
48+
{
49+
char byte1 = (*(buffer1.data() + i) >= 'A' && *(buffer1.data() + i) <= 'Z') ? *(buffer1.data() + i) - 'A' + 'a' : *(buffer1.data() + i);
50+
char byte2 = (*(buffer2.data() + i) >= 'A' && *(buffer2.data() + i) <= 'Z') ? *(buffer2.data() + i) - 'A' + 'a' : *(buffer2.data() + i);
51+
if (byte1 != byte2)
52+
{
53+
return false;
54+
}
55+
}
3756
return true;
3857
}
3958

@@ -49,9 +68,17 @@ bool PayloadContainsCaseInsensitive(const System::PacketBufferHandle & payload,
4968
return false;
5069
}
5170

52-
std::basic_string_view<uint8_t> payloadView(payload->Start(), payload->TotalLength());
53-
std::basic_string_view<uint8_t> patternView(pattern.data(), pattern.size());
54-
return payloadView.find(patternView) != std::basic_string_view<uint8_t>::npos;
71+
CharSpan payloadView(reinterpret_cast<char *>(payload->Start()), payload->TotalLength());
72+
CharSpan patternView((char *)(pattern.data()), pattern.size());
73+
74+
for (size_t i = 0; i <= payloadView.size() - patternView.size(); ++i)
75+
{
76+
if (CaseInsensitiveCompare(payloadView.SubSpan(i, patternView.size()), patternView, patternView.size()))
77+
{
78+
return true;
79+
}
80+
}
81+
return false;
5582
}
5683

5784
} // namespace
@@ -66,15 +93,13 @@ FilterOutcome HostNameFilter::Filter(const void * endpoint, const IPPacketInfo &
6693
{
6794
return FilterOutcome::kAllowPacket;
6895
}
69-
7096
return FilterOutcome::kDropPacket;
7197
}
7298

73-
CHIP_ERROR HostNameFilter::SetHostName(const ByteSpan & hostName)
99+
CHIP_ERROR HostNameFilter::SetHostName(const CharSpan & hostName)
74100
{
75-
const uint8_t * p = hostName.data();
76-
snprintf((char *) mHostName, sizeof(mHostName), "%02X%02X%02X%02X%02X%02X", p[0], p[1], p[2], p[3], p[4], p[5]);
77-
ReturnErrorCodeIf(!IsValidMdnsHostName(mHostName), CHIP_ERROR_INVALID_ARGUMENT);
101+
ReturnErrorCodeIf(!IsValidMdnsHostName(hostName), CHIP_ERROR_INVALID_ARGUMENT);
102+
memcpy(mHostName, hostName.data(), hostName.size());
78103
return CHIP_NO_ERROR;
79104
}
80105

@@ -90,7 +115,7 @@ FilterOutcome EndpointQueueFilter::FilterBeforeEnqueue(const void * endpoint, co
90115
VerifyOrReturnError(FilterOutcome::kAllowPacket == mTooManyFilter.FilterBeforeEnqueue(endpoint, pktInfo, pktPayload),
91116
FilterOutcome::kDropPacket);
92117

93-
if (!IsMdnsBroadcastPacket(endpoint, pktInfo, pktPayload))
118+
if (!IsMdnsBroadcastPacket(pktInfo, pktPayload))
94119
{
95120
return FilterOutcome::kAllowPacket;
96121
}

src/platform/silabs/EndpointQueueFilter.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@ struct EndpointFilter
2626
{
2727
virtual ~EndpointFilter() = default;
2828
virtual EndpointQueueFilter::FilterOutcome Filter(const void * endpoint, const IPPacketInfo & pktInfo,
29-
const chip::System::PacketBufferHandle & pktPayload) = 0;
29+
const System::PacketBufferHandle & pktPayload) = 0;
3030
};
3131

3232
struct HostNameFilter : EndpointFilter
3333
{
34-
static constexpr size_t kHostNameLengthMax = 13; // 6 bytes in hex and null terminator.
34+
static constexpr size_t kHostNameLengthMax = 12; // 6 bytes in hex.
3535

3636
EndpointQueueFilter::FilterOutcome Filter(const void * endpoint, const IPPacketInfo & pktInfo,
37-
const chip::System::PacketBufferHandle & pktPayload) override;
37+
const System::PacketBufferHandle & pktPayload) override;
3838

39-
// CHIP_ERROR SetHostName(const chip::CharSpan & name);
40-
CHIP_ERROR SetHostName(const chip::ByteSpan & addr);
39+
CHIP_ERROR SetHostName(const CharSpan & addr);
4140

4241
private:
4342
uint8_t mHostName[kHostNameLengthMax] = { 0 };
@@ -46,7 +45,7 @@ struct HostNameFilter : EndpointFilter
4645

4746
namespace SilabsEndpointQueueFilter {
4847

49-
class EndpointQueueFilter : public chip::Inet::EndpointQueueFilter
48+
class EndpointQueueFilter : public Inet::EndpointQueueFilter
5049
{
5150
public:
5251
static constexpr size_t kDefaultAllowedQueuedPackets = 10;
@@ -55,12 +54,12 @@ class EndpointQueueFilter : public chip::Inet::EndpointQueueFilter
5554
EndpointQueueFilter(size_t maxAllowedQueuedPackets);
5655

5756
FilterOutcome FilterBeforeEnqueue(const void * endpoint, const IPPacketInfo & pktInfo,
58-
const chip::System::PacketBufferHandle & pktPayload) override;
57+
const System::PacketBufferHandle & pktPayload) override;
5958

6059
FilterOutcome FilterAfterDequeue(const void * endpoint, const IPPacketInfo & pktInfo,
61-
const chip::System::PacketBufferHandle & pktPayload);
60+
const System::PacketBufferHandle & pktPayload);
6261

63-
CHIP_ERROR SetHostName(const chip::ByteSpan & addr) { return mHostNameFilter.SetHostName(addr); }
62+
CHIP_ERROR SetHostName(const CharSpan & addr) { return mHostNameFilter.SetHostName(addr); }
6463

6564
private:
6665
DropIfTooManyQueuedPacketsFilter mTooManyFilter;

0 commit comments

Comments
 (0)