Skip to content

Commit

Permalink
[NXP][border router] Sync Matter BR code with updates from ot-nxp (#3…
Browse files Browse the repository at this point in the history
…7006)

* [NXP][border router] Sync Matter BR code with updates from ot-nxp

This commit moves all of the BR processing inside the ot-nxp platform code using the br_rtos_manager module.
On Matter side we are keeping only the initialization of the border router function.

Signed-off-by: Marius Preda <[email protected]>

* Restyled by clang-format

---------

Signed-off-by: Marius Preda <[email protected]>
Co-authored-by: Marius Preda <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent f044060 commit 30cb6ce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 110 deletions.
129 changes: 26 additions & 103 deletions src/platform/nxp/common/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,28 +469,20 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState()
event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange;
event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn);

#if CHIP_ENABLE_OPENTHREAD
// In case of boot, start the Border Router services including MDNS Server, otherwise inform of link state change
// The posted event will signal the application to restart the Matter mDNS server instance
bool bLinkState = event.InternetConnectivityChange.IPv6 == kConnectivity_Established ? true : false;
BrHandleStateChange(bLinkState);
#endif
if (haveIPv6Conn)
{
event.InternetConnectivityChange.ipAddress = IPAddress(*addr6);
#if CHIP_ENABLE_OPENTHREAD
// In case of boot, start the Border Router services including MDNS Server
// The posted event will signal the application to restart the Matter mDNS server instance
BrHandleStateChange();
#endif
}
err = PlatformMgr().PostEvent(&event);
VerifyOrDie(err == CHIP_NO_ERROR);

ChipLogProgress(DeviceLayer, "%s Internet connectivity %s", "IPv6", (haveIPv6Conn) ? "ESTABLISHED" : "LOST");
}

#if CHIP_ENABLE_OPENTHREAD
if (haveIPv6Conn && UpdateIp6AddrList())
{
UpdateMdnsHost();
}
#endif
}

void ConnectivityManagerImpl::_NetifExtCallback(struct netif * netif, netif_nsc_reason_t reason,
Expand Down Expand Up @@ -532,107 +524,38 @@ void ConnectivityManagerImpl::StartWiFiManagement()
}
}
#if CHIP_ENABLE_OPENTHREAD
void ConnectivityManagerImpl::BrHandleStateChange(bool bLinkState)
void ConnectivityManagerImpl::BrHandleStateChange()
{
if (mBorderRouterInit == false)
{
if (bLinkState)
{
struct netif * extNetIfPtr = static_cast<struct netif *>(net_get_mlan_handle());
struct netif * thrNetIfPtr = ThreadStackMgrImpl().ThreadNetIf();
otInstance * thrInstancePtr;

// Need to wait for the wifi to be connected because the mlan netif can be !=null but not initialized
// properly. If the thread netif is !=null it means that it was fully initialized

// Lock OT task ?
if ((thrNetIfPtr) && (mWiFiStationState == kWiFiStationState_Connected))
{
// Initalize internal interface variables, these can be used by other modules like the DNSSD Impl to
// get the underlying IP interface
Inet::InterfaceId tmpExtIf(extNetIfPtr);
Inet::InterfaceId tmpThrIf(thrNetIfPtr);
mExternalNetIf = tmpExtIf;
mThreadNetIf = tmpThrIf;

mBorderRouterInit = true;
// Check if OT instance is init
thrInstancePtr = ThreadStackMgrImpl().OTInstance();

BrInitPlatform(thrInstancePtr, extNetIfPtr, thrNetIfPtr);
BrInitServices();

UpdateIp6AddrList();
UpdateMdnsHost();
BorderAgentInit(thrInstancePtr, mHostname);
}
}
}
else
{
InfraIfLinkState(bLinkState);
}
}

void ConnectivityManagerImpl::UpdateMdnsHost()
{
CHIP_ERROR err = CHIP_NO_ERROR;
otMdnsHost mdnsHost;
struct netif * extNetIfPtr = static_cast<struct netif *>(net_get_mlan_handle());
struct netif * thrNetIfPtr = ThreadStackMgrImpl().ThreadNetIf();

if (strlen(mHostname) == 0)
{
otMdnsHost mdnsHost;
uint8_t macBuffer[ConfigurationManager::kPrimaryMACAddressLength];
MutableByteSpan mac(macBuffer);
err = DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac);
SuccessOrExit(err);

chip::Dnssd::MakeHostName(mHostname, sizeof(mHostname), mac);
}

mdnsHost.mAddresses = mIp6AddrList;
mdnsHost.mAddressesLength = mIp6AddrNum;
mdnsHost.mHostName = mHostname;
mdnsHost.mInfraIfIndex = netif_get_index(mExternalNetIf.GetPlatformInterface());

// Allways use ID 0 for host
otMdnsRegisterHost(ThreadStackMgrImpl().OTInstance(), &mdnsHost, 0, nullptr);

exit:
return;
}

bool ConnectivityManagerImpl::UpdateIp6AddrList()
{
const ip6_addr_t * addr6 = nullptr;
bool bAddrChange = false;
uint32_t newIp6AddrNum = 0;
struct netif * extNetIfPtr = mExternalNetIf.GetPlatformInterface();
uint32_t lwipIterator, addrListIterator;
// Need to wait for the wifi to be connected because the mlan netif can be !=null but not initialized
// properly. If the thread netif is !=null it means that it was fully initialized

for (lwipIterator = 0; lwipIterator < LWIP_IPV6_NUM_ADDRESSES; lwipIterator++)
{
if (ip6_addr_ispreferred(netif_ip6_addr_state(extNetIfPtr, lwipIterator)) && (mIp6AddrNum <= kMaxIp6Addr))
// Lock OT task ?
if ((thrNetIfPtr) && (mWiFiStationState == kWiFiStationState_Connected))
{
addr6 = netif_ip6_addr(extNetIfPtr, lwipIterator);
for (addrListIterator = 0; addrListIterator < kMaxIp6Addr; addrListIterator++)
{
if (0 == memcmp(&mIp6AddrList[addrListIterator].mFields.m32, addr6->addr, sizeof(Inet::IPAddress)))
{
break;
}
}
if (addrListIterator == kMaxIp6Addr)
{
bAddrChange |= true;
}
memcpy(&mIp6AddrList[newIp6AddrNum++].mFields.m32, addr6->addr, sizeof(Inet::IPAddress));
// Initalize internal interface variables, these can be used by other modules like the DNSSD Impl to
// get the underlying IP interface
Inet::InterfaceId tmpExtIf(extNetIfPtr);
Inet::InterfaceId tmpThrIf(thrNetIfPtr);
mExternalNetIf = tmpExtIf;
mThreadNetIf = tmpThrIf;
mBorderRouterInit = true;

DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac);
chip::Dnssd::MakeHostName(mHostname, sizeof(mHostname), mac);

BrInitPlatform(ThreadStackMgrImpl().OTInstance(), extNetIfPtr, thrNetIfPtr);
BrInitMdnsHost(mHostname);
}
}

bAddrChange |= (newIp6AddrNum != mIp6AddrNum) ? true : false;
mIp6AddrNum = newIp6AddrNum;

return bAddrChange;
}

Inet::InterfaceId ConnectivityManagerImpl::GetThreadInterface()
Expand Down
8 changes: 1 addition & 7 deletions src/platform/nxp/common/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,10 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
static constexpr uint32_t kWlanInitWaitMs = CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL;

#if CHIP_ENABLE_OPENTHREAD
static constexpr uint8_t kMaxIp6Addr = 3;

Inet::InterfaceId mThreadNetIf;
Inet::InterfaceId mExternalNetIf;

char mHostname[chip::Dnssd::kHostNameMaxLength + 1] = "";
otIp6Address mIp6AddrList[kMaxIp6Addr];
uint32_t mIp6AddrNum = 0;
#endif

static int _WlanEventCallback(enum wlan_event_reason event, void * data);
Expand All @@ -166,9 +162,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
void OnStationDisconnected(void);
void UpdateInternetConnectivityState(void);
#if CHIP_ENABLE_OPENTHREAD
void BrHandleStateChange(bool bLinkState);
void UpdateMdnsHost(void);
bool UpdateIp6AddrList(void);
void BrHandleStateChange();
#endif /* CHIP_DEVICE_CONFIG_ENABLE_THREAD */
#endif
/* CHIP_DEVICE_CONFIG_ENABLE_WPA */
Expand Down

0 comments on commit 30cb6ce

Please sign in to comment.