33OpenThread threadLeaderNode;
44DataSet dataset;
55
6+ //  Track last known device role for state change detection
7+ ot_device_role_t  lastKnownRole = OT_ROLE_DISABLED;
8+ 
69void  setup () {
710  Serial.begin (115200 );
811
@@ -27,8 +30,84 @@ void setup() {
2730}
2831
2932void  loop () {
30-   //  Print network information every 5 seconds
31-   Serial.println (" ==============================================" 
32-   threadLeaderNode.otPrintNetworkInformation (Serial);
33+   //  Get current device role
34+   ot_device_role_t  currentRole = threadLeaderNode.otGetDeviceRole ();
35+ 
36+   //  Only print network information when not detached
37+   if  (currentRole != OT_ROLE_DETACHED && currentRole != OT_ROLE_DISABLED) {
38+     Serial.println (" ==============================================" 
39+     Serial.println (" OpenThread Network Information:" 
40+ 
41+     //  Basic network information
42+     Serial.printf (" Role: %s\r\n " otGetStringDeviceRole ());
43+     Serial.printf (" RLOC16: 0x%04x\r\n " getRloc16 ());
44+     Serial.printf (" Network Name: %s\r\n " getNetworkName ().c_str ());
45+     Serial.printf (" Channel: %d\r\n " getChannel ());
46+     Serial.printf (" PAN ID: 0x%04x\r\n " getPanId ());
47+ 
48+     //  Extended PAN ID
49+     const  uint8_t  *extPanId = threadLeaderNode.getExtendedPanId ();
50+     if  (extPanId) {
51+       Serial.print (" Extended PAN ID: " 
52+       for  (int  i = 0 ; i < OT_EXT_PAN_ID_SIZE; i++) {
53+         Serial.printf (" %02x" 
54+       }
55+       Serial.println ();
56+     }
57+ 
58+     //  Network Key
59+     const  uint8_t  *networkKey = threadLeaderNode.getNetworkKey ();
60+     if  (networkKey) {
61+       Serial.print (" Network Key: " 
62+       for  (int  i = 0 ; i < OT_NETWORK_KEY_SIZE; i++) {
63+         Serial.printf (" %02x" 
64+       }
65+       Serial.println ();
66+     }
67+ 
68+     //  Mesh Local EID
69+     IPAddress meshLocalEid = threadLeaderNode.getMeshLocalEid ();
70+     Serial.printf (" Mesh Local EID: %s\r\n " toString ().c_str ());
71+ 
72+     //  Leader RLOC
73+     IPAddress leaderRloc = threadLeaderNode.getLeaderRloc ();
74+     Serial.printf (" Leader RLOC: %s\r\n " toString ().c_str ());
75+ 
76+     //  Node RLOC
77+     IPAddress nodeRloc = threadLeaderNode.getRloc ();
78+     Serial.printf (" Node RLOC: %s\r\n " toString ().c_str ());
79+ 
80+     //  Demonstrate address listing with two different methods:
81+     //  Method 1: Unicast addresses using counting API (individual access)
82+     Serial.println (" \r\n --- Unicast Addresses (Using Count + Index API) ---" 
83+     size_t  unicastCount = threadLeaderNode.getUnicastAddressCount ();
84+     for  (size_t  i = 0 ; i < unicastCount; i++) {
85+       IPAddress addr = threadLeaderNode.getUnicastAddress (i);
86+       Serial.printf ("   [%zu]: %s\r\n " toString ().c_str ());
87+     }
88+ 
89+     //  Method 2: Multicast addresses using std::vector (bulk access)
90+     Serial.println (" \r\n --- Multicast Addresses (Using std::vector API) ---" 
91+     std::vector<IPAddress> allMulticast = threadLeaderNode.getAllMulticastAddresses ();
92+     for  (size_t  i = 0 ; i < allMulticast.size (); i++) {
93+       Serial.printf ("   [%zu]: %s\r\n " toString ().c_str ());
94+     }
95+ 
96+     //  Check for role change and clear cache if needed (only when active)
97+     if  (currentRole != lastKnownRole) {
98+       Serial.printf (
99+         " Role changed from %s to %s - clearing address cache\r\n " 5 ) ? otRoleString[lastKnownRole] : " Unknown" 
100+         threadLeaderNode.otGetStringDeviceRole ()
101+       );
102+       threadLeaderNode.clearAllAddressCache ();
103+       lastKnownRole = currentRole;
104+     }
105+   } else  {
106+     Serial.printf (" Thread Node Status: %s - Waiting for thread network start...\r\n " otGetStringDeviceRole ());
107+ 
108+     //  Update role tracking even when detached/disabled, but don't clear cache
109+     lastKnownRole = currentRole;
110+   }
111+ 
33112  delay (5000 );
34113}
0 commit comments