* refactor: "neighbors" command

This commit is contained in:
Scott Powell
2025-04-30 21:41:09 +10:00
parent 056bcf83d9
commit 8a8e89f282
4 changed files with 30 additions and 21 deletions

View File

@@ -480,27 +480,6 @@ protected:
char *reply = (char *) &temp[5]; char *reply = (char *) &temp[5];
if (is_retry) { if (is_retry) {
*reply = 0; *reply = 0;
#if MAX_NEIGHBOURS
} else if (memcmp(command, "neighbors", 9) == 0) {
char *dp = reply;
for (int i = 0; i < MAX_NEIGHBOURS && dp - reply < 136; i++) {
NeighbourInfo* neighbour = &neighbours[i];
if (neighbour->heard_timestamp == 0) continue; // skip empty slots
// add new line if not first item
if (i > 0) *dp++ = '\n';
char hex[10];
// get 4 bytes of neighbour id as hex
mesh::Utils::toHex(hex, neighbour->id.pub_key, 4);
// add next neighbour
sprintf(dp, "%s:%d:%d", hex, neighbour->advert_timestamp, neighbour->snr);
while (*dp) dp++; // find end of string
}
*dp = 0; // null terminator
#endif
} else { } else {
_cli.handleCommand(sender_timestamp, command, reply); _cli.handleCommand(sender_timestamp, command, reply);
} }
@@ -664,6 +643,29 @@ public:
radio_set_tx_power(power_dbm); radio_set_tx_power(power_dbm);
} }
void formatNeighborsReply(char *reply) override {
char *dp = reply;
#if MAX_NEIGHBOURS
for (int i = 0; i < MAX_NEIGHBOURS && dp - reply < 134; i++) {
NeighbourInfo* neighbour = &neighbours[i];
if (neighbour->heard_timestamp == 0) continue; // skip empty slots
// add new line if not first item
if (i > 0) *dp++ = '\n';
char hex[10];
// get 4 bytes of neighbour id as hex
mesh::Utils::toHex(hex, neighbour->id.pub_key, 4);
// add next neighbour
sprintf(dp, "%s:%d:%d", hex, neighbour->advert_timestamp, neighbour->snr);
while (*dp) dp++; // find end of string
}
#endif
*dp = 0; // null terminator
}
void loop() { void loop() {
mesh::Mesh::loop(); mesh::Mesh::loop();

View File

@@ -789,6 +789,10 @@ public:
radio_set_tx_power(power_dbm); radio_set_tx_power(power_dbm);
} }
void formatNeighborsReply(char *reply) override {
strcpy(reply, "not supported");
}
void loop() { void loop() {
mesh::Mesh::loop(); mesh::Mesh::loop();

View File

@@ -161,6 +161,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
} else { } else {
strcpy(reply, "(ERR: clock cannot go backwards)"); strcpy(reply, "(ERR: clock cannot go backwards)");
} }
} else if (memcmp(command, "neighbors", 9) == 0) {
_callbacks->formatNeighborsReply(reply);
} else if (memcmp(command, "password ", 9) == 0) { } else if (memcmp(command, "password ", 9) == 0) {
// change admin password // change admin password
StrHelper::strncpy(_prefs->password, &command[9], sizeof(_prefs->password)); StrHelper::strncpy(_prefs->password, &command[9], sizeof(_prefs->password));

View File

@@ -40,6 +40,7 @@ public:
virtual void eraseLogFile() = 0; virtual void eraseLogFile() = 0;
virtual void dumpLogFile() = 0; virtual void dumpLogFile() = 0;
virtual void setTxPower(uint8_t power_dbm) = 0; virtual void setTxPower(uint8_t power_dbm) = 0;
virtual void formatNeighborsReply(char *reply) = 0;
}; };
class CommonCLI { class CommonCLI {