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,93 @@ 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\n " , threadLeaderNode.otGetStringDeviceRole ());
43
+ Serial.printf (" RLOC16: 0x%04x\n " , threadLeaderNode.getRloc16 ());
44
+ Serial.printf (" Network Name: %s\n " , threadLeaderNode.getNetworkName ().c_str ());
45
+ Serial.printf (" Channel: %d\n " , threadLeaderNode.getChannel ());
46
+ Serial.printf (" PAN ID: 0x%04x\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\n " , meshLocalEid.toString ().c_str ());
71
+
72
+ // Leader RLOC
73
+ IPAddress leaderRloc = threadLeaderNode.getLeaderRloc ();
74
+ Serial.printf (" Leader RLOC: %s\n " , leaderRloc.toString ().c_str ());
75
+
76
+ // Node RLOC
77
+ IPAddress nodeRloc = threadLeaderNode.getRloc ();
78
+ Serial.printf (" Node RLOC: %s\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 (" \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\n " , i, addr.toString ().c_str ());
87
+ }
88
+
89
+ // Method 2: Multicast addresses using std::vector (bulk access)
90
+ Serial.println (" \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\n " , i, allMulticast[i].toString ().c_str ());
94
+ }
95
+
96
+ // Cache management information
97
+ Serial.println (" \n --- Address Access Methods ---" );
98
+ Serial.println (" Two ways to access addresses:" );
99
+ Serial.println (" 1. Count + Index: getUnicastAddressCount() + getUnicastAddress(index)" );
100
+ Serial.println (" 2. Vector: getAllUnicastAddresses() or getAllMulticastAddresses()" );
101
+ Serial.println (" Cache is automatically populated when empty" );
102
+
103
+ Serial.println (" ==============================================\n " );
104
+
105
+ // Check for role change and clear cache if needed (only when active)
106
+ if (currentRole != lastKnownRole) {
107
+ Serial.printf (" Role changed from %s to %s - clearing address cache\n " ,
108
+ (lastKnownRole < 5 ) ? otRoleString[lastKnownRole] : " Unknown" ,
109
+ threadLeaderNode.otGetStringDeviceRole ());
110
+ threadLeaderNode.clearAllAddressCache ();
111
+ lastKnownRole = currentRole;
112
+ }
113
+ } else {
114
+ Serial.printf (" Thread Node Status: %s - Waiting for network connection...\n " ,
115
+ threadLeaderNode.otGetStringDeviceRole ());
116
+
117
+ // Update role tracking even when detached/disabled, but don't clear cache
118
+ lastKnownRole = currentRole;
119
+ }
120
+
33
121
delay (5000 );
34
122
}
0 commit comments