Skip to content

Commit 5c677be

Browse files
committed
fix(openthread): Improves code
1 parent bbece1c commit 5c677be

File tree

10 files changed

+114
-39
lines changed

10 files changed

+114
-39
lines changed

libraries/OpenThread/examples/COAP/coap_lamp/coap_lamp.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
4848
Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role.");
4949
// wait for the expected Device Role to start
5050
uint8_t tries = 24; // 24 x 2.5 sec = 1 min
51-
while (tries && getOtDeviceRole() != expectedRole) {
51+
while (tries && otGetDeviceRole() != expectedRole) {
5252
Serial.print(".");
5353
delay(2500);
5454
tries--;
5555
}
5656
Serial.println();
5757
if (!tries) {
58-
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", getStringOtDeviceRole());
58+
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", otGetStringDeviceRole());
5959
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed!
6060
return false;
6161
}
62-
Serial.printf("Device is %s.\r\n", getStringOtDeviceRole());
62+
Serial.printf("Device is %s.\r\n", otGetStringDeviceRole());
6363
for (i = 0; i < nCmds2; i++) {
6464
if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) {
6565
break;

libraries/OpenThread/examples/COAP/coap_switch/coap_switch.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
4444
Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role.");
4545
// wait for the expected Device Role to start
4646
uint8_t tries = 24; // 24 x 2.5 sec = 1 min
47-
while (tries && getOtDeviceRole() != expectedRole1 && getOtDeviceRole() != expectedRole2) {
47+
while (tries && otGetDeviceRole() != expectedRole1 && otGetDeviceRole() != expectedRole2) {
4848
Serial.print(".");
4949
delay(2500);
5050
tries--;
5151
}
5252
Serial.println();
5353
if (!tries) {
54-
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", getStringOtDeviceRole());
54+
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", otGetStringDeviceRole());
5555
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed!
5656
return false;
5757
}
58-
Serial.printf("Device is %s.\r\n", getStringOtDeviceRole());
58+
Serial.printf("Device is %s.\r\n", otGetStringDeviceRole());
5959
for (i = 0; i < nCmds2; i++) {
6060
if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) {
6161
break;

libraries/OpenThread/examples/SimpleNode/SimpleNode.ino

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
void setup() {
2323
Serial.begin(115200);
2424
OThreadCLI.begin(); // AutoStart using Thread default settings
25+
otPrintNetworkInformation(Serial); // Print Current Thread Network Information
2526
}
2627

2728
void loop() {
2829
Serial.print("Thread Node State: ");
29-
Serial.println(getStringOtDeviceRole());
30+
Serial.println(otGetStringDeviceRole());
3031
delay(5000);
3132
}

libraries/OpenThread/examples/SimpleThreadNetwork/LeaderNode/LeaderNode.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ void setup() {
3232

3333
void loop() {
3434
Serial.print("Thread Node State: ");
35-
Serial.println(getStringOtDeviceRole());
35+
Serial.println(otGetStringDeviceRole());
3636
delay(5000);
3737
}

libraries/OpenThread/examples/SimpleThreadNetwork/RouterNode/RouterNode.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ void setup() {
3232

3333
void loop() {
3434
Serial.print("Thread Node State: ");
35-
Serial.println(getStringOtDeviceRole());
35+
Serial.println(otGetStringDeviceRole());
3636
delay(5000);
3737
}

libraries/OpenThread/keywords.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ available KEYWORD2
2929
read KEYWORD2
3030
peek KEYWORD2
3131
flush KEYWORD2
32-
getOtDeviceRole KEYWORD2
33-
getStringOtDeviceRole KEYWORD2
32+
otGetDeviceRole KEYWORD2
33+
otGetStringDeviceRole KEYWORD2
34+
otGetRespCmd KEYWORD2
3435
otExecCommand KEYWORD2
36+
otPrintNetworkInformation KEYWORD2
3537

3638
#######################################
3739
# Constants (LITERAL1)

libraries/OpenThread/src/OThreadCLI.cpp

+6-22
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ OpenThreadCLI::~OpenThreadCLI() {
220220
end();
221221
}
222222

223+
OpenThreadCLI::operator bool() const {
224+
return otStarted;
225+
}
226+
223227
static void ot_task_worker(void *aContext) {
224228
esp_vfs_eventfd_config_t eventfd_config = {
225229
.max_fds = 3,
@@ -301,36 +305,15 @@ void OpenThreadCLI::begin(bool OThreadAutoStart) {
301305
} else {
302306
log_i("Got active NVS dataset from OpenThread");
303307
}
304-
#if 1
305308
esp_err_t err = esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL);
306309
if (err != ESP_OK) {
307310
log_i("Failed to AUTO start OpenThread");
308311
} else {
309312
log_i("AUTO start OpenThread done");
310313
}
311314

312-
#else
313-
if (dataset.mLength > 0) {
314-
Serial.println("There is a dataset in NVS. Trying to start OpenThread...");
315-
log_i("There is a dataset in NVS. Trying to start OpenThread...");
316-
if (IDFesp_openthread_auto_start(&dataset) != ESP_OK) {
317-
Serial.println("Failed to start OpenThread using NVS dataset. Trying a new one...");
318-
log_i("Failed to start OpenThread using NVS dataset. Trying a new one...");
319-
if (IDFesp_openthread_auto_start(NULL) != ESP_OK) {
320-
Serial.println("Failed to start OpenThread using a new dataset");
321-
log_i("Failed to start OpenThread using a new dataset");
322-
}
323-
}
324-
} else {
325-
Serial.println("There is no dataset in NVS. Trying a new one...");
326-
log_i("There is no dataset in NVS. Trying a new one...");
327-
if (IDFesp_openthread_auto_start(NULL) != ESP_OK) {
328-
Serial.println("Failed to start OpenThread using a new dataset");
329-
log_i("Failed to start OpenThread using a new dataset");
330-
}
331-
}
332-
#endif
333315
}
316+
otStarted = true;
334317
return;
335318
}
336319

@@ -350,6 +333,7 @@ void OpenThreadCLI::end() {
350333
}
351334
setRxBufferSize(0);
352335
setTxBufferSize(0);
336+
otStarted = false;
353337
}
354338

355339

libraries/OpenThread/src/OThreadCLI.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ typedef std::function<void(void)> OnReceiveCb_t;
2323
class OpenThreadCLI : public Stream {
2424
private:
2525
static size_t setBuffer(xQueueHandle &queue, size_t len);
26+
bool otStarted = false;
2627
public:
2728
OpenThreadCLI();
2829
~OpenThreadCLI();
30+
operator bool() const;
2931

3032
// starts a task to read/write otStream. Default prompt is "ot> ". Set it to NULL to make it invisible.
3133
void startOpenThreadConsole(Stream& otStream, bool echoback = true, const char *prompt = "ot> ");

libraries/OpenThread/src/OThreadCLI_Util.cpp

+88-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,66 @@ static const char *otRoleString[] = {
1212
"Leader", ///< The Thread Leader role.
1313
};
1414

15-
ot_device_role_t getOtDeviceRole() {
15+
ot_device_role_t otGetDeviceRole() {
16+
if (!OThreadCLI) {
17+
return OT_ROLE_DISABLED;
18+
}
1619
otInstance *instance = esp_openthread_get_instance();
1720
return (ot_device_role_t) otThreadGetDeviceRole(instance);
1821
}
1922

20-
const char* getStringOtDeviceRole() {
21-
return otRoleString[getOtDeviceRole()];
23+
const char* otGetStringDeviceRole() {
24+
return otRoleString[otGetDeviceRole()];
25+
}
26+
27+
bool otGetRespCmd(const char *cmd, char *resp, uint32_t respTimeout) {
28+
if (!OThreadCLI) {
29+
return false;
30+
}
31+
StreamString cliRespAllLines;
32+
char cliResp[256] = {0};
33+
if (resp != NULL) {
34+
*resp = '\0';
35+
}
36+
if (cmd == NULL) {
37+
return true;
38+
}
39+
OThreadCLI.println(cmd);
40+
log_d("CMD[%s]", cmd);
41+
uint32_t timeout = millis() + respTimeout;
42+
while(millis() < timeout) {
43+
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
44+
// clip it on EOL
45+
for (int i = 0; i < len; i++) {
46+
if (cliResp[i] == '\r' || cliResp[i] == '\n') {
47+
cliResp[i] = '\0';
48+
}
49+
}
50+
log_d("Resp[%s]", cliResp);
51+
if (strncmp(cliResp, "Done", 4) && strncmp(cliResp, "Error", 4)) {
52+
cliRespAllLines += cliResp;
53+
cliRespAllLines.println(); // Adds whatever EOL is for the OS
54+
} else {
55+
break;
56+
}
57+
}
58+
if (!strncmp(cliResp, "Error", 4) || millis() > timeout) {
59+
return false;
60+
}
61+
if (resp != NULL) {
62+
strcpy(resp, cliRespAllLines.c_str());
63+
}
64+
return true;
2265
}
2366

2467
bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode) {
25-
char cliResp[256];
68+
if (!OThreadCLI) {
69+
return false;
70+
}
71+
char cliResp[256] = {0};
72+
if (cmd == NULL) {
73+
return true;
74+
}
2675
if (arg == NULL) {
2776
OThreadCLI.println(cmd);
2877
} else {
@@ -73,5 +122,40 @@ bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode
73122
}
74123
}
75124

125+
void otPrintNetworkInformation(Stream &output) {
126+
if (!OThreadCLI) {
127+
return;
128+
}
129+
char resp[512];
130+
output.println("Thread Setup:");
131+
if (otGetRespCmd("state", resp)) {
132+
output.printf("Node State: \t%s", resp);
133+
}
134+
if (otGetRespCmd("networkname", resp)) {
135+
output.printf("Network Name: \t%s", resp);
136+
}
137+
if (otGetRespCmd("channel", resp)) {
138+
output.printf("Channel: \t%s", resp);
139+
}
140+
if (otGetRespCmd("panid", resp)) {
141+
output.printf("Pan ID: \t%s", resp);
142+
}
143+
if (otGetRespCmd("extpanid", resp)) {
144+
output.printf("Ext Pan ID: \t%s", resp);
145+
}
146+
if (otGetRespCmd("networkkey", resp)) {
147+
output.printf("Network Key: \t%s", resp);
148+
}
149+
if (otGetRespCmd("ipaddr", resp)) {
150+
output.println("Node IP Addresses are:");
151+
output.printf("%s", resp);
152+
}
153+
if (otGetRespCmd("ipmaddr", resp)) {
154+
output.println("Node Multicast Addresses are:");
155+
output.printf("%s", resp);
156+
}
157+
}
158+
159+
76160
#endif /* CONFIG_OPENTHREAD_ENABLED */
77161
#endif /* SOC_IEEE802154_SUPPORTED */

libraries/OpenThread/src/OThreadCLI_Util.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ typedef struct {
1717
String errorMessage;
1818
} ot_cmd_return_t;
1919

20-
ot_device_role_t getOtDeviceRole();
21-
const char* getStringOtDeviceRole();
20+
ot_device_role_t otGetDeviceRole();
21+
const char* otGetStringDeviceRole();
22+
bool otGetRespCmd(const char *cmd, char *resp = NULL, uint32_t respTimeout = 5000);
2223
bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode = NULL);
24+
void otPrintNetworkInformation(Stream &output);
2325

2426
#endif /* CONFIG_OPENTHREAD_ENABLED */
2527
#endif /* SOC_IEEE802154_SUPPORTED */

0 commit comments

Comments
 (0)