From 7fece86fbebc889ffad9d05b5d3b0ca487a0bcdb Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 20 Jun 2024 19:09:11 -0300 Subject: [PATCH] feat(openthread): Improved Scan Example --- .../examples/ThreadScan/ThreadScan.ino | 45 ++++--------------- libraries/OpenThread/keywords.txt | 1 + libraries/OpenThread/src/OThreadCLI_Util.cpp | 33 ++++++++++++++ libraries/OpenThread/src/OThreadCLI_Util.h | 1 + 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/libraries/OpenThread/examples/ThreadScan/ThreadScan.ino b/libraries/OpenThread/examples/ThreadScan/ThreadScan.ino index 2eedfc732f0..905faa8a132 100644 --- a/libraries/OpenThread/examples/ThreadScan/ThreadScan.ino +++ b/libraries/OpenThread/examples/ThreadScan/ThreadScan.ino @@ -11,47 +11,20 @@ #include "OThreadCLI.h" #include "OThreadCLI_Util.h" -bool otPrintRespCLI(const char *cmd, Stream &output) { - char cliResp[256]; - if (cmd == NULL) { - return true; - } - OThreadCLI.println(cmd); - while (1) { - size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp)); - if (len == 0) { - return false; // timeout for reading a response from CLI - } - // clip it on EOL - for (int i = 0; i < len; i++) { - if (cliResp[i] == '\r' || cliResp[i] == '\n') { - cliResp[i] = '\0'; - } - } - if (!strncmp(cliResp, "Done", 4)) { - return true; // finished with success - } - if (!strncmp(cliResp, "Error", 4)) { - return false; // the CLI command or its arguments are not valid - } - output.println(cliResp); - } -} - void setup() { Serial.begin(115200); OThreadCLI.begin(true); // For scanning, AutoStart must be active, any setup - OThreadCLI.setTimeout(10000); // 10 seconds for reading a line from CLI - scanning takes time + OThreadCLI.setTimeout(100); // Set a timeout for the CLI response Serial.println(); - Serial.println("This sketch will continuosly scan the Thread Local Network and all devices IEEE 802.15.4 compatible"); + Serial.println("This sketch will continuously scan the Thread Local Network and all devices IEEE 802.15.4 compatible"); } void loop() { Serial.println(); - Serial.println("Scanning for near by IEEE 802.15.4 devices:"); - // 802.15.4 Scan just need a previous OThreadCLI.begin() to tun on the 802.15.4 stack - if (!otPrintRespCLI("scan", Serial)) { - Serial.println("802.15.4 Scan Failed..."); + Serial.println("Scanning for nearby IEEE 802.15.4 devices:"); + // 802.15.4 Scan just needs a previous OThreadCLI.begin() + if (!otPrintRespCLI("scan", Serial, 3000)) { + Serial.println("Scan Failed..."); } delay(5000); if (otGetDeviceRole() < OT_ROLE_CHILD) { @@ -60,9 +33,9 @@ void loop() { return; } Serial.println(); - Serial.println("Scanning - MLE Discover:"); - if (!otPrintRespCLI("discover", Serial)) { - Serial.println("MLE Discover Failed..."); + Serial.println("Scanning MLE Discover:"); + if (!otPrintRespCLI("discover", Serial, 3000)) { + Serial.println("Discover Failed..."); } delay(5000); } diff --git a/libraries/OpenThread/keywords.txt b/libraries/OpenThread/keywords.txt index c1a8b5410c6..68963e354cd 100644 --- a/libraries/OpenThread/keywords.txt +++ b/libraries/OpenThread/keywords.txt @@ -33,6 +33,7 @@ otGetDeviceRole KEYWORD2 otGetStringDeviceRole KEYWORD2 otGetRespCmd KEYWORD2 otExecCommand KEYWORD2 +otPrintRespCLI KEYWORD2 otPrintNetworkInformation KEYWORD2 ####################################### diff --git a/libraries/OpenThread/src/OThreadCLI_Util.cpp b/libraries/OpenThread/src/OThreadCLI_Util.cpp index bb9fb463700..16629a20c5c 100644 --- a/libraries/OpenThread/src/OThreadCLI_Util.cpp +++ b/libraries/OpenThread/src/OThreadCLI_Util.cpp @@ -123,6 +123,39 @@ bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode } } +bool otPrintRespCLI(const char *cmd, Stream &output, uint32_t respTimeout) { + char cliResp[256] = {0}; + if (cmd == NULL) { + return true; + } + OThreadCLI.println(cmd); + uint32_t timeout = millis() + respTimeout; + while (millis() < timeout) { + size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp)); + if (cliResp[0] == '\0') { + // Straem has timed out and it should try again using parameter respTimeout + continue; + } + // clip it on EOL + for (int i = 0; i < len; i++) { + if (cliResp[i] == '\r' || cliResp[i] == '\n') { + cliResp[i] = '\0'; + } + } + if (strncmp(cliResp, "Done", 4) && strncmp(cliResp, "Error", 4)) { + output.println(cliResp); + memset(cliResp, 0, sizeof(cliResp)); + timeout = millis() + respTimeout; // renew timeout, line per line + } else { + break; + } + } + if (!strncmp(cliResp, "Error", 4) || millis() > timeout) { + return false; + } + return true; +} + void otPrintNetworkInformation(Stream &output) { if (!OThreadCLI) { return; diff --git a/libraries/OpenThread/src/OThreadCLI_Util.h b/libraries/OpenThread/src/OThreadCLI_Util.h index b691b83f078..02a05c30b5f 100644 --- a/libraries/OpenThread/src/OThreadCLI_Util.h +++ b/libraries/OpenThread/src/OThreadCLI_Util.h @@ -21,6 +21,7 @@ ot_device_role_t otGetDeviceRole(); const char* otGetStringDeviceRole(); bool otGetRespCmd(const char *cmd, char *resp = NULL, uint32_t respTimeout = 5000); bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode = NULL); +bool otPrintRespCLI(const char *cmd, Stream &output, uint32_t respTimeout); void otPrintNetworkInformation(Stream &output); #endif /* CONFIG_OPENTHREAD_ENABLED */