Skip to content

Commit

Permalink
zigbee: Improves network formation sequence of events
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Gallegos Ramonet committed Dec 30, 2024
1 parent e7c52a4 commit b17fc02
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 40 deletions.
14 changes: 9 additions & 5 deletions src/zigbee/examples/zigbee-nwk-routing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,18 @@ NwkNetworkDiscoveryConfirm(Ptr<ZigbeeStack> stack, NlmeNetworkDiscoveryConfirmPa

if (params.m_status == NwkStatus::SUCCESS)
{
std::cout << " Network discovery confirm Received. Networks found:\n";
std::cout << " Network discovery confirm Received. Networks found ("
<< params.m_netDescList.size() << "):\n";

for (const auto& netDescriptor : params.m_netDescList)
{
std::cout << " ExtPanID: 0x" << std::hex << netDescriptor.m_extPanId << std::dec
<< " CH: " << static_cast<uint32_t>(netDescriptor.m_logCh) << std::hex
<< " Pan Id: 0x" << netDescriptor.m_panId << " stackprofile " << std::dec
<< static_cast<uint32_t>(netDescriptor.m_stackProfile) << "\n";
std::cout << " ExtPanID: 0x" << std::hex << netDescriptor.m_extPanId << "\n"
<< std::dec << " CH: " << static_cast<uint32_t>(netDescriptor.m_logCh)
<< "\n"
<< std::hex << " Pan ID: 0x" << netDescriptor.m_panId << "\n"
<< " Stack profile: " << std::dec
<< static_cast<uint32_t>(netDescriptor.m_stackProfile) << "\n"
<< "--------------------\n";
}

NlmeJoinRequestParams joinParams;
Expand Down
14 changes: 2 additions & 12 deletions src/zigbee/examples/zigbee-nwk-routing2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,10 @@
* Network Extended PAN id: 0X000000000000CA:FE (based on the PAN coordinator address)
*
*
* [Coordinator] ZC (dev0): [00:00:00:00:00:00:CA:FE] [00:00]
* [Router 1] ZR1 (dev1): [00:00:00:00:00:00:00:01] [short addr assigned by ZC]
* [Router 2] ZR2 (dev2): [00:00:00:00:00:00:00:02] [short addr assigned by ZR1]
* [Router 3] ZR3 (dev3): [00:00:00:00:00:00:00:03] [short addr assigned by ZR2]
* [Router 4] ZR4 (dev4): [00:00:00:00:00:00:00:04] [short addr assigned by ZR4]
*
* Topology:
*
* ZC--------ZR1------------ZR2----------ZR3
* |
* |
* ZR4
*
*
* Grid Topology
*
*
*
Expand Down Expand Up @@ -202,7 +192,7 @@ int
main(int argc, char* argv[])
{
LogComponentEnableAll(LogLevel(LOG_PREFIX_TIME | LOG_PREFIX_FUNC | LOG_PREFIX_NODE));
// LogComponentEnable("ZigbeeNwk", LOG_LEVEL_DEBUG);
LogComponentEnable("ZigbeeNwk", LOG_LEVEL_DEBUG);
// LogComponentEnable("LrWpanCsmaCa", LOG_LEVEL_DEBUG);
// LogComponentEnable("LrWpanMac", LOG_LEVEL_DEBUG);
// LogComponentEnable("LrWpanPhy", LOG_LEVEL_DEBUG);
Expand Down
69 changes: 52 additions & 17 deletions src/zigbee/model/zigbee-nwk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ ZigbeeNwk::NotifyConstructionCompleted()

m_netFormParams = {};
m_netFormParamsGen = nullptr;
m_beaconPayload = nullptr;
m_nwkNetworkAddress = Mac16Address("ff:ff");
m_nwkPanId = 0xffff;
m_nwkExtendedPanId = 0xffffffffffffffff;
Expand Down Expand Up @@ -1461,14 +1462,16 @@ ZigbeeNwk::MlmeStartConfirm(MlmeStartConfirmParams params)
}
else
{
UpdateBeaconPayload();
UpdateBeaconPayloadLength();
}
}
}
void
ZigbeeNwk::MlmeSetConfirm(MlmeSetConfirmParams params)
{
NS_LOG_FUNCTION(this << params.id);
if (m_pendPrimitiveNwk == NLME_NETWORK_FORMATION)
{
if (params.m_status == MacStatus::SUCCESS &&
Expand All @@ -1493,6 +1496,11 @@ ZigbeeNwk::MlmeSetConfirm(MlmeSetConfirmParams params)
startParams.m_panCoor = true;
m_mac->MlmeStartRequest(startParams);
}
else if (params.m_status == MacStatus::SUCCESS &&
params.id == MacPibAttributeIdentifier::macBeaconPayloadLength)
{
UpdateBeaconPayload();
}
else
{
m_pendPrimitiveNwk = NLDE_NLME_NONE;
Expand All @@ -1509,20 +1517,33 @@ ZigbeeNwk::MlmeSetConfirm(MlmeSetConfirmParams params)
}
else if (m_pendPrimitiveNwk == NLME_JOIN_INDICATION)
{
NlmeJoinIndicationParams joinIndParams = m_joinIndParams;
if (params.m_status == MacStatus::SUCCESS &&
params.id == MacPibAttributeIdentifier::macBeaconPayloadLength)
{
UpdateBeaconPayload();
}
else
{
NlmeJoinIndicationParams joinIndParams = m_joinIndParams;
m_pendPrimitiveNwk = NLDE_NLME_NONE;
m_joinIndParams = {};
m_pendPrimitiveNwk = NLDE_NLME_NONE;
m_joinIndParams = {};
if (!m_nlmeJoinIndicationCallback.IsNull())
{
m_nlmeJoinIndicationCallback(joinIndParams);
if (!m_nlmeJoinIndicationCallback.IsNull())
{
m_nlmeJoinIndicationCallback(joinIndParams);
}
}
}
else if (m_pendPrimitiveNwk == NLME_START_ROUTER)
{
if (params.m_status == MacStatus::SUCCESS &&
params.id == MacPibAttributeIdentifier::macBeaconPayload)
params.id == MacPibAttributeIdentifier::macBeaconPayloadLength)
{
UpdateBeaconPayload();
}
else if (params.m_status == MacStatus::SUCCESS &&
params.id == MacPibAttributeIdentifier::macBeaconPayload)
{
m_pendPrimitiveNwk = NLDE_NLME_NONE;
if (!m_nlmeStartRouterConfirmCallback.IsNull())
Expand Down Expand Up @@ -1559,10 +1580,11 @@ ZigbeeNwk::MlmeGetConfirm(MacStatus status,
capaInfo.SetDeviceType(zigbee::MacDeviceType::ROUTER);
m_nwkCapabilityInformation = capaInfo.GetCapability();
// Set Beacon payload before starting a network
// Set Beacon payload size followed by the beacon payload content
// before starting a network
// See Figure 3-37 Establishing a Network
// See also 3.6.7.
UpdateBeaconPayload();
UpdateBeaconPayloadLength();
}
else
{
Expand Down Expand Up @@ -1687,7 +1709,7 @@ ZigbeeNwk::MlmeCommStatusIndication(MlmeCommStatusIndicationParams params)
m_joinIndParams.m_rejoinNetwork == ASSOCIATION)
{
m_pendPrimitiveNwk = NLME_JOIN_INDICATION;
UpdateBeaconPayload();
UpdateBeaconPayloadLength();
}
else
{
Expand Down Expand Up @@ -3025,7 +3047,7 @@ ZigbeeNwk::AssignStreams(int64_t stream)
}
void
ZigbeeNwk::UpdateBeaconPayload()
ZigbeeNwk::UpdateBeaconPayloadLength()
{
NS_LOG_FUNCTION(this);
Expand All @@ -3037,14 +3059,27 @@ ZigbeeNwk::UpdateBeaconPayload()
beaconPayloadHeader.SetExtPanId(m_nwkExtendedPanId);
beaconPayloadHeader.SetTxOffset(0xFFFFFF);
// TODO: beaconPayload.SetNwkUpdateId(m_nwkUpdateId);
Ptr<Packet> payload = Create<Packet>();
payload->AddHeader(beaconPayloadHeader);
// Extract octets from payload and copy them into the macBeaconPayload attribute
// Set the beacon payload length in the MAC PIBs and then proceed to
// update the beacon payload
m_beaconPayload = Create<Packet>();
m_beaconPayload->AddHeader(beaconPayloadHeader);
Ptr<MacPibAttributes> pibAttr = Create<MacPibAttributes>();
pibAttr->macBeaconPayload.resize(payload->GetSize());
payload->CopyData(pibAttr->macBeaconPayload.data(), payload->GetSize());
pibAttr->macBeaconPayloadLength = m_beaconPayload->GetSize();
m_mac->MlmeSetRequest(MacPibAttributeIdentifier::macBeaconPayloadLength, pibAttr);
}
void
ZigbeeNwk::UpdateBeaconPayload()
{
NS_LOG_FUNCTION(this);
// Extract octets from m_beaconPayload and copy them into the
// macBeaconPayload attribute
Ptr<MacPibAttributes> pibAttr = Create<MacPibAttributes>();
pibAttr->macBeaconPayload.resize(m_beaconPayload->GetSize());
m_beaconPayload->CopyData(pibAttr->macBeaconPayload.data(), m_beaconPayload->GetSize());
m_beaconPayload = nullptr;
m_mac->MlmeSetRequest(MacPibAttributeIdentifier::macBeaconPayload, pibAttr);
}
Expand Down
13 changes: 12 additions & 1 deletion src/zigbee/model/zigbee-nwk.h
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,13 @@ class ZigbeeNwk : public Object
Mac16Address AllocateNetworkAddress();

/**
* Updates the contents of the MAC beacon payload with information of the NWK.
* Create and store a MAC beacon payload, then updates its registered size in the MAC.
* This is typically followed by updating the content of the beacon payload.
*/
void UpdateBeaconPayloadLength();

/**
* Updates the content of the beacon payload with the most recent information in the NWK.
*/
void UpdateBeaconPayload();

Expand Down Expand Up @@ -1495,6 +1501,11 @@ class ZigbeeNwk : public Object
*/
std::vector<NetworkDescriptor> m_networkDescriptorList;

/**
* Points to the beacon payload used during the network formation process.
*/
Ptr<Packet> m_beaconPayload;

/**
* Used to store the value of the PHY current channel.
*/
Expand Down
14 changes: 9 additions & 5 deletions src/zigbee/test/zigbee-rreq-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ ZigbeeRreqRetryTestCase::NwkNetworkDiscoveryConfirm(ZigbeeRreqRetryTestCase* tes

if (params.m_status == NwkStatus::SUCCESS)
{
std::cout << " Network discovery confirm Received. Networks found:\n";
std::cout << " Network discovery confirm Received. Networks found ("
<< params.m_netDescList.size() << "):\n";

for (const auto& netDescriptor : params.m_netDescList)
{
std::cout << " ExtPanID: 0x" << std::hex << netDescriptor.m_extPanId << std::dec
<< " CH: " << static_cast<uint32_t>(netDescriptor.m_logCh) << std::hex
<< " Pan Id: 0x" << netDescriptor.m_panId << " stackprofile " << std::dec
<< static_cast<uint32_t>(netDescriptor.m_stackProfile) << "\n";
std::cout << " ExtPanID: 0x" << std::hex << netDescriptor.m_extPanId << "\n"
<< std::dec << " CH: " << static_cast<uint32_t>(netDescriptor.m_logCh)
<< "\n"
<< std::hex << " Pan ID: 0x" << netDescriptor.m_panId << "\n"
<< " Stack profile: " << std::dec
<< static_cast<uint32_t>(netDescriptor.m_stackProfile) << "\n"
<< "--------------------\n";
}

NlmeJoinRequestParams joinParams;
Expand Down

0 comments on commit b17fc02

Please sign in to comment.