Skip to content

Commit

Permalink
feat(openthread): Improved Scan Example
Browse files Browse the repository at this point in the history
  • Loading branch information
SuGlider committed Jun 20, 2024
1 parent ce46f4a commit 7fece86
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
45 changes: 9 additions & 36 deletions libraries/OpenThread/examples/ThreadScan/ThreadScan.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
1 change: 1 addition & 0 deletions libraries/OpenThread/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ otGetDeviceRole KEYWORD2
otGetStringDeviceRole KEYWORD2
otGetRespCmd KEYWORD2
otExecCommand KEYWORD2
otPrintRespCLI KEYWORD2
otPrintNetworkInformation KEYWORD2

#######################################
Expand Down
33 changes: 33 additions & 0 deletions libraries/OpenThread/src/OThreadCLI_Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions libraries/OpenThread/src/OThreadCLI_Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 7fece86

Please sign in to comment.