Skip to content

Commit 472e388

Browse files
committed
Adds serial commands to get stats
- Added formatStatsReply, formatRadioStatsReply, and formatPacketStatsReply methods in MyMesh for both simple_repeater, simple_room_server, and simple_sensor. - Updated CommonCLI to handle new stats commands.
1 parent 4687ab7 commit 472e388

File tree

9 files changed

+114
-0
lines changed

9 files changed

+114
-0
lines changed

examples/simple_repeater/MyMesh.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "MyMesh.h"
22
#include <algorithm>
3+
#include "helpers/StatsFormatHelper.h"
34

45
/* ------------------------------ Config -------------------------------- */
56

@@ -785,6 +786,19 @@ void MyMesh::removeNeighbor(const uint8_t *pubkey, int key_len) {
785786
#endif
786787
}
787788

789+
void MyMesh::formatStatsReply(char *reply) {
790+
StatsFormatHelper::formatCoreStats(reply, board, *_ms, _err_flags, _mgr);
791+
}
792+
793+
void MyMesh::formatRadioStatsReply(char *reply) {
794+
StatsFormatHelper::formatRadioStats(reply, _radio, radio_driver, getTotalAirTime(), getReceiveAirTime());
795+
}
796+
797+
void MyMesh::formatPacketStatsReply(char *reply) {
798+
StatsFormatHelper::formatPacketStats(reply, radio_driver, getNumSentFlood(), getNumSentDirect(),
799+
getNumRecvFlood(), getNumRecvDirect());
800+
}
801+
788802
void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) {
789803
self_id = new_id;
790804
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)

examples/simple_repeater/MyMesh.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
181181
void setTxPower(uint8_t power_dbm) override;
182182
void formatNeighborsReply(char *reply) override;
183183
void removeNeighbor(const uint8_t* pubkey, int key_len) override;
184+
void formatStatsReply(char *reply) override;
185+
void formatRadioStatsReply(char *reply) override;
186+
void formatPacketStatsReply(char *reply) override;
184187

185188
mesh::LocalIdentity& getSelfId() override { return self_id; }
186189

examples/simple_room_server/MyMesh.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "MyMesh.h"
2+
#include <helpers/StatsFormatHelper.h>
23

34
#define REPLY_DELAY_MILLIS 1500
45
#define PUSH_NOTIFY_DELAY_MILLIS 2000
@@ -727,6 +728,19 @@ void MyMesh::clearStats() {
727728
((SimpleMeshTables *)getTables())->resetStats();
728729
}
729730

731+
void MyMesh::formatStatsReply(char *reply) {
732+
StatsFormatHelper::formatCoreStats(reply, board, *_ms, _err_flags, _mgr);
733+
}
734+
735+
void MyMesh::formatRadioStatsReply(char *reply) {
736+
StatsFormatHelper::formatRadioStats(reply, _radio, radio_driver, getTotalAirTime(), getReceiveAirTime());
737+
}
738+
739+
void MyMesh::formatPacketStatsReply(char *reply) {
740+
StatsFormatHelper::formatPacketStats(reply, radio_driver, getNumSentFlood(), getNumSentDirect(),
741+
getNumRecvFlood(), getNumRecvDirect());
742+
}
743+
730744
void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) {
731745
while (*command == ' ')
732746
command++; // skip leading spaces

examples/simple_room_server/MyMesh.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
190190
void formatNeighborsReply(char *reply) override {
191191
strcpy(reply, "not supported");
192192
}
193+
void formatStatsReply(char *reply) override;
194+
void formatRadioStatsReply(char *reply) override;
195+
void formatPacketStatsReply(char *reply) override;
193196

194197
mesh::LocalIdentity& getSelfId() override { return self_id; }
195198

examples/simple_sensor/SensorMesh.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "SensorMesh.h"
2+
#include <helpers/StatsFormatHelper.h>
23

34
/* ------------------------------ Config -------------------------------- */
45

@@ -769,6 +770,19 @@ void SensorMesh::setTxPower(uint8_t power_dbm) {
769770
radio_set_tx_power(power_dbm);
770771
}
771772

773+
void SensorMesh::formatStatsReply(char *reply) {
774+
StatsFormatHelper::formatCoreStats(reply, board, *_ms, _err_flags, _mgr);
775+
}
776+
777+
void SensorMesh::formatRadioStatsReply(char *reply) {
778+
StatsFormatHelper::formatRadioStats(reply, _radio, radio_driver, getTotalAirTime(), getReceiveAirTime());
779+
}
780+
781+
void SensorMesh::formatPacketStatsReply(char *reply) {
782+
StatsFormatHelper::formatPacketStats(reply, radio_driver, getNumSentFlood(), getNumSentDirect(),
783+
getNumRecvFlood(), getNumRecvDirect());
784+
}
785+
772786
float SensorMesh::getTelemValue(uint8_t channel, uint8_t type) {
773787
auto buf = telemetry.getBuffer();
774788
uint8_t size = telemetry.getSize();

examples/simple_sensor/SensorMesh.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
6969
void formatNeighborsReply(char *reply) override {
7070
strcpy(reply, "not supported");
7171
}
72+
void formatStatsReply(char *reply) override;
73+
void formatRadioStatsReply(char *reply) override;
74+
void formatPacketStatsReply(char *reply) override;
7275
mesh::LocalIdentity& getSelfId() override { return self_id; }
7376
void saveIdentity(const mesh::LocalIdentity& new_id) override;
7477
void clearStats() override { }

src/helpers/CommonCLI.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,12 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
663663
} else if (sender_timestamp == 0 && memcmp(command, "log", 3) == 0) {
664664
_callbacks->dumpLogFile();
665665
strcpy(reply, " EOF");
666+
} else if (sender_timestamp == 0 && memcmp(command, "stats-radio", 11) == 0) {
667+
_callbacks->formatRadioStatsReply(reply);
668+
} else if (sender_timestamp == 0 && memcmp(command, "stats-packets", 13) == 0) {
669+
_callbacks->formatPacketStatsReply(reply);
670+
} else if (sender_timestamp == 0 && memcmp(command, "stats", 5) == 0) {
671+
_callbacks->formatStatsReply(reply);
666672
} else {
667673
strcpy(reply, "Unknown command");
668674
}

src/helpers/CommonCLI.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class CommonCLICallbacks {
6666
virtual void removeNeighbor(const uint8_t* pubkey, int key_len) {
6767
// no op by default
6868
};
69+
virtual void formatStatsReply(char *reply) = 0;
70+
virtual void formatRadioStatsReply(char *reply) = 0;
71+
virtual void formatPacketStatsReply(char *reply) = 0;
6972
virtual mesh::LocalIdentity& getSelfId() = 0;
7073
virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0;
7174
virtual void clearStats() = 0;

src/helpers/StatsFormatHelper.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma once
2+
3+
#include "Mesh.h"
4+
5+
class StatsFormatHelper {
6+
public:
7+
static void formatCoreStats(char* reply,
8+
mesh::MainBoard& board,
9+
mesh::MillisecondClock& ms,
10+
uint16_t err_flags,
11+
mesh::PacketManager* mgr) {
12+
sprintf(reply,
13+
"{\"battery_mv\":%u,\"uptime_secs\":%u,\"errors\":%u,\"queue_len\":%u}",
14+
board.getBattMilliVolts(),
15+
ms.getMillis() / 1000,
16+
err_flags,
17+
mgr->getOutboundCount(0xFFFFFFFF)
18+
);
19+
}
20+
21+
template<typename RadioDriverType>
22+
static void formatRadioStats(char* reply,
23+
mesh::Radio* radio,
24+
RadioDriverType& driver,
25+
uint32_t total_air_time_ms,
26+
uint32_t total_rx_air_time_ms) {
27+
sprintf(reply,
28+
"{\"noise_floor\":%d,\"last_rssi\":%d,\"last_snr\":%.2f,\"tx_air_secs\":%u,\"rx_air_secs\":%u}",
29+
(int16_t)radio->getNoiseFloor(),
30+
(int16_t)driver.getLastRSSI(),
31+
driver.getLastSNR(),
32+
total_air_time_ms / 1000,
33+
total_rx_air_time_ms / 1000
34+
);
35+
}
36+
37+
template<typename RadioDriverType>
38+
static void formatPacketStats(char* reply,
39+
RadioDriverType& driver,
40+
uint32_t n_sent_flood,
41+
uint32_t n_sent_direct,
42+
uint32_t n_recv_flood,
43+
uint32_t n_recv_direct) {
44+
sprintf(reply,
45+
"{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u}",
46+
driver.getPacketsRecv(),
47+
driver.getPacketsSent(),
48+
n_sent_flood,
49+
n_sent_direct,
50+
n_recv_flood,
51+
n_recv_direct
52+
);
53+
}
54+
};

0 commit comments

Comments
 (0)