* repeater: CLI, added "region put" and "region remove" commands
This commit is contained in:
@@ -890,10 +890,6 @@ void MyMesh::clearStats() {
|
|||||||
((SimpleMeshTables *)getTables())->resetStats();
|
((SimpleMeshTables *)getTables())->resetStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_name_char(char c) {
|
|
||||||
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.' || c == '_' || c == '#';
|
|
||||||
}
|
|
||||||
|
|
||||||
void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) {
|
void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) {
|
||||||
if (region_load_active) {
|
if (region_load_active) {
|
||||||
if (*command == 0) { // empty line, signal to terminate 'load' operation
|
if (*command == 0) { // empty line, signal to terminate 'load' operation
|
||||||
@@ -907,7 +903,7 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
|
|||||||
int indent = np - command;
|
int indent = np - command;
|
||||||
|
|
||||||
char *ep = np;
|
char *ep = np;
|
||||||
while (is_name_char(*ep)) ep++;
|
while (RegionMap::is_name_char(*ep)) ep++;
|
||||||
if (*ep) { *ep++ = 0; } // set null terminator for end of name
|
if (*ep) { *ep++ = 0; } // set null terminator for end of name
|
||||||
|
|
||||||
while (*ep && *ep != 'F') ep++; // look for (optional) flags
|
while (*ep && *ep != 'F') ep++; // look for (optional) flags
|
||||||
@@ -1022,6 +1018,29 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
|
|||||||
} else if (n == 2 && strcmp(parts[1], "home") == 0) {
|
} else if (n == 2 && strcmp(parts[1], "home") == 0) {
|
||||||
auto home = region_map.getHomeRegion();
|
auto home = region_map.getHomeRegion();
|
||||||
sprintf(reply, " home is %s", home ? home->name : "*");
|
sprintf(reply, " home is %s", home ? home->name : "*");
|
||||||
|
} else if (n >= 3 && strcmp(parts[1], "put") == 0) {
|
||||||
|
auto parent = n >= 4 ? region_map.findByNamePrefix(parts[3]) : ®ion_map.getWildcard();
|
||||||
|
if (parent == NULL) {
|
||||||
|
strcpy(reply, "Err - unknown parent");
|
||||||
|
} else {
|
||||||
|
auto region = region_map.putRegion(parts[2], parent->id);
|
||||||
|
if (region == NULL) {
|
||||||
|
strcpy(reply, "Err - unable to put");
|
||||||
|
} else {
|
||||||
|
strcpy(reply, "OK");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (n >= 3 && strcmp(parts[1], "remove") == 0) {
|
||||||
|
auto region = region_map.findByName(parts[2]);
|
||||||
|
if (region) {
|
||||||
|
if (region_map.removeRegion(*region)) {
|
||||||
|
strcpy(reply, "OK");
|
||||||
|
} else {
|
||||||
|
strcpy(reply, "Err - not empty");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
strcpy(reply, "Err - not found");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
strcpy(reply, "Err - ??");
|
strcpy(reply, "Err - ??");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ RegionMap::RegionMap(TransportKeyStore& store) : _store(&store) {
|
|||||||
strcpy(wildcard.name, "*");
|
strcpy(wildcard.name, "*");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool RegionMap::is_name_char(char c) {
|
||||||
|
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.' || c == '_' || c == '#';
|
||||||
|
}
|
||||||
|
|
||||||
static File openWrite(FILESYSTEM* _fs, const char* filename) {
|
static File openWrite(FILESYSTEM* _fs, const char* filename) {
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
_fs->remove(filename);
|
_fs->remove(filename);
|
||||||
@@ -93,6 +97,12 @@ bool RegionMap::save(FILESYSTEM* _fs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RegionEntry* RegionMap::putRegion(const char* name, uint16_t parent_id, uint16_t id) {
|
RegionEntry* RegionMap::putRegion(const char* name, uint16_t parent_id, uint16_t id) {
|
||||||
|
const char* sp = name; // check for illegal name chars
|
||||||
|
while (*sp) {
|
||||||
|
if (!is_name_char(*sp)) return NULL; // error
|
||||||
|
sp++;
|
||||||
|
}
|
||||||
|
|
||||||
auto region = findByName(name);
|
auto region = findByName(name);
|
||||||
if (region) {
|
if (region) {
|
||||||
if (region->id == parent_id) return NULL; // ERROR: invalid parent!
|
if (region->id == parent_id) return NULL; // ERROR: invalid parent!
|
||||||
@@ -187,8 +197,9 @@ bool RegionMap::removeRegion(const RegionEntry& region) {
|
|||||||
if (i >= num_regions) return false; // failed (not found)
|
if (i >= num_regions) return false; // failed (not found)
|
||||||
|
|
||||||
num_regions--; // remove from regions array
|
num_regions--; // remove from regions array
|
||||||
while (i + 1 < num_regions) {
|
while (i < num_regions) {
|
||||||
regions[i] = regions[i + 1];
|
regions[i] = regions[i + 1];
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
return true; // success
|
return true; // success
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ class RegionMap {
|
|||||||
public:
|
public:
|
||||||
RegionMap(TransportKeyStore& store);
|
RegionMap(TransportKeyStore& store);
|
||||||
|
|
||||||
|
static bool is_name_char(char c);
|
||||||
|
|
||||||
bool load(FILESYSTEM* _fs);
|
bool load(FILESYSTEM* _fs);
|
||||||
bool save(FILESYSTEM* _fs);
|
bool save(FILESYSTEM* _fs);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user