Skip to content

Commit 6015fd7

Browse files
feat(openthread): native API extension (#11598)
* feat(openthread): native API extension * fix(openthread): wrong return type and parameter * fix(openthread): wrong field reference * fix(openthread): CR/LF fix * feat(openthread): print leader RLOC information * feat(openthread): code improvements * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 82d56bc commit 6015fd7

File tree

5 files changed

+565
-25
lines changed

5 files changed

+565
-25
lines changed

libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/LeaderNode.ino

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
OpenThread threadLeaderNode;
44
DataSet dataset;
55

6+
// Track last known device role for state change detection
7+
ot_device_role_t lastKnownRole = OT_ROLE_DISABLED;
8+
69
void setup() {
710
Serial.begin(115200);
811

@@ -27,8 +30,84 @@ void setup() {
2730
}
2831

2932
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+
33112
delay(5000);
34113
}

libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/RouterNode.ino

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,63 @@ void setup() {
2222
}
2323

2424
void loop() {
25-
// Print network information every 5 seconds
26-
Serial.println("==============================================");
27-
threadChildNode.otPrintNetworkInformation(Serial);
25+
// Get current device role
26+
ot_device_role_t currentRole = threadChildNode.otGetDeviceRole();
27+
28+
// Only print detailed network information when node is active
29+
if (currentRole != OT_ROLE_DETACHED && currentRole != OT_ROLE_DISABLED) {
30+
Serial.println("==============================================");
31+
Serial.println("OpenThread Network Information (Active Dataset):");
32+
33+
// Get and display the current active dataset
34+
const DataSet &activeDataset = threadChildNode.getCurrentDataSet();
35+
36+
Serial.printf("Role: %s\r\n", threadChildNode.otGetStringDeviceRole());
37+
Serial.printf("RLOC16: 0x%04x\r\n", threadChildNode.getRloc16());
38+
39+
// Dataset information
40+
Serial.printf("Network Name: %s\r\n", activeDataset.getNetworkName());
41+
Serial.printf("Channel: %d\r\n", activeDataset.getChannel());
42+
Serial.printf("PAN ID: 0x%04x\r\n", activeDataset.getPanId());
43+
44+
// Extended PAN ID from dataset
45+
const uint8_t *extPanId = activeDataset.getExtendedPanId();
46+
if (extPanId) {
47+
Serial.print("Extended PAN ID: ");
48+
for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) {
49+
Serial.printf("%02x", extPanId[i]);
50+
}
51+
Serial.println();
52+
}
53+
54+
// Network Key from dataset
55+
const uint8_t *networkKey = activeDataset.getNetworkKey();
56+
if (networkKey) {
57+
Serial.print("Network Key: ");
58+
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) {
59+
Serial.printf("%02x", networkKey[i]);
60+
}
61+
Serial.println();
62+
}
63+
64+
// Additional runtime information
65+
IPAddress meshLocalEid = threadChildNode.getMeshLocalEid();
66+
Serial.printf("Mesh Local EID: %s\r\n", meshLocalEid.toString().c_str());
67+
68+
IPAddress nodeRloc = threadChildNode.getRloc();
69+
Serial.printf("Node RLOC: %s\r\n", nodeRloc.toString().c_str());
70+
71+
IPAddress leaderRloc = threadChildNode.getLeaderRloc();
72+
Serial.printf("Leader RLOC: %s\r\n", leaderRloc.toString().c_str());
73+
74+
Serial.println();
75+
76+
} else {
77+
Serial.println("==============================================");
78+
Serial.printf("Thread Node Status: %s - Waiting for thread network start...\r\n", threadChildNode.otGetStringDeviceRole());
79+
80+
Serial.println();
81+
}
82+
2883
delay(5000);
2984
}

libraries/OpenThread/keywords.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ OpenThread KEYWORD1
1313
DataSet KEYWORD1
1414
ot_cmd_return_t KEYWORD1
1515
ot_device_role_t KEYWORD1
16+
OnReceiveCb_t KEYWORD1
1617

1718
#######################################
1819
# Methods and Functions (KEYWORD2)
@@ -59,6 +60,23 @@ stop KEYWORD2
5960
networkInterfaceUp KEYWORD2
6061
networkInterfaceDown KEYWORD2
6162
commitDataSet KEYWORD2
63+
getInstance KEYWORD2
64+
getCurrentDataSet KEYWORD2
65+
getMeshLocalPrefix KEYWORD2
66+
getMeshLocalEid KEYWORD2
67+
getLeaderRloc KEYWORD2
68+
getRloc KEYWORD2
69+
getRloc16 KEYWORD2
70+
getUnicastAddressCount KEYWORD2
71+
getUnicastAddress KEYWORD2
72+
getAllUnicastAddresses KEYWORD2
73+
getMulticastAddressCount KEYWORD2
74+
getMulticastAddress KEYWORD2
75+
getAllMulticastAddresses KEYWORD2
76+
clearUnicastAddressCache KEYWORD2
77+
clearMulticastAddressCache KEYWORD2
78+
clearAllAddressCache KEYWORD2
79+
end KEYWORD2
6280

6381
#######################################
6482
# Constants (LITERAL1)

0 commit comments

Comments
 (0)