M3 step 4: the LoRa Scanner App, Sniffer and Captures

The Sniffer lists packets newest first (time, RSSI, SNR, and for
Meshtastic the sender, receiver and hops), with the clear header and a
hex dump on Enter (Q96); `p` picks an EU868 preset, kept in Settings
(Q95). `c` starts a Capture: pcap with LoRaTap in /captures/lora, its own
Clean-up category, recorded by a small Service so it carries on with the
App closed (Q97, Q100). The Status Bar shows L while listening, bright on
each packet, and CAP while capturing (Q101). StorageService gains raw
appends for binary files. Debug Builds get `lora inject` to test all of
this with no transmitter in range: a Capture made on the device reads
back in TShark field for field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
2026-10-05 20:14:58 +02:00
co-authored by Claude Opus 5.5
parent 594748e99a
commit 2fce79aebd
23 changed files with 723 additions and 13 deletions
+10
View File
@@ -60,6 +60,10 @@ The Gemini App browses Geminispace (docs/milestones/G1.md): Tab and Shift+Tab pi
On a page, `b` bookmarks it, `s` saves it to the SD card to read offline (a non-text file goes to `/gemini/downloads/`), `S` saves it with the pages it links to on the same capsule (up to 30). The start page lists bookmarks and Saved Pages; inside a Saved Page, `r` refreshes it and `d` deletes it. With a card, every page streams through `/gemini/cache/` so a large one arrives whole even with IRC connected; what doesn't fit in memory stays on the card.
## LoRa Scanner
The LoRa Scanner (docs/milestones/M3.md) listens with the Cap's radio and **never transmits**. The Sniffer lists what it hears, newest first: time, RSSI, SNR, and for Meshtastic packets the sender and receiver (their last 4 hex digits) and hops. Enter shows a packet's details: the Meshtastic header (which is never encrypted) and a hex dump. `p` picks one of the 7 Meshtastic presets allowed in EU868 (LongFast by default), `c` starts or stops a Capture: a pcap file with LoRaTap headers in `/captures/lora/`, for Wireshark. A Capture keeps recording with the App closed; otherwise the radio sleeps when the App isn't open. The Status Bar shows `L` while the radio listens (bright for a moment on each packet) and `CAP` while capturing.
## Development aids
`scripts/serial_log.sh [seconds] [command…]` records the serial output, and can send commands to the firmware first. For example, `scripts/serial_log.sh 30 short sleep:12 burst` sets short screen timeouts, waits 12 s, then sends a burst of Toasts.
@@ -87,6 +91,12 @@ On a page, `b` bookmarks it, `s` saves it to the SD card to read offline (a non-
| `log level <0-5>` | ESP-IDF log level |
| `ls [folder]` / `rm <path>` | Lists a folder of the SD card, or deletes a file |
| `install <path>` | Update from SD with that `.ota` file, as Settings → Firmware does |
| `lora probe` | Finds the radio: chip, oscillator, antenna switch, DIO1 interrupt, noise floor |
| `lora status` | Radio settings, who's listening, packet and error counters, noise floor, task stack |
| `lora rx on` / `lora rx off` | Listens and prints each packet on the console |
| `lora preset <name>` / `lora custom <MHz> <BW kHz> <SF> <CR> <sync hex> [preamble]` | Receive settings: a Meshtastic preset, or anything else (`lora custom 868.1 125 7 5 34 8` for LoRaWAN) |
| `lora capture start` / `lora capture stop` | A LoRa Capture, as `c` in the App |
| `lora inject <hex> [rssi] [snr]` | Debug Builds: a packet into the Scanner as if received (nothing is sent) |
| `crash` | The last crash: which firmware, why, task, PC and backtrace (from the core dump in flash) |
| `coredump erase` | Forgets the core dump |
| `crash abort` / `crash wdt` | Debug Builds: crash on purpose, or hang the main loop until the watchdog fires |
+4 -1
View File
@@ -1,6 +1,6 @@
# M3 — Radio bring-up: the LoRa Scanner
**Status:** in progress (branch `m3`): steps 1 and 2 done.
**Status:** in progress (branch `m3`): steps 1 to 4 done.
**Goal:** the LoRa radio on the Cap works, receive only: a Radio Service owns it and shares the SPI bus with the SD card safely, and a LoRa Scanner App shows what's on the air, either packets (Sniffer) or energy across the band (Sweep). Nothing in M3 can transmit. The mesh comes on top of this in M4 (receive) and M5 (transmit).
@@ -16,6 +16,9 @@
- **DIO2 doesn't change reception** (within ±2 dB over three runs, P0 high). It likely selects TX versus RX in the FM8625H; it stays the RF switch, as in Meshtastic.
- **The noise floor at the desk is high** (-87 to -94 dBm, varying run to run), about 25 dB above thermal noise for 250 kHz. Something nearby is loud, possibly the Cardputer itself or the PC; Sweep (step 5) should show where it sits.
- **Step 2:** presets, frequencies and the channel hash are checked against Meshtastic's source (`MeshRadio.h`, `RadioInterface.cpp`): LongFast and the default key give hash 8, MediumFast 31, as Meshtastic shows. Captures were checked with TShark 4.2.5: every LoRaTap field reads back. Wireshark ignores the spec's quarter-dB packet RSSI below 0 dB SNR, so packet RSSI is plain dBm.
- **Step 3:** the DIO1 interrupt works (a 100 ms receive timeout wakes the task after 105 ms). While listening: four 1.7 MB uploads and Gemini pages to the card, no radio or card errors (the card refuses a write about once in five uploads with the radio asleep too; `put` now catches it). The ring takes 9.8 KB while listening; the radio task's stack peaks at 2.0 KB.
- **No packets yet, and a loud desk.** Twenty minutes on LongFast and on LoRaWAN's three uplink frequencies (SF7, SF9, SF12): no packet and no header, valid or not. The noise floor reads -83 to -94 dBm, against -112 dBm with the antenna switched off: 20 to 30 dB lost to something nearby, not the screen and not the GNSS receiver. Preamble detections are false alarms at this noise level (more on an empty frequency, 869.0 MHz, than on LongFast).
- **Step 4:** a Capture made on the device reads back in TShark field for field (time, frequency, SF, RSSI, SNR, payload). A Capture keeps the radio listening with the App closed; stopping it puts the radio back to sleep.
- **Cost:** RadioLib 7.8.1 and the probe add 23.6 KB of flash and 656 bytes of static RAM to the release firmware (1,679,843 bytes of 3,342,336).
## Decisions (design round 2026-10-05)
+12
View File
@@ -2,6 +2,8 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
namespace roro::lora {
@@ -16,6 +18,16 @@ static uint8_t dbmByte(float dbm) {
return static_cast<uint8_t>(std::clamp(v, 0L, 255L));
}
std::string capturePath(int64_t utcSeconds) {
time_t t = static_cast<time_t>(utcSeconds);
struct tm u;
gmtime_r(&t, &u);
char buf[48];
std::snprintf(buf, sizeof buf, "/captures/lora/%04d%02d%02d-%02d%02d%02d.pcap", u.tm_year + 1900, u.tm_mon + 1,
u.tm_mday, u.tm_hour, u.tm_min, u.tm_sec);
return buf;
}
void appendPcapHeader(std::vector<uint8_t>& out) {
le32(out, 0xA1B2C3D4);
le16(out, 2);
+4
View File
@@ -2,6 +2,7 @@
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
namespace roro::lora {
@@ -23,6 +24,9 @@ void appendPcapHeader(std::vector<uint8_t>& out);
void appendRecord(std::vector<uint8_t>& out, uint32_t seconds, uint32_t micros, const RxInfo& rx, const uint8_t* data,
size_t len);
// "/captures/lora/YYYYMMDD-HHMMSS.pcap" in UTC, dated like Tracks so Storage Clean-up can age it.
std::string capturePath(int64_t utcSeconds);
constexpr size_t kLoraTapSize = 15;
constexpr size_t kRecordOverhead = 16 + kLoraTapSize;
+83
View File
@@ -0,0 +1,83 @@
#include "packet_view.h"
#include <cmath>
#include <cstdio>
#include "meshtastic_header.h"
#include "meshtastic_presets.h"
namespace roro::lora {
using namespace meshtastic;
std::string row(const PacketSummary& p, const std::string& time) {
char s[64];
int n = std::snprintf(s, sizeof s, "%s %ld %.1f ", time.c_str(), std::lround(p.rssi), p.snr);
std::string out(s, n);
PacketHeader h;
if (!p.crcOk) return out + "bad CRC, " + std::to_string(p.len) + " B";
if (!p.meshtastic || !parseHeader(p.data, p.len, h)) return out + std::to_string(p.len) + " B";
auto shortId = [](uint32_t node) {
if (node == kBroadcast) return std::string("all");
char id[8];
std::snprintf(id, sizeof id, "%04x", static_cast<unsigned>(node & 0xFFFF));
return std::string(id);
};
out += shortId(h.from) + ">" + shortId(h.to);
if (h.hopsAway() >= 0) out += " " + std::to_string(h.hopsAway()) + "/" + std::to_string(h.hopStart());
return out;
}
std::vector<std::string> hexDump(const uint8_t* data, size_t len) {
std::vector<std::string> lines;
for (size_t at = 0; at < len; at += 8) {
char s[48];
int n = std::snprintf(s, sizeof s, "%04x", static_cast<unsigned>(at));
std::string text;
for (size_t i = 0; i < 8; ++i) {
if (at + i < len) {
n += std::snprintf(s + n, sizeof s - n, " %02x", data[at + i]);
uint8_t b = data[at + i];
text += b >= 0x20 && b < 0x7F ? static_cast<char>(b) : '.';
} else {
n += std::snprintf(s + n, sizeof s - n, " ");
}
}
lines.push_back(std::string(s, n) + " " + text);
}
return lines;
}
std::vector<std::string> headerLines(const uint8_t* data, size_t len) {
PacketHeader h;
if (!parseHeader(data, len, h)) return {};
char s[64];
std::vector<std::string> lines;
lines.push_back("From " + nodeId(h.from) + " to " + nodeId(h.to));
std::snprintf(s, sizeof s, "Packet %08x%s%s", static_cast<unsigned>(h.id), h.wantAck() ? ", wants an ack" : "",
h.viaMqtt() ? ", via MQTT" : "");
lines.push_back(s);
if (h.hopsAway() >= 0)
std::snprintf(s, sizeof s, "Hops %d of %u, limit %u left", h.hopsAway(), h.hopStart(), h.hopLimit());
else
std::snprintf(s, sizeof s, "Hop limit %u left (old firmware)", h.hopLimit());
lines.push_back(s);
// Which preset's default Channel (named after it, with the public key) has this hash.
std::string channel;
for (size_t i = 0; i < kEu868PresetCount && channel.empty(); ++i)
if (channelHash(kEu868Presets[i].name, kDefaultKey, sizeof kDefaultKey) == h.channelHash)
channel = std::string(" (") + kEu868Presets[i].name + ", default key)";
std::snprintf(s, sizeof s, "Channel 0x%02x%s", h.channelHash, channel.c_str());
lines.push_back(s);
if (h.relayNode) {
std::snprintf(s, sizeof s, "Relayed by ..%02x", h.relayNode);
lines.push_back(s);
}
if (h.nextHop) {
std::snprintf(s, sizeof s, "Next hop ..%02x", h.nextHop);
lines.push_back(s);
}
return lines;
}
} // namespace roro::lora
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
namespace roro::lora {
// What the LoRa Scanner shows of one packet (M3, Q96).
struct PacketSummary {
const uint8_t* data;
size_t len;
float rssi, snr;
bool crcOk;
bool meshtastic; // received on Meshtastic settings (sync word 0x2B): read its clear header
};
// One list row, at most about 36 characters: "21:45:07 -97 6.2 5678>all 1/3". Nodes by their default
// short name (the last 4 hex digits); headerLines() has the full numbers.
std::string row(const PacketSummary& p, const std::string& time);
// Eight bytes a line, with the printable ones: "0000 ff ff ff ff 78 56 34 12 ....xV4.".
std::vector<std::string> hexDump(const uint8_t* data, size_t len);
// The Meshtastic header, one field a line; empty when the packet is too short to have one.
std::vector<std::string> headerLines(const uint8_t* data, size_t len);
} // namespace roro::lora
+1
View File
@@ -31,6 +31,7 @@ const Definition kDefinitions[] = {
{"wifi_on", Kind::Bool, 1, nullptr, 0, 1},
{"gnss_on", Kind::Bool, 1, nullptr, 0, 1},
{"coord_dms", Kind::Bool, 0, nullptr, 0, 1},
{"lora_preset", Kind::Int, 0, nullptr, 0, 6}, // LongFast first
};
static_assert(sizeof(kDefinitions) / sizeof(kDefinitions[0]) == static_cast<size_t>(Setting::Count),
"every Setting needs a definition");
+1
View File
@@ -23,6 +23,7 @@ enum class Setting : uint8_t {
WifiEnabled, // bool: the Wi-Fi Service stays Connected when a Saved Network is in range
GnssEnabled, // bool: the GNSS Service reads the receiver (M2, Q58)
CoordinatesDms, // bool: show degrees, minutes and seconds instead of decimal degrees (Q64)
LoraPreset, // int: the LoRa Scanner's Meshtastic preset, an index into the EU868 list (M3, Q95)
Count
};
+1
View File
@@ -20,6 +20,7 @@ inline const CleanupCategory kCleanupCategories[] = {
{"IRC logs", "/irc"},
{"Wi-Fi scan logs", "/wifi/scans"},
{"Wi-Fi captures", "/captures/wifi"},
{"LoRa captures", "/captures/lora"},
{"GNSS tracks", "/gnss/tracks"},
};
+231
View File
@@ -0,0 +1,231 @@
#include "lora_scanner_app.h"
#include <Arduino.h>
#include <algorithm>
#include <cstdio>
#include <ctime>
#include "meshtastic_presets.h"
#include "packet_view.h"
#include "ui/canvas.h"
#include "ui/fonts.h"
#include "ui/widgets.h"
namespace roro {
namespace {
constexpr uint32_t kMessageMs = 4000;
}
void LoraScannerApp::onEnter() {
int i = std::clamp<int>(settings_.getInt(Setting::LoraPreset), 0, meshtastic::kEu868PresetCount - 1);
const meshtastic::Preset& p = meshtastic::kEu868Presets[i];
if (std::string(radio_.config().name) != p.name) radio_.setPreset(p);
radio_.listen(RadioService::App, true);
view_ = View::Packets;
selectedSeq_ = 0;
followNewest_ = true;
shownSeq_ = 0;
requestRedraw();
}
void LoraScannerApp::onExit() { radio_.listen(RadioService::App, false); }
bool LoraScannerApp::meshtasticSettings() const { return radio_.config().syncWord == meshtastic::kSyncWord; }
void LoraScannerApp::say(const std::string& text) {
message_ = text;
messageMs_ = millis();
requestRedraw();
}
bool LoraScannerApp::onKey(const KeyEvent& e) {
if (view_ == View::Details) {
int last = std::max<int>(0, static_cast<int>(details_.size()) - 8);
if (e.key == Key::Up) detailsTop_ = std::max(0, detailsTop_ - 1);
else if (e.key == Key::Down) detailsTop_ = std::min(last, detailsTop_ + 1);
else if (e.key == Key::Back || e.key == Key::Select) view_ = View::Packets;
else return true;
requestRedraw();
return true;
}
if (view_ == View::Presets) {
if (e.key == Key::Up) presets_.up();
else if (e.key == Key::Down) presets_.down();
else if (e.key == Key::Select) {
int i = presets_.selected();
settings_.setInt(Setting::LoraPreset, i);
radio_.setPreset(meshtastic::kEu868Presets[i]);
say(std::string("Listening on ") + meshtastic::kEu868Presets[i].name);
view_ = View::Packets;
} else if (e.key == Key::Back) view_ = View::Packets;
else return true;
requestRedraw();
return true;
}
// The packet list.
if (e.key == Key::Up || e.key == Key::Down) {
e.key == Key::Up ? packets_.up() : packets_.down();
selectedSeq_ = packets_.selected() >= 0 ? radio_.received() - packets_.selected() : 0;
followNewest_ = packets_.selected() <= 0;
requestRedraw();
return true;
}
if (e.key == Key::Select) {
openDetails();
return true;
}
if (e.key == Key::Char && (e.ch == 'p' || e.ch == 'P')) {
presets_.setCount(meshtastic::kEu868PresetCount);
presets_.select(settings_.getInt(Setting::LoraPreset));
view_ = View::Presets;
requestRedraw();
return true;
}
if (e.key == Key::Char && (e.ch == 'c' || e.ch == 'C')) {
if (capture_.capturing()) capture_.stop();
else {
std::string why = capture_.start(millis());
if (!why.empty()) say(why);
}
requestRedraw();
return true;
}
return false; // Back leaves the App
}
void LoraScannerApp::openDetails() {
RadioPacket p;
if (!selectedSeq_ || !radio_.packet(selectedSeq_, p)) return say("That packet has left the list");
details_.clear();
char line[64];
std::snprintf(line, sizeof line, "%s, %u bytes%s", timeOf(p).c_str(), p.len, p.crcOk ? "" : ", bad CRC");
details_.push_back(line);
std::snprintf(line, sizeof line, "%.0f dBm, SNR %.1f dB, noise %.0f", p.rx.rssi, p.rx.snr, p.rx.noiseFloor);
details_.push_back(line);
std::snprintf(line, sizeof line, "%.4f MHz, off by %.0f Hz", p.rx.frequencyHz / 1e6, p.frequencyError);
details_.push_back(line);
if (meshtasticSettings())
for (auto& l : lora::headerLines(p.data, p.len)) details_.push_back(l);
for (auto& l : lora::hexDump(p.data, p.len)) details_.push_back(l);
detailsTop_ = 0;
view_ = View::Details;
requestRedraw();
}
std::string LoraScannerApp::timeOf(const RadioPacket& p) const {
int64_t now = clock_.utcNow();
if (now < 0) { // no Clock yet: how long ago
char ago[16];
std::snprintf(ago, sizeof ago, "-%lus", (unsigned long)((millis() - p.ms) / 1000));
return ago;
}
time_t t = static_cast<time_t>(now - static_cast<int64_t>((millis() - p.ms) / 1000));
struct tm local;
localtime_r(&t, &local);
char buf[12];
std::snprintf(buf, sizeof buf, "%02d:%02d:%02d", local.tm_hour, local.tm_min, local.tm_sec);
return buf;
}
void LoraScannerApp::update(uint32_t nowMs) {
if (radio_.received() != shownSeq_ || nowMs - lastDrawMs_ >= 1000 ||
(!message_.empty() && nowMs - messageMs_ >= kMessageMs))
requestRedraw();
}
void LoraScannerApp::draw(Canvas& c) {
lastDrawMs_ = millis();
c.setTextDatum(top_left);
if (!radio_.present()) {
c.setFont(&fonts::bold);
c.setTextColor(theme::kWarning);
c.drawString("No LoRa radio found", 4, theme::kContent.y + 4);
c.setFont(&fonts::body);
c.setTextColor(theme::kMuted);
c.drawString("Is the Cap LoRa-1262 attached?", 4, theme::kContent.y + 22);
return;
}
switch (view_) {
case View::Packets: drawPackets(c); break;
case View::Details: drawDetails(c); break;
case View::Presets: drawPresets(c); break;
}
}
void LoraScannerApp::drawPackets(Canvas& c) {
const auto& area = theme::kContent;
RadioService::Config cfg = radio_.config();
uint32_t newest = radio_.received();
shownSeq_ = newest;
// What it's listening to, and the noise floor.
char head[64];
std::snprintf(head, sizeof head, "%s %.3f MHz, noise %.0f", cfg.name, cfg.frequencyHz / 1e6, radio_.noiseFloor());
c.setFont(&fonts::small);
c.setTextColor(radio_.listening() ? theme::kMuted : theme::kWarning);
c.drawString(radio_.listening() ? head : "Starting the radio...", 4, area.y + 2);
if (capture_.capturing()) {
c.setTextDatum(top_right);
c.setTextColor(theme::kWarning);
c.drawString(("CAP " + std::to_string(capture_.packets())).c_str(), area.w - 3, area.y + 2);
c.setTextDatum(top_left);
}
// The list, newest first; the selection stays on its packet as new ones arrive.
int count = static_cast<int>(radio_.available());
packets_.setCount(count);
if (!followNewest_ && selectedSeq_ && newest >= selectedSeq_ && newest - selectedSeq_ < static_cast<uint32_t>(count))
packets_.select(static_cast<int>(newest - selectedSeq_));
else if (count > 0) {
followNewest_ = true;
packets_.select(0);
selectedSeq_ = newest;
}
theme::Rect list{area.x, area.y + 12, area.w, kListRows * theme::kLineHeight};
if (count == 0) {
c.setFont(&fonts::body);
c.setTextColor(theme::kMuted);
c.drawString("No packets yet.", 4, list.y + 4);
c.drawString(meshtasticSettings() ? "Meshtastic nodes in range show here." : "Custom settings.", 4,
list.y + 4 + theme::kLineHeight);
} else {
bool mesh = meshtasticSettings();
widgets::list(c, packets_, list, [&](int i) {
RadioPacket p;
if (!radio_.packet(newest - i, p)) return std::string("(gone)");
return lora::row({p.data, p.len, p.rx.rssi, p.rx.snr, p.crcOk, mesh}, timeOf(p));
});
}
// Footer: a message for a few seconds, or the keys.
c.setFont(&fonts::small);
bool showMessage = !message_.empty() && millis() - messageMs_ < kMessageMs;
c.setTextColor(showMessage ? theme::kWarning : theme::kMuted);
std::string keys = std::string("Enter: details p: preset c: ") + (capture_.capturing() ? "stop capture" : "capture");
c.drawString(showMessage ? message_.c_str() : keys.c_str(), 4, area.y + area.h - 9);
}
void LoraScannerApp::drawDetails(Canvas& c) {
const auto& area = theme::kContent;
widgets::textLines(c, details_, detailsTop_, {area.x + 2, area.y + 2, area.w - 2, area.h - 2});
}
void LoraScannerApp::drawPresets(Canvas& c) {
const auto& area = theme::kContent;
c.setFont(&fonts::small);
c.setTextColor(theme::kMuted);
c.drawString("Meshtastic presets allowed in EU868", 4, area.y + 2);
widgets::list(
c, presets_, {area.x, area.y + 12, area.w, (kListRows + 1) * theme::kLineHeight},
[](int i) { return std::string(meshtastic::kEu868Presets[i].name); },
[](int i) {
char v[24];
const auto& p = meshtastic::kEu868Presets[i];
std::snprintf(v, sizeof v, "SF%u %.0fk %.3f", p.sf, p.bwKHz, meshtastic::eu868FrequencyHz(p) / 1e6);
return std::string(v);
});
}
} // namespace roro
+58
View File
@@ -0,0 +1,58 @@
#pragma once
#include <string>
#include <vector>
#include "app.h"
#include "list_model.h"
#include "services/clock_service.h"
#include "services/lora_capture_service.h"
#include "services/radio_service.h"
#include "settings.h"
#include "ui/theme.h"
namespace roro {
// The LoRa Scanner (docs/milestones/M3.md). Sniffer: the packets the radio hears, newest first,
// with the Meshtastic header read (Q96) and a hex dump on Enter; p picks the preset (Q95), c starts
// or stops a Capture (Q97). The radio listens while the App is open (Q100).
class LoraScannerApp : public App {
public:
LoraScannerApp(RadioService& radio, LoraCaptureService& capture, Settings& settings, ClockService& clock)
: radio_(radio), capture_(capture), settings_(settings), clock_(clock) {}
void onEnter() override;
void onExit() override;
bool onKey(const KeyEvent& e) override;
void update(uint32_t nowMs) override;
void draw(Canvas& c) override;
private:
enum class View { Packets, Details, Presets };
static constexpr int kListRows = 7;
void drawPackets(Canvas& c);
void drawDetails(Canvas& c);
void drawPresets(Canvas& c);
void openDetails();
std::string timeOf(const RadioPacket& p) const;
bool meshtasticSettings() const;
void say(const std::string& text); // a line in the footer for a few seconds
RadioService& radio_;
LoraCaptureService& capture_;
Settings& settings_;
ClockService& clock_;
View view_ = View::Packets;
ListModel packets_{kListRows};
ListModel presets_{kListRows + 1};
uint32_t shownSeq_ = 0; // the newest packet at the last redraw
uint32_t selectedSeq_ = 0; // what the selection points at, kept as packets arrive
bool followNewest_ = true; // until the user moves the selection off the top
std::vector<std::string> details_;
int detailsTop_ = 0;
uint32_t lastDrawMs_ = 0;
std::string message_;
uint32_t messageMs_ = 0;
};
} // namespace roro
+30
View File
@@ -12,6 +12,7 @@
#include "apps/gnss_app.h"
#include "apps/irc_app.h"
#include "apps/launcher_app.h"
#include "apps/lora_scanner_app.h"
#include "apps/settings_app.h"
#include "apps/wifi_tools_app.h"
#include "apps/setup_app.h"
@@ -31,6 +32,7 @@
#include "services/gemini_service.h"
#include "services/gnss_service.h"
#include "services/power_service.h"
#include "services/lora_capture_service.h"
#include "services/radio_service.h"
#include "services/storage_service.h"
#include "services/update_service.h"
@@ -58,6 +60,7 @@ static PowerService* power;
static ClockService* clockService;
static GnssService* gnssService;
static RadioService* radioService;
static LoraCaptureService* loraCapture;
static GeminiService* geminiService;
static SavedNetworks* savedNetworks;
static WifiService* wifi;
@@ -106,6 +109,11 @@ static StatusInfo currentStatus() {
s.gnssSatellites = g.satellitesUsed;
s.tracking = gnssService->tracking();
}
if (radioService && radioService->listening()) {
uint32_t last = radioService->lastPacketMs();
s.radio = last && millis() - last < 400 ? StatusInfo::Radio::Packet : StatusInfo::Radio::Listening;
}
s.capturing = loraCapture && loraCapture->capturing();
using WifiState = WifiController::State;
switch (wifi->state()) {
case WifiState::Connected: {
@@ -165,6 +173,8 @@ void setup() {
services.add(*battery);
services.add(*gnssService);
services.add(*radioService);
loraCapture = new LoraCaptureService(*radioService, *storageService, *clockService, bus);
services.add(*loraCapture);
services.add(*storageService);
services.add(*wifi);
services.add(*irc);
@@ -180,6 +190,7 @@ void setup() {
apps->registerApp({"wifi-tools", "Wi-Fi Tools", false, new WifiToolsApp(*wifi, *storageService, *clockService)});
apps->registerApp({"gnss", "GNSS", false, new GnssApp(*gnssService, settings)});
apps->registerApp({"gemini", "Gemini", false, new GeminiApp(*geminiService)});
apps->registerApp({"lora", "LoRa Scanner", false, new LoraScannerApp(*radioService, *loraCapture, settings, *clockService)});
apps->registerApp({"settings", "Settings", false,
new SettingsApp({settings, bus, *apps, *battery, *storageService, *clockService, *wifi, *savedNetworks, *update})});
apps->registerApp({"demo", "Widget demo", true, new DemoApp(bus)});
@@ -352,6 +363,7 @@ static const char* const kHelp =
"boot other restart into the other app slot (manual Rollback)\n"
"log level <0-5> ESP-IDF log level (0 none ... 5 verbose)\n"
"lora probe | lora status | lora rx on|off | lora preset <name> the LoRa radio, receive only\n"
"lora capture start|stop a LoRa Capture to /captures/lora (pcap, LoRaTap)\n"
"lora custom <MHz> <BW kHz> <SF> <CR 5-8> <sync hex> [preamble] e.g. 868.1 125 7 5 34 8 (LoRaWAN)\n"
"gnss status | gnss restart | gnss track start|stop | gnss nmea on|off | gnss send <sentence without $ and checksum>\n"
"crash the last crash: firmware, reason, task, backtrace\n"
@@ -364,6 +376,7 @@ static const char* const kHelp =
"sd list | cat <path> | log <text> | burst | sound on|off | short | normal\n"
#ifdef RORO_DEBUG
"crash abort|wdt crash on purpose (to test crash reports and Safe Mode)\n"
"lora inject <hex> [rssi] [snr] a packet into the LoRa Scanner as if received (nothing is sent)\n"
"coredump get (Debug Console only) send the raw core dump: use scripts/rdbg.py coredump\n"
"reset (Debug Console only) restart at once, even if the main loop is stuck\n"
"get <path> | put <path> <size> <sha256> | screenshot (Debug Console only) binary, see rdbg.py\n"
@@ -419,6 +432,23 @@ static void runCommand(String line) {
if (line == "lora probe" && radioService) radioService->probe(console);
if (line == "lora status" && radioService) radioService->printStatus(console);
if ((line == "lora rx on" || line == "lora rx off") && radioService) radioService->setEcho(line.endsWith("on"));
if (line == "lora capture start" && loraCapture) {
std::string why = loraCapture->start(millis());
console.printf("lora capture: %s\n", why.empty() ? loraCapture->path().c_str() : why.c_str());
}
if (line == "lora capture stop" && loraCapture) loraCapture->stop();
#ifdef RORO_DEBUG
if (line.startsWith("lora inject ") && radioService) { // <hex> [rssi] [snr]: as if received
String hex = line.substring(12);
int space = hex.indexOf(' ');
float rssi = -100, snr = 5;
if (space > 0) sscanf(hex.c_str() + space + 1, "%f %f", &rssi, &snr), hex = hex.substring(0, space);
uint8_t data[255];
size_t n = 0;
for (size_t i = 0; i + 1 < hex.length() && n < sizeof data; i += 2) data[n++] = strtoul(hex.substring(i, i + 2).c_str(), nullptr, 16);
radioService->inject(data, n, rssi, snr);
}
#endif
if (line.startsWith("lora custom ") && radioService) {
double mhz = 0, bw = 0;
unsigned sf = 0, cr = 0, sync = 0, preamble = 8;
+62
View File
@@ -0,0 +1,62 @@
#include "services/lora_capture_service.h"
#include <vector>
#include "loratap.h"
#include "platform/console.h"
namespace roro {
std::string LoraCaptureService::start(uint32_t nowMs) {
if (capturing()) return "";
if (!radio_.present()) return "No LoRa radio";
StorageState card = storage_.state();
if (!card.present) return "No SD card";
if (!card.capturesAllowed) return "The SD card is full";
int64_t now = clock_.utcNow();
if (now < 0) return "Waiting for the time (GNSS or Wi-Fi)";
path_ = lora::capturePath(now);
std::vector<uint8_t> header;
lora::appendPcapHeader(header);
storage_.appendBytes(path_, std::string(header.begin(), header.end()), true);
startUtc_ = now;
startMs_ = nowMs;
written_ = radio_.received(); // from now on
packets_ = 0;
radio_.listen(RadioService::Capture, true);
console.printf("lora: Capture %s started\n", path_.c_str());
notify("LoRa Capture started");
return "";
}
void LoraCaptureService::stop() {
if (!capturing()) return;
radio_.listen(RadioService::Capture, false);
console.printf("lora: Capture %s stopped, %lu packets\n", path_.c_str(), (unsigned long)packets_);
notify("LoRa Capture stopped: " + std::to_string(packets_) + " packets");
path_.clear();
}
void LoraCaptureService::tick(uint32_t nowMs) {
(void)nowMs;
if (!capturing()) return;
uint32_t newest = radio_.received();
for (uint32_t seq = written_ + 1; seq <= newest; ++seq) {
RadioPacket p;
if (!radio_.packet(seq, p)) continue; // already left the ring: too many at once
uint32_t sinceStart = p.ms - startMs_;
std::vector<uint8_t> record;
record.reserve(lora::kRecordOverhead + p.len);
lora::appendRecord(record, static_cast<uint32_t>(startUtc_ + sinceStart / 1000), (sinceStart % 1000) * 1000,
p.rx, p.data, p.len);
storage_.appendBytes(path_, std::string(record.begin(), record.end()), true);
++packets_;
}
written_ = newest;
}
void LoraCaptureService::notify(const std::string& text) {
bus_.publish(Event::withText(EventType::Notification, text.c_str(), static_cast<int32_t>(NotificationLevel::Info)));
}
} // namespace roro
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include <string>
#include "event_bus.h"
#include "service.h"
#include "services/clock_service.h"
#include "services/radio_service.h"
#include "services/storage_service.h"
namespace roro {
// A LoRa Sniffer Capture (CONTEXT.md: Capture; M3, Q97): every packet the radio receives, as pcap
// with LoRaTap headers, in /captures/lora/. Started and stopped by hand; keeps the radio listening
// and keeps recording whatever App is open (Q100).
class LoraCaptureService : public Service {
public:
LoraCaptureService(RadioService& radio, StorageService& storage, ClockService& clock, EventBus& bus)
: radio_(radio), storage_(storage), clock_(clock), bus_(bus) {}
const char* name() const override { return "lora-capture"; }
uint32_t tickIntervalMs() const override { return 200; }
void tick(uint32_t nowMs) override;
std::string start(uint32_t nowMs); // why it can't, or ""
void stop();
bool capturing() const { return !path_.empty(); }
const std::string& path() const { return path_; }
uint32_t packets() const { return packets_; }
private:
void notify(const std::string& text);
RadioService& radio_;
StorageService& storage_;
ClockService& clock_;
EventBus& bus_;
std::string path_;
uint32_t written_ = 0; // the radio's seq last written
uint32_t packets_ = 0;
int64_t startUtc_ = 0; // the Clock and millis() when it started: packet times follow from both
uint32_t startMs_ = 0;
};
} // namespace roro
+26
View File
@@ -108,6 +108,7 @@ void RadioService::run() {
void RadioService::startListening() {
if (!ring_) {
ring_.reset(new (std::nothrow) RadioPacket[kRing]);
ringFirst_ = seq_ + 1;
if (!ring_) {
console.println("radio: not enough memory to listen");
clients_ = 0;
@@ -169,14 +170,33 @@ void RadioService::readPacket() {
if (receive() != RADIOLIB_ERR_NONE) ++radioErrors_;
++restarts_;
store(p);
}
void RadioService::store(RadioPacket& p) {
xSemaphoreTake(lock_, portMAX_DELAY);
p.seq = seq_ + 1;
if (ring_) ring_[p.seq % kRing] = p;
seq_ = p.seq;
xSemaphoreGive(lock_);
++packets_;
lastPacketMs_ = p.ms;
}
#ifdef RORO_DEBUG
void RadioService::inject(const uint8_t* data, size_t len, float rssi, float snr) {
if (!listening_) return (void)console.println("lora inject: not listening");
RadioPacket p;
Config c = config();
p.ms = millis();
p.len = static_cast<uint8_t>(std::min(len, sizeof p.data));
std::copy(data, data + p.len, p.data);
p.rx = {c.frequencyHz, c.bandwidthKHz, c.spreadingFactor, rssi, snr, noise_, c.syncWord};
store(p);
console.printf("lora inject: #%lu, %u B\n", (unsigned long)p.seq, p.len);
}
#endif
// Continuous receive. Only RX done raises DIO1; preambles and headers are only recorded in the
// IRQ status, which sampleNoise() reads.
int16_t RadioService::receive() {
@@ -206,6 +226,12 @@ bool RadioService::packet(uint32_t seq, RadioPacket& out) const {
return ok;
}
uint32_t RadioService::available() const {
uint32_t newest = seq_, first = ringFirst_;
if (!listening_ || newest < first) return 0;
return std::min<uint32_t>(newest - first + 1, kRing);
}
void RadioService::listen(Client client, bool on) {
if (on) clients_ |= client;
else clients_ &= ~client;
+10
View File
@@ -68,7 +68,9 @@ class RadioService : public Service {
void setConfig(const Config& config); // custom settings (Q95), named "Custom"
uint32_t received() const { return seq_; } // seq of the newest packet
uint32_t lastPacketMs() const { return lastPacketMs_; } // millis(), 0 before the first
bool packet(uint32_t seq, RadioPacket& out) const; // false once it has left the ring
uint32_t available() const; // packets in the ring: seqs received()-available()+1 to received()
float noiseFloor() const { return noise_; }
Counters counters() const {
return {packets_, crcErrors_, radioErrors_, restarts_, preambles_, headers_, headerErrors_};
@@ -78,6 +80,11 @@ class RadioService : public Service {
void printStatus(Print& out) const;
void setEcho(bool echo);
void probe(Print& out); // `lora probe`: runs on the radio task
#ifdef RORO_DEBUG
// `lora inject`: a packet into the ring as if received, to test the App and Captures with no
// transmitter in range. Nothing goes on air.
void inject(const uint8_t* data, size_t len, float rssi, float snr);
#endif
private:
enum Notify : uint32_t { kIrq = 1, kRequest = 2 };
@@ -90,6 +97,7 @@ class RadioService : public Service {
void stopListening();
int16_t receive();
void readPacket();
void store(RadioPacket& p);
void sampleNoise();
void runProbe();
void wake(Notify why);
@@ -107,6 +115,8 @@ class RadioService : public Service {
std::atomic<bool> listening_{false}, configChanged_{false}, probeWanted_{false}, echo_{false};
std::atomic<uint32_t> seq_{0}, packets_{0}, crcErrors_{0}, radioErrors_{0}, restarts_{0};
std::atomic<uint32_t> preambles_{0}, headers_{0}, headerErrors_{0};
std::atomic<uint32_t> lastPacketMs_{0};
std::atomic<uint32_t> ringFirst_{1}; // the first seq the current ring can hold
std::atomic<float> noise_{0};
uint32_t lastNoiseMs_ = 0, echoed_ = 0;
Print* probeOut_ = nullptr;
+18 -10
View File
@@ -34,12 +34,20 @@ StorageState StorageService::state() const {
}
void StorageService::appendLine(const std::string& path, const std::string& line, bool capture) {
append(path, line, capture, true);
}
void StorageService::appendBytes(const std::string& path, const std::string& bytes, bool capture) {
append(path, bytes, capture, false);
}
void StorageService::append(const std::string& path, const std::string& data, bool capture, bool newline) {
lock();
size_t bytes = path.size() + line.size();
size_t bytes = path.size() + data.size();
bool allowed = capture ? monitor_.state().capturesAllowed : monitor_.state().logsAllowed;
bool accept = allowed && pendingBytes_ + bytes <= kMaxPendingBytes;
if (accept) {
pending_.emplace_back(path, line);
pending_.push_back({path, data, newline});
pendingBytes_ += bytes;
} else {
dropped_++;
@@ -199,25 +207,25 @@ void StorageService::poll() {
void StorageService::writePending() {
lock();
std::deque<std::pair<std::string, std::string>> batch;
std::deque<Pending> batch;
batch.swap(pending_);
pendingBytes_ = 0;
unlock();
if (batch.empty() || !mounted_) return;
// Keep each file's lines in order while opening each file once.
std::stable_sort(batch.begin(), batch.end(), [](const auto& a, const auto& b) { return a.first < b.first; });
std::stable_sort(batch.begin(), batch.end(), [](const auto& a, const auto& b) { return a.path < b.path; });
File file;
std::string openPath;
for (auto& [path, line] : batch) {
if (path != openPath) {
for (auto& p : batch) {
if (p.path != openPath) {
if (file) file.close();
file = SD.open(path.c_str(), FILE_APPEND, true); // true: create missing folders
openPath = path;
file = SD.open(p.path.c_str(), FILE_APPEND, true); // true: create missing folders
openPath = p.path;
}
if (file) {
file.write(reinterpret_cast<const uint8_t*>(line.data()), line.size());
file.write('\n');
file.write(reinterpret_cast<const uint8_t*>(p.data.data()), p.data.size());
if (p.newline) file.write('\n');
}
}
if (file) file.close();
+8 -1
View File
@@ -32,6 +32,8 @@ class StorageService : public Service {
// paused (over 90 % full, or no card) or if too much is already waiting. A Capture (or a Track,
// something the user started) keeps going past 90 %, until the card is full.
void appendLine(const std::string& path, const std::string& line, bool capture = false);
// The same, for binary files (a pcap Capture): the bytes as they are, no newline.
void appendBytes(const std::string& path, const std::string& bytes, bool capture = false);
uint32_t droppedLines() const { return dropped_; }
// Storage Clean-up: list every file of every category, then delete a selection.
@@ -73,7 +75,12 @@ class StorageService : public Service {
mutable SemaphoreHandle_t lock_ = nullptr;
// Shared with other tasks, guarded by lock_.
std::deque<std::pair<std::string, std::string>> pending_;
struct Pending {
std::string path, data;
bool newline;
};
std::deque<Pending> pending_;
void append(const std::string& path, const std::string& data, bool capture, bool newline);
size_t pendingBytes_ = 0;
uint32_t dropped_ = 0;
bool listingRequested_ = false;
+6
View File
@@ -49,6 +49,12 @@ void statusBar(Canvas& c, const StatusInfo& info) {
case StatusInfo::Wifi::None: break;
}
if (info.tracking) right("REC", kWarning);
if (info.capturing) right("CAP", kWarning);
switch (info.radio) { // Q101: muted while listening, bright for a moment on each packet
case StatusInfo::Radio::Listening: right("L", kMuted); break;
case StatusInfo::Radio::Packet: right("L", kAccent); break;
case StatusInfo::Radio::None: break;
}
switch (info.gnss) { // Q61: muted while searching, normal with a Fix, the count with a 3D Fix
case StatusInfo::Gnss::Searching: right("G", kMuted); break;
case StatusInfo::Gnss::TwoD: right("G", kText); break;
+3 -1
View File
@@ -27,12 +27,14 @@ struct StatusInfo {
enum class Gnss { None, Searching, TwoD, ThreeD } gnss = Gnss::None; // None: GNSS off (Q61)
int gnssSatellites = 0; // used in the Fix
bool tracking = false; // a Track is recording (Q63)
enum class Radio { None, Listening, Packet } radio = Radio::None; // M3, Q101: Packet flashes
bool capturing = false; // a LoRa Capture is recording (Q97)
bool operator==(const StatusInfo& o) const {
return title == o.title && batteryPercent == o.batteryPercent && clock == o.clock &&
sdPresent == o.sdPresent && sdLevel == o.sdLevel && compose == o.compose && wifi == o.wifi &&
wifiBars == o.wifiBars && unread == o.unread && gnss == o.gnss && gnssSatellites == o.gnssSatellites &&
tracking == o.tracking;
tracking == o.tracking && radio == o.radio && capturing == o.capturing;
}
bool operator!=(const StatusInfo& o) const { return !(*this == o); }
};
+5
View File
@@ -83,11 +83,16 @@ void test_rssi_clamped() {
TEST_ASSERT_EQUAL_UINT8(159, out[16 + 12]);
}
void test_capture_path() {
TEST_ASSERT_EQUAL_STRING("/captures/lora/20261005-200000.pcap", capturePath(1791230400).c_str());
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_pcap_global_header);
RUN_TEST(test_record_with_loratap_v0);
RUN_TEST(test_negative_snr);
RUN_TEST(test_rssi_clamped);
RUN_TEST(test_capture_path);
return UNITY_END();
}
@@ -0,0 +1,75 @@
#include <unity.h>
#include <string>
#include <vector>
#include "packet_view.h"
using namespace roro::lora;
void setUp() {}
void tearDown() {}
static const uint8_t kMeshtastic[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0x78, 0x56, 0x34, 0x12, 0xEF, 0xBE, 0xAD, 0xDE,
0x62, // hop start 3, hop limit 2: relayed once
0x08, 0x00, 0x42, 0x01, 0x02, 0x03,
};
void test_row_for_a_meshtastic_packet() {
PacketSummary p{kMeshtastic, sizeof kMeshtastic, -97.4f, 6.25f, true, true};
// Time, RSSI, SNR, then who sent it to whom (by default short name: the last 4 hex digits,
// as Meshtastic names a Node) and how far it came. Fits 40 columns with two node numbers.
TEST_ASSERT_EQUAL_STRING("21:45:07 -97 6.2 5678>all 1/3", row(p, "21:45:07").c_str());
const uint8_t direct[] = {0x0a, 0x0b, 0x0c, 0x0d, 0xc4, 0xd3, 0xb2, 0xa1, 1, 2, 3, 4, 0x6a, 8, 0, 0xaa};
PacketSummary d{direct, sizeof direct, -117.0f, -9.25f, true, true};
TEST_ASSERT_EQUAL_STRING("21:45:07 -117 -9.2 d3c4>0b0a 1/3", row(d, "21:45:07").c_str());
TEST_ASSERT_TRUE(row(d, "21:45:07").size() <= 38);
}
void test_row_for_something_else() {
const uint8_t data[] = {0x40, 0x01, 0x02};
PacketSummary p{data, sizeof data, -120.6f, -7.5f, true, false};
TEST_ASSERT_EQUAL_STRING("21:45:07 -121 -7.5 3 B", row(p, "21:45:07").c_str());
}
// Not on Meshtastic settings (LoRaWAN, say): the same bytes aren't read as a Meshtastic header.
void test_row_not_meshtastic() {
PacketSummary p{kMeshtastic, sizeof kMeshtastic, -97.0f, 6.0f, true, false};
TEST_ASSERT_EQUAL_STRING("21:45:07 -97 6.0 19 B", row(p, "21:45:07").c_str());
}
void test_row_with_a_bad_crc() {
PacketSummary p{kMeshtastic, sizeof kMeshtastic, -97.0f, 6.0f, false, true};
TEST_ASSERT_EQUAL_STRING("21:45:07 -97 6.0 bad CRC, 19 B", row(p, "21:45:07").c_str());
}
void test_hex_dump() {
auto lines = hexDump(kMeshtastic, sizeof kMeshtastic);
TEST_ASSERT_EQUAL_size_t(3, lines.size());
TEST_ASSERT_EQUAL_STRING("0000 ff ff ff ff 78 56 34 12 ....xV4.", lines[0].c_str());
TEST_ASSERT_EQUAL_STRING("0008 ef be ad de 62 08 00 42 ....b..B", lines[1].c_str());
TEST_ASSERT_EQUAL_STRING("0010 01 02 03 ...", lines[2].c_str());
}
void test_header_lines() {
auto lines = headerLines(kMeshtastic, sizeof kMeshtastic);
TEST_ASSERT_EQUAL_size_t(5, lines.size());
TEST_ASSERT_EQUAL_STRING("From !12345678 to all", lines[0].c_str());
TEST_ASSERT_EQUAL_STRING("Packet deadbeef", lines[1].c_str());
TEST_ASSERT_EQUAL_STRING("Hops 1 of 3, limit 2 left", lines[2].c_str());
TEST_ASSERT_EQUAL_STRING("Channel 0x08 (LongFast, default key)", lines[3].c_str());
TEST_ASSERT_EQUAL_STRING("Relayed by ..42", lines[4].c_str());
TEST_ASSERT_EQUAL_size_t(0, headerLines(kMeshtastic, 12).size()); // too short to be Meshtastic
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_row_for_a_meshtastic_packet);
RUN_TEST(test_row_for_something_else);
RUN_TEST(test_row_not_meshtastic);
RUN_TEST(test_row_with_a_bad_crc);
RUN_TEST(test_hex_dump);
RUN_TEST(test_header_lines);
return UNITY_END();
}
+2
View File
@@ -28,6 +28,8 @@ void test_defaults_when_store_is_empty() {
TEST_ASSERT_TRUE(s.getBool(Setting::WifiEnabled));
TEST_ASSERT_TRUE(s.getBool(Setting::GnssEnabled)); // Q58: on by default
TEST_ASSERT_FALSE(s.getBool(Setting::CoordinatesDms)); // Q64: decimal degrees
TEST_ASSERT_EQUAL_INT32(0, s.getInt(Setting::LoraPreset)); // Q95: LongFast
TEST_ASSERT_FALSE(s.setInt(Setting::LoraPreset, 7)); // seven EU868 presets
TEST_ASSERT_EQUAL_STRING("EU868", s.getString(Setting::Region).c_str());
TEST_ASSERT_EQUAL_STRING("CET-1CEST,M3.5.0,M10.5.0/3", s.getString(Setting::Timezone).c_str());
}