Skip to content

Commit

Permalink
Filter by vendor/product id (if available) in SetUpCodePairer. (#23019)
Browse files Browse the repository at this point in the history
* Filter by vendor/product id (if available) in SetUpCodePairer.

If we have vendor/product information, from a QR code or long manual code, we
should use that to filter out advertisements for other vendors or products.

* Address review comment.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Dec 6, 2023
1 parent bce7291 commit db26fda
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
42 changes: 33 additions & 9 deletions src/controller/SetUpCodePairer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,20 @@ CHIP_ERROR SetUpCodePairer::StartDiscoverOverIP(SetupPayload & payload)
auto & discriminator = payload.discriminator;
if (discriminator.IsShortDiscriminator())
{
currentFilter.type = Dnssd::DiscoveryFilterType::kShortDiscriminator;
currentFilter.code = discriminator.GetShortValue();
mCurrentFilter.type = Dnssd::DiscoveryFilterType::kShortDiscriminator;
mCurrentFilter.code = discriminator.GetShortValue();
}
else
{
currentFilter.type = Dnssd::DiscoveryFilterType::kLongDiscriminator;
currentFilter.code = discriminator.GetLongValue();
mCurrentFilter.type = Dnssd::DiscoveryFilterType::kLongDiscriminator;
mCurrentFilter.code = discriminator.GetLongValue();
}
mPayloadVendorID = payload.vendorID;
mPayloadProductID = payload.productID;

// Handle possibly-sync callbacks.
mWaitingForDiscovery[kIPTransport] = true;
CHIP_ERROR err = mCommissioner->DiscoverCommissionableNodes(currentFilter);
CHIP_ERROR err = mCommissioner->DiscoverCommissionableNodes(mCurrentFilter);
if (err != CHIP_NO_ERROR)
{
mWaitingForDiscovery[kIPTransport] = false;
Expand All @@ -186,7 +189,9 @@ CHIP_ERROR SetUpCodePairer::StopConnectOverIP()
ChipLogDetail(Controller, "Stopping commissioning discovery over DNS-SD");

mWaitingForDiscovery[kIPTransport] = false;
currentFilter.type = Dnssd::DiscoveryFilterType::kNone;
mCurrentFilter.type = Dnssd::DiscoveryFilterType::kNone;
mPayloadVendorID = kNotAvailable;
mPayloadProductID = kNotAvailable;

mCommissioner->StopCommissionableDiscovery();
return CHIP_NO_ERROR;
Expand Down Expand Up @@ -286,19 +291,38 @@ void SetUpCodePairer::OnBLEDiscoveryError(CHIP_ERROR err)
}
#endif // CONFIG_NETWORK_LAYER_BLE

bool SetUpCodePairer::IdIsPresent(uint16_t vendorOrProductID)
{
return vendorOrProductID != kNotAvailable;
}

bool SetUpCodePairer::NodeMatchesCurrentFilter(const Dnssd::DiscoveredNodeData & nodeData) const
{
if (nodeData.commissionData.commissioningMode == 0)
{
return false;
}

switch (currentFilter.type)
// The advertisement may not include a vendor id.
if (IdIsPresent(mPayloadVendorID) && IdIsPresent(nodeData.commissionData.vendorId) &&
mPayloadVendorID != nodeData.commissionData.vendorId)
{
return false;
}

// The advertisement may not include a product id.
if (IdIsPresent(mPayloadProductID) && IdIsPresent(nodeData.commissionData.productId) &&
mPayloadProductID != nodeData.commissionData.productId)
{
return false;
}

switch (mCurrentFilter.type)
{
case Dnssd::DiscoveryFilterType::kShortDiscriminator:
return ((nodeData.commissionData.longDiscriminator >> 8) & 0x0F) == currentFilter.code;
return ((nodeData.commissionData.longDiscriminator >> 8) & 0x0F) == mCurrentFilter.code;
case Dnssd::DiscoveryFilterType::kLongDiscriminator:
return nodeData.commissionData.longDiscriminator == currentFilter.code;
return nodeData.commissionData.longDiscriminator == mCurrentFilter.code;
default:
return false;
}
Expand Down
10 changes: 9 additions & 1 deletion src/controller/SetUpCodePairer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
#endif // CONFIG_NETWORK_LAYER_BLE

bool NodeMatchesCurrentFilter(const Dnssd::DiscoveredNodeData & nodeData) const;
Dnssd::DiscoveryFilter currentFilter;
static bool IdIsPresent(uint16_t vendorOrProductID);

Dnssd::DiscoveryFilter mCurrentFilter;
// The vendor id and product id from the SetupPayload. They may be 0, which
// indicates "not available" (e.g. because the SetupPayload came from a
// short manual code). In that case we should not filter on those values.
static constexpr uint16_t kNotAvailable = 0;
uint16_t mPayloadVendorID = kNotAvailable;
uint16_t mPayloadProductID = kNotAvailable;

DeviceCommissioner * mCommissioner = nullptr;
System::Layer * mSystemLayer = nullptr;
Expand Down

0 comments on commit db26fda

Please sign in to comment.