mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
feat: enhance command handling to support multiple commands in a single input
This commit is contained in:
parent
49b37d5622
commit
20051b983e
2 changed files with 67 additions and 2 deletions
|
|
@ -719,7 +719,40 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
||||||
if (is_retry) {
|
if (is_retry) {
|
||||||
*reply = 0;
|
*reply = 0;
|
||||||
} else {
|
} 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);
|
int text_len = strlen(reply);
|
||||||
if (text_len > 0) {
|
if (text_len > 0) {
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,39 @@ void loop() {
|
||||||
Serial.print('\n');
|
Serial.print('\n');
|
||||||
command[len - 1] = 0; // replace newline with C string null terminator
|
command[len - 1] = 0; // replace newline with C string null terminator
|
||||||
char reply[160];
|
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]) {
|
if (reply[0]) {
|
||||||
Serial.print(" -> "); Serial.println(reply);
|
Serial.print(" -> "); Serial.println(reply);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue