diff --git a/.github/workflows/run-unit-tests.yml b/.github/workflows/run-unit-tests.yml index 1b6ba6aa9..200abfe39 100644 --- a/.github/workflows/run-unit-tests.yml +++ b/.github/workflows/run-unit-tests.yml @@ -460,6 +460,7 @@ jobs: python3 -B test/test_companion_hardware_serial_gps.py -v python3 -B test/test_companion_contact_lifetime.py -v python3 -B test/test_contact_indices.py -v + python3 -B test/test_small_table_sort.py -v python3 -B test/test_companion_identity_cli.py -v python3 -B test/test_companion_packet_stream.py -v python3 -B test/test_companion_uncached_storage.py -v diff --git a/docs/radio_profiles.md b/docs/radio_profiles.md index c25f2ae64..d68ffd0fe 100644 --- a/docs/radio_profiles.md +++ b/docs/radio_profiles.md @@ -33,6 +33,30 @@ get radio2.status The tuple is **frequency in MHz, bandwidth in kHz, spreading factor, coding-rate denominator, mode, optional preamble in symbols**. `5` means coding rate 4/5. +When setting a saved, temporary or scheduled second profile, the response warns +if its occupied bandwidth overlaps the primary channel or comes within **6 kHz +of either edge**. This check uses both channel bandwidths, regardless of SF or +coding rate. The change still applies. The warning also appears when reading +the second profile, including while the switching timing self-test is pending. + +The warning suggests a nearby center frequency with the same bandwidth, SF and +coding rate that clears the buffer. It chooses the nearest whole-kHz frequency +on the requested side of the primary channel (above for identical centers), +checks the radio's supported frequency range, and tries the other side if +needed. The suggestion is not applied automatically. + +For example, with `radio` at **910.525 MHz / 62.5 kHz**, a second 62.5 kHz +profile at 910.550 MHz produces: + +```text +WARN radio2 overlaps radio1 (6kHz guard); try 910.594MHz +``` + +The center spacing must exceed `(primary bandwidth + second bandwidth) / 2 + +6 kHz`; touching that boundary also warns. Frequency comparisons use the stored +RF centers rounded to the nearest Hz. This is a channel-spacing aid; choose +the suggested frequency only when it also suits the network and local band plan. + | Mode | Receive on profile 2 | Transmit on profile 2 | | --- | --- | --- | | `rx` | Yes | Only infrastructure replies explicitly configured with `tx.reply ... force` | diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 875b3ec9e..ed79f8127 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #if defined(ESP32_PLATFORM) && defined(WIFI_SSID) @@ -956,7 +957,7 @@ static int sort_by_recent(const void *a, const void *b) { int MyMesh::getRecentlyHeard(AdvertPath dest[], int max_num) { if (max_num > ADVERT_PATH_TABLE_SIZE) max_num = ADVERT_PATH_TABLE_SIZE; - qsort(advert_paths, ADVERT_PATH_TABLE_SIZE, sizeof(advert_paths[0]), sort_by_recent); + mesh::sortSmallTable(advert_paths, ADVERT_PATH_TABLE_SIZE, sizeof(advert_paths[0]), sort_by_recent); for (int i = 0; i < max_num; i++) { dest[i] = advert_paths[i]; diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index 6b62e9feb..61d01feee 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -1068,8 +1069,8 @@ void BaseChatMesh::scanRecentContacts(int last_n, ContactVisitor* visitor) { sort_array[i] = i; } table = contacts; // pass via global *sigh* :-( - qsort(sort_array, num_contacts, sizeof(sort_array[0]), - cmp_contact_list_order); + mesh::sortSmallTable(sort_array, num_contacts, sizeof(sort_array[0]), + cmp_contact_list_order); if (last_n == 0) { last_n = num_contacts; // scan ALL diff --git a/src/helpers/RadioProfileCLI.cpp b/src/helpers/RadioProfileCLI.cpp index 9ca5d9c1e..edc554606 100644 --- a/src/helpers/RadioProfileCLI.cpp +++ b/src/helpers/RadioProfileCLI.cpp @@ -10,10 +10,24 @@ #include "radiolib/HilTxFrequencyGuard.h" #include #include +#include #include namespace mesh { namespace { +// Keep bounded response appends out of each formatter's call site. This +// shares their length/bounds/varargs setup on flash-constrained boards. +void appendReply(char* reply, size_t capacity, const char* format, ...) + __attribute__((format(printf, 3, 4))); +void appendReply(char* reply, size_t capacity, const char* format, ...) { + if (!capacity) return; + const size_t used = strlen(reply); + if (used >= capacity) return; + va_list args; + va_start(args, format); + vsnprintf(reply + used, capacity - used, format, args); + va_end(args); +} constexpr size_t ImageSize = 24; const char* const ImagePath = "/radio_profiles"; const char* const BackupPath = "/radio_profiles.bak"; @@ -55,6 +69,20 @@ uint32_t remainingMillis(uint32_t end, uint32_t now) { const int32_t remaining = (int32_t)(end - now); return remaining > 0 ? (uint32_t)remaining : 0; } +uint32_t frequencyHz(float mhz) { + return uint32_t(double(mhz) * 1000000.0 + 0.5); +} +uint32_t secondaryClearanceHz(const RadioProfiles& profiles) { + // Frequencies are MHz, bandwidths are kHz. Expand radio1's occupied + // bandwidth by 6 kHz on each side; SF and coding rate do not affect it. + return uint32_t((double(profiles.primary.bw) + double(profiles.secondary.params.bw)) + * 500.0 + 6000.5); +} +bool secondaryOverlaps(uint32_t primary_hz, float secondary_mhz, uint32_t clearance_hz) { + const uint32_t secondary_hz = frequencyHz(secondary_mhz); + return (primary_hz > secondary_hz ? primary_hz - secondary_hz : secondary_hz - primary_hz) + <= clearance_hz; +} } RadioProfileCLI::ImageReadResult RadioProfileCLI::readImage( @@ -600,8 +628,7 @@ bool RadioProfileCLI::parseSuffix(const char* input, unsigned fields, char* lega void RadioProfileCLI::appendPreamble(char* reply, size_t capacity, uint8_t profile) const { if (!capacity || !radio_ || !radio_->profiles()) return; - const size_t used = strlen(reply); - if (used < capacity) snprintf(reply + used, capacity - used, ",preamble=%u", + appendReply(reply, capacity, ",preamble=%u", (unsigned)radio_->profilePreamble(profile)); appendChirpWarning(reply, capacity, *radio_->profiles()); } @@ -612,8 +639,7 @@ void RadioProfileCLI::appendSavedPreamble(char* reply, size_t capacity, uint8_t preview.primary.sf = sf; preview.primary.bw = bw; preview.primary.preamble = primary_preamble_; if (preview.primary != radio_->profiles()->primary) preview.resetSwitchTest(); - const size_t used = strlen(reply); - if (used < capacity) snprintf(reply + used, capacity - used, ",preamble=%u%s", + appendReply(reply, capacity, ",preamble=%u%s", preview.preamble(0, rxPowerSavingPreambleForParams(sf, bw)), primary_preamble_ ? "" : " (auto)"); appendChirpWarning(reply, capacity, preview); } @@ -627,51 +653,88 @@ void RadioProfileCLI::appendPrimaryChirpWarning(char* reply, size_t capacity, ui appendChirpWarning(reply, capacity, preview); } +void RadioProfileCLI::appendSecondaryWarnings(char* reply, size_t capacity, + const RadioProfiles& preview) const { + if (!capacity) return; + if (preview.enabled() && RadioProfiles::valid(preview.primary) + && RadioProfiles::valid(preview.secondary.params)) { + const uint32_t primary_hz = frequencyHz(preview.primary.freq); + const uint32_t clearance_hz = secondaryClearanceHz(preview); + if (secondaryOverlaps(primary_hz, preview.secondary.params.freq, clearance_hz)) { + appendReply(reply, capacity, + "; WARN radio2 overlaps radio1 (6kHz guard)"); + const bool prefer_upper = preview.secondary.params.freq >= preview.primary.freq; + // Suggest the nearest whole-kHz center outside the guarded interval on + // the requested side. Verify the float round-trip and the radio's own + // frequency limits; fall back to the other side near a band limit. + for (unsigned side = 0; side < 2; ++side) { + const bool upper = prefer_upper != bool(side); + uint32_t khz = upper ? (primary_hz + clearance_hz) / 1000 + 1 + : (primary_hz - clearance_hz - 1) / 1000; + auto suggested = preview.secondary.params; + char frequency_text[12]; + bool clear = false; + for (unsigned step = 0; step < 2; ++step) { + snprintf(frequency_text, sizeof(frequency_text), "%lu.%03lu", + (unsigned long)(khz / 1000), (unsigned long)(khz % 1000)); + // Validate the exact displayed token through the command parser: + // its float rounding can differ from double-to-float conversion. + if (!cli::parseDecimalStrict(frequency_text, suggested.freq)) break; + clear = !secondaryOverlaps(primary_hz, suggested.freq, clearance_hz); + if (clear) break; + if (upper) ++khz; else --khz; + } + if (clear && radio_->validateProfile(suggested)) { + appendReply(reply, capacity, + "; try %sMHz", frequency_text); + break; + } + } + } + } + // The overlap warning must be visible even while retune timing is pending. + appendChirpWarning(reply, capacity, preview); +} + void RadioProfileCLI::appendChirpWarning(char* reply, size_t capacity, const RadioProfiles& preview) { if (!capacity || !preview.enabled()) return; if (!preview.recommendationReady()) { - const size_t used = strlen(reply); - if (used < capacity) snprintf(reply + used, capacity - used, + appendReply(reply, capacity, "; timing self-test pending; wait=%lus", (unsigned long)((preview.recommendationRemainingMs(millis()) + 999) / 1000)); return; } const auto timing = preview.chirpTiming(); if (!timing.valid) return; - size_t used = strlen(reply); const bool over_a = timing.preamble[0] > RadioProfiles::MaxAutomaticPreamble; const bool over_b = timing.preamble[1] > RadioProfiles::MaxAutomaticPreamble; - if (used < capacity && (over_a || over_b)) - snprintf(reply + used, capacity - used, "; WARN need >%u: %s", + if (over_a || over_b) + appendReply(reply, capacity, "; WARN need >%u: %s", unsigned(RadioProfiles::MaxAutomaticPreamble), over_a && over_b ? "radio,radio2" : over_a ? "radio" : "radio2"); - used = strlen(reply); const bool short_a = preview.primary.preamble && preview.primary.preamble < timing.preamble[0]; const bool short_b = preview.secondary.params.preamble && preview.secondary.params.preamble < timing.preamble[1]; const bool a = timing.preamble[0] > 32 || short_a, b = timing.preamble[1] > 32 || short_b; - if (used < capacity) { - if (a && b) snprintf(reply + used, capacity - used, "; WARN recommended preamble: radio=%.0f,radio2=%.0f", - timing.preamble[0], timing.preamble[1]); - else if (a || b) snprintf(reply + used, capacity - used, "; WARN recommended preamble: %s=%.0f", - a ? "radio" : "radio2", timing.preamble[a ? 0 : 1]); - } - used = strlen(reply); - if (used < capacity && (short_a || short_b)) - snprintf(reply + used, capacity - used, "; short override: %s", + if (a && b) appendReply(reply, capacity, "; WARN recommended preamble: radio=%.0f,radio2=%.0f", + timing.preamble[0], timing.preamble[1]); + else if (a || b) appendReply(reply, capacity, "; WARN recommended preamble: %s=%.0f", + a ? "radio" : "radio2", timing.preamble[a ? 0 : 1]); + if (short_a || short_b) + appendReply(reply, capacity, "; short override: %s", short_a && short_b ? "radio,radio2" : short_a ? "radio" : "radio2"); // Preamble-bearing acknowledgements and getters share this formatter. Only // report the measured allowance for the same calibrated profile pair; a new // pair still has to activate and complete its self-test after the first ACK. // Dedicated timing/status getters already include this value under their // existing switch/budget labels. - used = strlen(reply); - if (used < capacity && !strstr(reply, "switch=") && !strstr(reply, "budget=")) - snprintf(reply + used, capacity - used, "; switch=%luus", + if (!strstr(reply, "switch=") && !strstr(reply, "budget=")) + appendReply(reply, capacity, "; switch=%luus", (unsigned long)preview.switchBudgetUs()); } void RadioProfileCLI::formatConfig(char* reply, size_t capacity, const RadioProfileConfig& config, - bool temporary, uint32_t remaining, bool automatic) const { + bool temporary, uint32_t remaining, bool automatic, + const Schedule* schedule) const { if (config.mode == RadioProfileMode::Off) { snprintf(reply, capacity, "> off"); return; } RadioProfiles preview = *radio_->profiles(); preview.setSecondary(config, temporary); @@ -679,14 +742,16 @@ void RadioProfileCLI::formatConfig(char* reply, size_t capacity, const RadioProf snprintf(reply, capacity, "> %.3f,%.3f,%u,%u,%s,%u%s%s", config.params.freq, config.params.bw, config.params.sf, config.params.cr, modeName(config.mode), preamble, config.params.preamble ? "" : " (auto)", automatic ? "; auto-owned" : ""); - if (temporary) { + if (schedule) { + appendReply(reply, capacity, "; @%lu-%lu", + (unsigned long)schedule->start, (unsigned long)schedule->end); + } else if (temporary) { uint32_t minutes = (remaining + 59999UL) / 60000UL; - const size_t used = strlen(reply); - if (used < capacity) snprintf(reply + used, capacity - used, "; %lud%luh%lum (%lu min) left", + appendReply(reply, capacity, "; %lud%luh%lum (%lu min) left", (unsigned long)(minutes / 1440), (unsigned long)(minutes / 60 % 24), (unsigned long)(minutes % 60), (unsigned long)minutes); } - appendChirpWarning(reply, capacity, preview); + appendSecondaryWarnings(reply, capacity, preview); } bool RadioProfileCLI::handle(const char* command, char* reply, size_t capacity, bool remote_origin) { @@ -777,8 +842,7 @@ bool RadioProfileCLI::handle(const char* command, char* reply, size_t capacity, t.listen_us[0] / t.symbol_us[0], t.listen_us[1] / t.symbol_us[1], t.preamble[0], t.preamble[1], (unsigned long)t.switch_us, (unsigned long)t.loop_us, t.loop_us ? " (estimate)" : ""); - size_t used = strlen(reply); - if (used < capacity) snprintf(reply + used, capacity - used, "; test=%u/%u,%u/%u%s", + appendReply(reply, capacity, "; test=%u/%u,%u/%u%s", p.switch_test_samples[0], RadioProfiles::SwitchTestSamplesPerDirection, p.switch_test_samples[1], RadioProfiles::SwitchTestSamplesPerDirection, p.switchTestReady() ? " ready" : " pending"); @@ -824,8 +888,7 @@ bool RadioProfileCLI::handle(const char* command, char* reply, size_t capacity, (unsigned long)p.switches, (unsigned long)p.switch_failures, (unsigned long)p.longest_switch_us, radio_->profilePreamble(0), radio_->profilePreamble(1)); if (p.enabled()) { - size_t used = strlen(reply); - if (used < capacity) snprintf(reply + used, capacity - used, "; test=%u/%u,%u/%u%s budget=%luus", + appendReply(reply, capacity, "; test=%u/%u,%u/%u%s budget=%luus", p.switch_test_samples[0], RadioProfiles::SwitchTestSamplesPerDirection, p.switch_test_samples[1], RadioProfiles::SwitchTestSamplesPerDirection, p.recommendationReady() ? " ready" : " pending", (unsigned long)p.switchBudgetUs()); @@ -920,17 +983,14 @@ bool RadioProfileCLI::handle(const char* command, char* reply, size_t capacity, if (!index) { snprintf(reply, capacity, "> slots: "); for (unsigned i = 0; i < 4; ++i) if (schedules_[first+i].active) { - const size_t used = strlen(reply); - if (used < capacity) snprintf(reply + used, capacity-used, "%u@%lu ", i+1, (unsigned long)schedules_[first+i].start); + appendReply(reply, capacity, "%u@%lu ", i+1, (unsigned long)schedules_[first+i].start); } return true; } const auto& s = schedules_[first+index-1]; if (!s.active) snprintf(reply, capacity, "> off"); else { - formatConfig(reply, capacity, s.config, s.temporary, remainingMillis(s.end_ms, millis())); - const size_t used = strlen(reply); - if (used < capacity) snprintf(reply+used, capacity-used, "; @%lu-%lu", (unsigned long)s.start, (unsigned long)s.end); + formatConfig(reply, capacity, s.config, s.temporary, remainingMillis(s.end_ms, millis()), false, &s); } return true; } @@ -996,7 +1056,7 @@ bool RadioProfileCLI::handle(const char* command, char* reply, size_t capacity, noteSecondaryManualChange(); publish(); #endif snprintf(reply, capacity, "OK - %s %u queued, preamble=%u", key, i+1, preview.preamble(1, 32)); - appendChirpWarning(reply, capacity, preview); return true; + appendSecondaryWarnings(reply, capacity, preview); return true; } snprintf(reply, capacity, "Error: all four schedule slots occupied"); return true; } @@ -1030,7 +1090,7 @@ bool RadioProfileCLI::handle(const char* command, char* reply, size_t capacity, snprintf(reply, capacity, "OK - radio2 %s%s; preamble=%u", modeName(config.mode), remote_command_ ? " pending after reply" : "", preview.preamble(1, 32)); } else snprintf(reply, capacity, "Error: settings could not be saved"); - if (!strncmp(reply, "OK", 2)) appendChirpWarning(reply, capacity, preview); + if (!strncmp(reply, "OK", 2)) appendSecondaryWarnings(reply, capacity, preview); return true; } diff --git a/src/helpers/RadioProfileCLI.h b/src/helpers/RadioProfileCLI.h index f431e898c..9c69f757b 100644 --- a/src/helpers/RadioProfileCLI.h +++ b/src/helpers/RadioProfileCLI.h @@ -92,7 +92,9 @@ class RadioProfileCLI { bool secondaryRuntimeBusy() const; void noteSecondaryManualChange(); void formatConfig(char* reply, size_t capacity, const RadioProfileConfig& config, - bool temporary, uint32_t remaining_ms, bool automatic = false) const; + bool temporary, uint32_t remaining_ms, bool automatic = false, + const Schedule* schedule = nullptr) const; + void appendSecondaryWarnings(char* reply, size_t capacity, const RadioProfiles& preview) const; public: void begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool infrastructure_replies = false); void loop(); diff --git a/src/helpers/SmallTableSort.h b/src/helpers/SmallTableSort.h new file mode 100644 index 000000000..b68b69d1d --- /dev/null +++ b/src/helpers/SmallTableSort.h @@ -0,0 +1,63 @@ +#pragma once + +#include +#include + +namespace mesh { + +// Compact iterative heapsort avoids the general qsort implementation on +// flash-constrained STM32/nRF52 boards. It retains O(n log n) comparison work +// without recursion or dynamic storage, including the nRF52 contact tables. +// As with qsort, equally ranked items have no guaranteed tie order. +// Callers provide an array of count trivially-copyable objects of width bytes. +#if defined(STM32_PLATFORM) || defined(NRF52_PLATFORM) +#if defined(__GNUC__) && !defined(__clang__) +__attribute__((noinline, noclone)) +#elif defined(__clang__) +__attribute__((noinline)) +#endif +inline void sortSmallTable(void* items, size_t count, size_t width, + int (*compare)(const void*, const void*)) { + if (count < 2 || !width) return; + auto* bytes = static_cast(items); + const auto swapRecords = [width](unsigned char* first, unsigned char* second) { + for (size_t i = 0; i < width; ++i) { + const unsigned char temporary = first[i]; + first[i] = second[i]; + second[i] = temporary; + } + }; + size_t parent = count / 2, remaining = count; + for (;;) { + size_t root = 0; + if (parent) { + root = --parent; + } else { + if (--remaining == 0) return; + swapRecords(bytes, bytes + remaining * width); + } + // First build a maximum heap from its last parent toward the root, then + // move each maximum to the sorted suffix and restore the shortened heap. + while (root < remaining / 2) { + size_t child = root * 2 + 1; + auto* child_record = bytes + child * width; + if (child + 1 < remaining && compare(child_record, child_record + width) < 0) { + ++child; + child_record += width; + } + auto* root_record = bytes + root * width; + if (compare(root_record, child_record) >= 0) break; + swapRecords(root_record, child_record); + root = child; + } + } +} +#else +inline void sortSmallTable(void* items, size_t count, size_t width, + int (*compare)(const void*, const void*)) { + if (count < 2 || !width) return; + qsort(items, count, width, compare); +} +#endif + +} // namespace mesh diff --git a/test/fixtures/radio_profiles/cli_test.cpp b/test/fixtures/radio_profiles/cli_test.cpp index c0abcb5ea..782908610 100644 --- a/test/fixtures/radio_profiles/cli_test.cpp +++ b/test/fixtures/radio_profiles/cli_test.cpp @@ -217,6 +217,287 @@ static void relativeSchedules() { f.cmd("set tempradioat2 912.5,500,8,5,rx,+3,+1,80", false); } +static const char* spectrum_warning = "WARN radio2 overlaps radio1 (6kHz guard)"; + +static void expectSpectrumWarning(const char* reply, bool expected = true) { + if ((strstr(reply, spectrum_warning) != nullptr) != expected) { + fprintf(stderr, "spectrum warning expected=%d: %s\n", expected, reply); + assert(false); + } +} + +static float suggestedSpectrumFrequency(const char* reply) { + expectSpectrumWarning(reply); + const char* suggestion = strstr(reply, "; try "); + float frequency = 0; + assert(suggestion && sscanf(suggestion, "; try %fMHz", &frequency) == 1); + assert(std::isfinite(frequency)); + return frequency; +} + +static void spectrumOverlap() { + // The warning concerns occupied RF spectrum, regardless of SF/CR or which + // profile has the wider bandwidth. The 6 kHz guard is an edge-to-edge gap. + struct Case { float primary_bw, secondary_bw; const char* frequency; bool warn; }; + const Case cases[] = { + {62.5f, 62.5f, "909.500000", true}, + {62.5f, 62.5f, "909.550000", true}, + {62.5f, 62.5f, "909.450000", true}, + {125, 31.25f, "909.540000", true}, // radio2 contained in radio1 + {31.25f, 125, "909.500000", true}, // radio1 contained in radio2 + {62.5f, 62.5f, "909.562500", true}, // exact occupied-band edge touch + {62.5f, 62.5f, "909.437500", true}, + {62.5f, 62.5f, "909.565000", true}, // no band overlap; inside guard + {62.5f, 62.5f, "909.435000", true}, + {62.5f, 62.5f, "909.568500", true}, // configured decimal guard boundary + {62.5f, 62.5f, "909.431500", true}, + {62.5f, 62.5f, "909.570000", false}, + {62.5f, 62.5f, "909.430000", false}, + {125, 62.5f, "909.599750", true}, + {125, 62.5f, "909.400250", true}, + {125, 62.5f, "909.601000", false}, + {125, 62.5f, "909.399000", false}, + }; + for (const auto& c : cases) { + Fixture f; + f.radio.p.primary.bw = c.primary_bw; + char command[100]; + snprintf(command, sizeof(command), "set radio2 %s,%g,9,7,rx", + c.frequency, c.secondary_bw); + expectSpectrumWarning(f.cmd(command), c.warn); + // A warning cannot reject the request or alter the requested modem tuple. + f.advance(2000); + assert(f.radio.p.secondary.mode == mesh::RadioProfileMode::Rx); + assert(f.radio.p.secondary.params.bw == c.secondary_bw); + assert(f.radio.p.secondary.params.sf == 9 && f.radio.p.secondary.params.cr == 7); + expectSpectrumWarning(f.cmd("get radio2"), c.warn); + if (c.warn) { + const float suggestion = suggestedSpectrumFrequency(f.reply); + const float requested = strtof(c.frequency, nullptr); + if (requested < f.radio.p.primary.freq) assert(suggestion < f.radio.p.primary.freq); + else assert(suggestion > f.radio.p.primary.freq); // same center chooses above + snprintf(command, sizeof(command), "set radio2 %.3f,%g,9,7,rx", suggestion, c.secondary_bw); + expectSpectrumWarning(f.cmd(command), false); // suggested tuple must be safe + f.advance(2000); + assert(f.radio.p.secondary.params.bw == c.secondary_bw); + assert(f.radio.p.secondary.params.sf == 9 && f.radio.p.secondary.params.cr == 7); + } else assert(!strstr(f.reply, "; try ")); + } + // These actual float centers round to exactly 17,700 Hz of separation. + // Together with supported 7.8/15.6 kHz bandwidths, they exercise equality + // under the same units users set, rather than a decimal approximation. + for (bool below : {false, true}) for (bool outside : {false, true}) { + Fixture f; + f.radio.p.primary.freq = 900.0f; f.radio.p.primary.bw = 7.8f; + float frequency = below ? 899.9822998046875f : 900.0177001953125f; + if (outside) frequency = std::nextafter(frequency, below ? -INFINITY : INFINITY); + const auto distance = llabs(llround(double(frequency) * 1000000) - 900000000LL); + assert(distance == (outside ? 17761 : 17700)); + char command[100]; + snprintf(command, sizeof(command), "set radio2 %.9f,15.6,9,7,rx", frequency); + expectSpectrumWarning(f.cmd(command), !outside); + } + // 10.4f is slightly below decimal 10.4: truncating the computed guard + // would lose a whole Hz and miss this inclusive 42,450 Hz boundary. + for (bool outside : {false, true}) { + Fixture f; + f.radio.p.primary.freq = 150.0f; f.radio.p.primary.bw = 10.4f; + float frequency = 150.042449951171875f; + if (outside) frequency = std::nextafter(frequency, INFINITY); + assert(llround(double(frequency) * 1000000) - 150000000LL + == (outside ? 42465 : 42450)); + // The compact CLI decimal parser accumulates digits in float. These + // short inputs produce the exact stored floats whose boundary we test. + const char* text = outside ? "150.04247" : "150.04246"; + float parsed = 0; + assert(mesh::cli::parseDecimalStrict(text, parsed) && parsed == frequency); + char command[100]; + snprintf(command, sizeof(command), "set radio2 %s,62.5,8,7,rx", text); + expectSpectrumWarning(f.cmd(command), !outside); + } + { + Fixture f; + f.radio.p.primary.freq = 910.525f; + f.cmd("set radio2 910.525,62.5,8,7,rx"); + assert(strstr(f.reply, "; try 910.594MHz")); + expectSpectrumWarning(f.cmd("set radio2 910.594,62.5,8,7,rx"), false); + } + for (const char* frequency : {"909.500000", "909.450000"}) { + Fixture f; + char command[100]; + snprintf(command, sizeof(command), "set radio2 %s,62.5,8,7,rx", frequency); + f.cmd(command); + // Nearest outward whole-kHz center, with the requested bandwidth retained. + assert(strstr(f.reply, !strcmp(frequency, "909.500000") + ? "; try 909.569MHz" : "; try 909.431MHz")); + } + { + Fixture f; + f.radio.p.primary.bw = 125; + f.cmd("set radio2 909.540,31.25,8,7,rx"); + assert(strstr(f.reply, "; try 909.585MHz")); + } + { + Fixture f; + f.radio.p.primary.freq = 150.01f; f.radio.p.primary.bw = 31.25f; + f.cmd("set radio2 150.001,31.25,8,7,rx"); + const float frequency = suggestedSpectrumFrequency(f.reply); + assert(frequency > f.radio.p.primary.freq); // lower suggestion violates 150 MHz limit + char command[100]; + snprintf(command, sizeof(command), "set radio2 %.3f,31.25,8,7,rx", frequency); + expectSpectrumWarning(f.cmd(command), false); + } + { + Fixture f; + expectSpectrumWarning(f.cmd("set radio2 909.5,62.5,7,5,rxtx")); + const char* warning = strstr(f.reply, spectrum_warning); + const char* timing = strstr(f.reply, "timing self-test pending"); + assert(timing && warning < timing); // warning is visible before calibration + assert(!f.radio.p.enabled()); // the normal 2-second delay is retained + expectSpectrumWarning(f.cmd("get radio2")); + f.advance(2000); + f.finishTimingTest(); + expectSpectrumWarning(f.cmd("get radio2")); + assert(!strstr(f.reply, "timing self-test pending")); + expectSpectrumWarning(f.cmd("set radio2 off"), false); + expectSpectrumWarning(f.cmd("get radio2"), false); + expectSpectrumWarning(f.cmd("set radio2 909.5,63,7,5,rx", false), false); + expectSpectrumWarning(f.cmd("set radio2 909.5,62.5,13,5,rx", false), false); + } + { + Fixture f; + f.fs.fail_write = true; + expectSpectrumWarning(f.cmd("set radio2 909.5,62.5,7,5,rx", false), false); + assert(!f.radio.p.enabled()); + assert(!f.cli.hasReplyMutation()); + } + { + Fixture f; + f.cmd("set radio2 911.5,62.5,7,5,rx"); f.advance(2000); + expectSpectrumWarning(f.cmd("set tempradio2 909.5,62.5,8,7,rxtx,1")); + assert(f.radio.p.secondary.params.freq == 911.5f); + expectSpectrumWarning(f.cmd("get tempradio2")); // pending temporary tuple + expectSpectrumWarning(f.cmd("get radio2"), false); // saved tuple is separate + f.advance(2000); + expectSpectrumWarning(f.cmd("get tempradio2")); + assert(f.radio.p.secondary_temporary); + assert(f.radio.p.secondary.params.freq == 909.5f); + f.advance(60000); + expectSpectrumWarning(f.cmd("get tempradio2"), false); + assert(f.radio.p.secondary.params.freq == 911.5f); + } + for (bool temporary : {false, true}) { + Fixture f; + const char* set = temporary ? "set tempradioat2 909.5,62.5,8,7,rx,+1,+2" + : "set radioat2 909.5,62.5,8,7,rx,+1"; + const char* get = temporary ? "get tempradioat2 1" : "get radioat2 1"; + expectSpectrumWarning(f.cmd(set)); + expectSpectrumWarning(f.cmd(get)); + if (temporary) { + char reply[158]; memset(reply, 0x55, sizeof(reply)); + assert(f.cli.handle(get, reply, 157)); + assert(static_cast(reply[157]) == 0x55); + assert(memchr(reply, 0, 157)); + char timestamps[48]; + snprintf(timestamps, sizeof(timestamps), "@%lu-%lu", + (unsigned long)(f.clock.epoch + 60), + (unsigned long)(f.clock.epoch + 120)); + assert(strstr(reply, timestamps)); + expectSpectrumWarning(reply); + assert(strstr(reply, "; try 909.569MHz")); + } + assert(!f.radio.p.enabled()); // warning does not activate a future schedule + f.advance(60000); + assert(f.radio.p.secondary.params.freq == 909.5f); + assert(f.radio.p.secondary_temporary == temporary); + } + for (bool temporary : {false, true}) for (bool delivered : {false, true}) { + Fixture f(true); + f.cmd("set radio2 911.5,62.5,7,5,rx"); f.advance(2000); + const auto saved = f.fs.files["/radio_profiles"]; + f.cli.beginReplyCommand(); + expectSpectrumWarning(f.cmd(temporary ? "set tempradio2 909.5,62.5,8,7,rxtx,1" + : "set radio2 909.5,62.5,8,7,rxtx")); + f.cli.endReplyCommand(); + assert(f.cli.hasReplyMutation()); + assert(f.fs.files["/radio_profiles"] == saved); + f.advance(2000); + assert(f.radio.p.secondary.params.freq == 911.5f); // old RF retained for ACK + assert(f.cli.finishReplyMutation(delivered)); + f.advance(1); + assert(!f.cli.hasReplyMutation()); + assert(f.radio.p.secondary.params.freq == (delivered ? 909.5f : 911.5f)); + } +#if MESH_ENABLE_FLEET_CONTROL + for (bool temporary : {false, true}) { + Fixture f(true); + f.cli.beginReplyCommand(); + expectSpectrumWarning(f.cmd(temporary ? "set tempradioat2 909.5,62.5,8,7,rx,+1,+2" + : "set radioat2 909.5,62.5,8,7,rx,+1")); + f.cli.endReplyCommand(); + assert(f.cli.hasReplyMutation()); + const char* get = temporary ? "get tempradioat2 1" : "get radioat2 1"; + assert(!strcmp(f.cmd(get), "> off")); + assert(f.cli.finishReplyMutation(true)); f.advance(1); + expectSpectrumWarning(f.cmd(get)); + assert(!f.radio.p.enabled()); + f.advance(60000); + assert(f.radio.p.secondary.params.freq == 909.5f); + } +#endif + { + Fixture f; + f.cmd("set radio2 909.5,62.5,8,7,rxtx"); + // The public command path must retain terminators and adjacent bytes for + // every response budget, including the CLI's tagged 157-byte response. + for (size_t capacity = 0; capacity <= 160; ++capacity) { + char bytes[162]; memset(bytes, 0x55, sizeof(bytes)); + assert(f.cli.handle("get radio2", bytes, capacity)); + assert(static_cast(bytes[capacity]) == 0x55); + if (capacity) assert(memchr(bytes, 0, capacity)); + if (capacity >= 157) expectSpectrumWarning(bytes); + } + } + // Reapply the literal displayed suggestion through the actual CLI parser. + // Its float fractional accumulation can differ from a double-to-float + // candidate, especially at high frequencies and near whole-kHz edges. + unsigned roundtrips = 0; + for (float base : {150.125f, 433.125f, 868.125f, 915.125f, 2400.125f}) { + for (float bw : {7.8f, 10.4f, 15.6f, 20.8f, 31.25f, + 41.7f, 62.5f, 125.0f, 250.0f, 500.0f}) { + for (float offset : {0.0f, 0.000001f, 0.000499f, 0.000500f, 0.000501f, + 0.000999f, 0.001000f, 0.001001f, 0.001499f, 0.001501f}) { + for (bool below : {false, true}) { + Fixture f; + f.radio.p.primary.freq = base + offset; + f.radio.p.primary.bw = bw; + const float requested = f.radio.p.primary.freq + (below ? -0.001f : 0.001f); + char command[100]; + snprintf(command, sizeof(command), "set radio2 %.6f,%g,7,5,rx", requested, bw); + expectSpectrumWarning(f.cmd(command)); + const char* begin = strstr(f.reply, "; try "); + assert(begin); begin += strlen("; try "); + const char* end = strstr(begin, "MHz"); + assert(end && end - begin < 24); + char suggestion[24]; + memcpy(suggestion, begin, end - begin); suggestion[end - begin] = 0; + const char* decimal = strchr(suggestion, '.'); + assert(decimal && strlen(decimal + 1) == 3); + snprintf(command, sizeof(command), "set radio2 %s,%g,7,5,rx", suggestion, bw); + f.cmd(command); + if (strstr(f.reply, spectrum_warning)) { + fprintf(stderr, "Unsafe suggestion: primary=%.12f BW=%g requested=%.12f suggestion=%s\n", + f.radio.p.primary.freq, bw, requested, suggestion); + } + expectSpectrumWarning(f.reply, false); + ++roundtrips; + } + } + } + } + assert(roundtrips == 1000); +} + int main(int argc, char** argv) { if (argc == 2) { if (!strcmp(argv[1], "permanent_order")) permanentScheduleOrder(); @@ -226,6 +507,7 @@ int main(int argc, char** argv) { else if (!strcmp(argv[1], "schedule_permutations")) schedulePermutations(); else if (!strcmp(argv[1], "storage_isolation")) storageBackoffDoesNotDelayTemporaryWindows(); else if (!strcmp(argv[1], "relative_schedule")) relativeSchedules(); + else if (!strcmp(argv[1], "spectrum_overlap")) spectrumOverlap(); else return 2; return 0; } diff --git a/test/test_contact_indices.py b/test/test_contact_indices.py index 5e7f9ec02..9a320db8f 100644 --- a/test/test_contact_indices.py +++ b/test/test_contact_indices.py @@ -27,6 +27,7 @@ HARNESS = r''' #include #include #include +#include #define MESH_DEBUG_PRINTLN(...) ((void)0) @INDEX_TYPES@ struct ContactInfo { diff --git a/test/test_radio_profile_cli.py b/test/test_radio_profile_cli.py index 92f2ed90c..94a73846e 100644 --- a/test/test_radio_profile_cli.py +++ b/test/test_radio_profile_cli.py @@ -27,7 +27,7 @@ class RadioProfileCLITest(unittest.TestCase): '-o', str(exe)], check=True, capture_output=True, text=True) for scenario in (None, 'permanent_order', 'temporary_boundary', 'lease_creation', 'save_failure', 'schedule_permutations', 'storage_isolation', - 'relative_schedule'): + 'relative_schedule', 'spectrum_overlap'): with self.subTest(fleet=fleet_enabled, carrier=carrier_enabled, scenario=scenario or 'commands_persistence_and_timers'): subprocess.run([str(exe)] + ([scenario] if scenario else []), check=True) diff --git a/test/test_radio_profile_spiffs_missing.py b/test/test_radio_profile_spiffs_missing.py index ca3e16e05..bb83a093e 100644 --- a/test/test_radio_profile_spiffs_missing.py +++ b/test/test_radio_profile_spiffs_missing.py @@ -35,6 +35,7 @@ HARNESS = r''' #include #include #include +#include struct State { bool present = false, directory = false, metadata_error = false; diff --git a/test/test_small_table_sort.py b/test/test_small_table_sort.py new file mode 100644 index 000000000..de868fb17 --- /dev/null +++ b/test/test_small_table_sort.py @@ -0,0 +1,171 @@ +#!/usr/bin/env python3 +"""Exercise the production small-table sorter and its platform fallback.""" +from pathlib import Path +import os +import shutil +import subprocess +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] + +HARNESS = r''' +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct Contact { + uint8_t flags; + uint32_t timestamp; +}; +static std::array contacts; +static size_t comparisons; +static int compareIndexes(const void* a, const void* b) { + ++comparisons; + const Contact& left = contacts[*static_cast(a)]; + const Contact& right = contacts[*static_cast(b)]; + return mesh::compareContactListOrder(left.flags, left.timestamp, + right.flags, right.timestamp); +} +struct Advert { + uint8_t public_key[8]; + char name[32]; + uint32_t timestamp; + uint8_t path[64]; + uint16_t identity; +}; +static_assert(sizeof(uint16_t) == 2, "production compact contact index"); +static_assert(sizeof(Advert) > 100, "exercise wide bytewise record swaps"); +static int compareAdverts(const void* a, const void* b) { + ++comparisons; + const auto left = static_cast(a)->timestamp; + const auto right = static_cast(b)->timestamp; + return left > right ? -1 : left < right ? 1 : 0; +} +// Defined in a second translation unit: both consumers must share the inline +// header's single STM32/nRF52 function body and compatible callback signature. +void sortElsewhere(void*, size_t, size_t, int (*)(const void*, const void*)); + +static void checkComparisonBound(size_t count) { + size_t height = 0; + for (size_t level = count; level > 1; level = (level + 1) / 2) ++height; + // Allow two comparisons per heap level and linear heap construction. This + // also admits the platform qsort fallback without fixing one comparison + // sequence or the unspecified tie order. Quadratic selection sort fails + // this bound on both 108- and 358-entry tables. + assert(comparisons <= count * (2 * height + 2)); +} + +static void checkIndexes(size_t count, unsigned order, std::mt19937& random) { + std::vector indexes(count); + std::iota(indexes.begin(), indexes.end(), uint16_t(0)); + std::sort(indexes.begin(), indexes.end(), [](uint16_t a, uint16_t b) { + return compareIndexes(&a, &b) < 0; + }); + if (order == 1) std::reverse(indexes.begin(), indexes.end()); + else if (order == 2) std::shuffle(indexes.begin(), indexes.end(), random); + comparisons = 0; + mesh::sortSmallTable(indexes.data(), count, sizeof(indexes[0]), compareIndexes); + checkComparisonBound(count); + std::vector seen(count); + for (size_t i = 0; i < count; ++i) { + assert(indexes[i] < count && !seen[indexes[i]]); + seen[indexes[i]] = true; + if (i) assert(compareIndexes(&indexes[i - 1], &indexes[i]) <= 0); + } +} +static void checkAdverts(size_t count, unsigned order, std::mt19937& random) { + std::vector original(count), records(count); + for (size_t i = 0; i < count; ++i) { + // Include object padding in the preservation check. Repeated timestamps + // intentionally leave equal-key ordering unspecified, matching qsort. + std::memset(&original[i], static_cast(i ^ 0xa5), sizeof(Advert)); + original[i].identity = static_cast(i); + original[i].timestamp = static_cast((i * 7) % 5); + records[i] = original[i]; + } + std::sort(records.begin(), records.end(), [](const Advert& a, const Advert& b) { + return compareAdverts(&a, &b) < 0; + }); + if (order == 1) std::reverse(records.begin(), records.end()); + else if (order == 2) std::shuffle(records.begin(), records.end(), random); + comparisons = 0; + sortElsewhere(records.data(), count, sizeof(Advert), compareAdverts); + checkComparisonBound(count); + std::vector seen(count); + for (size_t i = 0; i < count; ++i) { + const auto identity = records[i].identity; + assert(identity < count && !seen[identity]); + seen[identity] = true; + assert(std::memcmp(&records[i], &original[identity], sizeof(Advert)) == 0); + if (i) assert(compareAdverts(&records[i - 1], &records[i]) <= 0); + } +} +int main() { + std::mt19937 random(0x1a180001); + mesh::sortSmallTable(nullptr, 0, sizeof(uint16_t), compareIndexes); + uint16_t untouched = 0x1234; + mesh::sortSmallTable(&untouched, contacts.size(), 0, compareIndexes); + assert(untouched == 0x1234); + for (unsigned pattern = 0; pattern < 3; ++pattern) { + for (size_t i = 0; i < contacts.size(); ++i) { + contacts[i].flags = pattern == 0 ? 0 : static_cast(i % 3 == 0); + contacts[i].timestamp = pattern == 2 ? 42 : static_cast((i * 7919) % 31); + } + for (size_t count : {0U, 1U, 2U, 16U, 100U, 108U, 350U, 358U}) { + for (unsigned order = 0; order < 3; ++order) { + checkIndexes(count, order, random); + checkAdverts(count, order, random); + } + } + } + puts("PASS: compact indexes, wide records, duplicate keys, order and permutation"); +} +''' + +SECOND_TRANSLATION_UNIT = r''' +#include +void sortElsewhere(void* items, size_t count, size_t width, + int (*compare)(const void*, const void*)) { + mesh::sortSmallTable(items, count, width, compare); +} +''' + + +class SmallTableSortTests(unittest.TestCase): + def test_sort_on_all_platforms(self): + compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++") + self.assertIsNotNone(compiler, "a host C++17 compiler is required") + for platform in (None, "STM32_PLATFORM", "NRF52_PLATFORM", "ESP32_PLATFORM", "RP2040_PLATFORM"): + with self.subTest(platform=platform or "native"): + with tempfile.TemporaryDirectory(prefix="small-table-sort-") as directory: + work = Path(directory) + (work / "test.cpp").write_text(HARNESS) + (work / "second.cpp").write_text(SECOND_TRANSLATION_UNIT) + binary = work / "sort" + command = [compiler, "-std=c++17", "-O2", "-Wall", "-Wextra", "-Werror", + f"-I{ROOT / 'src'}", str(work / "test.cpp"), + str(work / "second.cpp"), "-o", str(binary)] + if platform: + command.insert(1, f"-D{platform}=1") + if sys.platform.startswith("linux"): + command[1:1] = ["-fsanitize=address,undefined", "-fno-sanitize-recover=all", + "-fno-pie", "-no-pie"] + built = subprocess.run(command, capture_output=True, text=True, timeout=60) + self.assertEqual(built.returncode, 0, built.stdout + built.stderr) + tested = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10) + self.assertEqual(tested.returncode, 0, tested.stdout + tested.stderr) + self.assertIn("duplicate keys, order and permutation", tested.stdout) + + +if __name__ == "__main__": + unittest.main()