Skip to content

Commit a528908

Browse files
committed
Add statistics commands and response handling in MyMesh
- Introduced new commands for retrieving statistics: CMD_GET_STATS_CORE, CMD_GET_STATS_RADIO, and CMD_GET_STATS_PACKETS. - Implemented corresponding response handling methods to format and send statistics data. - Updated MyMesh constructor to initialize new member variables for managing statistics. - Included StatsFormatHelper for formatting statistics replies.
1 parent 1520f4d commit a528908

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

examples/companion_radio/MyMesh.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
#define CMD_SEND_PATH_DISCOVERY_REQ 52
5353
#define CMD_SET_FLOOD_SCOPE 54 // v8+
5454
#define CMD_SEND_CONTROL_DATA 55 // v8+
55+
#define CMD_GET_STATS_CORE 56
56+
#define CMD_GET_STATS_RADIO 57
57+
#define CMD_GET_STATS_PACKETS 58
5558

5659
#define RESP_CODE_OK 0
5760
#define RESP_CODE_ERR 1
@@ -77,6 +80,9 @@
7780
#define RESP_CODE_CUSTOM_VARS 21
7881
#define RESP_CODE_ADVERT_PATH 22
7982
#define RESP_CODE_TUNING_PARAMS 23
83+
#define RESP_CODE_STATS_CORE 24
84+
#define RESP_CODE_STATS_RADIO 25
85+
#define RESP_CODE_STATS_PACKETS 26
8086

8187
#define SEND_TIMEOUT_BASE_MILLIS 500
8288
#define FLOOD_SEND_TIMEOUT_FACTOR 16.0f
@@ -704,7 +710,7 @@ uint32_t MyMesh::calcDirectTimeoutMillisFor(uint32_t pkt_airtime_millis, uint8_t
704710
void MyMesh::onSendTimeout() {}
705711

706712
MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMeshTables &tables, DataStore& store, AbstractUITask* ui)
707-
: BaseChatMesh(radio, *new ArduinoMillis(), rng, rtc, *new StaticPoolPacketManager(16), tables),
713+
: BaseChatMesh(radio, *(_ms_clock = new ArduinoMillis()), rng, rtc, *(_pkt_mgr = new StaticPoolPacketManager(16)), tables),
708714
_serial(NULL), telemetry(MAX_PACKET_PAYLOAD - 4), _store(&store), _ui(ui) {
709715
_iter_started = false;
710716
_cli_rescue = false;
@@ -1529,6 +1535,45 @@ void MyMesh::handleCmdFrame(size_t len) {
15291535
} else {
15301536
writeErrFrame(ERR_CODE_NOT_FOUND);
15311537
}
1538+
} else if (cmd_frame[0] == CMD_GET_STATS_CORE) {
1539+
char json_reply[160];
1540+
formatStatsReply(json_reply);
1541+
int i = 0;
1542+
out_frame[i++] = RESP_CODE_STATS_CORE;
1543+
int json_len = strlen(json_reply);
1544+
if (i + json_len <= MAX_FRAME_SIZE) {
1545+
memcpy(&out_frame[i], json_reply, json_len);
1546+
i += json_len;
1547+
_serial->writeFrame(out_frame, i);
1548+
} else {
1549+
writeErrFrame(ERR_CODE_TABLE_FULL);
1550+
}
1551+
} else if (cmd_frame[0] == CMD_GET_STATS_RADIO) {
1552+
char json_reply[160];
1553+
formatRadioStatsReply(json_reply);
1554+
int i = 0;
1555+
out_frame[i++] = RESP_CODE_STATS_RADIO;
1556+
int json_len = strlen(json_reply);
1557+
if (i + json_len <= MAX_FRAME_SIZE) {
1558+
memcpy(&out_frame[i], json_reply, json_len);
1559+
i += json_len;
1560+
_serial->writeFrame(out_frame, i);
1561+
} else {
1562+
writeErrFrame(ERR_CODE_TABLE_FULL);
1563+
}
1564+
} else if (cmd_frame[0] == CMD_GET_STATS_PACKETS) {
1565+
char json_reply[160];
1566+
formatPacketStatsReply(json_reply);
1567+
int i = 0;
1568+
out_frame[i++] = RESP_CODE_STATS_PACKETS;
1569+
int json_len = strlen(json_reply);
1570+
if (i + json_len <= MAX_FRAME_SIZE) {
1571+
memcpy(&out_frame[i], json_reply, json_len);
1572+
i += json_len;
1573+
_serial->writeFrame(out_frame, i);
1574+
} else {
1575+
writeErrFrame(ERR_CODE_TABLE_FULL);
1576+
}
15321577
} else if (cmd_frame[0] == CMD_FACTORY_RESET && memcmp(&cmd_frame[1], "reset", 5) == 0) {
15331578
bool success = _store->formatFileSystem();
15341579
if (success) {
@@ -1565,6 +1610,21 @@ void MyMesh::enterCLIRescue() {
15651610
Serial.println("========= CLI Rescue =========");
15661611
}
15671612

1613+
void MyMesh::formatStatsReply(char *reply) {
1614+
// Use StatsFormatHelper
1615+
// Note: err_flags is private in Dispatcher, so we use 0
1616+
StatsFormatHelper::formatCoreStats(reply, board, *_ms_clock, 0, _pkt_mgr);
1617+
}
1618+
1619+
void MyMesh::formatRadioStatsReply(char *reply) {
1620+
StatsFormatHelper::formatRadioStats(reply, _radio, radio_driver, getTotalAirTime(), getReceiveAirTime());
1621+
}
1622+
1623+
void MyMesh::formatPacketStatsReply(char *reply) {
1624+
StatsFormatHelper::formatPacketStats(reply, radio_driver, getNumSentFlood(), getNumSentDirect(),
1625+
getNumRecvFlood(), getNumRecvDirect());
1626+
}
1627+
15681628
void MyMesh::checkCLIRescueCmd() {
15691629
int len = strlen(cli_command);
15701630
while (Serial.available() && len < sizeof(cli_command)-1) {

examples/companion_radio/MyMesh.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969

7070
#include <helpers/BaseChatMesh.h>
7171
#include <helpers/TransportKeyStore.h>
72+
#include <helpers/StatsFormatHelper.h>
7273

7374
/* -------------------------------------------------------------------------------------- */
7475

@@ -170,6 +171,11 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
170171
void checkCLIRescueCmd();
171172
void checkSerialInterface();
172173

174+
// Stats methods
175+
void formatStatsReply(char *reply);
176+
void formatRadioStatsReply(char *reply);
177+
void formatPacketStatsReply(char *reply);
178+
173179
// helpers, short-cuts
174180
void savePrefs() { _store->savePrefs(_prefs, sensors.node_lat, sensors.node_lon); }
175181
void saveChannels() { _store->saveChannels(this); }
@@ -178,6 +184,8 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
178184
private:
179185
DataStore* _store;
180186
NodePrefs _prefs;
187+
mesh::PacketManager* _pkt_mgr; // stored for stats access
188+
mesh::MillisecondClock* _ms_clock; // stored for stats access
181189
uint32_t pending_login;
182190
uint32_t pending_status;
183191
uint32_t pending_telemetry, pending_discovery; // pending _TELEMETRY_REQ

0 commit comments

Comments
 (0)