Skip to content

Commit

Permalink
OpenThread Example Improvement (#10299)
Browse files Browse the repository at this point in the history
* feat(openthread): add extended example

Creates a new example that mixes different APIs

* feat(openthread): create cj.json file

Adds neessary CI file

* feat(openthread): improve the example

Update ExtendedRoterNode.ino with more use of API

* feat(openthread): improve the example

Adds OpenThread Native calls to the example

* feat(openthread): improve the example 

Update LeaderNode.ino example to add OpenThread Native calls.

* fix(openthread): bad formatting using space

Update keywords.txt to use TAB instead of SPACE in order to recognize correctly the keywords.

* fix(openthread): bad example file name - typo

Changed ExtendedRoterNode to ExtendedRouterNode - Typo error.

* feat(openthread): add extended example ci.json file

Added CI file to the example.

* fix(openthread): deleted bad file names in the example

Delete libraries/OpenThread/examples/SimpleThreadNetwork/ExtendedRoterNode directory

* fix(openthread): typo in commentaries

* fix(openthread): typo in commentaries

* ci(pre-commit): Apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
SuGlider and pre-commit-ci-lite[bot] authored Sep 9, 2024
1 parent 532d5f2 commit cbe0f2f
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "OThreadCLI.h"
#include "OThreadCLI_Util.h"

// Leader node shall use the same Network Key and channel
#define CLI_NETWORK_KEY "00112233445566778899aabbccddeeff"
#define CLI_NETWORK_CHANEL "24"
bool otStatus = true;

void setup() {
Serial.begin(115200);
OThreadCLI.begin(false); // No AutoStart - fresh start
Serial.println("Setting up OpenThread Node as Router/Child");
Serial.println("Make sure the Leader Node is already running");

otStatus &= otExecCommand("dataset", "clear");
otStatus &= otExecCommand("dataset networkkey", CLI_NETWORK_KEY);
otStatus &= otExecCommand("dataset channel", CLI_NETWORK_CHANEL);
otStatus &= otExecCommand("dataset", "commit active");
otStatus &= otExecCommand("ifconfig", "up");
otStatus &= otExecCommand("thread", "start");

if (!otStatus) {
Serial.println("\r\n\t===> Failed starting Thread Network!");
return;
}
// wait for the node to enter in the router state
uint32_t timeout = millis() + 90000; // waits 90 seconds to
while (otGetDeviceRole() != OT_ROLE_CHILD && otGetDeviceRole() != OT_ROLE_ROUTER) {
Serial.print(".");
if (millis() > timeout) {
Serial.println("\r\n\t===> Timeout! Failed.");
otStatus = false;
break;
}
delay(500);
}

if (otStatus) {
// print the PanID using 2 methods

// CLI
char resp[256];
if (otGetRespCmd("panid", resp)) {
Serial.printf("\r\nPanID[using CLI]: %s\r\n", resp);
} else {
Serial.printf("\r\nPanID[using CLI]: FAILED!\r\n");
}

// OpenThread API
Serial.printf("PanID[using OT API]: 0x%x\r\n", (uint16_t)otLinkGetPanId(esp_openthread_get_instance()));
}
Serial.println("\r\n");
}

void loop() {
if (otStatus) {
Serial.println("Thread NetworkInformation: ");
Serial.println("---------------------------");
otPrintNetworkInformation(Serial);
Serial.println("---------------------------");
} else {
Serial.println("Some OpenThread operation has failed...");
}
delay(10000);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"targets": {
"esp32": false,
"esp32c2": false,
"esp32c3": false,
"esp32s2": false,
"esp32s3": false
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
/*
* OpenThread.begin(false) will not automatically start a node in a Thread Network
* A Leader node is the first device, that has a complete dataset, to start Thread
* A complete dataset is easily achieved by using the OpenThread CLI command "dataset init new"
*
* In order to allow other node to join the network,
* all of them shall use the same network master key
* The network master key is a 16-byte key that is used to secure the network
*
* Using the same channel will make the process faster
*
*/
OpenThread.begin(false) will not automatically start a node in a Thread Network
A Leader node is the first device, that has a complete dataset, to start Thread
A complete dataset is easily achieved by using the OpenThread CLI command "dataset init new"
In order to allow other node to join the network,
all of them shall use the same network master key
The network master key is a 16-byte key that is used to secure the network
Using the same channel will make the process faster
*/

#include "OThreadCLI.h"
#include "OThreadCLI_Util.h"

#define CLI_NETWORK_KEY "dataset networkkey 00112233445566778899aabbccddeeff"
#define CLI_NETWORK_CHANEL "dataset channel 24"

otInstance *aInstance = NULL;

void setup() {
Serial.begin(115200);
OThreadCLI.begin(false); // No AutoStart - fresh start
Serial.println();
Serial.println("Setting up OpenThread Node as Leader");
aInstance = esp_openthread_get_instance();

OThreadCLI.println("dataset init new");
OThreadCLI.println(CLI_NETWORK_KEY);
Expand All @@ -31,7 +35,53 @@ void setup() {
}

void loop() {
Serial.println("=============================================");
Serial.print("Thread Node State: ");
Serial.println(otGetStringDeviceRole());

// Native OpenThread API calls:
// wait until the node become Child or Router
if (otGetDeviceRole() == OT_ROLE_LEADER) {
// Network Name
const char *networkName = otThreadGetNetworkName(aInstance);
Serial.printf("Network Name: %s\r\n", networkName);
// Channel
uint8_t channel = otLinkGetChannel(aInstance);
Serial.printf("Channel: %d\r\n", channel);
// PAN ID
uint16_t panId = otLinkGetPanId(aInstance);
Serial.printf("PanID: 0x%04x\r\n", panId);
// Extended PAN ID
const otExtendedPanId *extPanId = otThreadGetExtendedPanId(aInstance);
Serial.printf("Extended PAN ID: ");
for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) {
Serial.printf("%02x", extPanId->m8[i]);
}
Serial.println();
// Network Key
otNetworkKey networkKey;
otThreadGetNetworkKey(aInstance, &networkKey);
Serial.printf("Network Key: ");
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) {
Serial.printf("%02x", networkKey.m8[i]);
}
Serial.println();
// IP Addresses
char buf[OT_IP6_ADDRESS_STRING_SIZE];
const otNetifAddress *address = otIp6GetUnicastAddresses(aInstance);
while (address != NULL) {
otIp6AddressToString(&address->mAddress, buf, sizeof(buf));
Serial.printf("IP Address: %s\r\n", buf);
address = address->mNext;
}
// Multicast IP Addresses
const otNetifMulticastAddress *mAddress = otIp6GetMulticastAddresses(aInstance);
while (mAddress != NULL) {
otIp6AddressToString(&mAddress->mAddress, buf, sizeof(buf));
printf("Multicast IP Address: %s\n", buf);
mAddress = mAddress->mNext;
}
}

delay(5000);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
/*
* OpenThread.begin(false) will not automatically start a node in a Thread Network
* A Router/Child node is the device that will join an existing Thread Network
*
* In order to allow this node to join the network,
* it shall use the same network master key as used by the Leader Node
* The network master key is a 16-byte key that is used to secure the network
*
* Using the same channel will make the process faster
*
*/
OpenThread.begin(false) will not automatically start a node in a Thread Network
A Router/Child node is the device that will join an existing Thread Network
In order to allow this node to join the network,
it shall use the same network master key as used by the Leader Node
The network master key is a 16-byte key that is used to secure the network
Using the same channel will make the process faster
*/

#include "OThreadCLI.h"
#include "OThreadCLI_Util.h"

#define CLI_NETWORK_KEY "dataset networkkey 00112233445566778899aabbccddeeff"
#define CLI_NETWORK_CHANEL "dataset channel 24"

otInstance *aInstance = NULL;

void setup() {
Serial.begin(115200);
OThreadCLI.begin(false); // No AutoStart - fresh start
Serial.println();
Serial.println("Setting up OpenThread Node as Router/Child");
Serial.println("Make sure the Leader Node is already running");
aInstance = esp_openthread_get_instance();

OThreadCLI.println("dataset clear");
OThreadCLI.println(CLI_NETWORK_KEY);
Expand All @@ -31,7 +35,53 @@ void setup() {
}

void loop() {
Serial.println("=============================================");
Serial.print("Thread Node State: ");
Serial.println(otGetStringDeviceRole());

// Native OpenThread API calls:
// wait until the node become Child or Router
if (otGetDeviceRole() == OT_ROLE_CHILD || otGetDeviceRole() == OT_ROLE_ROUTER) {
// Network Name
const char *networkName = otThreadGetNetworkName(aInstance);
Serial.printf("Network Name: %s\r\n", networkName);
// Channel
uint8_t channel = otLinkGetChannel(aInstance);
Serial.printf("Channel: %d\r\n", channel);
// PAN ID
uint16_t panId = otLinkGetPanId(aInstance);
Serial.printf("PanID: 0x%04x\r\n", panId);
// Extended PAN ID
const otExtendedPanId *extPanId = otThreadGetExtendedPanId(aInstance);
Serial.printf("Extended PAN ID: ");
for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) {
Serial.printf("%02x", extPanId->m8[i]);
}
Serial.println();
// Network Key
otNetworkKey networkKey;
otThreadGetNetworkKey(aInstance, &networkKey);
Serial.printf("Network Key: ");
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) {
Serial.printf("%02x", networkKey.m8[i]);
}
Serial.println();
// IP Addresses
char buf[OT_IP6_ADDRESS_STRING_SIZE];
const otNetifAddress *address = otIp6GetUnicastAddresses(aInstance);
while (address != NULL) {
otIp6AddressToString(&address->mAddress, buf, sizeof(buf));
Serial.printf("IP Address: %s\r\n", buf);
address = address->mNext;
}
// Multicast IP Addresses
const otNetifMulticastAddress *mAddress = otIp6GetMulticastAddresses(aInstance);
while (mAddress != NULL) {
otIp6AddressToString(&mAddress->mAddress, buf, sizeof(buf));
printf("Multicast IP Address: %s\n", buf);
mAddress = mAddress->mNext;
}
}

delay(5000);
}
6 changes: 3 additions & 3 deletions libraries/OpenThread/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ peek KEYWORD2
flush KEYWORD2
otGetDeviceRole KEYWORD2
otGetStringDeviceRole KEYWORD2
otGetRespCmd KEYWORD2
otGetRespCmd KEYWORD2
otExecCommand KEYWORD2
otPrintRespCLI KEYWORD2
otPrintNetworkInformation KEYWORD2
otPrintRespCLI KEYWORD2
otPrintNetworkInformation KEYWORD2

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

0 comments on commit cbe0f2f

Please sign in to comment.