Skip to content

Commit

Permalink
fix(openthread): Improves code
Browse files Browse the repository at this point in the history
  • Loading branch information
SuGlider committed Jun 20, 2024
1 parent bbece1c commit 5c677be
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 39 deletions.
6 changes: 3 additions & 3 deletions libraries/OpenThread/examples/COAP/coap_lamp/coap_lamp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role.");
// wait for the expected Device Role to start
uint8_t tries = 24; // 24 x 2.5 sec = 1 min
while (tries && getOtDeviceRole() != expectedRole) {
while (tries && otGetDeviceRole() != expectedRole) {
Serial.print(".");
delay(2500);
tries--;
}
Serial.println();
if (!tries) {
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", getStringOtDeviceRole());
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", otGetStringDeviceRole());
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed!
return false;
}
Serial.printf("Device is %s.\r\n", getStringOtDeviceRole());
Serial.printf("Device is %s.\r\n", otGetStringDeviceRole());
for (i = 0; i < nCmds2; i++) {
if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role.");
// wait for the expected Device Role to start
uint8_t tries = 24; // 24 x 2.5 sec = 1 min
while (tries && getOtDeviceRole() != expectedRole1 && getOtDeviceRole() != expectedRole2) {
while (tries && otGetDeviceRole() != expectedRole1 && otGetDeviceRole() != expectedRole2) {
Serial.print(".");
delay(2500);
tries--;
}
Serial.println();
if (!tries) {
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", getStringOtDeviceRole());
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", otGetStringDeviceRole());
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed!
return false;
}
Serial.printf("Device is %s.\r\n", getStringOtDeviceRole());
Serial.printf("Device is %s.\r\n", otGetStringDeviceRole());
for (i = 0; i < nCmds2; i++) {
if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) {
break;
Expand Down
3 changes: 2 additions & 1 deletion libraries/OpenThread/examples/SimpleNode/SimpleNode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
void setup() {
Serial.begin(115200);
OThreadCLI.begin(); // AutoStart using Thread default settings
otPrintNetworkInformation(Serial); // Print Current Thread Network Information
}

void loop() {
Serial.print("Thread Node State: ");
Serial.println(getStringOtDeviceRole());
Serial.println(otGetStringDeviceRole());
delay(5000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ void setup() {

void loop() {
Serial.print("Thread Node State: ");
Serial.println(getStringOtDeviceRole());
Serial.println(otGetStringDeviceRole());
delay(5000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ void setup() {

void loop() {
Serial.print("Thread Node State: ");
Serial.println(getStringOtDeviceRole());
Serial.println(otGetStringDeviceRole());
delay(5000);
}
6 changes: 4 additions & 2 deletions libraries/OpenThread/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ available KEYWORD2
read KEYWORD2
peek KEYWORD2
flush KEYWORD2
getOtDeviceRole KEYWORD2
getStringOtDeviceRole KEYWORD2
otGetDeviceRole KEYWORD2
otGetStringDeviceRole KEYWORD2
otGetRespCmd KEYWORD2
otExecCommand KEYWORD2
otPrintNetworkInformation KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
28 changes: 6 additions & 22 deletions libraries/OpenThread/src/OThreadCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ OpenThreadCLI::~OpenThreadCLI() {
end();
}

OpenThreadCLI::operator bool() const {
return otStarted;
}

static void ot_task_worker(void *aContext) {
esp_vfs_eventfd_config_t eventfd_config = {
.max_fds = 3,
Expand Down Expand Up @@ -301,36 +305,15 @@ void OpenThreadCLI::begin(bool OThreadAutoStart) {
} else {
log_i("Got active NVS dataset from OpenThread");
}
#if 1
esp_err_t err = esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL);
if (err != ESP_OK) {
log_i("Failed to AUTO start OpenThread");
} else {
log_i("AUTO start OpenThread done");
}

#else
if (dataset.mLength > 0) {
Serial.println("There is a dataset in NVS. Trying to start OpenThread...");
log_i("There is a dataset in NVS. Trying to start OpenThread...");
if (IDFesp_openthread_auto_start(&dataset) != ESP_OK) {
Serial.println("Failed to start OpenThread using NVS dataset. Trying a new one...");
log_i("Failed to start OpenThread using NVS dataset. Trying a new one...");
if (IDFesp_openthread_auto_start(NULL) != ESP_OK) {
Serial.println("Failed to start OpenThread using a new dataset");
log_i("Failed to start OpenThread using a new dataset");
}
}
} else {
Serial.println("There is no dataset in NVS. Trying a new one...");
log_i("There is no dataset in NVS. Trying a new one...");
if (IDFesp_openthread_auto_start(NULL) != ESP_OK) {
Serial.println("Failed to start OpenThread using a new dataset");
log_i("Failed to start OpenThread using a new dataset");
}
}
#endif
}
otStarted = true;
return;
}

Expand All @@ -350,6 +333,7 @@ void OpenThreadCLI::end() {
}
setRxBufferSize(0);
setTxBufferSize(0);
otStarted = false;
}


Expand Down
2 changes: 2 additions & 0 deletions libraries/OpenThread/src/OThreadCLI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ typedef std::function<void(void)> OnReceiveCb_t;
class OpenThreadCLI : public Stream {
private:
static size_t setBuffer(xQueueHandle &queue, size_t len);
bool otStarted = false;
public:
OpenThreadCLI();
~OpenThreadCLI();
operator bool() const;

// starts a task to read/write otStream. Default prompt is "ot> ". Set it to NULL to make it invisible.
void startOpenThreadConsole(Stream& otStream, bool echoback = true, const char *prompt = "ot> ");
Expand Down
92 changes: 88 additions & 4 deletions libraries/OpenThread/src/OThreadCLI_Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,66 @@ static const char *otRoleString[] = {
"Leader", ///< The Thread Leader role.
};

ot_device_role_t getOtDeviceRole() {
ot_device_role_t otGetDeviceRole() {
if (!OThreadCLI) {
return OT_ROLE_DISABLED;
}
otInstance *instance = esp_openthread_get_instance();
return (ot_device_role_t) otThreadGetDeviceRole(instance);
}

const char* getStringOtDeviceRole() {
return otRoleString[getOtDeviceRole()];
const char* otGetStringDeviceRole() {
return otRoleString[otGetDeviceRole()];
}

bool otGetRespCmd(const char *cmd, char *resp, uint32_t respTimeout) {
if (!OThreadCLI) {
return false;
}
StreamString cliRespAllLines;
char cliResp[256] = {0};
if (resp != NULL) {
*resp = '\0';
}
if (cmd == NULL) {
return true;
}
OThreadCLI.println(cmd);
log_d("CMD[%s]", cmd);
uint32_t timeout = millis() + respTimeout;
while(millis() < timeout) {
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
// clip it on EOL
for (int i = 0; i < len; i++) {
if (cliResp[i] == '\r' || cliResp[i] == '\n') {
cliResp[i] = '\0';
}
}
log_d("Resp[%s]", cliResp);
if (strncmp(cliResp, "Done", 4) && strncmp(cliResp, "Error", 4)) {
cliRespAllLines += cliResp;
cliRespAllLines.println(); // Adds whatever EOL is for the OS
} else {
break;
}
}
if (!strncmp(cliResp, "Error", 4) || millis() > timeout) {
return false;
}
if (resp != NULL) {
strcpy(resp, cliRespAllLines.c_str());
}
return true;
}

bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode) {
char cliResp[256];
if (!OThreadCLI) {
return false;
}
char cliResp[256] = {0};
if (cmd == NULL) {
return true;
}
if (arg == NULL) {
OThreadCLI.println(cmd);
} else {
Expand Down Expand Up @@ -73,5 +122,40 @@ bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode
}
}

void otPrintNetworkInformation(Stream &output) {
if (!OThreadCLI) {
return;
}
char resp[512];
output.println("Thread Setup:");
if (otGetRespCmd("state", resp)) {
output.printf("Node State: \t%s", resp);
}
if (otGetRespCmd("networkname", resp)) {
output.printf("Network Name: \t%s", resp);
}
if (otGetRespCmd("channel", resp)) {
output.printf("Channel: \t%s", resp);
}
if (otGetRespCmd("panid", resp)) {
output.printf("Pan ID: \t%s", resp);
}
if (otGetRespCmd("extpanid", resp)) {
output.printf("Ext Pan ID: \t%s", resp);
}
if (otGetRespCmd("networkkey", resp)) {
output.printf("Network Key: \t%s", resp);
}
if (otGetRespCmd("ipaddr", resp)) {
output.println("Node IP Addresses are:");
output.printf("%s", resp);
}
if (otGetRespCmd("ipmaddr", resp)) {
output.println("Node Multicast Addresses are:");
output.printf("%s", resp);
}
}


#endif /* CONFIG_OPENTHREAD_ENABLED */
#endif /* SOC_IEEE802154_SUPPORTED */
6 changes: 4 additions & 2 deletions libraries/OpenThread/src/OThreadCLI_Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ typedef struct {
String errorMessage;
} ot_cmd_return_t;

ot_device_role_t getOtDeviceRole();
const char* getStringOtDeviceRole();
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);
void otPrintNetworkInformation(Stream &output);

#endif /* CONFIG_OPENTHREAD_ENABLED */
#endif /* SOC_IEEE802154_SUPPORTED */

0 comments on commit 5c677be

Please sign in to comment.