feat(repeater): channel bot in the analyzer build -- listens and answers without repeating

The #tth-test bot needed a host process (slidge) plus a dedicated serial
companion node. Putting it in the firmware removes both: the observer node
answers by itself, and the serial console stays free for the CoreScope feed.

Not a port of the Solo bot. That one lives in examples/companion_radio on a
branch 610 commits diverged from ours, and it can only be configured from an
on-device BotScreen -- a XIAO has no display or buttons, so it would compile
and never arm. What made a local implementation cheap is that mesh::Mesh
already exposes everything:

  * Mesh::onRecvPacket() calls searchChannelsByHash() + onGroupDataRecv() and
    THEN routeRecvPacket() (Mesh.cpp:236-249), so reading a channel is
    independent of forwarding it. Both hooks are no-op virtuals, which is the
    only reason a repeater is normally deaf to channel traffic.
  * allowPacketForward() gates only RECEIVED packets, so a node with
    `repeat off` can still originate a reply. That is the point here: the
    observer sits metres from another repeater and must never forward, but it
    does have to answer.
  * createGroupDatagram() is in mesh::Mesh and sendFloodScoped() is a plain
    alias for sendFlood(), so none of the chat/contact machinery is needed.

Written against the mesh::Mesh API only, so it drops into
examples/simple_room_server unchanged if we ever want it there.

Commands are the ones only an analyzer can answer: !path (per-hop hashes at
the packet's own hash width, not a hardcoded 1 byte -- one node here runs 3),
!snr from the frame that carried the request, !heard from the registry.

Two guards, because this node's replies are its only transmissions:
anti-loop compares the sender prefix against _prefs.node_name (the runtime
name, so it holds even when stored prefs override the build-time one), and a
cooldown caps the reply rate. Both are needed: the cooldown alone would only
slow a ping-pong down, not stop it.

The channel key comes in as hex via -D BOT_CHANNEL_PSK, which is the form
`meshcore-cli get_channel <n>` prints -- no base64 round trip and no
base64.hpp dependency in this build. Hash derivation matches
BaseChatMesh::addChannel(); cross-checked against a live node, whose
128-bit key hashes to the same channel_hash the node reports.

All of it compiles out without BOT_CHANNEL_PSK. Costs 2.4 KB flash and 48 B
RAM over the plain analyzer build.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-08-04 18:30:47 +02:00
1 parent e217b6d6f7
commit cca22dd45d
3 files changed
+345

No files matched your search

+28
View File
@@ -209,6 +209,13 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
// radio garbage never enters the feed)
uint8_t pktfeed_stage[MAX_TRANS_UNIT];
uint8_t pktfeed_stage_len; // 0 = nothing staged
#endif
#ifdef BOT_CHANNEL_PSK
mesh::GroupChannel bot_channel; // 1 B hash + 32 B secret, derived from BOT_CHANNEL_PSK
bool bot_channel_valid; // false = PSK malformed, bot silently off
unsigned long bot_next_reply_at; // cooldown gate, 0 = not armed
uint32_t bot_replies_sent;
uint32_t bot_last_reply_secs; // RTC time of the last reply, 0 = never
#endif
CayenneLPP telemetry;
unsigned long set_radio_at, revert_radio_at;
@@ -238,6 +245,18 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
void stagePktFeed(const uint8_t raw[], int len);
void commitPktFeed(const mesh::Packet* pkt);
void formatPktFeedReply(char* reply, int max_len, uint32_t cursor);
#endif
#ifdef BOT_CHANNEL_PSK
// CUSTOM (TeTeHacko): channel bot. Definitions in ChannelBot.h, included at
// the bottom of MyMesh.cpp. See that file for why this lives in the analyzer
// build rather than in companion_radio.
void botInit();
bool botTriggerMatches(const char* text) const;
void botFormatPath(char* out, const mesh::Packet* pkt) const;
int botCommandReply(char* out, int max_len, const char* cmd, const mesh::Packet* pkt);
int botScanCommands(char* out, int max_len, const char* text, const mesh::Packet* pkt);
void botSendReply(const mesh::Packet* pkt, const char* text);
bool botHandleCommand(const char* command, char* reply);
#endif
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood);
uint8_t handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
@@ -255,6 +274,15 @@ protected:
}
bool allowPacketForward(const mesh::Packet* packet) override;
#ifdef BOT_CHANNEL_PSK
// CUSTOM (TeTeHacko): both are no-op virtuals in mesh::Mesh, which is the only
// reason a repeater is normally deaf to channel traffic. Overriding them does
// NOT affect forwarding -- Mesh::onRecvPacket() calls routeRecvPacket() either
// way (Mesh.cpp:236-249).
int searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel channels[], int max_matches) override;
void onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::GroupChannel& channel,
uint8_t* data, size_t len) override;
#endif
const char* getLogDateTime() override;
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;