From 20051b983e2ce020a8366e6839ea777b0f52aba2 Mon Sep 17 00:00:00 2001 From: overkillfpv Date: Mon, 20 Apr 2026 21:14:26 +1000 Subject: [PATCH] feat: enhance command handling to support multiple commands in a single input --- examples/simple_repeater/MyMesh.cpp | 35 ++++++++++++++++++++++++++++- examples/simple_repeater/main.cpp | 34 +++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 666f79fc..33b733ec 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -719,7 +719,40 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, if (is_retry) { *reply = 0; } else { - handleCommand(sender_timestamp, command, reply); + reply[0] = 0; + int reply_remaining = 160; + char *reply_ptr = reply; + static char cmd_buf[160]; + strncpy(cmd_buf, command, sizeof(cmd_buf) - 1); + cmd_buf[sizeof(cmd_buf) - 1] = 0; + char *cmd_tok = cmd_buf; + char *sep; + bool first = true; + while (cmd_tok && reply_remaining > 1) { + sep = strchr(cmd_tok, ';'); + if (sep) *sep = 0; + // trim leading spaces + while (*cmd_tok == ' ') cmd_tok++; + if (*cmd_tok) { + static char single_reply[160]; + single_reply[0] = 0; + handleCommand(sender_timestamp, cmd_tok, single_reply); + int slen = strlen(single_reply); + if (slen > 0) { + if (!first && reply_remaining > 1) { + *reply_ptr++ = ';'; + reply_remaining--; + } + int copy_len = (slen < reply_remaining) ? slen : reply_remaining; + memcpy(reply_ptr, single_reply, copy_len); + reply_ptr += copy_len; + reply_remaining -= copy_len; + first = false; + } + } + cmd_tok = sep ? sep + 1 : NULL; + } + *reply_ptr = 0; } int text_len = strlen(reply); if (text_len > 0) { diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index e37078ce..a630132e 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -124,7 +124,39 @@ void loop() { Serial.print('\n'); command[len - 1] = 0; // replace newline with C string null terminator char reply[160]; - the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial! + reply[0] = 0; + int reply_remaining = sizeof(reply) - 1; + char *reply_ptr = reply; + static char cmd_buf[sizeof(command)]; + strncpy(cmd_buf, command, sizeof(cmd_buf) - 1); + cmd_buf[sizeof(cmd_buf) - 1] = 0; + char *cmd_tok = cmd_buf; + char *sep; + bool first = true; + while (cmd_tok && reply_remaining > 1) { + sep = strchr(cmd_tok, ';'); + if (sep) *sep = 0; + while (*cmd_tok == ' ') cmd_tok++; + if (*cmd_tok) { + static char single_reply[160]; + single_reply[0] = 0; + the_mesh.handleCommand(0, cmd_tok, single_reply); // NOTE: there is no sender_timestamp via serial! + int slen = strlen(single_reply); + if (slen > 0) { + if (!first && reply_remaining > 1) { + *reply_ptr++ = ';'; + reply_remaining--; + } + int copy_len = (slen < reply_remaining) ? slen : reply_remaining; + memcpy(reply_ptr, single_reply, copy_len); + reply_ptr += copy_len; + reply_remaining -= copy_len; + first = false; + } + } + cmd_tok = sep ? sep + 1 : NULL; + } + *reply_ptr = 0; if (reply[0]) { Serial.print(" -> "); Serial.println(reply); }