1 /*
2  *  Copyright (c) 2024, 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 <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include "platform/nexus_core.hpp"
34 #include "platform/nexus_node.hpp"
35 
36 namespace ot {
37 namespace Nexus {
38 
TestFormJoin(void)39 void TestFormJoin(void)
40 {
41     // Validate basic operations, forming a network and
42     // joining as router, FED, MED, and SED
43 
44     Core nexus;
45 
46     Node &leader  = nexus.CreateNode();
47     Node &fed     = nexus.CreateNode();
48     Node &sed     = nexus.CreateNode();
49     Node &med     = nexus.CreateNode();
50     Node &router1 = nexus.CreateNode();
51     Node &router2 = nexus.CreateNode();
52 
53     nexus.AdvanceTime(0);
54 
55     for (Node &node : nexus.GetNodes())
56     {
57         node.GetInstance().SetLogLevel(kLogLevelInfo);
58     }
59 
60     Log("---------------------------------------------------------------------------------------");
61     Log("Form network");
62 
63     leader.Form();
64     nexus.AdvanceTime(13 * 1000);
65     VerifyOrQuit(leader.Get<Mle::Mle>().IsLeader());
66 
67     Log("---------------------------------------------------------------------------------------");
68     Log("Join an FED");
69 
70     fed.Join(leader, Node::kAsFed);
71     nexus.AdvanceTime(2 * 1000);
72     VerifyOrQuit(fed.Get<Mle::Mle>().IsChild());
73 
74     Log("---------------------------------------------------------------------------------------");
75     Log("Join an SED");
76 
77     sed.Join(leader, Node::kAsSed);
78     nexus.AdvanceTime(2 * 1000);
79     VerifyOrQuit(sed.Get<Mle::Mle>().IsChild());
80 
81     Log("---------------------------------------------------------------------------------------");
82     Log("Join an MED");
83 
84     med.Join(leader, Node::kAsMed);
85     nexus.AdvanceTime(2 * 1000);
86     VerifyOrQuit(med.Get<Mle::Mle>().IsChild());
87 
88     Log("---------------------------------------------------------------------------------------");
89     Log("Join two routers");
90 
91     router1.Join(leader);
92     router2.Join(leader);
93 
94     Log("---------------------------------------------------------------------------------------");
95     Log("Check all nodes roles and device modes");
96 
97     nexus.AdvanceTime(300 * 1000);
98 
99     VerifyOrQuit(leader.Get<Mle::Mle>().IsLeader());
100     VerifyOrQuit(fed.Get<Mle::Mle>().IsChild());
101     VerifyOrQuit(sed.Get<Mle::Mle>().IsChild());
102     VerifyOrQuit(router1.Get<Mle::Mle>().IsRouter());
103     VerifyOrQuit(router2.Get<Mle::Mle>().IsRouter());
104 
105     VerifyOrQuit(fed.Get<Mle::Mle>().IsRxOnWhenIdle());
106     VerifyOrQuit(fed.Get<Mle::Mle>().IsFullThreadDevice());
107 
108     VerifyOrQuit(med.Get<Mle::Mle>().IsRxOnWhenIdle());
109     VerifyOrQuit(!med.Get<Mle::Mle>().IsFullThreadDevice());
110     VerifyOrQuit(med.Get<Mle::Mle>().IsMinimalEndDevice());
111 
112     VerifyOrQuit(!sed.Get<Mle::Mle>().IsRxOnWhenIdle());
113     VerifyOrQuit(!sed.Get<Mle::Mle>().IsFullThreadDevice());
114 }
115 
116 } // namespace Nexus
117 } // namespace ot
118 
main(void)119 int main(void)
120 {
121     ot::Nexus::TestFormJoin();
122     printf("All tests passed\n");
123     return 0;
124 }
125