Browse Source

* refactor: "neighbors" command

pull/242/head
Scott Powell 1 year ago
parent
commit
8a8e89f282
  1. 44
      examples/simple_repeater/main.cpp
  2. 4
      examples/simple_room_server/main.cpp
  3. 2
      src/helpers/CommonCLI.cpp
  4. 1
      src/helpers/CommonCLI.h

44
examples/simple_repeater/main.cpp

@ -480,27 +480,6 @@ protected:
char *reply = (char *) &temp[5];
if (is_retry) {
*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 {
_cli.handleCommand(sender_timestamp, command, reply);
}
@ -664,6 +643,29 @@ public:
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() {
mesh::Mesh::loop();

4
examples/simple_room_server/main.cpp

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

2
src/helpers/CommonCLI.cpp

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

1
src/helpers/CommonCLI.h

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

Loading…
Cancel
Save