3
3
OpenThread threadLeaderNode;
4
4
DataSet dataset;
5
5
6
+ // Track last known device role for state change detection
7
+ ot_device_role_t lastKnownRole = OT_ROLE_DISABLED;
8
+
6
9
void setup () {
7
10
Serial.begin (115200 );
8
11
@@ -27,8 +30,84 @@ void setup() {
27
30
}
28
31
29
32
void 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 " , threadLeaderNode.otGetStringDeviceRole ());
43
+ Serial.printf (" RLOC16: 0x%04x\r\n " , threadLeaderNode.getRloc16 ());
44
+ Serial.printf (" Network Name: %s\r\n " , threadLeaderNode.getNetworkName ().c_str ());
45
+ Serial.printf (" Channel: %d\r\n " , threadLeaderNode.getChannel ());
46
+ Serial.printf (" PAN ID: 0x%04x\r\n " , threadLeaderNode.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" , extPanId[i]);
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" , networkKey[i]);
64
+ }
65
+ Serial.println ();
66
+ }
67
+
68
+ // Mesh Local EID
69
+ IPAddress meshLocalEid = threadLeaderNode.getMeshLocalEid ();
70
+ Serial.printf (" Mesh Local EID: %s\r\n " , meshLocalEid.toString ().c_str ());
71
+
72
+ // Leader RLOC
73
+ IPAddress leaderRloc = threadLeaderNode.getLeaderRloc ();
74
+ Serial.printf (" Leader RLOC: %s\r\n " , leaderRloc.toString ().c_str ());
75
+
76
+ // Node RLOC
77
+ IPAddress nodeRloc = threadLeaderNode.getRloc ();
78
+ Serial.printf (" Node RLOC: %s\r\n " , nodeRloc.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 " , i, addr.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 " , i, allMulticast[i].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 " , (lastKnownRole < 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 " , threadLeaderNode.otGetStringDeviceRole ());
107
+
108
+ // Update role tracking even when detached/disabled, but don't clear cache
109
+ lastKnownRole = currentRole;
110
+ }
111
+
33
112
delay (5000 );
34
113
}
0 commit comments