Skip to content

Commit 7fece86

Browse files
committed
feat(openthread): Improved Scan Example
1 parent ce46f4a commit 7fece86

File tree

4 files changed

+44
-36
lines changed

4 files changed

+44
-36
lines changed

libraries/OpenThread/examples/ThreadScan/ThreadScan.ino

+9-36
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,20 @@
1111
#include "OThreadCLI.h"
1212
#include "OThreadCLI_Util.h"
1313

14-
bool otPrintRespCLI(const char *cmd, Stream &output) {
15-
char cliResp[256];
16-
if (cmd == NULL) {
17-
return true;
18-
}
19-
OThreadCLI.println(cmd);
20-
while (1) {
21-
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
22-
if (len == 0) {
23-
return false; // timeout for reading a response from CLI
24-
}
25-
// clip it on EOL
26-
for (int i = 0; i < len; i++) {
27-
if (cliResp[i] == '\r' || cliResp[i] == '\n') {
28-
cliResp[i] = '\0';
29-
}
30-
}
31-
if (!strncmp(cliResp, "Done", 4)) {
32-
return true; // finished with success
33-
}
34-
if (!strncmp(cliResp, "Error", 4)) {
35-
return false; // the CLI command or its arguments are not valid
36-
}
37-
output.println(cliResp);
38-
}
39-
}
40-
4114
void setup() {
4215
Serial.begin(115200);
4316
OThreadCLI.begin(true); // For scanning, AutoStart must be active, any setup
44-
OThreadCLI.setTimeout(10000); // 10 seconds for reading a line from CLI - scanning takes time
17+
OThreadCLI.setTimeout(100); // Set a timeout for the CLI response
4518
Serial.println();
46-
Serial.println("This sketch will continuosly scan the Thread Local Network and all devices IEEE 802.15.4 compatible");
19+
Serial.println("This sketch will continuously scan the Thread Local Network and all devices IEEE 802.15.4 compatible");
4720
}
4821

4922
void loop() {
5023
Serial.println();
51-
Serial.println("Scanning for near by IEEE 802.15.4 devices:");
52-
// 802.15.4 Scan just need a previous OThreadCLI.begin() to tun on the 802.15.4 stack
53-
if (!otPrintRespCLI("scan", Serial)) {
54-
Serial.println("802.15.4 Scan Failed...");
24+
Serial.println("Scanning for nearby IEEE 802.15.4 devices:");
25+
// 802.15.4 Scan just needs a previous OThreadCLI.begin()
26+
if (!otPrintRespCLI("scan", Serial, 3000)) {
27+
Serial.println("Scan Failed...");
5528
}
5629
delay(5000);
5730
if (otGetDeviceRole() < OT_ROLE_CHILD) {
@@ -60,9 +33,9 @@ void loop() {
6033
return;
6134
}
6235
Serial.println();
63-
Serial.println("Scanning - MLE Discover:");
64-
if (!otPrintRespCLI("discover", Serial)) {
65-
Serial.println("MLE Discover Failed...");
36+
Serial.println("Scanning MLE Discover:");
37+
if (!otPrintRespCLI("discover", Serial, 3000)) {
38+
Serial.println("Discover Failed...");
6639
}
6740
delay(5000);
6841
}

libraries/OpenThread/keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ otGetDeviceRole KEYWORD2
3333
otGetStringDeviceRole KEYWORD2
3434
otGetRespCmd KEYWORD2
3535
otExecCommand KEYWORD2
36+
otPrintRespCLI KEYWORD2
3637
otPrintNetworkInformation KEYWORD2
3738

3839
#######################################

libraries/OpenThread/src/OThreadCLI_Util.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,39 @@ bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode
123123
}
124124
}
125125

126+
bool otPrintRespCLI(const char *cmd, Stream &output, uint32_t respTimeout) {
127+
char cliResp[256] = {0};
128+
if (cmd == NULL) {
129+
return true;
130+
}
131+
OThreadCLI.println(cmd);
132+
uint32_t timeout = millis() + respTimeout;
133+
while (millis() < timeout) {
134+
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
135+
if (cliResp[0] == '\0') {
136+
// Straem has timed out and it should try again using parameter respTimeout
137+
continue;
138+
}
139+
// clip it on EOL
140+
for (int i = 0; i < len; i++) {
141+
if (cliResp[i] == '\r' || cliResp[i] == '\n') {
142+
cliResp[i] = '\0';
143+
}
144+
}
145+
if (strncmp(cliResp, "Done", 4) && strncmp(cliResp, "Error", 4)) {
146+
output.println(cliResp);
147+
memset(cliResp, 0, sizeof(cliResp));
148+
timeout = millis() + respTimeout; // renew timeout, line per line
149+
} else {
150+
break;
151+
}
152+
}
153+
if (!strncmp(cliResp, "Error", 4) || millis() > timeout) {
154+
return false;
155+
}
156+
return true;
157+
}
158+
126159
void otPrintNetworkInformation(Stream &output) {
127160
if (!OThreadCLI) {
128161
return;

libraries/OpenThread/src/OThreadCLI_Util.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ot_device_role_t otGetDeviceRole();
2121
const char* otGetStringDeviceRole();
2222
bool otGetRespCmd(const char *cmd, char *resp = NULL, uint32_t respTimeout = 5000);
2323
bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode = NULL);
24+
bool otPrintRespCLI(const char *cmd, Stream &output, uint32_t respTimeout);
2425
void otPrintNetworkInformation(Stream &output);
2526

2627
#endif /* CONFIG_OPENTHREAD_ENABLED */

0 commit comments

Comments
 (0)