Skip to content

Commit

Permalink
Merge branch 'remotely_reset'
Browse files Browse the repository at this point in the history
  • Loading branch information
iPAS committed Dec 13, 2021
2 parents 3b70262 + f72bb4f commit 5fecb4c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
12 changes: 12 additions & 0 deletions Main/all_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@
#include "bind.h"


// ------------
// From cli.ino
// ------------
extern const char * remote_commands[];
#define REMOTE_CMD_HELLO remote_commands[0]
#define REMOTE_CMD_PING remote_commands[1]
#define REMOTE_CMD_RESET remote_commands[2]

extern const char * remote_responses[];
#define REMOTE_RESP_PING remote_responses[1]


#endif // __ALL_HEADERS_H__
45 changes: 39 additions & 6 deletions Main/cli.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ static Command cmd_status_report;
static Command cmd_gps_report;
static Command cmd_axp_report;
static Command cmd_flood_ping;
static Command cmd_reset;

const char * remote_commands[] = {
">>> hello\n",
">>> ping\n",
">>> reset\n",
};
const char * remote_responses[] = {
"",
"+++ pong\n",
"",
};

// ----------------------------------------------------------------------------
static boolean is_numeric(String str) {
Expand Down Expand Up @@ -83,6 +95,7 @@ void on_cmd_help(cmd *c) {
"\tgps [sink_id] -- send GPS report to sink [default 0]",
"\taxp [sink_id] -- send AXP report to sink [default 0]",
"\tping [sink_id] -- ping to sink for testing [default 0]",
"\treset [sink_id] -- reset the sink [default 0]",
};
uint8_t i;
Command cmd(c);
Expand Down Expand Up @@ -137,9 +150,8 @@ void on_cmd_flood_send(cmd *c) {
term_println("[CLI] send: illegal sink id"); // Illegal id
}
else {
const char msg[] = "hello\n";
if (flood_send_to(id, msg, sizeof(msg)-1)) {
term_printf("[CLI] send: '%s' to %d", msg, id);
if (flood_send_to(id, REMOTE_CMD_HELLO, strlen(REMOTE_CMD_HELLO))) {
term_printf("[CLI] send: hello to %d", id);
}
else {
term_println("[CLI] send: flood_send_to() failed!");
Expand Down Expand Up @@ -220,16 +232,35 @@ void on_cmd_flood_ping(cmd *c) {
term_println("[CLI] ping: illegal sink id"); // Illegal id
}
else {
const char msg[] = "ping\n";
if (flood_send_to(id, msg, sizeof(msg)-1)) {
term_printf("[CLI] ping: '%s' to %d", msg, id);
if (flood_send_to(id, REMOTE_CMD_PING, strlen(REMOTE_CMD_PING))) {
term_printf("[CLI] ping: to %d", id);
}
else {
term_println("[CLI] ping: flood_send_to() failed!");
}
}
}

// ----------------------------------------------------------------------------
void on_cmd_reset(cmd *c) {
Command cmd(c);
Argument arg = cmd.getArgument("id");
long id;
bool legal_id = extract_id(arg, &id);

if (legal_id == false) {
term_println("[CLI] reset: illegal sink id"); // Illegal id
}
else {
if (flood_send_to(id, REMOTE_CMD_RESET, strlen(REMOTE_CMD_RESET))) {
term_printf("[CLI] reset: to %d", id);
}
else {
term_println("[CLI] reset: flood_send_to() failed!");
}
}
}

// ----------------------------------------------------------------------------
void cli_setup() {
Serial.begin(115200);
Expand All @@ -252,6 +283,8 @@ void cli_setup() {
cmd_axp_report.addPositionalArgument("id", "0"); // Default value is "0"
cmd_flood_ping = cli.addCommand("ping", on_cmd_flood_ping);
cmd_flood_ping.addPositionalArgument("id", "0"); // Default value is "0"
cmd_reset = cli.addCommand("reset", on_cmd_reset);
cmd_reset.addPositionalArgument("id", "0"); // Default value is "0"
}

// ----------------------------------------------------------------------------
Expand Down
32 changes: 24 additions & 8 deletions Main/lora.ino
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,30 @@ void on_flood_receive(void *message, uint8_t len) {
term_println("[/D]");


// If coming from "ping" CLI
if (strncmp((const char *)data, "ping\n", data_len) == 0) {
const char msg[] = "pong\n";
if (flood_send_to(hdr->originSource, msg, sizeof(msg)-1)) {
term_printf("[LORA] pong: back to %d", hdr->originSource);
}
else {
term_println("[LORA] pong: flood_send_to() failed!");
// Command processing
for (i = 0; i < sizeof(remote_commands)/sizeof(remote_commands[0]); i++) {
if (strncmp((const char *)data, remote_commands[i], data_len) == 0) {

if (remote_commands[i] == REMOTE_CMD_HELLO) {
// Do nothing
}
else

if (remote_commands[i] == REMOTE_CMD_PING) {
if (flood_send_to(hdr->originSource, REMOTE_RESP_PING, strlen(REMOTE_RESP_PING))) {
term_printf("[LORA] pong: back to %d", hdr->originSource);
}
else {
term_println("[LORA] pong: flood_send_to() failed!");
}
}
else

if (remote_commands[i] == REMOTE_CMD_RESET) {
term_printf("[LORA] reset: from %d", hdr->originSource);
ESP.restart(); // TODO: delay reset!!
}

}
}
}
Expand Down

0 comments on commit 5fecb4c

Please sign in to comment.