Skip to content

Commit fb4fe02

Browse files
committed
Add cli commands "region list {allowed|denied}"
1 parent 6d99f4b commit fb4fe02

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

examples/simple_repeater/MyMesh.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,25 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
11421142
} else {
11431143
strcpy(reply, "Err - not found");
11441144
}
1145+
} else if (n >= 3 && strcmp(parts[1], "list") == 0) {
1146+
uint8_t mask = 0;
1147+
bool invert = false;
1148+
1149+
if (strcmp(parts[2], "allowed") == 0) {
1150+
mask = REGION_DENY_FLOOD;
1151+
invert = false; // list regions that DON'T have DENY flag
1152+
} else if (strcmp(parts[2], "denied") == 0) {
1153+
mask = REGION_DENY_FLOOD;
1154+
invert = true; // list regions that DO have DENY flag
1155+
} else {
1156+
strcpy(reply, "Err - use 'allowed' or 'denied'");
1157+
return;
1158+
}
1159+
1160+
int len = region_map.exportNamesTo(reply, 160, mask, invert);
1161+
if (len == 0) {
1162+
strcpy(reply, "-none-");
1163+
}
11451164
} else {
11461165
strcpy(reply, "Err - ??");
11471166
}

src/helpers/RegionMap.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,25 +296,32 @@ size_t RegionMap::exportTo(char *dest, size_t max_len) const {
296296
return bs.length();
297297
}
298298

299-
int RegionMap::exportNamesTo(char *dest, int max_len, uint8_t mask) {
299+
int RegionMap::exportNamesTo(char *dest, int max_len, uint8_t mask, bool invert) {
300300
char *dp = dest;
301-
if ((wildcard.flags & mask) == 0) {
301+
302+
// Check wildcard region
303+
bool wildcard_matches = invert ? (wildcard.flags & mask) : !(wildcard.flags & mask);
304+
if (wildcard_matches) {
302305
*dp++ = '*';
303306
*dp++ = ',';
304307
}
305308

306-
for (int i = 0; i < num_regions; i++) {
309+
for (int i = 0; i < num_regions; i++) {
307310
auto region = &regions[i];
308-
if ((region->flags & mask) == 0) { // region allowed? (per 'mask' param)
309-
const char* name = skip_hash(region->name);
310-
int len = strlen(name);
311+
312+
// Check if region matches the filter criteria
313+
bool region_matches = invert ? (region->flags & mask) : !(region->flags & mask);
314+
315+
if (region_matches) {
316+
int len = strlen(region->name);
311317
if ((dp - dest) + len + 2 < max_len) { // only append if name will fit
312-
memcpy(dp, name, len);
318+
memcpy(dp, region->name, len);
313319
dp += len;
314320
*dp++ = ',';
315321
}
316322
}
317323
}
324+
318325
if (dp > dest) { dp--; } // don't include trailing comma
319326

320327
*dp = 0; // set null terminator

src/helpers/RegionMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class RegionMap {
4949
int getCount() const { return num_regions; }
5050
const RegionEntry* getByIdx(int i) const { return &regions[i]; }
5151
const RegionEntry* getRoot() const { return &wildcard; }
52-
int exportNamesTo(char *dest, int max_len, uint8_t mask);
52+
int exportNamesTo(char *dest, int max_len, uint8_t mask, bool invert = false);
5353

5454
void exportTo(Stream& out) const;
5555
size_t exportTo(char *dest, size_t max_len) const;

0 commit comments

Comments
 (0)