Skip to content

Commit

Permalink
binding: Add supports of self binding and enable multicast loop back …
Browse files Browse the repository at this point in the history
…by default
  • Loading branch information
wqx6 committed Dec 16, 2024
1 parent cfdaf79 commit cc67e7c
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ static void BoundDeviceChangedHandler(const EmberBindingTableEntry & binding, ch
ChipLogError(NotSpecified, "OnOff command failed: %" CHIP_ERROR_FORMAT, error.Format());
};

VerifyOrDie(peer_device != nullptr && peer_device->ConnectionReady());
if (!peer_device)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
VerifyOrDie(peer_device->ConnectionReady());
if (sSwitchOnOffState)
{
Clusters::OnOff::Commands::On::Type onCommand;
Expand Down
10 changes: 10 additions & 0 deletions examples/all-clusters-app/ameba/main/BindingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
// It should always enter here if isReadAttribute is true
if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (!peer_device)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::Identify::Id:
Expand Down Expand Up @@ -105,6 +110,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (!peer_device)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::Identify::Id:
Expand Down
6 changes: 6 additions & 0 deletions examples/all-clusters-app/nxp/mw320/binding-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ static void BoundDeviceChangedHandler(const EmberBindingTableEntry & binding, ch
return;
}

if (binding.type == MATTER_UNICAST_BINDING && peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}

if (binding.type == MATTER_UNICAST_BINDING && binding.local == 1 &&
binding.clusterId.value_or(Clusters::OnOff::Id) == Clusters::OnOff::Id)
{
Expand Down
5 changes: 5 additions & 0 deletions examples/light-switch-app/ameba/main/BindingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::OnOff::Id:
Expand Down
5 changes: 5 additions & 0 deletions examples/light-switch-app/asr/src/BindingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ void BindingHandler::LightSwitchChangedHandler(const EmberBindingTableEntry & aB
}
else if (aBinding.type == MATTER_UNICAST_BINDING && !data->IsGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->ClusterId)
{
case Clusters::OnOff::Id:
Expand Down
5 changes: 5 additions & 0 deletions examples/light-switch-app/cc13x4_26x4/src/BindingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::OnOff::Id:
Expand Down
5 changes: 5 additions & 0 deletions examples/light-switch-app/esp32/main/BindingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::OnOff::Id:
Expand Down
5 changes: 5 additions & 0 deletions examples/light-switch-app/genio/src/BindingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::OnOff::Id:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ void BindingHandler::LightSwitchChangedHandler(const EmberBindingTableEntry & bi
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->IsGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->ClusterId)
{
case Clusters::OnOff::Id:
Expand Down
5 changes: 5 additions & 0 deletions examples/light-switch-app/nrfconnect/main/BindingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ void BindingHandler::LightSwitchChangedHandler(const EmberBindingTableEntry & bi
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->IsGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->ClusterId)
{
case Clusters::OnOff::Id:
Expand Down
3 changes: 1 addition & 2 deletions examples/light-switch-app/qpg/src/binding-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ static void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Op
case Clusters::OnOff::Id:
case Clusters::LevelControl::Id:
case Clusters::ColorControl::Id:
// TODO should not happen?
VerifyOrReturn(peer_device != nullptr, ChipLogError(NotSpecified, "Peer is nullptr"));
VerifyOrReturn(peer_device != nullptr, ChipLogProgress(NotSpecified, "Binding to self"));

ProcessSwitchUnicastBindingCommand(data->commandId, binding, peer_device->GetExchangeManager(),
peer_device->GetSecureSession().Value(), data);
Expand Down
5 changes: 5 additions & 0 deletions examples/light-switch-app/silabs/src/BindingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::OnOff::Id:
Expand Down
10 changes: 10 additions & 0 deletions examples/light-switch-app/telink/src/binding-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
// It should always enter here if isReadAttribute is true
if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::Identify::Id:
Expand All @@ -142,6 +147,11 @@ void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, Operation
}
else if (binding.type == MATTER_UNICAST_BINDING && !data->isGroup)
{
if (peer_device == nullptr)
{
ChipLogProgress(NotSpecified, "Binding to self");
return;
}
switch (data->clusterId)
{
case Clusters::Identify::Id:
Expand Down
30 changes: 24 additions & 6 deletions src/app/clusters/bindings/BindingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,20 @@ class BindingFabricTableDelegate : public chip::FabricTable::Delegate

BindingFabricTableDelegate gFabricTableDelegate;

} // namespace
bool IsSelfNodeId(const chip::ScopedNodeId & nodeId)
{
chip::FabricTable & fabricTable = chip::Server::GetInstance().GetFabricTable();
for (const chip::FabricInfo & fabricInfo : fabricTable)
{
if (fabricInfo.GetScopedNodeId() == nodeId)
{
return true;
}
}
return false;
}

namespace {} // namespace
} // namespace

namespace chip {

Expand Down Expand Up @@ -189,10 +200,17 @@ CHIP_ERROR BindingManager::NotifyBoundClusterChanged(EndpointId endpoint, Cluste
{
if (iter->type == MATTER_UNICAST_BINDING)
{
error = mPendingNotificationMap.AddPendingNotification(iter.GetIndex(), bindingContext);
SuccessOrExit(error);
error = EstablishConnection(ScopedNodeId(iter->nodeId, iter->fabricIndex));
SuccessOrExit(error);
if (IsSelfNodeId(ScopedNodeId(iter->fabricIndex, iter->nodeId)))
{
mBoundDeviceChangedHandler(*iter, nullptr, bindingContext->GetContext());
}
else
{
error = mPendingNotificationMap.AddPendingNotification(iter.GetIndex(), bindingContext);
SuccessOrExit(error);
error = EstablishConnection(ScopedNodeId(iter->nodeId, iter->fabricIndex));
SuccessOrExit(error);
}
}
else if (iter->type == MATTER_MULTICAST_BINDING)
{
Expand Down
3 changes: 2 additions & 1 deletion src/app/clusters/bindings/BindingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ namespace chip {
* The connection is managed by the stack and peer_device is guaranteed to be available.
* The application shall decide the content to be sent to the peer.
*
* For unicast bindings peer_device will be a connected peer and group will be empty.
* For unicast bindings to other devices, peer_device will be a connected peer.
* For unicast bindings to self, peer_device will be nullptr.
* For multicast bindings peer_device will be nullptr.
*
* E.g. The application will send on/off commands to peer for the OnOff cluster.
Expand Down
5 changes: 3 additions & 2 deletions src/inet/UDPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ uint16_t UDPEndPointImplLwIP::GetBoundPort() const

CHIP_ERROR UDPEndPointImplLwIP::ListenImpl()
{
mLwIPEndPointType = LwIPEndPointType::UDP;
RunOnTCPIP([this]() { udp_recv(mUDP, LwIPReceiveUDPMessage, this); });
return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -420,11 +421,11 @@ CHIP_ERROR UDPEndPointImplLwIP::SetMulticastLoopback(IPVersion aIPVersion, bool
{
if (aLoopback)
{
udp_set_flags(mUDP, UDP_FLAGS_MULTICAST_LOOP);
RunOnTCPIP([this]() { udp_set_flags(mUDP, UDP_FLAGS_MULTICAST_LOOP); });
}
else
{
udp_clear_flags(mUDP, UDP_FLAGS_MULTICAST_LOOP);
RunOnTCPIP([this]() { udp_clear_flags(mUDP, UDP_FLAGS_MULTICAST_LOOP); });
}
return CHIP_NO_ERROR;
}
Expand Down
11 changes: 11 additions & 0 deletions src/transport/raw/UDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ CHIP_ERROR UDP::MulticastGroupJoinLeave(const Transport::PeerAddress & address,

if (join)
{
#if INET_CONFIG_ENABLE_IPV4
if (mUDPEndPoint->SetMulticastLoopback(address.GetIPAddress().IsIPv4() ? Inet::kIPVersion_4 : Inet::kIPVersion_6, true) !=
CHIP_NO_ERROR)
#else
if (mUDPEndPoint->SetMulticastLoopback(Inet::kIPVersion_6, true) != CHIP_NO_ERROR)
#endif
{
ChipLogError(Inet,
"Failed to set multicast loop back, this could lead to that the device cannot receive multicast message "
"sent by itself");
}
ChipLogProgress(Inet, "Joining Multicast Group with address %s", addressStr);
return mUDPEndPoint->JoinMulticastGroup(mUDPEndPoint->GetBoundInterface(), address.GetIPAddress());
}
Expand Down

0 comments on commit cc67e7c

Please sign in to comment.