Skip to content

Commit 1d2a115

Browse files
authored
Merge pull request #900 from michaelhart/dev
Add stats to serial CLI
2 parents cb4468b + 81ab944 commit 1d2a115

File tree

9 files changed

+114
-0
lines changed

9 files changed

+114
-0
lines changed

examples/simple_repeater/MyMesh.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,19 @@ void MyMesh::removeNeighbor(const uint8_t *pubkey, int key_len) {
787787
#endif
788788
}
789789

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

examples/simple_repeater/MyMesh.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <helpers/IdentityStore.h>
3131
#include <helpers/SimpleMeshTables.h>
3232
#include <helpers/StaticPoolPacketManager.h>
33+
#include <helpers/StatsFormatHelper.h>
3334
#include <helpers/TxtDataHelpers.h>
3435

3536
#ifdef WITH_BRIDGE
@@ -183,6 +184,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
183184
void setTxPower(uint8_t power_dbm) override;
184185
void formatNeighborsReply(char *reply) override;
185186
void removeNeighbor(const uint8_t* pubkey, int key_len) override;
187+
void formatStatsReply(char *reply) override;
188+
void formatRadioStatsReply(char *reply) override;
189+
void formatPacketStatsReply(char *reply) override;
186190

187191
mesh::LocalIdentity& getSelfId() override { return self_id; }
188192

examples/simple_room_server/MyMesh.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,19 @@ void MyMesh::clearStats() {
729729
((SimpleMeshTables *)getTables())->resetStats();
730730
}
731731

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

examples/simple_room_server/MyMesh.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <helpers/AdvertDataHelpers.h>
1919
#include <helpers/TxtDataHelpers.h>
2020
#include <helpers/CommonCLI.h>
21+
#include <helpers/StatsFormatHelper.h>
2122
#include <helpers/ClientACL.h>
2223
#include <RTClib.h>
2324
#include <target.h>
@@ -192,6 +193,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
192193
void formatNeighborsReply(char *reply) override {
193194
strcpy(reply, "not supported");
194195
}
196+
void formatStatsReply(char *reply) override;
197+
void formatRadioStatsReply(char *reply) override;
198+
void formatPacketStatsReply(char *reply) override;
195199

196200
mesh::LocalIdentity& getSelfId() override { return self_id; }
197201

examples/simple_sensor/SensorMesh.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,19 @@ void SensorMesh::setTxPower(uint8_t power_dbm) {
769769
radio_set_tx_power(power_dbm);
770770
}
771771

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

examples/simple_sensor/SensorMesh.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <helpers/AdvertDataHelpers.h>
2121
#include <helpers/TxtDataHelpers.h>
2222
#include <helpers/CommonCLI.h>
23+
#include <helpers/StatsFormatHelper.h>
2324
#include <helpers/ClientACL.h>
2425
#include <RTClib.h>
2526
#include <target.h>
@@ -69,6 +70,9 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
6970
void formatNeighborsReply(char *reply) override {
7071
strcpy(reply, "not supported");
7172
}
73+
void formatStatsReply(char *reply) override;
74+
void formatRadioStatsReply(char *reply) override;
75+
void formatPacketStatsReply(char *reply) override;
7276
mesh::LocalIdentity& getSelfId() override { return self_id; }
7377
void saveIdentity(const mesh::LocalIdentity& new_id) override;
7478
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-packets", 13) == 0 && (command[13] == 0 || command[13] == ' ')) {
667+
_callbacks->formatPacketStatsReply(reply);
668+
} else if (sender_timestamp == 0 && memcmp(command, "stats-radio", 11) == 0 && (command[11] == 0 || command[11] == ' ')) {
669+
_callbacks->formatRadioStatsReply(reply);
670+
} else if (sender_timestamp == 0 && memcmp(command, "stats-core", 10) == 0 && (command[10] == 0 || command[10] == ' ')) {
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)