Skip to content

Commit 20051b9

Browse files
committed
feat: enhance command handling to support multiple commands in a single input
1 parent 49b37d5 commit 20051b9

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

examples/simple_repeater/MyMesh.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,40 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
719719
if (is_retry) {
720720
*reply = 0;
721721
} else {
722-
handleCommand(sender_timestamp, command, reply);
722+
reply[0] = 0;
723+
int reply_remaining = 160;
724+
char *reply_ptr = reply;
725+
static char cmd_buf[160];
726+
strncpy(cmd_buf, command, sizeof(cmd_buf) - 1);
727+
cmd_buf[sizeof(cmd_buf) - 1] = 0;
728+
char *cmd_tok = cmd_buf;
729+
char *sep;
730+
bool first = true;
731+
while (cmd_tok && reply_remaining > 1) {
732+
sep = strchr(cmd_tok, ';');
733+
if (sep) *sep = 0;
734+
// trim leading spaces
735+
while (*cmd_tok == ' ') cmd_tok++;
736+
if (*cmd_tok) {
737+
static char single_reply[160];
738+
single_reply[0] = 0;
739+
handleCommand(sender_timestamp, cmd_tok, single_reply);
740+
int slen = strlen(single_reply);
741+
if (slen > 0) {
742+
if (!first && reply_remaining > 1) {
743+
*reply_ptr++ = ';';
744+
reply_remaining--;
745+
}
746+
int copy_len = (slen < reply_remaining) ? slen : reply_remaining;
747+
memcpy(reply_ptr, single_reply, copy_len);
748+
reply_ptr += copy_len;
749+
reply_remaining -= copy_len;
750+
first = false;
751+
}
752+
}
753+
cmd_tok = sep ? sep + 1 : NULL;
754+
}
755+
*reply_ptr = 0;
723756
}
724757
int text_len = strlen(reply);
725758
if (text_len > 0) {

examples/simple_repeater/main.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,39 @@ void loop() {
124124
Serial.print('\n');
125125
command[len - 1] = 0; // replace newline with C string null terminator
126126
char reply[160];
127-
the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
127+
reply[0] = 0;
128+
int reply_remaining = sizeof(reply) - 1;
129+
char *reply_ptr = reply;
130+
static char cmd_buf[sizeof(command)];
131+
strncpy(cmd_buf, command, sizeof(cmd_buf) - 1);
132+
cmd_buf[sizeof(cmd_buf) - 1] = 0;
133+
char *cmd_tok = cmd_buf;
134+
char *sep;
135+
bool first = true;
136+
while (cmd_tok && reply_remaining > 1) {
137+
sep = strchr(cmd_tok, ';');
138+
if (sep) *sep = 0;
139+
while (*cmd_tok == ' ') cmd_tok++;
140+
if (*cmd_tok) {
141+
static char single_reply[160];
142+
single_reply[0] = 0;
143+
the_mesh.handleCommand(0, cmd_tok, single_reply); // NOTE: there is no sender_timestamp via serial!
144+
int slen = strlen(single_reply);
145+
if (slen > 0) {
146+
if (!first && reply_remaining > 1) {
147+
*reply_ptr++ = ';';
148+
reply_remaining--;
149+
}
150+
int copy_len = (slen < reply_remaining) ? slen : reply_remaining;
151+
memcpy(reply_ptr, single_reply, copy_len);
152+
reply_ptr += copy_len;
153+
reply_remaining -= copy_len;
154+
first = false;
155+
}
156+
}
157+
cmd_tok = sep ? sep + 1 : NULL;
158+
}
159+
*reply_ptr = 0;
128160
if (reply[0]) {
129161
Serial.print(" -> "); Serial.println(reply);
130162
}

0 commit comments

Comments
 (0)