|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, The OpenThread Authors. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * 3. Neither the name of the copyright holder nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <stdio.h> |
| 30 | + |
| 31 | +#include "platform/nexus_core.hpp" |
| 32 | +#include "platform/nexus_node.hpp" |
| 33 | + |
| 34 | +namespace ot { |
| 35 | +namespace Nexus { |
| 36 | + |
| 37 | +/** |
| 38 | + * Time to advance for a node to form a network and become leader, in milliseconds. |
| 39 | + */ |
| 40 | +static constexpr uint32_t kFormNetworkTime = 13 * 1000; |
| 41 | + |
| 42 | +/** |
| 43 | + * Time to advance for a node to join as a child and upgrade to a router, in milliseconds. |
| 44 | + */ |
| 45 | +static constexpr uint32_t kAttachToRouterTime = 30 * 1000; |
| 46 | + |
| 47 | +void TestRouterReattach(void) |
| 48 | +{ |
| 49 | + Core nexus; |
| 50 | + const uint8_t kNumRouters = 32; |
| 51 | + Node *nodes[kNumRouters]; |
| 52 | + |
| 53 | + Log("Creating %u nodes", kNumRouters); |
| 54 | + for (uint8_t i = 0; i < kNumRouters; i++) |
| 55 | + { |
| 56 | + nodes[i] = &nexus.CreateNode(); |
| 57 | + nodes[i]->SetName("Node", i + 1); |
| 58 | + } |
| 59 | + |
| 60 | + nexus.AdvanceTime(0); |
| 61 | + |
| 62 | + // Step 1: Start Leader |
| 63 | + Log("Step 1: Starting Leader"); |
| 64 | + nodes[0]->Get<Mle::Mle>().SetRouterUpgradeThreshold(32); |
| 65 | + nodes[0]->Get<Mle::Mle>().SetRouterDowngradeThreshold(32); |
| 66 | + nodes[0]->Form(); |
| 67 | + nexus.AdvanceTime(kFormNetworkTime); |
| 68 | + VerifyOrQuit(nodes[0]->Get<Mle::Mle>().IsLeader()); |
| 69 | + |
| 70 | + // Step 2: Start other 31 routers |
| 71 | + Log("Step 2: Starting other 31 routers"); |
| 72 | + for (uint8_t i = 1; i < kNumRouters; i++) |
| 73 | + { |
| 74 | + nodes[i]->Get<Mle::Mle>().SetRouterUpgradeThreshold(32); |
| 75 | + nodes[i]->Get<Mle::Mle>().SetRouterDowngradeThreshold(32); |
| 76 | + nodes[i]->Get<Mle::Mle>().SetRouterSelectionJitter(1); |
| 77 | + nodes[i]->Join(*nodes[0]); |
| 78 | + nexus.AdvanceTime(kAttachToRouterTime); |
| 79 | + Log("Node %u role: %s", i + 1, nodes[i]->GetExtendedRoleString()); |
| 80 | + VerifyOrQuit(nodes[i]->Get<Mle::Mle>().IsRouter()); |
| 81 | + } |
| 82 | + |
| 83 | + // Step 3: Reset Node 2 (index 1) |
| 84 | + Log("Step 3: Resetting Node 2"); |
| 85 | + nodes[1]->Reset(); |
| 86 | + nodes[1]->Get<Mle::Mle>().SetRouterUpgradeThreshold(32); |
| 87 | + nodes[1]->Get<Mle::Mle>().SetRouterDowngradeThreshold(32); |
| 88 | + nodes[1]->Get<Mle::Mle>().SetRouterSelectionJitter(3); |
| 89 | + |
| 90 | + // Re-enable and start |
| 91 | + Log("Step 4: Restarting Node 2"); |
| 92 | + nodes[1]->Get<ThreadNetif>().Up(); |
| 93 | + SuccessOrQuit(nodes[1]->Get<Mle::Mle>().Start()); |
| 94 | + VerifyOrQuit(nodes[1]->Get<Mle::Mle>().GetRouterDowngradeThreshold() == 32); |
| 95 | + |
| 96 | + // Verify it restores as Router |
| 97 | + Log("Step 5: Verifying Node 2 restores as Router"); |
| 98 | + nexus.AdvanceTime(1000); |
| 99 | + VerifyOrQuit(nodes[1]->Get<Mle::Mle>().IsRouter()); |
| 100 | + |
| 101 | + // Verify it doesn't downgrade after Router Selection Jitter |
| 102 | + Log("Step 6: Verifying Node 2 does not downgrade"); |
| 103 | + nexus.AdvanceTime(5000); |
| 104 | + VerifyOrQuit(nodes[1]->Get<Mle::Mle>().IsRouter()); |
| 105 | + |
| 106 | + nexus.SaveTestInfo("test_router_reattach.json"); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace Nexus |
| 110 | +} // namespace ot |
| 111 | + |
| 112 | +int main(void) |
| 113 | +{ |
| 114 | + ot::Nexus::TestRouterReattach(); |
| 115 | + printf("All tests passed\n"); |
| 116 | + return 0; |
| 117 | +} |
0 commit comments