2025-01-13 14:07:48 +11:00
|
|
|
#include <Arduino.h> // needed for PlatformIO
|
|
|
|
|
#include <Mesh.h>
|
2025-01-20 20:22:40 +11:00
|
|
|
|
2025-09-08 21:46:19 +10:00
|
|
|
#include "MyMesh.h"
|
2025-06-03 19:01:03 +10:00
|
|
|
|
2025-03-05 12:52:29 +11:00
|
|
|
#ifdef DISPLAY_CLASS
|
|
|
|
|
#include "UITask.h"
|
|
|
|
|
static UITask ui_task(display);
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-03-10 22:35:31 -07:00
|
|
|
#ifdef ETHERNET_ENABLED
|
2026-03-13 10:48:42 -07:00
|
|
|
#define ETHERNET_CLI_BANNER "MeshCore Repeater CLI"
|
|
|
|
|
#include <helpers/nrf52/EthernetCLI.h>
|
2026-03-09 14:52:01 -07:00
|
|
|
#endif
|
|
|
|
|
|
2025-01-13 14:07:48 +11:00
|
|
|
StdRNG fast_rng;
|
|
|
|
|
SimpleMeshTables tables;
|
2025-01-22 19:47:53 +11:00
|
|
|
|
2025-03-27 19:34:16 +11:00
|
|
|
MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
|
2025-01-13 14:07:48 +11:00
|
|
|
|
|
|
|
|
void halt() {
|
|
|
|
|
while (1) ;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 09:14:15 +02:00
|
|
|
#if defined(SIM_PLATFORM) && defined(__EMSCRIPTEN__)
|
|
|
|
|
#include <emscripten.h>
|
|
|
|
|
// Phase 3 sim ether: a JS-visible "did this repeater just relay a packet"
|
|
|
|
|
// hook. MyMesh (src/Mesh.h's Mesh base class) already keeps an exact count
|
|
|
|
|
// of packets actually re-transmitted in the repeater/transport role --
|
|
|
|
|
// n_forwarded, incremented at the real ACTION_RETRANSMIT decision points in
|
|
|
|
|
// src/Mesh.cpp (routeRecvPacket()) and decremented in onRetransmitCancelled()
|
|
|
|
|
// if an overhear cancels a queued retransmit before it goes out -- and
|
|
|
|
|
// already exposes it publicly as getNumForwarded() (the same number
|
|
|
|
|
// DiagnosticsScreen.h prints on a real companion_radio's screen). Rather
|
|
|
|
|
// than re-deriving "was this a relay" from TX/RX byte timing (fragile,
|
|
|
|
|
// and duplicates logic the real Mesh class already gets right, including
|
|
|
|
|
// the overhear-cancellation edge case), this just exports that exact
|
|
|
|
|
// counter for JS to poll and diff on its own ether tick -- zero changes
|
|
|
|
|
// to any shared/platform-agnostic src/ file.
|
|
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE uint32_t sim_repeater_get_relay_count() {
|
|
|
|
|
return the_mesh.getNumForwarded();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Same "has setup() actually finished" readiness flag as
|
|
|
|
|
// examples/companion_radio/main.cpp -- see that file's comment for why
|
|
|
|
|
// this is needed instead of a fixed post-ready delay.
|
|
|
|
|
static bool g_sim_ready = false;
|
|
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE int sim_is_ready() {
|
|
|
|
|
return g_sim_ready ? 1 : 0;
|
|
|
|
|
}
|
2026-09-03 23:19:36 +02:00
|
|
|
|
|
|
|
|
// Same idea as companion_radio's sim_test_advert_flood() -- MyMesh's own
|
|
|
|
|
// updateAdvertTimer() (called once from begin()) doesn't fire the repeater's
|
2026-09-04 20:53:48 +02:00
|
|
|
// first self-advert for a full 6 minutes (advert_interval defaults to 3,
|
2026-09-03 23:19:36 +02:00
|
|
|
// scaled *2*60*1000ms -- see MyMesh::updateAdvertTimer()/begin() in this
|
|
|
|
|
// same MyMesh.cpp), so a host page that wants hero/B to discover the
|
|
|
|
|
// repeater as a contact immediately on boot (rather than waiting out that
|
|
|
|
|
// timer) needs some way to trigger it early. sendSelfAdvertisement(0, true)
|
|
|
|
|
// is the exact same call updateAdvertTimer()'s scheduled path eventually
|
|
|
|
|
// makes, just invoked now instead of on a timer -- real crypto, real
|
|
|
|
|
// packet, nothing faked.
|
|
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE int sim_test_advert_flood() {
|
|
|
|
|
if (!g_sim_ready) return 0;
|
|
|
|
|
the_mesh.sendSelfAdvertisement(0, true);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2026-09-03 09:14:15 +02:00
|
|
|
#endif
|
|
|
|
|
|
2025-08-01 19:28:44 +10:00
|
|
|
static char command[160];
|
2026-03-10 22:35:31 -07:00
|
|
|
#ifdef ETHERNET_ENABLED
|
|
|
|
|
static char ethernet_command[160];
|
2026-03-09 14:52:01 -07:00
|
|
|
#endif
|
2025-01-15 17:14:08 +11:00
|
|
|
|
2025-12-23 12:48:08 +07:00
|
|
|
// For power saving
|
2026-02-13 23:05:44 +07:00
|
|
|
unsigned long POWERSAVING_FIRSTSLEEP_SECS = 120; // The first sleep (if enabled) from boot
|
2025-12-23 12:48:08 +07:00
|
|
|
|
2026-02-27 22:51:19 -05:00
|
|
|
#if defined(PIN_USER_BTN) && defined(_SEEED_SENSECAP_SOLAR_H_)
|
|
|
|
|
static unsigned long userBtnDownAt = 0;
|
|
|
|
|
#define USER_BTN_HOLD_OFF_MILLIS 1500
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-01-13 14:07:48 +11:00
|
|
|
void setup() {
|
|
|
|
|
Serial.begin(115200);
|
2026-09-03 09:14:15 +02:00
|
|
|
#ifndef SIM_PLATFORM
|
|
|
|
|
// Skip this one-shot boot pause in the sim: under Emscripten it would
|
|
|
|
|
// synchronously block the browser's single JS thread for a full second
|
|
|
|
|
// (Arduino.h's sim delay() is a real std::this_thread::sleep_for(), and
|
|
|
|
|
// this runs before emscripten_set_main_loop ever hands control back) --
|
|
|
|
|
// harmless on a real board's own thread, unnecessary UX friction here.
|
2025-01-19 20:22:02 +11:00
|
|
|
delay(1000);
|
2026-09-03 09:14:15 +02:00
|
|
|
#endif
|
2025-01-13 14:07:48 +11:00
|
|
|
|
|
|
|
|
board.begin();
|
2025-01-15 02:14:52 +11:00
|
|
|
|
2026-03-04 03:13:53 +01:00
|
|
|
#ifdef HAS_EXTERNAL_WATCHDOG
|
|
|
|
|
external_watchdog.begin();
|
2026-02-09 11:43:41 +08:00
|
|
|
#endif
|
|
|
|
|
|
2026-02-05 13:35:04 +00:00
|
|
|
#if defined(MESH_DEBUG) && defined(NRF52_PLATFORM)
|
|
|
|
|
// give some extra time for serial to settle so
|
|
|
|
|
// boot debug messages can be seen on terminal
|
|
|
|
|
delay(5000);
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-04-01 16:00:09 +13:00
|
|
|
#ifdef DISPLAY_CLASS
|
2025-05-19 14:16:55 +10:00
|
|
|
if (display.begin()) {
|
2025-04-01 16:00:09 +13:00
|
|
|
display.startFrame();
|
2025-09-02 13:56:24 +08:00
|
|
|
display.setCursor(0, 0);
|
2025-04-01 16:00:09 +13:00
|
|
|
display.print("Please wait...");
|
|
|
|
|
display.endFrame();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-06-27 19:55:17 +01:00
|
|
|
if (!radio_init()) {
|
2026-02-05 13:35:04 +00:00
|
|
|
MESH_DEBUG_PRINTLN("Radio init failed!");
|
2025-06-27 19:55:17 +01:00
|
|
|
halt();
|
|
|
|
|
}
|
2025-01-15 02:14:52 +11:00
|
|
|
|
2026-05-01 14:47:07 +10:00
|
|
|
fast_rng.begin(radio_driver.getRngSeed());
|
2025-02-02 11:03:23 +11:00
|
|
|
|
2025-01-27 11:13:37 +11:00
|
|
|
FILESYSTEM* fs;
|
2025-05-11 09:26:32 +02:00
|
|
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
2025-01-20 20:22:40 +11:00
|
|
|
InternalFS.begin();
|
2025-01-27 11:13:37 +11:00
|
|
|
fs = &InternalFS;
|
2025-02-04 01:35:04 +11:00
|
|
|
IdentityStore store(InternalFS, "");
|
2025-01-20 20:22:40 +11:00
|
|
|
#elif defined(ESP32)
|
2025-01-13 14:07:48 +11:00
|
|
|
SPIFFS.begin(true);
|
2025-01-27 11:13:37 +11:00
|
|
|
fs = &SPIFFS;
|
2025-01-13 14:07:48 +11:00
|
|
|
IdentityStore store(SPIFFS, "/identity");
|
2025-04-21 21:17:03 +01:00
|
|
|
#elif defined(RP2040_PLATFORM)
|
|
|
|
|
LittleFS.begin();
|
|
|
|
|
fs = &LittleFS;
|
|
|
|
|
IdentityStore store(LittleFS, "/identity");
|
2025-04-22 15:26:04 +10:00
|
|
|
store.begin();
|
2026-09-03 09:14:15 +02:00
|
|
|
#elif defined(SIM_PLATFORM)
|
|
|
|
|
// Real files under ./sim_data_repeater/ (relative to the process's cwd,
|
|
|
|
|
// native; or the Emscripten virtual FS, wasm) -- deliberately a DIFFERENT
|
|
|
|
|
// root than examples/companion_radio/main.cpp's "./sim_data" so a
|
|
|
|
|
// repeater instance's identity can never collide with a companion
|
|
|
|
|
// instance's, even if both happened to run from the same cwd (native) or
|
|
|
|
|
// the same page (wasm, see variants/sim/sim_main.cpp's SIM_FS_ROOT).
|
|
|
|
|
// "static" (not a plain local) so sim_fs outlives setup() -- IdentityStore
|
|
|
|
|
// only stores a pointer to it, same reasoning as the ui_task static above.
|
|
|
|
|
static SimFS sim_fs("./sim_data_repeater");
|
|
|
|
|
fs = &sim_fs;
|
|
|
|
|
IdentityStore store(sim_fs, "/identity");
|
|
|
|
|
store.begin();
|
2025-01-20 20:22:40 +11:00
|
|
|
#else
|
|
|
|
|
#error "need to define filesystem"
|
|
|
|
|
#endif
|
2025-01-13 14:07:48 +11:00
|
|
|
if (!store.load("_main", the_mesh.self_id)) {
|
2025-02-04 01:35:04 +11:00
|
|
|
MESH_DEBUG_PRINTLN("Generating new keypair");
|
2025-03-27 19:34:16 +11:00
|
|
|
the_mesh.self_id = radio_new_identity(); // create new random identity
|
2025-04-06 12:34:09 +10:00
|
|
|
int count = 0;
|
|
|
|
|
while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
|
|
|
|
|
the_mesh.self_id = radio_new_identity(); count++;
|
|
|
|
|
}
|
2025-01-13 14:07:48 +11:00
|
|
|
store.save("_main", the_mesh.self_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.print("Repeater ID: ");
|
|
|
|
|
mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println();
|
|
|
|
|
|
2025-01-15 17:14:08 +11:00
|
|
|
command[0] = 0;
|
2026-03-10 22:35:31 -07:00
|
|
|
#ifdef ETHERNET_ENABLED
|
|
|
|
|
ethernet_command[0] = 0;
|
2026-03-09 14:52:01 -07:00
|
|
|
#endif
|
2025-01-15 17:14:08 +11:00
|
|
|
|
2025-05-03 13:14:03 +10:00
|
|
|
sensors.begin();
|
|
|
|
|
|
2025-01-27 11:13:37 +11:00
|
|
|
the_mesh.begin(fs);
|
2025-01-13 14:07:48 +11:00
|
|
|
|
2025-03-05 12:52:29 +11:00
|
|
|
#ifdef DISPLAY_CLASS
|
2025-04-20 17:40:58 -07:00
|
|
|
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
|
2025-03-05 12:52:29 +11:00
|
|
|
#endif
|
|
|
|
|
|
2026-03-10 22:35:31 -07:00
|
|
|
#ifdef ETHERNET_ENABLED
|
|
|
|
|
ethernet_start_task();
|
2026-03-09 14:52:01 -07:00
|
|
|
#endif
|
|
|
|
|
|
2026-01-26 22:20:36 +13:00
|
|
|
// send out initial zero hop Advertisement to the mesh
|
2026-01-26 22:39:39 +13:00
|
|
|
#if ENABLE_ADVERT_ON_BOOT == 1
|
2026-01-26 22:20:36 +13:00
|
|
|
the_mesh.sendSelfAdvertisement(16000, false);
|
2026-01-26 22:39:39 +13:00
|
|
|
#endif
|
2026-05-25 09:04:47 -04:00
|
|
|
|
2026-05-25 09:50:28 -04:00
|
|
|
board.onBootComplete();
|
2026-09-03 09:14:15 +02:00
|
|
|
#if defined(SIM_PLATFORM) && defined(__EMSCRIPTEN__)
|
|
|
|
|
g_sim_ready = true;
|
|
|
|
|
#endif
|
2025-01-13 14:07:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
2026-03-09 14:52:01 -07:00
|
|
|
// Handle Serial CLI
|
2025-01-15 17:14:08 +11:00
|
|
|
int len = strlen(command);
|
|
|
|
|
while (Serial.available() && len < sizeof(command)-1) {
|
|
|
|
|
char c = Serial.read();
|
2025-03-07 00:16:22 +01:00
|
|
|
if (c != '\n') {
|
2025-01-15 17:14:08 +11:00
|
|
|
command[len++] = c;
|
|
|
|
|
command[len] = 0;
|
2025-11-03 22:53:14 +11:00
|
|
|
Serial.print(c);
|
2025-01-15 17:14:08 +11:00
|
|
|
}
|
2025-11-03 22:53:14 +11:00
|
|
|
if (c == '\r') break;
|
2025-01-15 17:14:08 +11:00
|
|
|
}
|
|
|
|
|
if (len == sizeof(command)-1) { // command buffer full
|
|
|
|
|
command[sizeof(command)-1] = '\r';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (len > 0 && command[len - 1] == '\r') { // received complete line
|
2025-11-03 22:53:14 +11:00
|
|
|
Serial.print('\n');
|
2025-01-15 17:14:08 +11:00
|
|
|
command[len - 1] = 0; // replace newline with C string null terminator
|
2025-01-22 12:11:43 +11:00
|
|
|
char reply[160];
|
2026-03-09 14:52:01 -07:00
|
|
|
reply[0] = 0;
|
2026-03-10 22:35:31 -07:00
|
|
|
#ifdef ETHERNET_ENABLED
|
|
|
|
|
if (!ethernet_handle_command(command, reply)) {
|
|
|
|
|
the_mesh.handleCommand(0, command, reply);
|
|
|
|
|
}
|
|
|
|
|
#else
|
2025-07-08 17:50:06 +10:00
|
|
|
the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
|
2026-03-10 22:35:31 -07:00
|
|
|
#endif
|
2025-01-22 12:11:43 +11:00
|
|
|
if (reply[0]) {
|
|
|
|
|
Serial.print(" -> "); Serial.println(reply);
|
2025-01-15 17:14:08 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
command[0] = 0; // reset command buffer
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 22:35:31 -07:00
|
|
|
#ifdef ETHERNET_ENABLED
|
2026-03-13 10:48:42 -07:00
|
|
|
ethernet_loop_maintain();
|
|
|
|
|
if (ethernet_read_line(ethernet_command, sizeof(ethernet_command))) {
|
|
|
|
|
char reply[160];
|
|
|
|
|
reply[0] = 0;
|
|
|
|
|
if (!ethernet_handle_command(ethernet_command, reply)) {
|
|
|
|
|
the_mesh.handleCommand(0, ethernet_command, reply);
|
2026-03-09 14:52:01 -07:00
|
|
|
}
|
2026-03-13 10:48:42 -07:00
|
|
|
ethernet_send_reply(reply);
|
|
|
|
|
ethernet_command[0] = 0;
|
2026-03-09 14:52:01 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-07-02 21:55:19 -04:00
|
|
|
#if defined(PIN_USER_BTN) && defined(_SEEED_SENSECAP_SOLAR_H_) && !defined(DISPLAY_CLASS)
|
2026-02-27 22:51:19 -05:00
|
|
|
// Hold the user button to power off the SenseCAP Solar repeater.
|
|
|
|
|
int btnState = digitalRead(PIN_USER_BTN);
|
|
|
|
|
if (btnState == LOW) {
|
|
|
|
|
if (userBtnDownAt == 0) {
|
|
|
|
|
userBtnDownAt = millis();
|
|
|
|
|
} else if ((unsigned long)(millis() - userBtnDownAt) >= USER_BTN_HOLD_OFF_MILLIS) {
|
|
|
|
|
Serial.println("Powering off...");
|
|
|
|
|
board.powerOff(); // does not return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
userBtnDownAt = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-01-13 14:07:48 +11:00
|
|
|
the_mesh.loop();
|
2025-05-03 13:14:03 +10:00
|
|
|
sensors.loop();
|
2025-09-08 21:46:19 +10:00
|
|
|
#ifdef DISPLAY_CLASS
|
|
|
|
|
ui_task.loop();
|
|
|
|
|
#endif
|
2025-10-30 16:45:50 +11:00
|
|
|
rtc_clock.tick();
|
2025-12-23 12:48:08 +07:00
|
|
|
|
2026-03-04 03:13:53 +01:00
|
|
|
#ifdef HAS_EXTERNAL_WATCHDOG
|
|
|
|
|
external_watchdog.loop();
|
2026-02-09 11:43:41 +08:00
|
|
|
#endif
|
2026-02-01 14:46:55 +11:00
|
|
|
if (the_mesh.getNodePrefs()->powersaving_enabled && !the_mesh.hasPendingWork()) {
|
2026-02-13 23:05:44 +07:00
|
|
|
#if defined(NRF52_PLATFORM)
|
2026-05-06 13:16:54 +07:00
|
|
|
board.sleep(0); // nrf ignores seconds param, sleeps whenever possible
|
2026-02-13 23:05:44 +07:00
|
|
|
#else
|
|
|
|
|
if (the_mesh.millisHasNowPassed(POWERSAVING_FIRSTSLEEP_SECS * 1000)) { // To check if it is time to sleep
|
2026-05-06 13:16:54 +07:00
|
|
|
board.sleep(30); // Sleep. Wake up after a while or when receiving a LoRa packet
|
2025-12-23 12:48:08 +07:00
|
|
|
}
|
2026-02-13 23:05:44 +07:00
|
|
|
#endif
|
2025-12-23 12:48:08 +07:00
|
|
|
}
|
2025-01-13 14:07:48 +11:00
|
|
|
}
|