Add Meshtext Companion channel chat and automatic radio2 setup
This commit is contained in:
1 parent
1f1a499482
commit
05907f49e0
52 files changed
+7700
-128
No files matched your search
@@ -0,0 +1,604 @@
|
||||
#include "MyMesh.h"
|
||||
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
#include <Preferences.h>
|
||||
#include <esp_system.h>
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#include <helpers/UTF8Helpers.h>
|
||||
|
||||
namespace {
|
||||
constexpr size_t LegacySettingsSize = 16;
|
||||
constexpr size_t SettingsSize = 40;
|
||||
bool validShortName(const char* name) {
|
||||
if (!name) return false;
|
||||
const size_t length = strnlen(name, 17);
|
||||
if (length > 16 || mesh::validUtf8PrefixLength(name, length) != length) return false;
|
||||
unsigned characters = 0;
|
||||
for (size_t i = 0; i < length;) {
|
||||
const uint8_t first = static_cast<uint8_t>(name[i++]);
|
||||
uint32_t scalar = first;
|
||||
unsigned remaining = 0;
|
||||
if (first >= 0xf0) { scalar &= 7; remaining = 3; }
|
||||
else if (first >= 0xe0) { scalar &= 15; remaining = 2; }
|
||||
else if (first >= 0xc0) { scalar &= 31; remaining = 1; }
|
||||
while (remaining--) scalar = (scalar << 6) | (static_cast<uint8_t>(name[i++]) & 63);
|
||||
// Reject control/bidi characters in a displayed NodeInfo short name.
|
||||
if (scalar <= 32 || (scalar >= 0x7f && scalar <= 0x9f) || scalar == ':'
|
||||
|| (scalar >= 0x200b && scalar <= 0x200f) || (scalar >= 0x2028 && scalar <= 0x202f)
|
||||
|| (scalar >= 0x2060 && scalar <= 0x206f) || scalar == 0xfeff) return false;
|
||||
if (++characters > 4) return false;
|
||||
}
|
||||
return true; // Empty is the stored default, selected explicitly by the CLI.
|
||||
}
|
||||
void makeWireShortName(const char* configured, uint32_t node, char out[5]) {
|
||||
// Stock User.short_name has four UTF-8 bytes, not four Unicode scalars.
|
||||
// Project older saved labels without changing their persistent settings.
|
||||
const size_t length = mesh::validUtf8PrefixLength(configured, 4);
|
||||
if (length) {
|
||||
memcpy(out, configured, length);
|
||||
out[length] = 0;
|
||||
} else {
|
||||
snprintf(out, 5, "%04lx", (unsigned long)(node & 0xffff));
|
||||
}
|
||||
}
|
||||
bool validNodeOverride(uint32_t node) {
|
||||
return node == 0 || (node >= 4 && node != UINT32_MAX);
|
||||
}
|
||||
void makeSettings(uint8_t image[SettingsSize], mesh::mt::Region region, uint32_t slot,
|
||||
uint8_t hops, const char* short_name, uint32_t node_override) {
|
||||
memset(image, 0, SettingsSize);
|
||||
memcpy(image, "MT03", 4);
|
||||
image[4] = static_cast<uint8_t>(region);
|
||||
image[5] = hops;
|
||||
for (unsigned i = 0; i < 4; ++i) image[8 + i] = slot >> (8 * i);
|
||||
memcpy(image + 12, short_name, strlen(short_name));
|
||||
for (unsigned i = 0; i < 4; ++i) image[32 + i] = node_override >> (8 * i);
|
||||
mesh::Utils::sha256(image + 36, 4, image, 36);
|
||||
}
|
||||
bool describe(const ChannelDetails& channel, mesh::mt::Channel& wire) {
|
||||
return mesh::mt::describeChannel(channel.name, channel.channel.secret, wire,
|
||||
[](uint8_t* out, size_t size, const uint8_t* in, size_t length) {
|
||||
mesh::Utils::sha256(out, size, in, length);
|
||||
});
|
||||
}
|
||||
bool nextToken(const char*& cursor, char* out, size_t capacity) {
|
||||
while (*cursor == ' ' || *cursor == '\t') ++cursor;
|
||||
size_t size = 0;
|
||||
while (*cursor && *cursor != ' ' && *cursor != '\t') {
|
||||
if (size + 1 >= capacity) return false;
|
||||
out[size++] = *cursor++;
|
||||
}
|
||||
out[size] = 0;
|
||||
return size != 0;
|
||||
}
|
||||
bool endTokens(const char* cursor) {
|
||||
while (*cursor == ' ' || *cursor == '\t') ++cursor;
|
||||
return !*cursor;
|
||||
}
|
||||
}
|
||||
|
||||
mesh::mt::Region MyMesh::meshtextRegion() const {
|
||||
return _mt_region == mesh::mt::Region::Unset
|
||||
? mesh::mt::inferRegion(_prefs.freq) : _mt_region;
|
||||
}
|
||||
|
||||
void MyMesh::refreshMeshtextIdentity() {
|
||||
uint8_t digest[4];
|
||||
mesh::Utils::sha256(digest, sizeof(digest), self_id.pub_key, PUB_KEY_SIZE);
|
||||
_mt_default_node = uint32_t(digest[0]) | (uint32_t(digest[1]) << 8)
|
||||
| (uint32_t(digest[2]) << 16) | (uint32_t(digest[3]) << 24);
|
||||
if (_mt_default_node < 4 || _mt_default_node == UINT32_MAX) _mt_default_node = 0x0badf00d;
|
||||
_mt_node = _mt_node_override ? _mt_node_override : _mt_default_node;
|
||||
_mt_seen.clear();
|
||||
// Importing a new MeshCore identity may change the derived sender ID, but
|
||||
// must never restart the packet sequence used by any earlier sender ID.
|
||||
}
|
||||
|
||||
void MyMesh::beginMeshtext() {
|
||||
refreshMeshtextIdentity();
|
||||
// Independent random boot seed, then monotonic IDs. Never reuse a packet ID
|
||||
// within this boot (including failed admission) with the same CTR sender.
|
||||
_mt_packet_id = esp_random();
|
||||
if (!_mt_packet_id) _mt_packet_id = 1;
|
||||
Preferences prefs;
|
||||
if (!prefs.begin("mesh-mt", false)) { _mt_settings_valid = false; return; }
|
||||
const size_t size = prefs.getBytesLength("settings");
|
||||
if (size) {
|
||||
uint8_t image[SettingsSize] = {}, expected[SettingsSize] = {};
|
||||
_mt_settings_valid = (size == SettingsSize || size == LegacySettingsSize)
|
||||
&& prefs.getBytes("settings", image, size) == size;
|
||||
if (_mt_settings_valid) {
|
||||
const auto region = static_cast<mesh::mt::Region>(image[4]);
|
||||
const bool legacy = size == LegacySettingsSize;
|
||||
const bool legacy_v1 = legacy && memcmp(image, "MT01", 4) == 0;
|
||||
const uint8_t hops = legacy_v1 ? 7 : image[5];
|
||||
const uint32_t slot = uint32_t(image[8]) | (uint32_t(image[9]) << 8)
|
||||
| (uint32_t(image[10]) << 16) | (uint32_t(image[11]) << 24);
|
||||
if (legacy) {
|
||||
memcpy(expected, legacy_v1 ? "MT01" : "MT02", 4);
|
||||
expected[4] = image[4]; expected[5] = legacy_v1 ? 0 : hops;
|
||||
memcpy(expected + 8, image + 8, 4);
|
||||
mesh::Utils::sha256(expected + 12, 4, expected, 12);
|
||||
} else if (validShortName(reinterpret_cast<const char*>(image + 12))) {
|
||||
const uint32_t node_override = uint32_t(image[32]) | (uint32_t(image[33]) << 8)
|
||||
| (uint32_t(image[34]) << 16) | (uint32_t(image[35]) << 24);
|
||||
if (validNodeOverride(node_override))
|
||||
makeSettings(expected, region, slot, hops, reinterpret_cast<const char*>(image + 12), node_override);
|
||||
}
|
||||
_mt_settings_valid = region <= mesh::mt::Region::ANZ && slot <= 1024 && hops <= 7
|
||||
&& memcmp(image, expected, size) == 0;
|
||||
if (_mt_settings_valid) {
|
||||
_mt_region = region; _mt_slot = slot; _mt_hop_limit = hops;
|
||||
if (!legacy) {
|
||||
memcpy(_mt_short_name, image + 12, sizeof(_mt_short_name));
|
||||
_mt_node_override = uint32_t(image[32]) | (uint32_t(image[33]) << 8)
|
||||
| (uint32_t(image[34]) << 16) | (uint32_t(image[35]) << 24);
|
||||
_mt_node = _mt_node_override ? _mt_node_override : _mt_default_node;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
prefs.end();
|
||||
refreshMeshtextProfile();
|
||||
}
|
||||
|
||||
bool MyMesh::saveMeshtextSettings(mesh::mt::Region region, uint32_t slot, uint8_t hops,
|
||||
const char* short_name, uint32_t node_override) {
|
||||
if (hops == UINT8_MAX) hops = _mt_hop_limit;
|
||||
if (!short_name) short_name = _mt_short_name;
|
||||
if (node_override == UINT32_MAX) node_override = _mt_node_override;
|
||||
if (region > mesh::mt::Region::ANZ || slot > 1024 || hops > 7 || !validShortName(short_name)
|
||||
|| !validNodeOverride(node_override) || hasForeignRadioFrame()) return false;
|
||||
uint8_t image[SettingsSize], verify[SettingsSize];
|
||||
makeSettings(image, region, slot, hops, short_name, node_override);
|
||||
Preferences prefs;
|
||||
if (!prefs.begin("mesh-mt", false)) return false;
|
||||
// NVS commits this one blob atomically; never split region and slot into
|
||||
// separate commits which could join an unintended network after power loss.
|
||||
const bool ok = prefs.putBytes("settings", image, sizeof(image)) == sizeof(image)
|
||||
&& prefs.getBytes("settings", verify, sizeof(verify)) == sizeof(verify)
|
||||
&& memcmp(image, verify, sizeof(image)) == 0;
|
||||
prefs.end();
|
||||
if (!ok) {
|
||||
// A failed read-back cannot prove whether NVS committed. Suspend the
|
||||
// backend rather than silently use one network now and another at boot.
|
||||
_mt_settings_valid = false;
|
||||
_mt_profile_dirty = true;
|
||||
refreshMeshtextProfile();
|
||||
return false;
|
||||
}
|
||||
_mt_region = region; _mt_slot = slot; _mt_hop_limit = hops; _mt_settings_valid = true;
|
||||
memcpy(_mt_short_name, image + 12, sizeof(_mt_short_name));
|
||||
_mt_node_override = node_override;
|
||||
_mt_node = node_override ? node_override : _mt_default_node;
|
||||
// Keep the packet sequence monotonic even when changing away from and back
|
||||
// to a sender ID. Never reset it on a name/settings change (CTR nonce reuse).
|
||||
_mt_profile_dirty = true;
|
||||
_mt_seen.clear();
|
||||
refreshMeshtextProfile();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::eraseMeshtextSettings() {
|
||||
// The filesystem erase also removes channels/identity on the next boot.
|
||||
// Do not keep sending with their still-resident RAM copies until then.
|
||||
_mt_active = false;
|
||||
_mt_settings_valid = false;
|
||||
_mt_profile_dirty = true;
|
||||
_mt_seen.clear();
|
||||
_radio_profiles.clearAutoSecondary();
|
||||
_radio_profiles.clearSecondaryProtocol();
|
||||
Preferences prefs;
|
||||
if (!prefs.begin("mesh-mt", false)) {
|
||||
_mt_status = "Meshtext settings erase failed";
|
||||
return false;
|
||||
}
|
||||
const bool ok = !prefs.getBytesLength("settings") || prefs.remove("settings");
|
||||
prefs.end();
|
||||
if (ok) {
|
||||
_mt_region = mesh::mt::Region::Unset; _mt_slot = 0; _mt_hop_limit = 7;
|
||||
_mt_short_name[0] = 0; _mt_node_override = 0; _mt_node = _mt_default_node;
|
||||
}
|
||||
_mt_status = ok ? "settings erased; reboot required" : "Meshtext settings erase failed";
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool MyMesh::isMeshtextChannel(const ChannelDetails& channel) const {
|
||||
const auto alias = parseCompanionChannelAlias(channel.name);
|
||||
if (alias.kind != CompanionChannelKind::Meshtext) return false;
|
||||
// With the backend present, Meshtext names are explicit protocol requests even
|
||||
// while setup is blocked. Never silently emit a different wire protocol.
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::validateMeshtextChannelChange(int index, const ChannelDetails& candidate) {
|
||||
const auto candidate_kind = parseCompanionChannelAlias(candidate.name).kind;
|
||||
// Unrelated MeshCore edits and removals remain possible even when an old
|
||||
// Meshtext configuration is suspended or incompatible with a new firmware build.
|
||||
if (!candidate.name[0] || candidate_kind == CompanionChannelKind::MeshCore) return true;
|
||||
bool have_preset = false, have_mt = false, have_r2 = false;
|
||||
mesh::mt::Preset preset = mesh::mt::Preset::LongFast;
|
||||
for (int i = 0; i < MAX_GROUP_CHANNELS; ++i) {
|
||||
ChannelDetails channel;
|
||||
if (i == index) channel = candidate;
|
||||
else if (!BaseChatMesh::getChannel(i, channel)) continue;
|
||||
if (!channel.name[0]) continue;
|
||||
const auto alias = parseCompanionChannelAlias(channel.name);
|
||||
if (alias.kind == CompanionChannelKind::Radio2) have_r2 = true;
|
||||
if (alias.kind != CompanionChannelKind::Meshtext) continue;
|
||||
mesh::mt::Preset found;
|
||||
const bool named = mesh::mt::parsePreset(alias.tail, found);
|
||||
mesh::mt::Channel wire;
|
||||
if (!describe(channel, wire)) return false;
|
||||
have_mt = true;
|
||||
if (named) {
|
||||
if (have_preset && preset != found) return false;
|
||||
preset = found; have_preset = true;
|
||||
}
|
||||
}
|
||||
// radio2 is one modem profile, not a second physical transceiver. There is
|
||||
// no silent last-channel-wins behavior and no protocol bridging.
|
||||
if (have_mt && have_r2) return false;
|
||||
if (!have_mt) return true;
|
||||
if (!_radio->supportsMeshtext() || !_mt_settings_valid) return false;
|
||||
if (have_preset) {
|
||||
mesh::RadioProfileParams requested;
|
||||
if (!mesh::mt::modemConfig(meshtextRegion(), preset, requested, _mt_slot)
|
||||
|| !_radio->validateProfile(requested)) return false;
|
||||
if (_radio_profiles.manualSecondaryConfigured()) {
|
||||
const auto saved = _radio_profiles.savedSecondaryConfig();
|
||||
if (!mesh::mt::sameModulation(saved.params, requested)
|
||||
|| !mesh::mt::frequencyInRegion(meshtextRegion(), saved.params)) return false;
|
||||
} else if (!_radio_profiles.canAutoSecondary()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MyMesh::refreshMeshtextProfile() {
|
||||
const auto* current_profiles = _radio->profiles();
|
||||
const uint32_t primary_generation = current_profiles ? current_profiles->generation[0] : 0;
|
||||
const uint32_t secondary_generation = current_profiles ? current_profiles->generation[1] : 0;
|
||||
const uint32_t intent_generation = _radio_profiles.secondaryIntentGeneration();
|
||||
if (!_mt_profile_dirty && primary_generation == _mt_primary_generation
|
||||
&& secondary_generation == _mt_secondary_generation && intent_generation == _mt_intent_generation
|
||||
&& _mt_last_radio_ready == _radio_available && _mt_primary_frequency == _prefs.freq) return;
|
||||
// Hashing names and scanning the channel table every radio loop would add
|
||||
// blind time to the dual-profile scanner. Recompute only on configuration
|
||||
// or lease/generation edges; RX/TX still derive a fresh bounded wire copy.
|
||||
struct CacheUpdate {
|
||||
MyMesh& owner;
|
||||
~CacheUpdate() {
|
||||
const auto* p = owner._radio->profiles();
|
||||
owner._mt_primary_generation = p ? p->generation[0] : 0;
|
||||
owner._mt_secondary_generation = p ? p->generation[1] : 0;
|
||||
owner._mt_intent_generation = owner._radio_profiles.secondaryIntentGeneration();
|
||||
owner._mt_last_radio_ready = owner._radio_available;
|
||||
owner._mt_primary_frequency = owner._prefs.freq;
|
||||
owner._mt_profile_dirty = false;
|
||||
}
|
||||
} cache_update{*this};
|
||||
_mt_active = false;
|
||||
bool have_mt = false, have_r2 = false, have_preset = false, conflict = false;
|
||||
mesh::mt::Preset preset = mesh::mt::Preset::LongFast;
|
||||
for (int i = 0; i < MAX_GROUP_CHANNELS; ++i) {
|
||||
ChannelDetails channel;
|
||||
if (!BaseChatMesh::getChannel(i, channel) || !channel.name[0]) continue;
|
||||
const auto alias = parseCompanionChannelAlias(channel.name);
|
||||
if (alias.kind == CompanionChannelKind::Radio2) have_r2 = true;
|
||||
if (alias.kind != CompanionChannelKind::Meshtext) continue;
|
||||
mesh::mt::Preset found;
|
||||
const bool named = mesh::mt::parsePreset(alias.tail, found);
|
||||
have_mt = true;
|
||||
mesh::mt::Channel wire;
|
||||
if (!describe(channel, wire)) conflict = true;
|
||||
if (named) {
|
||||
if (have_preset && preset != found) conflict = true;
|
||||
preset = found; have_preset = true;
|
||||
}
|
||||
}
|
||||
const char* blocked = nullptr;
|
||||
if (!have_mt) blocked = "no Meshtext channels";
|
||||
else if (conflict || have_r2) blocked = "conflicting radio2 channels/presets";
|
||||
else if (!_radio_available || !_radio->supportsMeshtext()) blocked = "modem unavailable";
|
||||
else if (!_mt_settings_valid) blocked = "invalid Meshtext settings; set mt.region to repair";
|
||||
else if (meshtextRegion() == mesh::mt::Region::Unset) blocked = "region ambiguous; set mt.region";
|
||||
if (blocked) {
|
||||
_mt_status = blocked;
|
||||
_radio_profiles.clearAutoSecondary();
|
||||
_radio_profiles.clearSecondaryProtocol();
|
||||
return;
|
||||
}
|
||||
mesh::RadioProfileParams requested;
|
||||
if (have_preset && (!mesh::mt::modemConfig(meshtextRegion(), preset, requested, _mt_slot)
|
||||
|| !_radio->validateProfile(requested))) {
|
||||
_mt_status = "preset/slot unavailable in this region or modem";
|
||||
_radio_profiles.clearAutoSecondary();
|
||||
_radio_profiles.clearSecondaryProtocol();
|
||||
return;
|
||||
}
|
||||
if (_radio_profiles.manualSecondaryConfigured()) {
|
||||
const auto saved = _radio_profiles.savedSecondaryConfig();
|
||||
if (!mesh::mt::frequencyInRegion(meshtextRegion(), saved.params)
|
||||
|| (have_preset && !mesh::mt::sameModulation(saved.params, requested))) {
|
||||
_mt_status = "manual radio2 does not match Meshtext preset/region";
|
||||
_radio_profiles.clearSecondaryProtocol();
|
||||
return;
|
||||
}
|
||||
_mt_active = _radio_profiles.useSecondaryProtocol(mesh::RadioProfileProtocol::Meshtext);
|
||||
} else if (have_preset) {
|
||||
mesh::RadioProfileConfig config;
|
||||
config.params = requested; config.mode = mesh::RadioProfileMode::RxTx;
|
||||
const auto* profiles = _radio->profiles();
|
||||
if (_radio_profiles.autoSecondaryActive() && profiles
|
||||
&& profiles->secondary.params != requested) {
|
||||
_radio_profiles.clearAutoSecondary(); // only release the profile we own
|
||||
}
|
||||
_mt_active = _radio_profiles.requestAutoSecondary(config);
|
||||
} else {
|
||||
// A custom channel cannot reconstruct an RF preset after reboot. Losing
|
||||
// its last named-preset dependency must release the auto owner, not leave
|
||||
// a ghost-enabled secondary or silently turn into MeshCore channel chat.
|
||||
_radio_profiles.clearAutoSecondary();
|
||||
_radio_profiles.clearSecondaryProtocol();
|
||||
_mt_status = "custom Meshtext channel needs a named preset or manual radio2";
|
||||
return;
|
||||
}
|
||||
_mt_status = _mt_active ? "active" : "radio2 manually off, leased, scheduled or busy";
|
||||
}
|
||||
|
||||
bool MyMesh::sendMeshtextText(const ChannelDetails& channel, const char* text, size_t length) {
|
||||
refreshMeshtextProfile();
|
||||
if (!_mt_active || !text || !length || hasForeignRadioFrame()) return false;
|
||||
const auto* profiles = _radio->profiles();
|
||||
if (!profiles || !profiles->canTransmit(1)
|
||||
|| profiles->secondary.params.protocol != mesh::RadioProfileProtocol::Meshtext) return false;
|
||||
mesh::mt::Channel wire;
|
||||
if (!describe(channel, wire)) return false;
|
||||
// Stock TEXT_MESSAGE_APP contains the user's text only. Sender identity is
|
||||
// in the radio header; name metadata requires a separately requested User.
|
||||
if (length > mesh::mt::MaxTextSize) return false;
|
||||
if (!_mt_packet_id) return false; // exhaustion: do not wrap/reuse CTR nonces.
|
||||
const uint32_t id = _mt_packet_id++;
|
||||
uint8_t raw[mesh::mt::MaxFrameSize];
|
||||
// Outbound Meshtext channel chat defaults to seven hops; the CLI can reduce it.
|
||||
// This does not enable forwarding or ACK responses in this Companion.
|
||||
const size_t size = mesh::mt::encodeText(_mt_node, id, _mt_hop_limit, wire.name, wire.key,
|
||||
wire.key_length, text, length, raw);
|
||||
return size && queueForeignRadioFrame(raw, size, 1, profiles->generation[1]);
|
||||
}
|
||||
|
||||
bool MyMesh::sendMeshtextNodeInfo(const ChannelDetails& channel) {
|
||||
refreshMeshtextProfile();
|
||||
if (!_mt_active || hasForeignRadioFrame()) return false;
|
||||
const auto* profiles = _radio->profiles();
|
||||
if (!profiles || !profiles->canTransmit(1)
|
||||
|| profiles->secondary.params.protocol != mesh::RadioProfileProtocol::Meshtext) return false;
|
||||
mesh::mt::Channel wire;
|
||||
if (!describe(channel, wire)) return false;
|
||||
const size_t full_length = strnlen(_prefs.node_name, sizeof(_prefs.node_name));
|
||||
if (full_length == sizeof(_prefs.node_name)
|
||||
|| mesh::validUtf8PrefixLength(_prefs.node_name, full_length) != full_length) return false;
|
||||
const size_t long_length = mesh::validUtf8PrefixLength(_prefs.node_name, 24);
|
||||
char short_name[5];
|
||||
makeWireShortName(_mt_short_name, _mt_node, short_name);
|
||||
if (!_mt_packet_id) return false;
|
||||
// Share the text packet sequence: separate counters would reuse CTR nonces.
|
||||
const uint32_t id = _mt_packet_id++;
|
||||
uint8_t raw[mesh::mt::MaxFrameSize];
|
||||
const size_t size = mesh::mt::encodeNodeInfo(_mt_node, id, _mt_hop_limit, wire.name, wire.key,
|
||||
wire.key_length, _prefs.node_name, long_length, short_name, strlen(short_name), raw);
|
||||
// Only the explicit CLI reaches here. No timer, RX reply or chat piggyback.
|
||||
return size && queueForeignRadioFrame(raw, size, 1, profiles->generation[1]);
|
||||
}
|
||||
|
||||
bool MyMesh::onForeignRadioFrame(const uint8_t* bytes, size_t length, uint8_t profile,
|
||||
uint32_t generation, float snr, float rssi) {
|
||||
(void)rssi;
|
||||
const auto* profiles = _radio->profiles();
|
||||
if (!_mt_active || !profiles || profile != 1 || generation != profiles->generation[1]) return false;
|
||||
for (int i = 0; i < MAX_GROUP_CHANNELS; ++i) {
|
||||
ChannelDetails channel;
|
||||
mesh::mt::Channel wire;
|
||||
if (!BaseChatMesh::getChannel(i, channel) || !describe(channel, wire)) continue;
|
||||
mesh::mt::DecodedText decoded;
|
||||
if (!mesh::mt::decodeText(bytes, length, wire.name, wire.key, wire.key_length, _mt_node, decoded)) continue;
|
||||
if (_mt_seen.seen(decoded.from, decoded.id)) return true;
|
||||
char text[MAX_FRAME_SIZE];
|
||||
const int prefix = snprintf(text, sizeof(text), "!%08lx: ", (unsigned long)decoded.from);
|
||||
size_t copy = decoded.text_len;
|
||||
if (copy > sizeof(text) - prefix - 1) copy = sizeof(text) - prefix - 1;
|
||||
// Preserve UTF-8 when fitting a stock peer text into the Companion envelope.
|
||||
while (copy < decoded.text_len && copy && (uint8_t(decoded.text[copy]) & 0xc0) == 0x80) --copy;
|
||||
memcpy(text + prefix, decoded.text, copy); text[prefix + copy] = 0;
|
||||
const uint32_t timestamp = getRTCClock()->getCurrentTime();
|
||||
ChannelDetails policy_channel;
|
||||
if (!getChannel(i, policy_channel)) return false;
|
||||
if (queueChannelText(i, policy_channel.channel.secret, snr, decoded.hops, timestamp, text, false)) {
|
||||
_mt_seen.remember(decoded.from, decoded.id);
|
||||
++_mt_received;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void MyMesh::onForeignRadioSendComplete(bool success) {
|
||||
if (success) ++_mt_sent;
|
||||
else ++_mt_failed;
|
||||
}
|
||||
|
||||
bool MyMesh::handleMeshtextCommand(const char* command, char* reply, size_t capacity) {
|
||||
const char* nodeinfo_args = !strncmp(command, "send mt.nodeinfo ", 17) ? command + 17 : nullptr;
|
||||
if (!strcmp(command, "send mt.nodeinfo")) nodeinfo_args = "";
|
||||
if (nodeinfo_args) {
|
||||
char index_text[12]; uint32_t index = 0;
|
||||
ChannelDetails channel;
|
||||
if (!nextToken(nodeinfo_args, index_text, sizeof(index_text)) || !endTokens(nodeinfo_args)
|
||||
|| !mesh::cli::parseUnsignedIntegerStrict(index_text, index) || index >= MAX_GROUP_CHANNELS) {
|
||||
snprintf(reply, capacity, "Error: use send mt.nodeinfo <channel slot>");
|
||||
} else if (!getChannelForSave(index, channel) || !isMeshtextChannel(channel)) {
|
||||
snprintf(reply, capacity, "Error: configured Meshtext channel required");
|
||||
} else if (!sendMeshtextNodeInfo(channel)) {
|
||||
snprintf(reply, capacity, "Error: NodeInfo not queued; check get mt.status, TX mode or pending TX");
|
||||
} else {
|
||||
snprintf(reply, capacity, "OK - NodeInfo queued on Meshtext channel %lu; no automatic adverts",
|
||||
(unsigned long)index);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
const char* channel_args = !strncmp(command, "set mt.channel ", 15) ? command + 15 : nullptr;
|
||||
const char* channel_get = !strncmp(command, "get mt.channel ", 15) ? command + 15 : nullptr;
|
||||
const char* channel_delete = !strncmp(command, "delete mt.channel ", 18) ? command + 18 : nullptr;
|
||||
if (channel_args || channel_get || channel_delete) {
|
||||
const char* cursor = channel_args ? channel_args : channel_get ? channel_get : channel_delete;
|
||||
char index_text[12]; uint32_t index = 0;
|
||||
if (!nextToken(cursor, index_text, sizeof(index_text))
|
||||
|| !mesh::cli::parseUnsignedIntegerStrict(index_text, index) || index >= MAX_GROUP_CHANNELS) {
|
||||
snprintf(reply, capacity, "Error: invalid channel slot"); return true;
|
||||
}
|
||||
ChannelDetails previous{}, channel{};
|
||||
if (!getChannelForSave(index, previous)) {
|
||||
snprintf(reply, capacity, "Error: channel slot unavailable"); return true;
|
||||
}
|
||||
if (channel_get || channel_delete) {
|
||||
if (!endTokens(cursor) || !previous.name[0] || !isMeshtextChannel(previous)) {
|
||||
snprintf(reply, capacity, "Error: Meshtext channel not found or extra arguments"); return true;
|
||||
}
|
||||
if (channel_get) {
|
||||
mesh::mt::Channel wire;
|
||||
if (!describe(previous, wire)) snprintf(reply, capacity, "Error: invalid Meshtext channel");
|
||||
else snprintf(reply, capacity, "> %lu %s; wire=%s; AES-%u; key not displayed",
|
||||
(unsigned long)index, previous.name, wire.name, unsigned(wire.key_length * 8));
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
char key_text[65];
|
||||
if (!nextToken(cursor, channel.name, sizeof(channel.name))
|
||||
|| !nextToken(cursor, key_text, sizeof(key_text)) || !endTokens(cursor)
|
||||
|| !isMeshtextChannel(channel)) {
|
||||
snprintf(reply, capacity, "Error: use set mt.channel <slot> <mt-name> <32/64 hex digits|AQ==>"); return true;
|
||||
}
|
||||
if (!strcmp(key_text, "AQ==")) memcpy(channel.channel.secret, mesh::mt::DefaultPsk, 16);
|
||||
else {
|
||||
const size_t digits = strlen(key_text);
|
||||
if ((digits != 32 && digits != 64)
|
||||
|| !mesh::Utils::fromHex(channel.channel.secret, digits / 2, key_text)) {
|
||||
snprintf(reply, capacity, "Error: key must be 32/64 hex digits or AQ=="); return true;
|
||||
}
|
||||
}
|
||||
if (!validateMeshtextChannelChange(index, channel)) {
|
||||
snprintf(reply, capacity, "Error: conflicting Meshtext preset, region, manual radio2 or busy owner"); return true;
|
||||
}
|
||||
}
|
||||
if (!setChannel(index, channel)) snprintf(reply, capacity, "Error: invalid Meshtext channel");
|
||||
else if (!saveChannels()) {
|
||||
setChannel(index, previous);
|
||||
snprintf(reply, capacity, "Error: channel not saved; previous channel restored");
|
||||
} else {
|
||||
_mt_seen.clear(); _mt_profile_dirty = true;
|
||||
refreshMeshtextProfile();
|
||||
snprintf(reply, capacity, "OK - Meshtext channel %lu %s; %s", (unsigned long)index,
|
||||
channel_delete ? "deleted" : "saved", _mt_status);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
const char* preset_value = !strncmp(command, "set mt.preset ", 14) ? command + 14 : nullptr;
|
||||
if (preset_value) {
|
||||
mesh::mt::Preset preset; mesh::RadioProfileParams requested;
|
||||
if (!mesh::mt::parsePreset(preset_value, preset)
|
||||
|| !mesh::mt::modemConfig(meshtextRegion(), preset, requested, _mt_slot)
|
||||
|| !_radio->supportsMeshtext() || !_radio->validateProfile(requested)) {
|
||||
snprintf(reply, capacity, "Error: invalid Meshtext preset/region/slot; use set mt.region first"); return true;
|
||||
}
|
||||
// Explicit provisioning for custom private names. This is a manual saved
|
||||
// radio2 choice, not an automatic alias owner, so deletion cannot undo it.
|
||||
char radio_command[96];
|
||||
snprintf(radio_command, sizeof(radio_command), "set radio2 %.6f,%g,%u,%u,rxtx",
|
||||
requested.freq, requested.bw, requested.sf, requested.cr);
|
||||
_radio_profiles.handle(radio_command, reply, capacity);
|
||||
_mt_profile_dirty = true;
|
||||
refreshMeshtextProfile();
|
||||
return true;
|
||||
}
|
||||
const bool status = !strcmp(command, "get mt") || !strcmp(command, "get mt.status");
|
||||
const bool get_region = !strcmp(command, "get mt.region");
|
||||
const bool get_slot = !strcmp(command, "get mt.slot");
|
||||
const bool get_hops = !strcmp(command, "get mt.hops");
|
||||
const bool get_node = !strcmp(command, "get mt.node");
|
||||
const bool get_short_name = !strcmp(command, "get mt.shortname");
|
||||
const char* region_value = !strncmp(command, "set mt.region ", 14) ? command + 14 : nullptr;
|
||||
const char* slot_value = !strncmp(command, "set mt.slot ", 12) ? command + 12 : nullptr;
|
||||
const char* hops_value = !strncmp(command, "set mt.hops ", 12) ? command + 12 : nullptr;
|
||||
const char* node_value = !strncmp(command, "set mt.node ", 12) ? command + 12 : nullptr;
|
||||
const char* short_name_value = !strncmp(command, "set mt.shortname ", 17) ? command + 17 : nullptr;
|
||||
if (!strcmp(command, "set mt.node")) node_value = "";
|
||||
if (!strcmp(command, "set mt.shortname")) short_name_value = "";
|
||||
if (!status && !get_region && !get_slot && !get_hops && !get_node && !get_short_name
|
||||
&& !region_value && !slot_value && !hops_value && !node_value && !short_name_value) return false;
|
||||
if (region_value || slot_value || hops_value || node_value || short_name_value) {
|
||||
auto region = _mt_region; uint32_t slot = _mt_slot;
|
||||
uint32_t hops = _mt_hop_limit;
|
||||
uint32_t node_override = _mt_node_override;
|
||||
const char* short_name = _mt_short_name;
|
||||
if (region_value && !mesh::mt::parseRegion(region_value, region)) {
|
||||
snprintf(reply, capacity, "Error: use set mt.region auto|us|eu868|eu433|anz"); return true;
|
||||
}
|
||||
if (slot_value && (!mesh::cli::parseUnsignedIntegerStrict(slot_value, slot) || slot > 1024)) {
|
||||
snprintf(reply, capacity, "Error: mt.slot must be 0 (preset default) or 1..1024"); return true;
|
||||
}
|
||||
if (hops_value && (!mesh::cli::parseUnsignedIntegerStrict(hops_value, hops) || hops > 7)) {
|
||||
snprintf(reply, capacity, "Error: mt.hops must be 0..7"); return true;
|
||||
}
|
||||
if (node_value) {
|
||||
if (!strcmp(node_value, "default")) node_override = 0;
|
||||
else {
|
||||
if (*node_value == '!') ++node_value;
|
||||
uint8_t bytes[4];
|
||||
if (strlen(node_value) != 8 || !mesh::Utils::fromHex(bytes, sizeof(bytes), node_value)) {
|
||||
snprintf(reply, capacity, "Error: use set mt.node !<8 hex digits>|default"); return true;
|
||||
}
|
||||
node_override = (uint32_t(bytes[0]) << 24) | (uint32_t(bytes[1]) << 16)
|
||||
| (uint32_t(bytes[2]) << 8) | uint32_t(bytes[3]);
|
||||
if (!node_override || !validNodeOverride(node_override)) {
|
||||
snprintf(reply, capacity, "Error: mt.node IDs 0..3 and ffffffff are reserved; use default to reset"); return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (short_name_value) {
|
||||
short_name = !strcmp(short_name_value, "default") ? "" : short_name_value;
|
||||
if ((!*short_name && strcmp(short_name_value, "default")) || !validShortName(short_name)) {
|
||||
snprintf(reply, capacity, "Error: mt.shortname must be 1..4 UTF-8 characters (no spaces/control/colon), or default"); return true;
|
||||
}
|
||||
}
|
||||
if (!saveMeshtextSettings(region, slot, hops, short_name, node_override))
|
||||
snprintf(reply, capacity, "Error: Meshtext settings not saved (pending TX or storage error)");
|
||||
else if (node_value)
|
||||
snprintf(reply, capacity, "OK - mt.node=!%08lx (%s); choose a unique ID", (unsigned long)_mt_node,
|
||||
_mt_node_override ? "saved override" : "identity default");
|
||||
else if (short_name_value)
|
||||
snprintf(reply, capacity, "OK - mt.shortname=%s; node=!%08lx", _mt_short_name[0] ? _mt_short_name : "default",
|
||||
(unsigned long)_mt_node);
|
||||
else snprintf(reply, capacity, "OK - mt.region=%s mt.slot=%lu mt.hops=%lu; %s", mesh::mt::regionName(region), (unsigned long)slot, (unsigned long)hops, _mt_status);
|
||||
return true;
|
||||
}
|
||||
refreshMeshtextProfile();
|
||||
if (get_region) snprintf(reply, capacity, "> %s; effective=%s", mesh::mt::regionName(_mt_region), mesh::mt::regionName(meshtextRegion()));
|
||||
else if (get_slot) snprintf(reply, capacity, "> %lu%s", (unsigned long)_mt_slot, _mt_slot ? "" : " (preset default)");
|
||||
else if (get_hops) snprintf(reply, capacity, "> %u", _mt_hop_limit);
|
||||
else if (get_node) snprintf(reply, capacity, "> !%08lx (%s)", (unsigned long)_mt_node,
|
||||
_mt_node_override ? "saved override" : "identity default");
|
||||
else if (get_short_name) {
|
||||
char short_name[5]; makeWireShortName(_mt_short_name, _mt_node, short_name);
|
||||
snprintf(reply, capacity, "> %s; effective=%s (manual NodeInfo only)",
|
||||
_mt_short_name[0] ? _mt_short_name : "default", short_name);
|
||||
}
|
||||
else {
|
||||
const auto* profiles = _radio->profiles();
|
||||
const auto p = profiles ? profiles->secondary.params : mesh::RadioProfileParams{};
|
||||
snprintf(reply, capacity, "> Meshtext !%08lx; %s; %s; %.3f/%g/SF%u/CR%u; hops=%u; RX=%lu TX=%lu fail=%lu; RX time-shared",
|
||||
(unsigned long)_mt_node, _mt_status, _radio_profiles.autoSecondaryActive() ? "auto radio2" : "manual radio2",
|
||||
p.freq, p.bw, p.sf, p.cr, _mt_hop_limit, (unsigned long)_mt_received, (unsigned long)_mt_sent, (unsigned long)_mt_failed);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "NotificationSettingsFile.h"
|
||||
#include "MyMesh.h"
|
||||
#include <helpers/CompanionTxRoutingCLI.h>
|
||||
#include <helpers/CompanionChannelAlias.h>
|
||||
#include <helpers/FleetCommand.h>
|
||||
#include "CompanionBluetooth.h"
|
||||
#include "CompanionWireless.h"
|
||||
@@ -1639,7 +1640,8 @@ static bool __attribute__((noinline)) hasEmergencyClientRepeat(uint16_t key) {
|
||||
}
|
||||
|
||||
bool MyMesh::filterRecvFloodPacket(mesh::Packet* packet) {
|
||||
if (emergency_client_repeat_packet != NULL && packet->getPathHashCount() > 0) {
|
||||
if (emergency_client_repeat_packet != NULL && packet->getPathHashCount() > 0
|
||||
&& packet->radio_origin == emergency_client_repeat_packet->radio_origin) {
|
||||
if (emergencyClientRepeatKey(packet) == emergency_client_repeat_key) {
|
||||
releasePacket(emergency_client_repeat_packet);
|
||||
emergency_client_repeat_packet = NULL;
|
||||
@@ -1658,6 +1660,127 @@ bool MyMesh::allowPacketForward(const mesh::Packet* packet) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::isAliasRadio2Defined() const {
|
||||
const auto profiles = _radio->profiles();
|
||||
return profiles != nullptr && profiles->enabled();
|
||||
}
|
||||
|
||||
bool MyMesh::resolveChannelAlias(ChannelDetails& channel) const {
|
||||
uint8_t original_key[16];
|
||||
memcpy(original_key, channel.channel.secret, sizeof(original_key));
|
||||
if (!resolveCompanionChannelKey(channel.name, channel.channel.secret,
|
||||
[](uint8_t* out, size_t size, const uint8_t* text, int length) {
|
||||
mesh::Utils::sha256(out, size, text, length);
|
||||
})) return false;
|
||||
const auto kind = parseCompanionChannelAlias(channel.name).kind;
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
if (isMeshtextChannel(channel)) channel.channel.tx_radio = mesh::RADIO_TX_OFF;
|
||||
#endif
|
||||
if (isAliasRadio2Defined()
|
||||
&& (kind == CompanionChannelKind::Radio2 || kind == CompanionChannelKind::Meshtext)) {
|
||||
channel.channel.tx_radio = kind == CompanionChannelKind::Radio2
|
||||
? mesh::RADIO_TX_SECONDARY : mesh::RADIO_TX_OFF;
|
||||
}
|
||||
if (memcmp(original_key, channel.channel.secret, sizeof(original_key)) != 0) {
|
||||
// BaseChatMesh already cached the hash for ordinary/private keys. Only a
|
||||
// rewritten automatic alias needs another SHA-256 during receive scans.
|
||||
mesh::Utils::sha256(channel.channel.hash, sizeof(channel.channel.hash), channel.channel.secret, 16);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::getChannel(int channel_idx, ChannelDetails& channel) {
|
||||
return BaseChatMesh::getChannel(channel_idx, channel) && resolveChannelAlias(channel);
|
||||
}
|
||||
|
||||
bool MyMesh::setChannel(int channel_idx, const ChannelDetails& channel) {
|
||||
if (parseCompanionChannelAlias(channel.name).kind == CompanionChannelKind::Invalid) return false;
|
||||
return BaseChatMesh::setChannel(channel_idx, channel);
|
||||
}
|
||||
|
||||
int MyMesh::searchChannelsForPacket(const mesh::Packet* packet, const uint8_t* hash,
|
||||
mesh::GroupChannel matches[], int max_matches) {
|
||||
int count = 0;
|
||||
const bool radio2_defined = isAliasRadio2Defined();
|
||||
for (int priority = 0; priority < 2; ++priority) {
|
||||
for (int i = 0; i < MAX_GROUP_CHANNELS && count < max_matches; ++i) {
|
||||
ChannelDetails channel;
|
||||
if (!getChannel(i, channel) || channel.name[0] == 0
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
|| isMeshtextChannel(channel)
|
||||
#endif
|
||||
|| companionChannelReceivePriority(channel.name, packet->radio_origin, radio2_defined) != priority
|
||||
|| channel.channel.hash[0] != hash[0]) continue;
|
||||
bool duplicate = false;
|
||||
for (int j = 0; j < count; ++j) {
|
||||
if (memcmp(matches[j].secret, channel.channel.secret, sizeof(channel.channel.secret)) == 0) {
|
||||
duplicate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!duplicate) matches[count++] = channel.channel;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int MyMesh::findChannelIdxForPacket(const mesh::GroupChannel& key, const mesh::Packet* packet) {
|
||||
const bool radio2_defined = isAliasRadio2Defined();
|
||||
for (int priority = 0; priority < 2; ++priority) {
|
||||
for (int i = 0; i < MAX_GROUP_CHANNELS; ++i) {
|
||||
ChannelDetails channel;
|
||||
if (getChannel(i, channel) && channel.name[0] != 0
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
&& !isMeshtextChannel(channel)
|
||||
#endif
|
||||
&& companionChannelReceivePriority(channel.name, packet->radio_origin, radio2_defined) == priority
|
||||
&& memcmp(channel.channel.secret, key.secret, sizeof(key.secret)) == 0) return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool MyMesh::allowGroupPacketOnRadio(mesh::Packet* packet) {
|
||||
if (!isAliasRadio2Defined() || packet->payload_len < 1) return true;
|
||||
if (packet->payload_len > MAX_PACKET_PAYLOAD) return false;
|
||||
// Reject only an authenticated key match. The one-byte channel hash alone
|
||||
// must not suppress unrelated channels or opaque relaying.
|
||||
for (int i = 0; i < MAX_GROUP_CHANNELS; ++i) {
|
||||
ChannelDetails channel;
|
||||
if (!getChannel(i, channel) || channel.name[0] == 0
|
||||
|| channel.channel.hash[0] != packet->payload[0]) continue;
|
||||
const auto alias = parseCompanionChannelAlias(channel.name);
|
||||
if (alias.kind != CompanionChannelKind::Radio2
|
||||
&& alias.kind != CompanionChannelKind::Meshtext) continue;
|
||||
uint8_t data[MAX_PACKET_PAYLOAD];
|
||||
if (mesh::Utils::MACThenDecrypt(channel.channel.secret, data, packet->payload + 1,
|
||||
packet->payload_len - 1) <= 0) continue;
|
||||
const int selected = findChannelIdxForPacket(channel.channel, packet);
|
||||
if (selected < 0) return false;
|
||||
ChannelDetails selected_channel;
|
||||
if (getChannel(selected, selected_channel)
|
||||
&& parseCompanionChannelAlias(selected_channel.name).kind == CompanionChannelKind::Radio2) {
|
||||
// Also bind direct-path relaying, which runs before local delivery.
|
||||
packet->tx_radio = mesh::RADIO_TX_SECONDARY;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type,
|
||||
const mesh::GroupChannel& key, uint8_t* data, size_t len) {
|
||||
const int index = findChannelIdxForPacket(key, packet);
|
||||
if (index < 0) return;
|
||||
ChannelDetails channel;
|
||||
if (getChannel(index, channel) && isAliasRadio2Defined()
|
||||
&& parseCompanionChannelAlias(channel.name).kind == CompanionChannelKind::Radio2) {
|
||||
// Forwarding and the emergency hold copy stay on the originating profile.
|
||||
packet->tx_radio = mesh::RADIO_TX_SECONDARY;
|
||||
}
|
||||
BaseChatMesh::onGroupDataRecv(packet, type, key, data, len);
|
||||
}
|
||||
|
||||
#ifdef COMPANION_MESH_CLOCK_SYNC
|
||||
void MyMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
|
||||
uint32_t timestamp, const uint8_t* app_data,
|
||||
@@ -1923,74 +2046,11 @@ void MyMesh::onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packe
|
||||
const bool is_emergency_channel =
|
||||
memcmp(channel.secret, EMERGENCY_CHANNEL_SECRET,
|
||||
sizeof(EMERGENCY_CHANNEL_SECRET)) == 0;
|
||||
int i = 0;
|
||||
if (app_target_ver >= 3) {
|
||||
out_frame[i++] = RESP_CODE_CHANNEL_MSG_RECV_V3;
|
||||
out_frame[i++] = (int8_t)(pkt->getSNR() * 4);
|
||||
out_frame[i++] = 0; // reserved1
|
||||
out_frame[i++] = 0; // reserved2
|
||||
} else {
|
||||
out_frame[i++] = RESP_CODE_CHANNEL_MSG_RECV;
|
||||
}
|
||||
|
||||
uint8_t channel_idx = findChannelIdx(channel);
|
||||
out_frame[i++] = channel_idx;
|
||||
uint8_t path_len = out_frame[i++] = pkt->isRouteFlood() ? pkt->path_len : 0xFF;
|
||||
|
||||
out_frame[i++] = TXT_TYPE_PLAIN;
|
||||
memcpy(&out_frame[i], ×tamp, 4);
|
||||
i += 4;
|
||||
int tlen = strlen(text); // TODO: UTF-8 ??
|
||||
if (i + tlen > MAX_FRAME_SIZE) {
|
||||
tlen = MAX_FRAME_SIZE - i;
|
||||
}
|
||||
memcpy(&out_frame[i], text, tlen);
|
||||
i += tlen;
|
||||
const bool queued = addToOfflineQueue(out_frame, i);
|
||||
|
||||
#if COMPANION_FEATURE_NOTIFICATIONS
|
||||
const bool notification_managed = _notifications.message(nullptr, channel.secret, _serial->isConnected(), millis());
|
||||
if (_ui) _ui->setMessageNotificationOverride(notification_managed, _notifications.overridesScreen());
|
||||
#else
|
||||
const bool notification_managed = false;
|
||||
#endif
|
||||
if (_serial->isConnected()) {
|
||||
uint8_t frame[1];
|
||||
frame[0] = PUSH_CODE_MSG_WAITING;
|
||||
_serial->writeFrame(frame, 1);
|
||||
} else {
|
||||
#ifdef DISPLAY_CLASS
|
||||
if (_ui && !notification_managed) _ui->notify(UIEventType::channelMessage);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if COMPANION_FEATURE_TEXT_TERMINAL
|
||||
if (hasTerminalOutput()
|
||||
&& _terminal_display.shouldShowChannel(is_emergency_channel)) {
|
||||
ChannelDetails details;
|
||||
const char* channel_name = getChannel(channel_idx, details) ? details.name : "Unknown";
|
||||
terminalOutput().printf(
|
||||
"\r\nCHANNEL MSG -> %s (%s)\r\n %s\r\n> ", channel_name,
|
||||
pkt->isRouteDirect() ? "DIRECT" : "FLOOD", text);
|
||||
}
|
||||
#endif
|
||||
#ifdef DISPLAY_CLASS
|
||||
// Get the channel name from the channel index
|
||||
const char *channel_name = "Unknown";
|
||||
ChannelDetails channel_details;
|
||||
if (getChannel(channel_idx, channel_details)) {
|
||||
channel_name = channel_details.name;
|
||||
}
|
||||
char channel_label[64];
|
||||
snprintf(channel_label, sizeof(channel_label), "Ch %u %s",
|
||||
(unsigned int)channel_idx, channel_name);
|
||||
if (_ui) {
|
||||
_ui->newMsg(path_len, channel_label, text, offline_queue_len,
|
||||
channel_idx, channel_name, queued ? offline_queue_len - 1 : -1);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (_ui) _ui->setMessageNotificationOverride(false);
|
||||
const int selected_channel = findChannelIdxForPacket(channel, pkt);
|
||||
if (selected_channel < 0) return;
|
||||
queueChannelText(selected_channel, channel.secret, pkt->getSNR(),
|
||||
pkt->isRouteFlood() ? pkt->path_len : 0xFF,
|
||||
timestamp, text, is_emergency_channel);
|
||||
|
||||
if (pkt->isRouteFlood() && is_emergency_channel) {
|
||||
bool zero_path = pkt->getPathHashCount() == 0;
|
||||
@@ -2016,6 +2076,84 @@ void MyMesh::onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packe
|
||||
}
|
||||
}
|
||||
|
||||
bool MyMesh::queueChannelText(uint8_t channel_idx, const uint8_t secret[32], float snr,
|
||||
uint8_t path_len, uint32_t timestamp, const char* text,
|
||||
bool is_emergency_channel) {
|
||||
if (!text || channel_idx >= MAX_GROUP_CHANNELS) return false;
|
||||
int i = 0;
|
||||
if (app_target_ver >= 3) {
|
||||
out_frame[i++] = RESP_CODE_CHANNEL_MSG_RECV_V3;
|
||||
const float bounded_snr = !isfinite(snr) ? 0 : snr < -32 ? -32 : snr > 31.75f ? 31.75f : snr;
|
||||
out_frame[i++] = (int8_t)(bounded_snr * 4);
|
||||
out_frame[i++] = 0; // reserved1
|
||||
out_frame[i++] = 0; // reserved2
|
||||
} else {
|
||||
out_frame[i++] = RESP_CODE_CHANNEL_MSG_RECV;
|
||||
}
|
||||
|
||||
out_frame[i++] = channel_idx;
|
||||
out_frame[i++] = path_len;
|
||||
|
||||
out_frame[i++] = TXT_TYPE_PLAIN;
|
||||
memcpy(&out_frame[i], ×tamp, 4);
|
||||
i += 4;
|
||||
int tlen = strlen(text); // TODO: UTF-8 ??
|
||||
if (i + tlen > MAX_FRAME_SIZE) {
|
||||
tlen = MAX_FRAME_SIZE - i;
|
||||
while (tlen > 0 && (uint8_t(text[tlen]) & 0xc0) == 0x80) --tlen;
|
||||
}
|
||||
memcpy(&out_frame[i], text, tlen);
|
||||
i += tlen;
|
||||
const bool queued = addToOfflineQueue(out_frame, i);
|
||||
if (!queued) return false;
|
||||
|
||||
#if COMPANION_FEATURE_NOTIFICATIONS
|
||||
const bool notification_managed = _notifications.message(nullptr, secret, _serial && _serial->isConnected(), millis());
|
||||
if (_ui) _ui->setMessageNotificationOverride(notification_managed, _notifications.overridesScreen());
|
||||
#else
|
||||
const bool notification_managed = false;
|
||||
#endif
|
||||
if (_serial && _serial->isConnected()) {
|
||||
uint8_t frame[1];
|
||||
frame[0] = PUSH_CODE_MSG_WAITING;
|
||||
_serial->writeFrame(frame, 1);
|
||||
} else {
|
||||
#ifdef DISPLAY_CLASS
|
||||
if (_ui && !notification_managed) _ui->notify(UIEventType::channelMessage);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if COMPANION_FEATURE_TEXT_TERMINAL
|
||||
if (hasTerminalOutput()
|
||||
&& _terminal_display.shouldShowChannel(is_emergency_channel)) {
|
||||
ChannelDetails details;
|
||||
const char* channel_name = getChannel(channel_idx, details) ? details.name : "Unknown";
|
||||
terminalOutput().printf(
|
||||
"\r\nCHANNEL MSG -> %s (%s)\r\n %s\r\n> ", channel_name,
|
||||
path_len == 0xFF ? "DIRECT" : "FLOOD", text);
|
||||
}
|
||||
#endif
|
||||
#ifdef DISPLAY_CLASS
|
||||
// Get the channel name from the channel index
|
||||
const char *channel_name = "Unknown";
|
||||
ChannelDetails channel_details;
|
||||
if (getChannel(channel_idx, channel_details)) {
|
||||
channel_name = channel_details.name;
|
||||
}
|
||||
char channel_label[64];
|
||||
snprintf(channel_label, sizeof(channel_label), "Ch %u %s",
|
||||
(unsigned int)channel_idx, channel_name);
|
||||
if (_ui) {
|
||||
_ui->newMsg(path_len, channel_label, text, offline_queue_len,
|
||||
channel_idx, channel_name, queued ? offline_queue_len - 1 : -1);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (_ui) _ui->setMessageNotificationOverride(false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MyMesh::onChannelDataRecv(const mesh::GroupChannel &channel, mesh::Packet *pkt, uint16_t data_type,
|
||||
const uint8_t *data, size_t data_len) {
|
||||
if (data_len > MAX_CHANNEL_DATA_LENGTH) {
|
||||
@@ -2030,7 +2168,9 @@ void MyMesh::onChannelDataRecv(const mesh::GroupChannel &channel, mesh::Packet *
|
||||
out_frame[i++] = 0; // reserved1
|
||||
out_frame[i++] = 0; // reserved2
|
||||
|
||||
uint8_t channel_idx = findChannelIdx(channel);
|
||||
const int selected_channel = findChannelIdxForPacket(channel, pkt);
|
||||
if (selected_channel < 0) return;
|
||||
uint8_t channel_idx = selected_channel;
|
||||
out_frame[i++] = channel_idx;
|
||||
out_frame[i++] = pkt->isRouteFlood() ? pkt->path_len : 0xFF;
|
||||
out_frame[i++] = (uint8_t)(data_type & 0xFF);
|
||||
@@ -2954,6 +3094,9 @@ void MyMesh::begin(bool has_display, bool radio_available,
|
||||
#endif
|
||||
|
||||
configureRadioFromPrefs();
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
beginMeshtext();
|
||||
#endif
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
_mqtt_enabled = CompanionMqttSetupPortal::loadEnabled();
|
||||
@@ -3412,6 +3555,14 @@ bool MyMesh::handleTxRoutingCommand(const char* command, char* reply, size_t rep
|
||||
if (parsed.set && policy != parsed.policy) {
|
||||
bool saved;
|
||||
if (parsed.channel) {
|
||||
const auto alias = parseCompanionChannelAlias(channel.name);
|
||||
if (isAliasRadio2Defined()
|
||||
&& (alias.kind == CompanionChannelKind::Radio2 || alias.kind == CompanionChannelKind::Meshtext)) {
|
||||
snprintf(reply, reply_size, "Error: channel prefix controls routing; rename it to change routing");
|
||||
return true;
|
||||
}
|
||||
// Persist the app PSK, not the resolved wire-key copy used for reporting.
|
||||
getChannelForSave(channel_index, channel);
|
||||
const ChannelDetails previous = channel;
|
||||
channel.channel.tx_radio = parsed.policy;
|
||||
saved = setChannel(channel_index, channel) && saveChannels();
|
||||
@@ -3460,6 +3611,9 @@ bool MyMesh::handleLocalControlCommand(const char* command, char* reply,
|
||||
}
|
||||
|
||||
if (handleTxRoutingCommand(command, reply, reply_size)) return true;
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
if (handleMeshtextCommand(command, reply, reply_size)) return true;
|
||||
#endif
|
||||
if (mesh::handleUsbLoggingWatchdogCommand(command, reply, reply_size)) return true;
|
||||
#if COMPANION_FEATURE_OTA_CLI
|
||||
const auto* ota_context = mesh::ota::ota_context_if_active();
|
||||
@@ -5733,7 +5887,16 @@ void MyMesh::handleCmdFrame(size_t len) {
|
||||
} else {
|
||||
ChannelDetails channel;
|
||||
bool success = getChannel(channel_idx, channel);
|
||||
if (success && sendGroupMessage(msg_timestamp, channel.channel, _prefs.node_name, text, len - i)) {
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
if (success && isMeshtextChannel(channel)) {
|
||||
if (sendMeshtextText(channel, text, len - i)) writeOKFrame();
|
||||
else writeErrFrame(ERR_CODE_BAD_STATE);
|
||||
} else
|
||||
#endif
|
||||
if (success && isAliasRadio2Defined()
|
||||
&& parseCompanionChannelAlias(channel.name).kind == CompanionChannelKind::Meshtext) {
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD);
|
||||
} else if (success && sendGroupMessage(msg_timestamp, channel.channel, _prefs.node_name, text, len - i)) {
|
||||
writeOKFrame();
|
||||
} else {
|
||||
writeErrFrame(ERR_CODE_NOT_FOUND); // bad channel_idx
|
||||
@@ -5777,6 +5940,13 @@ void MyMesh::handleCmdFrame(size_t len) {
|
||||
ChannelDetails channel;
|
||||
if (!getChannel(channel_idx, channel)) {
|
||||
writeErrFrame(ERR_CODE_NOT_FOUND); // bad channel_idx
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
} else if (isMeshtextChannel(channel)) {
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD); // channel text only, never raw/admin data
|
||||
#endif
|
||||
} else if (isAliasRadio2Defined()
|
||||
&& parseCompanionChannelAlias(channel.name).kind == CompanionChannelKind::Meshtext) {
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD);
|
||||
} else if (data_type == DATA_TYPE_RESERVED) {
|
||||
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
|
||||
} else if (payload_len > MAX_GROUP_DATA_LENGTH) {
|
||||
@@ -6250,6 +6420,11 @@ void MyMesh::handleCmdFrame(size_t len) {
|
||||
#if ENABLE_PRIVATE_KEY_IMPORT
|
||||
if (!mesh::LocalIdentity::validatePrivateKey(&cmd_frame[1])) {
|
||||
writeErrFrame(ERR_CODE_ILLEGAL_ARG); // invalid key
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
} else if (hasForeignRadioFrame()) {
|
||||
// Finish queued/sending MT text before changing its sender identity.
|
||||
writeErrFrame(ERR_CODE_BAD_STATE);
|
||||
#endif
|
||||
} else if (!flushContactsBeforeReboot()) {
|
||||
// loadContacts() resets the dirty-page map, so never replace the
|
||||
// identity and reload while newer contact state exists only in RAM.
|
||||
@@ -6259,6 +6434,9 @@ void MyMesh::handleCmdFrame(size_t len) {
|
||||
identity.readFrom(&cmd_frame[1], 64);
|
||||
if (_store->saveMainIdentity(identity)) {
|
||||
self_id = identity;
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
refreshMeshtextIdentity();
|
||||
#endif
|
||||
#if defined(ENABLE_OTA)
|
||||
mesh::ota::ota_refresh_seeder_identity(self_id.pub_key);
|
||||
#endif
|
||||
@@ -6447,36 +6625,99 @@ void MyMesh::handleCmdFrame(size_t len) {
|
||||
} else if (cmd_frame[0] == CMD_GET_CHANNEL && len >= 2) {
|
||||
uint8_t channel_idx = cmd_frame[1];
|
||||
ChannelDetails channel;
|
||||
if (getChannel(channel_idx, channel)) {
|
||||
if (getChannelForSave(channel_idx, channel)) {
|
||||
int i = 0;
|
||||
out_frame[i++] = RESP_CODE_CHANNEL_INFO;
|
||||
out_frame[i++] = channel_idx;
|
||||
strcpy((char *)&out_frame[i], channel.name);
|
||||
i += 32;
|
||||
memcpy(&out_frame[i], channel.channel.secret, 16);
|
||||
i += 16; // NOTE: only 128-bit supported
|
||||
size_t key_size = 16; // Original app response remains byte-for-byte compatible.
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
if (len >= 3 && cmd_frame[2] == 32 && isMeshtextChannel(channel)) key_size = 32;
|
||||
#endif
|
||||
memcpy(&out_frame[i], channel.channel.secret, key_size);
|
||||
i += key_size;
|
||||
_serial->writeFrame(out_frame, i);
|
||||
} else {
|
||||
writeErrFrame(ERR_CODE_NOT_FOUND);
|
||||
}
|
||||
} else if (cmd_frame[0] == CMD_SET_CHANNEL && len >= 2 + 32 + 32) {
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD); // not supported (yet)
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
const uint8_t channel_idx = cmd_frame[1];
|
||||
ChannelDetails previous{}, channel{};
|
||||
StrHelper::strncpy(channel.name, (char *)&cmd_frame[2], 32);
|
||||
memcpy(channel.channel.secret, &cmd_frame[2 + 32], sizeof(channel.channel.secret));
|
||||
if (parseCompanionChannelAlias(channel.name).kind != CompanionChannelKind::Meshtext) {
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD); // MeshCore wire protocol is still AES-128.
|
||||
return;
|
||||
}
|
||||
if (!validateMeshtextChannelChange(channel_idx, channel)) {
|
||||
writeErrFrame(ERR_CODE_BAD_STATE);
|
||||
return;
|
||||
}
|
||||
if (!getChannelForSave(channel_idx, previous) || !setChannel(channel_idx, channel)) {
|
||||
writeErrFrame(ERR_CODE_NOT_FOUND);
|
||||
} else if (!saveChannels()) {
|
||||
setChannel(channel_idx, previous);
|
||||
writeErrFrame(ERR_CODE_FILE_IO_ERROR);
|
||||
} else {
|
||||
_mt_seen.clear();
|
||||
_mt_profile_dirty = true;
|
||||
refreshMeshtextProfile();
|
||||
writeOKFrame();
|
||||
}
|
||||
#else
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD);
|
||||
#endif
|
||||
} else if (cmd_frame[0] == CMD_SET_CHANNEL && len >= 2 + 32 + 16) {
|
||||
uint8_t channel_idx = cmd_frame[1];
|
||||
ChannelDetails previous;
|
||||
ChannelDetails previous{};
|
||||
ChannelDetails channel;
|
||||
StrHelper::strncpy(channel.name, (char *)&cmd_frame[2], 32);
|
||||
memset(channel.channel.secret, 0, sizeof(channel.channel.secret));
|
||||
memcpy(channel.channel.secret, &cmd_frame[2 + 32], 16); // NOTE: only 128-bit supported
|
||||
const bool have_previous = getChannel(channel_idx, previous);
|
||||
const auto alias = parseCompanionChannelAlias(channel.name);
|
||||
if (alias.kind == CompanionChannelKind::Invalid) {
|
||||
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
|
||||
return;
|
||||
}
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
// Legacy apps fetch only 16 bytes. A label-only edit must not silently
|
||||
// truncate a provisioned MT AES-256 key to AES-128. An explicit 32-byte SET
|
||||
// (zero upper half for AES-128) is the unambiguous way to replace that key.
|
||||
if (alias.kind == CompanionChannelKind::Meshtext
|
||||
&& getChannelForSave(channel_idx, previous) && isMeshtextChannel(previous)
|
||||
&& !memcmp(channel.channel.secret, previous.channel.secret, 16)) {
|
||||
memcpy(channel.channel.secret + 16, previous.channel.secret + 16, 16);
|
||||
}
|
||||
if (!validateMeshtextChannelChange(channel_idx, channel)) {
|
||||
writeErrFrame(ERR_CODE_BAD_STATE); // Conflicting preset, manual RF, region or busy owner.
|
||||
return;
|
||||
}
|
||||
#else
|
||||
if (alias.kind == CompanionChannelKind::Meshtext && isAliasRadio2Defined()) {
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD); // Meshtext transport not implemented yet.
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
const bool have_previous = getChannelForSave(channel_idx, previous);
|
||||
ChannelDetails wire_channel = channel;
|
||||
ChannelDetails previous_wire_channel = previous;
|
||||
resolveChannelAlias(wire_channel);
|
||||
if (have_previous) resolveChannelAlias(previous_wire_channel);
|
||||
if (have_previous && channel.name[0] != 0 && previous.name[0] != 0
|
||||
&& !memcmp(channel.channel.secret, previous.channel.secret, sizeof(channel.channel.secret))) {
|
||||
&& !memcmp(wire_channel.channel.secret, previous_wire_channel.channel.secret, sizeof(channel.channel.secret))) {
|
||||
channel.channel.tx_radio = previous.channel.tx_radio;
|
||||
}
|
||||
if (!have_previous
|
||||
|| !setChannel(channel_idx, channel)) {
|
||||
writeErrFrame(ERR_CODE_NOT_FOUND); // bad channel_idx
|
||||
} else if (saveChannels()) {
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
_mt_seen.clear();
|
||||
_mt_profile_dirty = true;
|
||||
refreshMeshtextProfile();
|
||||
#endif
|
||||
writeOKFrame();
|
||||
} else {
|
||||
// AtomicFileWriter preserves the last complete channels file. Keep the
|
||||
@@ -8695,6 +8936,21 @@ void MyMesh::handleTerminalDisplayCommand(const char* arguments) {
|
||||
|
||||
void MyMesh::sendTerminalChannelMessage(ChannelDetails& channel,
|
||||
const char* text) {
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
if (isMeshtextChannel(channel)) {
|
||||
if (text && sendMeshtextText(channel, text, strlen(text))) {
|
||||
terminalOutput().printf(" Queued to %s (Meshtext).\r\n", channel.name);
|
||||
} else {
|
||||
terminalOutput().printf(" ERROR: %s; use get mt.status\r\n", _mt_status);
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (isAliasRadio2Defined()
|
||||
&& parseCompanionChannelAlias(channel.name).kind == CompanionChannelKind::Meshtext) {
|
||||
terminalOutput().print(" ERROR: Meshtext channel transport is not available\r\n");
|
||||
return;
|
||||
}
|
||||
const size_t prefix_len = strlen(_prefs.node_name) + 2; // "name: "
|
||||
const size_t max_text_len = prefix_len < MAX_TEXT_LEN
|
||||
? MAX_TEXT_LEN - prefix_len : 0;
|
||||
@@ -9570,6 +9826,20 @@ void MyMesh::handleTerminalCommand(char* command) {
|
||||
" board\r\n"
|
||||
" version\r\n");
|
||||
terminalOutput().print(" get storage.layout\r\n");
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
terminalOutput().print(" get mt.status / get mt.region / get mt.slot / get mt.hops\r\n"
|
||||
" get mt.node / get mt.shortname\r\n"
|
||||
" set mt.region <auto|us|eu868|eu433|anz>\r\n"
|
||||
" set mt.slot <0|1-based frequency slot>\r\n"
|
||||
" set mt.hops <0..7> (saved; default 7)\r\n"
|
||||
" set mt.node <!8hex|default> (saved; use a unique ID)\r\n"
|
||||
" set mt.shortname <1..4 UTF-8 characters|default> (saved; NodeInfo uses 4 bytes)\r\n"
|
||||
" send mt.nodeinfo <channel slot> (one manual advert; no automatic adverts)\r\n"
|
||||
" set mt.preset <longfast|shortturbo|...> (saved manual radio2)\r\n"
|
||||
" set mt.channel <slot> <mt-name> <32/64 hex digits|AQ==>\r\n"
|
||||
" get mt.channel <slot> / delete mt.channel <slot>\r\n"
|
||||
" Join #mt-longfast or #mt-shortturbo in the app for automatic radio2\r\n");
|
||||
#endif
|
||||
#if defined(NRF52_PLATFORM) && defined(EXTRAFS) && !defined(QSPIFLASH)
|
||||
terminalOutput().print(" get flash.health\r\n");
|
||||
#endif
|
||||
@@ -9830,6 +10100,12 @@ bool MyMesh::handleDirectCommand(const char* command, char* reply, size_t reply_
|
||||
stopContactsIterator();
|
||||
resetContacts();
|
||||
mesh::resetLazyPersistenceAfterSuccess(dirty_contacts_expiry, dirty_contacts_failures);
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
if (!eraseMeshtextSettings()) {
|
||||
snprintf(reply, reply_size, "File system erased; Error: MT settings erase failed");
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
snprintf(reply, reply_size, "File system erase: OK");
|
||||
} else {
|
||||
snprintf(reply, reply_size, "File system erase: Err");
|
||||
@@ -10369,6 +10645,12 @@ complete_line:
|
||||
}
|
||||
} else if (strcmp(cli_command, "erase") == 0) {
|
||||
bool success = _store->formatFileSystem();
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
if (success && !eraseMeshtextSettings()) {
|
||||
output.println(" Error: file system erased, but MT settings erase failed");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (success) {
|
||||
output.println(" > erase done");
|
||||
} else {
|
||||
@@ -10611,6 +10893,9 @@ void MyMesh::loop() {
|
||||
#endif
|
||||
|
||||
_radio_profiles.loop();
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
refreshMeshtextProfile();
|
||||
#endif
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
if (_mqtt_bridge) _mqtt_bridge->servicePendingClockCorrection();
|
||||
#endif
|
||||
|
||||
@@ -26,6 +26,10 @@
|
||||
|
||||
#include "DataStore.h"
|
||||
#include <helpers/RadioProfileCLI.h>
|
||||
#include <helpers/MeshtextConfig.h>
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
#include <helpers/CompanionMeshtextChannel.h>
|
||||
#endif
|
||||
#include <helpers/CompanionNotificationPolicy.h>
|
||||
#include "NodePrefs.h"
|
||||
|
||||
@@ -351,6 +355,37 @@ protected:
|
||||
uint8_t getExtraAckTransmitCount() const override;
|
||||
bool filterRecvFloodPacket(mesh::Packet* packet) override;
|
||||
bool allowPacketForward(const mesh::Packet* packet) override;
|
||||
bool allowGroupPacketOnRadio(mesh::Packet* packet) override;
|
||||
int searchChannelsForPacket(const mesh::Packet* packet, 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;
|
||||
bool isAliasRadio2Defined() const;
|
||||
bool resolveChannelAlias(ChannelDetails& channel) const;
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
void beginMeshtext();
|
||||
void refreshMeshtextIdentity();
|
||||
void refreshMeshtextProfile();
|
||||
bool isMeshtextChannel(const ChannelDetails& channel) const;
|
||||
bool validateMeshtextChannelChange(int index, const ChannelDetails& channel);
|
||||
bool handleMeshtextCommand(const char* command, char* reply, size_t capacity);
|
||||
bool saveMeshtextSettings(mesh::mt::Region region, uint32_t slot, uint8_t hops = UINT8_MAX,
|
||||
const char* short_name = nullptr, uint32_t node_override = UINT32_MAX);
|
||||
bool eraseMeshtextSettings();
|
||||
bool sendMeshtextText(const ChannelDetails& channel, const char* text, size_t length);
|
||||
bool sendMeshtextNodeInfo(const ChannelDetails& channel);
|
||||
bool onForeignRadioFrame(const uint8_t* bytes, size_t length, uint8_t profile,
|
||||
uint32_t generation, float snr, float rssi) override;
|
||||
void onForeignRadioSendComplete(bool success) override;
|
||||
bool allowForeignRadioTransmit() const override {
|
||||
return _radio_available && _scheduled_reboot_at == 0 && _mt_active;
|
||||
}
|
||||
mesh::mt::Region meshtextRegion() const;
|
||||
#endif
|
||||
bool queueChannelText(uint8_t index, const uint8_t secret[32], float snr,
|
||||
uint8_t path_len, uint32_t timestamp, const char* text,
|
||||
bool is_emergency_channel);
|
||||
int findChannelIdxForPacket(const mesh::GroupChannel& channel, const mesh::Packet* packet);
|
||||
bool allowFloodRetry(const mesh::Packet* packet) const override;
|
||||
uint8_t getFloodRetryMaxPathLength(const mesh::Packet* packet) const override;
|
||||
uint8_t getFloodRetryMaxAttempts(const mesh::Packet* packet) const override;
|
||||
@@ -436,13 +471,18 @@ protected:
|
||||
bool getContactForSave(uint32_t idx, ContactInfo& contact) override { return getContactByIdx(idx, contact); }
|
||||
ContactInfo* getContactForStore(uint32_t idx) override { return getContactPtrByIdx(idx); }
|
||||
void onContactCacheFlushed() override;
|
||||
bool onChannelLoaded(uint8_t channel_idx, const ChannelDetails& ch) override { return setChannel(channel_idx, ch); }
|
||||
bool getChannelForSave(uint8_t channel_idx, ChannelDetails& ch) override { return getChannel(channel_idx, ch); }
|
||||
// A legacy malformed alias must not quarantine every other saved channel.
|
||||
// The raw slot remains visible to the app so it can be renamed or deleted.
|
||||
bool onChannelLoaded(uint8_t channel_idx, const ChannelDetails& ch) override { return BaseChatMesh::setChannel(channel_idx, ch); }
|
||||
bool getChannelForSave(uint8_t channel_idx, ChannelDetails& ch) override { return BaseChatMesh::getChannel(channel_idx, ch); }
|
||||
|
||||
void clearPendingReqs();
|
||||
bool hasPendingReqs() const;
|
||||
|
||||
public:
|
||||
// Runtime copies use the wire PSK; storage/app keep the original alias PSK.
|
||||
bool getChannel(int channel_idx, ChannelDetails& channel);
|
||||
bool setChannel(int channel_idx, const ChannelDetails& channel);
|
||||
bool savePrefs() {
|
||||
const uint8_t previous_usb_debug = _prefs.usb_debug_enabled;
|
||||
_prefs.usb_debug_enabled = _prefs.usb_debug_enabled == 1 ? 1 : 0;
|
||||
@@ -647,6 +687,20 @@ private:
|
||||
|
||||
DataStore* _store;
|
||||
mesh::RadioProfileCLI _radio_profiles;
|
||||
#if MESH_ENABLE_MESHTEXT_CHAT
|
||||
mesh::mt::Region _mt_region = mesh::mt::Region::Unset;
|
||||
uint32_t _mt_slot = 0, _mt_node = 0, _mt_packet_id = 0;
|
||||
uint32_t _mt_default_node = 0, _mt_node_override = 0;
|
||||
char _mt_short_name[17] = {};
|
||||
uint8_t _mt_hop_limit = 7;
|
||||
uint32_t _mt_sent = 0, _mt_failed = 0, _mt_received = 0;
|
||||
mesh::mt::SeenPackets _mt_seen;
|
||||
bool _mt_settings_valid = true, _mt_active = false;
|
||||
bool _mt_profile_dirty = true, _mt_last_radio_ready = false;
|
||||
uint32_t _mt_primary_generation = 0, _mt_secondary_generation = 0, _mt_intent_generation = 0;
|
||||
float _mt_primary_frequency = 0;
|
||||
const char* _mt_status = "no Meshtext channels";
|
||||
#endif
|
||||
uint16_t _temp_radio_preamble = 0;
|
||||
CompanionNodePrefs _prefs;
|
||||
#if COMPANION_FEATURE_NOTIFICATIONS
|
||||
|
||||
Reference in new issue
Block a user