Skip to content

feat(openthread): native API extension #11598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
OpenThread threadLeaderNode;
DataSet dataset;

// Track last known device role for state change detection
ot_device_role_t lastKnownRole = OT_ROLE_DISABLED;

void setup() {
Serial.begin(115200);

Expand All @@ -27,8 +30,84 @@ void setup() {
}

void loop() {
// Print network information every 5 seconds
Serial.println("==============================================");
threadLeaderNode.otPrintNetworkInformation(Serial);
// Get current device role
ot_device_role_t currentRole = threadLeaderNode.otGetDeviceRole();

// Only print network information when not detached
if (currentRole != OT_ROLE_DETACHED && currentRole != OT_ROLE_DISABLED) {
Serial.println("==============================================");
Serial.println("OpenThread Network Information:");

// Basic network information
Serial.printf("Role: %s\r\n", threadLeaderNode.otGetStringDeviceRole());
Serial.printf("RLOC16: 0x%04x\r\n", threadLeaderNode.getRloc16());
Serial.printf("Network Name: %s\r\n", threadLeaderNode.getNetworkName().c_str());
Serial.printf("Channel: %d\r\n", threadLeaderNode.getChannel());
Serial.printf("PAN ID: 0x%04x\r\n", threadLeaderNode.getPanId());

// Extended PAN ID
const uint8_t *extPanId = threadLeaderNode.getExtendedPanId();
if (extPanId) {
Serial.print("Extended PAN ID: ");
for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) {
Serial.printf("%02x", extPanId[i]);
}
Serial.println();
}

// Network Key
const uint8_t *networkKey = threadLeaderNode.getNetworkKey();
if (networkKey) {
Serial.print("Network Key: ");
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) {
Serial.printf("%02x", networkKey[i]);
}
Serial.println();
}

// Mesh Local EID
IPAddress meshLocalEid = threadLeaderNode.getMeshLocalEid();
Serial.printf("Mesh Local EID: %s\r\n", meshLocalEid.toString().c_str());

// Leader RLOC
IPAddress leaderRloc = threadLeaderNode.getLeaderRloc();
Serial.printf("Leader RLOC: %s\r\n", leaderRloc.toString().c_str());

// Node RLOC
IPAddress nodeRloc = threadLeaderNode.getRloc();
Serial.printf("Node RLOC: %s\r\n", nodeRloc.toString().c_str());

// Demonstrate address listing with two different methods:
// Method 1: Unicast addresses using counting API (individual access)
Serial.println("\r\n--- Unicast Addresses (Using Count + Index API) ---");
size_t unicastCount = threadLeaderNode.getUnicastAddressCount();
for (size_t i = 0; i < unicastCount; i++) {
IPAddress addr = threadLeaderNode.getUnicastAddress(i);
Serial.printf(" [%zu]: %s\r\n", i, addr.toString().c_str());
}

// Method 2: Multicast addresses using std::vector (bulk access)
Serial.println("\r\n--- Multicast Addresses (Using std::vector API) ---");
std::vector<IPAddress> allMulticast = threadLeaderNode.getAllMulticastAddresses();
for (size_t i = 0; i < allMulticast.size(); i++) {
Serial.printf(" [%zu]: %s\r\n", i, allMulticast[i].toString().c_str());
}

// Check for role change and clear cache if needed (only when active)
if (currentRole != lastKnownRole) {
Serial.printf(
"Role changed from %s to %s - clearing address cache\r\n", (lastKnownRole < 5) ? otRoleString[lastKnownRole] : "Unknown",
threadLeaderNode.otGetStringDeviceRole()
);
threadLeaderNode.clearAllAddressCache();
lastKnownRole = currentRole;
}
} else {
Serial.printf("Thread Node Status: %s - Waiting for thread network start...\r\n", threadLeaderNode.otGetStringDeviceRole());

// Update role tracking even when detached/disabled, but don't clear cache
lastKnownRole = currentRole;
}

delay(5000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,63 @@ void setup() {
}

void loop() {
// Print network information every 5 seconds
Serial.println("==============================================");
threadChildNode.otPrintNetworkInformation(Serial);
// Get current device role
ot_device_role_t currentRole = threadChildNode.otGetDeviceRole();

// Only print detailed network information when node is active
if (currentRole != OT_ROLE_DETACHED && currentRole != OT_ROLE_DISABLED) {
Serial.println("==============================================");
Serial.println("OpenThread Network Information (Active Dataset):");

// Get and display the current active dataset
const DataSet &activeDataset = threadChildNode.getCurrentDataSet();

Serial.printf("Role: %s\r\n", threadChildNode.otGetStringDeviceRole());
Serial.printf("RLOC16: 0x%04x\r\n", threadChildNode.getRloc16());

// Dataset information
Serial.printf("Network Name: %s\r\n", activeDataset.getNetworkName());
Serial.printf("Channel: %d\r\n", activeDataset.getChannel());
Serial.printf("PAN ID: 0x%04x\r\n", activeDataset.getPanId());

// Extended PAN ID from dataset
const uint8_t *extPanId = activeDataset.getExtendedPanId();
if (extPanId) {
Serial.print("Extended PAN ID: ");
for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) {
Serial.printf("%02x", extPanId[i]);
}
Serial.println();
}

// Network Key from dataset
const uint8_t *networkKey = activeDataset.getNetworkKey();
if (networkKey) {
Serial.print("Network Key: ");
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) {
Serial.printf("%02x", networkKey[i]);
}
Serial.println();
}

// Additional runtime information
IPAddress meshLocalEid = threadChildNode.getMeshLocalEid();
Serial.printf("Mesh Local EID: %s\r\n", meshLocalEid.toString().c_str());

IPAddress nodeRloc = threadChildNode.getRloc();
Serial.printf("Node RLOC: %s\r\n", nodeRloc.toString().c_str());

IPAddress leaderRloc = threadChildNode.getLeaderRloc();
Serial.printf("Leader RLOC: %s\r\n", leaderRloc.toString().c_str());

Serial.println();

} else {
Serial.println("==============================================");
Serial.printf("Thread Node Status: %s - Waiting for thread network start...\r\n", threadChildNode.otGetStringDeviceRole());

Serial.println();
}

delay(5000);
}
18 changes: 18 additions & 0 deletions libraries/OpenThread/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ OpenThread KEYWORD1
DataSet KEYWORD1
ot_cmd_return_t KEYWORD1
ot_device_role_t KEYWORD1
OnReceiveCb_t KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down Expand Up @@ -59,6 +60,23 @@ stop KEYWORD2
networkInterfaceUp KEYWORD2
networkInterfaceDown KEYWORD2
commitDataSet KEYWORD2
getInstance KEYWORD2
getCurrentDataSet KEYWORD2
getMeshLocalPrefix KEYWORD2
getMeshLocalEid KEYWORD2
getLeaderRloc KEYWORD2
getRloc KEYWORD2
getRloc16 KEYWORD2
getUnicastAddressCount KEYWORD2
getUnicastAddress KEYWORD2
getAllUnicastAddresses KEYWORD2
getMulticastAddressCount KEYWORD2
getMulticastAddress KEYWORD2
getAllMulticastAddresses KEYWORD2
clearUnicastAddressCache KEYWORD2
clearMulticastAddressCache KEYWORD2
clearAllAddressCache KEYWORD2
end KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
Loading
Loading