Add recent repeater prefix search
This commit is contained in:
1 parent
6ddbaf3274
commit
81ff965c7c
10 files changed
+396
-108
No files matched your search
@@ -23,6 +23,123 @@ enum class NoArgCommandMatch : uint8_t {
|
||||
HasArguments,
|
||||
};
|
||||
|
||||
enum class RecentRepeaterGetMatch : uint8_t {
|
||||
NoMatch = 0,
|
||||
Valid,
|
||||
Invalid,
|
||||
};
|
||||
|
||||
struct RecentRepeaterGetQuery {
|
||||
int page;
|
||||
uint8_t search_prefix[3];
|
||||
uint8_t search_prefix_len;
|
||||
};
|
||||
|
||||
inline const char* skipRecentRepeaterSpaces(const char* text) {
|
||||
while (text != nullptr && (*text == ' ' || *text == '\t')) text++;
|
||||
return text;
|
||||
}
|
||||
|
||||
inline int recentRepeaterHexNibble(char c) {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline bool parseRecentRepeaterPage(const char* text, int& page) {
|
||||
text = skipRecentRepeaterSpaces(text);
|
||||
if (text == nullptr || *text == 0) {
|
||||
page = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t parsed = 0;
|
||||
bool saw_digit = false;
|
||||
while (*text >= '0' && *text <= '9') {
|
||||
saw_digit = true;
|
||||
const uint8_t digit = static_cast<uint8_t>(*text++ - '0');
|
||||
// The formatter clamps the requested page to the available page count.
|
||||
// Saturating here avoids signed overflow from hostile remote CLI input.
|
||||
if (parsed <= 65535UL) {
|
||||
parsed = parsed * 10UL + digit;
|
||||
if (parsed > 65535UL) parsed = 65535UL;
|
||||
}
|
||||
}
|
||||
text = skipRecentRepeaterSpaces(text);
|
||||
if (!saw_digit || *text != 0) return false;
|
||||
page = parsed == 0 ? 1 : static_cast<int>(parsed);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void formatRecentRepeaterAge(char* output, size_t output_size,
|
||||
uint32_t age_seconds) {
|
||||
if (output == nullptr || output_size == 0) return;
|
||||
if (age_seconds >= 3600UL) {
|
||||
snprintf(output, output_size, "%luh",
|
||||
static_cast<unsigned long>(age_seconds / 3600UL));
|
||||
} else if (age_seconds >= 60UL) {
|
||||
snprintf(output, output_size, "%lum",
|
||||
static_cast<unsigned long>(age_seconds / 60UL));
|
||||
} else {
|
||||
snprintf(output, output_size, "%lus",
|
||||
static_cast<unsigned long>(age_seconds));
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the portion after `get `. Besides the existing list/page forms, this
|
||||
// accepts `recent.repeaters search <2|4|6 hex> [page [N]|N]`.
|
||||
inline RecentRepeaterGetMatch parseRecentRepeaterGet(
|
||||
const char* config, RecentRepeaterGetQuery& query) {
|
||||
query.page = 1;
|
||||
memset(query.search_prefix, 0, sizeof(query.search_prefix));
|
||||
query.search_prefix_len = 0;
|
||||
if (config == nullptr || strncmp(config, "recent.repeater", 15) != 0) {
|
||||
return RecentRepeaterGetMatch::NoMatch;
|
||||
}
|
||||
|
||||
const char* cursor = config + 15;
|
||||
if (*cursor == 's') cursor++;
|
||||
if (*cursor != 0 && *cursor != ' ' && *cursor != '\t') {
|
||||
return RecentRepeaterGetMatch::NoMatch;
|
||||
}
|
||||
cursor = skipRecentRepeaterSpaces(cursor);
|
||||
if (*cursor == 0) return RecentRepeaterGetMatch::Valid;
|
||||
|
||||
if (strncmp(cursor, "search", 6) == 0
|
||||
&& (cursor[6] == 0 || cursor[6] == ' ' || cursor[6] == '\t')) {
|
||||
cursor = skipRecentRepeaterSpaces(cursor + 6);
|
||||
const char* hex = cursor;
|
||||
while (*cursor != 0 && *cursor != ' ' && *cursor != '\t') cursor++;
|
||||
const size_t hex_len = static_cast<size_t>(cursor - hex);
|
||||
if (hex_len != 2 && hex_len != 4 && hex_len != 6) {
|
||||
return RecentRepeaterGetMatch::Invalid;
|
||||
}
|
||||
for (size_t i = 0; i < hex_len; i += 2) {
|
||||
const int hi = recentRepeaterHexNibble(hex[i]);
|
||||
const int lo = recentRepeaterHexNibble(hex[i + 1]);
|
||||
if (hi < 0 || lo < 0) return RecentRepeaterGetMatch::Invalid;
|
||||
query.search_prefix[i / 2] = static_cast<uint8_t>((hi << 4) | lo);
|
||||
}
|
||||
query.search_prefix_len = static_cast<uint8_t>(hex_len / 2);
|
||||
|
||||
cursor = skipRecentRepeaterSpaces(cursor);
|
||||
if (strncmp(cursor, "page", 4) == 0
|
||||
&& (cursor[4] == 0 || cursor[4] == ' ' || cursor[4] == '\t')) {
|
||||
cursor = skipRecentRepeaterSpaces(cursor + 4);
|
||||
}
|
||||
return parseRecentRepeaterPage(cursor, query.page)
|
||||
? RecentRepeaterGetMatch::Valid : RecentRepeaterGetMatch::Invalid;
|
||||
}
|
||||
|
||||
if (strncmp(cursor, "page", 4) == 0
|
||||
&& (cursor[4] == 0 || cursor[4] == ' ' || cursor[4] == '\t')) {
|
||||
cursor = skipRecentRepeaterSpaces(cursor + 4);
|
||||
}
|
||||
return parseRecentRepeaterPage(cursor, query.page)
|
||||
? RecentRepeaterGetMatch::Valid : RecentRepeaterGetMatch::Invalid;
|
||||
}
|
||||
|
||||
inline StandaloneWiFiKey classifyStandaloneWiFiGet(const char* config) {
|
||||
if (config == nullptr) return StandaloneWiFiKey::None;
|
||||
if (strcmp(config, "wifi.ssid") == 0) return StandaloneWiFiKey::SSID;
|
||||
|
||||
+10
-28
@@ -158,31 +158,6 @@ static bool otaCommandNeedsTempRadio(const char* command) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool parseRecentRepeaterGet(const char* config, int& page) {
|
||||
if (strncmp(config, "recent.repeater", 15) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* cursor = &config[15];
|
||||
if (*cursor == 's') {
|
||||
cursor++;
|
||||
}
|
||||
if (*cursor != 0 && *cursor != ' ') {
|
||||
return false;
|
||||
}
|
||||
|
||||
while (*cursor == ' ') cursor++;
|
||||
if (strncmp(cursor, "page", 4) == 0 && (cursor[4] == 0 || cursor[4] == ' ')) {
|
||||
cursor += 4;
|
||||
while (*cursor == ' ') cursor++;
|
||||
}
|
||||
|
||||
page = 1;
|
||||
if (*cursor) page = _atoi(cursor);
|
||||
if (page < 1) page = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool isValidName(const char *n) {
|
||||
while (*n) {
|
||||
if (*n == '[' || *n == ']' || *n == '/' || *n == '\\' || *n == ':' || *n == ',' || *n == '?' || *n == '*') return false;
|
||||
@@ -4506,7 +4481,9 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
strcpy(reply, "Error, retry configuration unsupported on this role");
|
||||
return;
|
||||
}
|
||||
int recent_page = 1;
|
||||
mesh::cli::RecentRepeaterGetQuery recent_query;
|
||||
const mesh::cli::RecentRepeaterGetMatch recent_query_match =
|
||||
mesh::cli::parseRecentRepeaterGet(config, recent_query);
|
||||
if (memcmp(config, "dutycycle", 9) == 0) {
|
||||
float dc = 100.0f / (_prefs->airtime_factor + 1.0f);
|
||||
int dc_int = (int)dc;
|
||||
@@ -4677,8 +4654,13 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
formatSnrDbX4(cr8, sizeof(cr8), _prefs->direct_retry_cr8_snr_x4);
|
||||
sprintf(reply, "> %s,%s,%s,%s", cr4, cr5, cr7, cr8);
|
||||
}
|
||||
} else if (parseRecentRepeaterGet(config, recent_page)) {
|
||||
_callbacks->formatRecentRepeatersReply(reply, recent_page);
|
||||
} else if (recent_query_match == mesh::cli::RecentRepeaterGetMatch::Valid) {
|
||||
_callbacks->formatRecentRepeatersReply(
|
||||
reply, recent_query.page,
|
||||
recent_query.search_prefix_len == 0 ? NULL : recent_query.search_prefix,
|
||||
recent_query.search_prefix_len);
|
||||
} else if (recent_query_match == mesh::cli::RecentRepeaterGetMatch::Invalid) {
|
||||
strcpy(reply, "Error, use: get recent.repeaters [page] | search <2|4|6 hex> [page]");
|
||||
} else if (memcmp(config, "owner.info", 10) == 0) {
|
||||
*reply++ = '>';
|
||||
*reply++ = ' ';
|
||||
|
||||
@@ -279,8 +279,12 @@ public:
|
||||
virtual void formatRadioStatsReply(char *reply) = 0;
|
||||
virtual void formatRadioDiagReply(char *reply) { strcpy(reply, "Not supported"); }
|
||||
virtual void formatPacketStatsReply(char *reply) = 0;
|
||||
virtual void formatRecentRepeatersReply(char *reply, int page) {
|
||||
virtual void formatRecentRepeatersReply(char *reply, int page,
|
||||
const uint8_t* search_prefix = NULL,
|
||||
uint8_t search_prefix_len = 0) {
|
||||
(void)page;
|
||||
(void)search_prefix;
|
||||
(void)search_prefix_len;
|
||||
if (reply != NULL) reply[0] = 0;
|
||||
}
|
||||
virtual bool setRecentRepeater(const uint8_t* prefix, uint8_t prefix_len, int8_t snr_x4) {
|
||||
|
||||
@@ -189,6 +189,50 @@ private:
|
||||
return a_idx < b_idx;
|
||||
}
|
||||
|
||||
bool recentRepeaterMatchesFilter(const RecentRepeaterInfo& info,
|
||||
const uint8_t* search_prefix,
|
||||
uint8_t search_prefix_len) const {
|
||||
if (search_prefix_len == 0) return true;
|
||||
return search_prefix != NULL && search_prefix_len <= MAX_ROUTE_HASH_BYTES
|
||||
&& routeHashPrefixesOverlap(info.prefix, info.prefix_len,
|
||||
search_prefix, search_prefix_len);
|
||||
}
|
||||
|
||||
const RecentRepeaterInfo* getRecentRepeaterBySortedIdxFiltered(
|
||||
int idx_wanted, const uint8_t* search_prefix,
|
||||
uint8_t search_prefix_len) const {
|
||||
if (_max_recent_repeaters == 0 || idx_wanted < 0
|
||||
|| search_prefix_len > MAX_ROUTE_HASH_BYTES
|
||||
|| (search_prefix_len > 0 && search_prefix == NULL)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const RecentRepeaterInfo* last = NULL;
|
||||
int last_idx = -1;
|
||||
for (int rank = 0; rank <= idx_wanted; rank++) {
|
||||
const RecentRepeaterInfo* best = NULL;
|
||||
int best_idx = -1;
|
||||
for (int i = 0; i < _recent_repeater_count; i++) {
|
||||
const RecentRepeaterInfo* info = &_recent_repeaters[i];
|
||||
if (!recentRepeaterMatchesFilter(*info, search_prefix,
|
||||
search_prefix_len)) {
|
||||
continue;
|
||||
}
|
||||
if (last != NULL && !recentRepeaterComesBefore(*last, last_idx, *info, i)) {
|
||||
continue;
|
||||
}
|
||||
if (best == NULL || recentRepeaterComesBefore(*info, i, *best, best_idx)) {
|
||||
best = info;
|
||||
best_idx = i;
|
||||
}
|
||||
}
|
||||
if (best == NULL) return NULL;
|
||||
last = best;
|
||||
last_idx = best_idx;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
void recordRecentRepeater(const mesh::Packet* packet) {
|
||||
if (_max_recent_repeaters == 0) {
|
||||
return;
|
||||
@@ -419,35 +463,30 @@ public:
|
||||
return _recent_repeater_count;
|
||||
}
|
||||
const RecentRepeaterInfo* getRecentRepeaterBySortedIdx(int idx_wanted) const {
|
||||
if (_max_recent_repeaters == 0) {
|
||||
return NULL;
|
||||
return getRecentRepeaterBySortedIdxFiltered(idx_wanted, NULL, 0);
|
||||
}
|
||||
int getRecentRepeaterMatchingCount(const uint8_t* search_prefix,
|
||||
uint8_t search_prefix_len) const {
|
||||
if (_max_recent_repeaters == 0 || search_prefix == NULL
|
||||
|| search_prefix_len == 0
|
||||
|| search_prefix_len > MAX_ROUTE_HASH_BYTES) {
|
||||
return 0;
|
||||
}
|
||||
if (idx_wanted < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const RecentRepeaterInfo* last = NULL;
|
||||
int last_idx = -1;
|
||||
for (int rank = 0; rank <= idx_wanted; rank++) {
|
||||
const RecentRepeaterInfo* best = NULL;
|
||||
int best_idx = -1;
|
||||
for (int i = 0; i < _recent_repeater_count; i++) {
|
||||
const RecentRepeaterInfo* info = &_recent_repeaters[i];
|
||||
if (last != NULL && !recentRepeaterComesBefore(*last, last_idx, *info, i)) {
|
||||
continue;
|
||||
}
|
||||
if (best == NULL || recentRepeaterComesBefore(*info, i, *best, best_idx)) {
|
||||
best = info;
|
||||
best_idx = i;
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 0; i < _recent_repeater_count; i++) {
|
||||
if (recentRepeaterMatchesFilter(_recent_repeaters[i], search_prefix,
|
||||
search_prefix_len)) {
|
||||
count++;
|
||||
}
|
||||
if (best == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
last = best;
|
||||
last_idx = best_idx;
|
||||
}
|
||||
return last;
|
||||
return count;
|
||||
}
|
||||
const RecentRepeaterInfo* getRecentRepeaterMatchingBySortedIdx(
|
||||
const uint8_t* search_prefix, uint8_t search_prefix_len,
|
||||
int idx_wanted) const {
|
||||
if (search_prefix == NULL || search_prefix_len == 0) return NULL;
|
||||
return getRecentRepeaterBySortedIdxFiltered(
|
||||
idx_wanted, search_prefix, search_prefix_len);
|
||||
}
|
||||
|
||||
const RecentRepeaterInfo* findRecentRepeaterByHash(const uint8_t* hash, uint8_t hash_len) const {
|
||||
|
||||
Reference in new issue
Block a user