Skip to content

Commit 3507a9d

Browse files
committed
feat(openthread): onReceice example
1 parent 28d7c44 commit 3507a9d

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"targets": {
3+
"esp32": false,
4+
"esp32c2": false,
5+
"esp32c3": false,
6+
"esp32s2": false,
7+
"esp32s3": false
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
OpenThread.begin() will automatically start a node in a Thread Network
3+
This will demonstrate how to capture the CLI response in a callback function
4+
The device state shall change from "disabled" to valid Thread states along time
5+
*/
6+
7+
#include "OThreadCLI.h"
8+
9+
// reads all the lines sent by CLI, one by one
10+
// ignores some lines that are just a sequence of \r\n
11+
void otReceivedLine() {
12+
String line = "";
13+
while (OThreadCLI.available() > 0) {
14+
char ch = OThreadCLI.read();
15+
if (ch != '\r' && ch != '\n') {
16+
line += ch;
17+
}
18+
}
19+
// ignores empty lines, usually EOL sequence
20+
if (line.length() > 0) {
21+
Serial.print("OpenThread CLI RESP===> ");
22+
Serial.println(line.c_str());
23+
}
24+
}
25+
26+
void setup() {
27+
Serial.begin(115200);
28+
OThreadCLI.begin(); // AutoStart
29+
OThreadCLI.onReceive(otReceivedLine);
30+
}
31+
32+
void loop() {
33+
// sends the "state" command to the CLI every second
34+
// the onReceive() Callback Function will read and process the response
35+
OThreadCLI.println("state");
36+
delay(1000);
37+
}

libraries/OpenThread/src/OThreadCLI.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ static int ot_cli_output_callback(void *context, const char *format, va_list arg
9999
for (int i = 0; i < ret; i++) {
100100
xQueueSend(rx_queue, &buf[i], 0);
101101
}
102+
// if there is a user callback function in place, it shall have the priority
103+
// to process/consume the Stream data received from OpenThread CLI, which is available in its RX Buffer
104+
if (otConsole.responseCallBack != NULL) {
105+
otConsole.responseCallBack();
106+
}
102107
}
103108
}
104109
}
@@ -144,13 +149,8 @@ static void ot_cli_console_worker(void *context) {
144149
if (c == '\n' && lastReadChar != '\n') {
145150
// wait for the OpenThread CLI to finish processing the command
146151
xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
147-
// if there is a user callback function in place, it shall have the priority
148-
// to process/consume the Stream data received from OpenThread CLI, which is available in its RX Buffer
149-
if (cli->responseCallBack != NULL) {
150-
cli->responseCallBack();
151-
}
152152
// read response from OpenThread CLI and send it to the Stream
153-
while (OThreadCLI.available()) {
153+
while (OThreadCLI.available() > 0) {
154154
char c = OThreadCLI.read();
155155
// echo it back to the console
156156
if (cli->echoback) {
@@ -182,7 +182,7 @@ void OpenThreadCLI::setStream(Stream& otStream) {
182182
otConsole.cliStream = &otStream;
183183
}
184184

185-
void onReceive(OnReceiveCb_t func) {
185+
void OpenThreadCLI::onReceive(OnReceiveCb_t func) {
186186
otConsole.responseCallBack = func; // NULL will set it off
187187
}
188188

0 commit comments

Comments
 (0)