Skip to content

Commit cdbeacd

Browse files
author
Scott Powell
committed
Merge branch 'dev' into ext-trace
2 parents b33d226 + 30ccc1f commit cdbeacd

File tree

12 files changed

+475
-44
lines changed

12 files changed

+475
-44
lines changed

docs/stats_binary_frames.md

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
# Stats Binary Frame Structures
2+
3+
Binary frame structures for companion radio stats commands. All multi-byte integers use little-endian byte order.
4+
5+
## Command Codes
6+
7+
| Command | Code | Description |
8+
|---------|------|-------------|
9+
| `CMD_GET_STATS` | 56 | Get statistics (2-byte command: code + sub-type) |
10+
11+
### Stats Sub-Types
12+
13+
The `CMD_GET_STATS` command uses a 2-byte frame structure:
14+
- **Byte 0:** `CMD_GET_STATS` (56)
15+
- **Byte 1:** Stats sub-type:
16+
- `STATS_TYPE_CORE` (0) - Get core device statistics
17+
- `STATS_TYPE_RADIO` (1) - Get radio statistics
18+
- `STATS_TYPE_PACKETS` (2) - Get packet statistics
19+
20+
## Response Codes
21+
22+
| Response | Code | Description |
23+
|----------|------|-------------|
24+
| `RESP_CODE_STATS` | 24 | Statistics response (2-byte response: code + sub-type) |
25+
26+
### Stats Response Sub-Types
27+
28+
The `RESP_CODE_STATS` response uses a 2-byte header structure:
29+
- **Byte 0:** `RESP_CODE_STATS` (24)
30+
- **Byte 1:** Stats sub-type (matches command sub-type):
31+
- `STATS_TYPE_CORE` (0) - Core device statistics response
32+
- `STATS_TYPE_RADIO` (1) - Radio statistics response
33+
- `STATS_TYPE_PACKETS` (2) - Packet statistics response
34+
35+
---
36+
37+
## RESP_CODE_STATS + STATS_TYPE_CORE (24, 0)
38+
39+
**Total Frame Size:** 11 bytes
40+
41+
| Offset | Size | Type | Field Name | Description | Range/Notes |
42+
|--------|------|------|------------|-------------|-------------|
43+
| 0 | 1 | uint8_t | response_code | Always `0x18` (24) | - |
44+
| 1 | 1 | uint8_t | stats_type | Always `0x00` (STATS_TYPE_CORE) | - |
45+
| 2 | 2 | uint16_t | battery_mv | Battery voltage in millivolts | 0 - 65,535 |
46+
| 4 | 4 | uint32_t | uptime_secs | Device uptime in seconds | 0 - 4,294,967,295 |
47+
| 8 | 2 | uint16_t | errors | Error flags bitmask | - |
48+
| 10 | 1 | uint8_t | queue_len | Outbound packet queue length | 0 - 255 |
49+
50+
### Example Structure (C/C++)
51+
52+
```c
53+
struct StatsCore {
54+
uint8_t response_code; // 0x18
55+
uint8_t stats_type; // 0x00 (STATS_TYPE_CORE)
56+
uint16_t battery_mv;
57+
uint32_t uptime_secs;
58+
uint16_t errors;
59+
uint8_t queue_len;
60+
} __attribute__((packed));
61+
```
62+
63+
---
64+
65+
## RESP_CODE_STATS + STATS_TYPE_RADIO (24, 1)
66+
67+
**Total Frame Size:** 14 bytes
68+
69+
| Offset | Size | Type | Field Name | Description | Range/Notes |
70+
|--------|------|------|------------|-------------|-------------|
71+
| 0 | 1 | uint8_t | response_code | Always `0x18` (24) | - |
72+
| 1 | 1 | uint8_t | stats_type | Always `0x01` (STATS_TYPE_RADIO) | - |
73+
| 2 | 2 | int16_t | noise_floor | Radio noise floor in dBm | -140 to +10 |
74+
| 4 | 1 | int8_t | last_rssi | Last received signal strength in dBm | -128 to +127 |
75+
| 5 | 1 | int8_t | last_snr | SNR scaled by 4 | Divide by 4.0 for dB |
76+
| 6 | 4 | uint32_t | tx_air_secs | Cumulative transmit airtime in seconds | 0 - 4,294,967,295 |
77+
| 10 | 4 | uint32_t | rx_air_secs | Cumulative receive airtime in seconds | 0 - 4,294,967,295 |
78+
79+
### Example Structure (C/C++)
80+
81+
```c
82+
struct StatsRadio {
83+
uint8_t response_code; // 0x18
84+
uint8_t stats_type; // 0x01 (STATS_TYPE_RADIO)
85+
int16_t noise_floor;
86+
int8_t last_rssi;
87+
int8_t last_snr; // Divide by 4.0 to get actual SNR in dB
88+
uint32_t tx_air_secs;
89+
uint32_t rx_air_secs;
90+
} __attribute__((packed));
91+
```
92+
93+
---
94+
95+
## RESP_CODE_STATS + STATS_TYPE_PACKETS (24, 2)
96+
97+
**Total Frame Size:** 26 bytes
98+
99+
| Offset | Size | Type | Field Name | Description | Range/Notes |
100+
|--------|------|------|------------|-------------|-------------|
101+
| 0 | 1 | uint8_t | response_code | Always `0x18` (24) | - |
102+
| 1 | 1 | uint8_t | stats_type | Always `0x02` (STATS_TYPE_PACKETS) | - |
103+
| 2 | 4 | uint32_t | recv | Total packets received | 0 - 4,294,967,295 |
104+
| 6 | 4 | uint32_t | sent | Total packets sent | 0 - 4,294,967,295 |
105+
| 10 | 4 | uint32_t | flood_tx | Packets sent via flood routing | 0 - 4,294,967,295 |
106+
| 14 | 4 | uint32_t | direct_tx | Packets sent via direct routing | 0 - 4,294,967,295 |
107+
| 18 | 4 | uint32_t | flood_rx | Packets received via flood routing | 0 - 4,294,967,295 |
108+
| 22 | 4 | uint32_t | direct_rx | Packets received via direct routing | 0 - 4,294,967,295 |
109+
110+
### Notes
111+
112+
- Counters are cumulative from boot and may wrap.
113+
- `recv = flood_rx + direct_rx`
114+
- `sent = flood_tx + direct_tx`
115+
116+
### Example Structure (C/C++)
117+
118+
```c
119+
struct StatsPackets {
120+
uint8_t response_code; // 0x18
121+
uint8_t stats_type; // 0x02 (STATS_TYPE_PACKETS)
122+
uint32_t recv;
123+
uint32_t sent;
124+
uint32_t flood_tx;
125+
uint32_t direct_tx;
126+
uint32_t flood_rx;
127+
uint32_t direct_rx;
128+
} __attribute__((packed));
129+
```
130+
131+
---
132+
133+
## Command Usage Example (Python)
134+
135+
```python
136+
# Send CMD_GET_STATS command
137+
def send_get_stats_core(serial_interface):
138+
"""Send command to get core stats"""
139+
cmd = bytes([56, 0]) # CMD_GET_STATS (56) + STATS_TYPE_CORE (0)
140+
serial_interface.write(cmd)
141+
142+
def send_get_stats_radio(serial_interface):
143+
"""Send command to get radio stats"""
144+
cmd = bytes([56, 1]) # CMD_GET_STATS (56) + STATS_TYPE_RADIO (1)
145+
serial_interface.write(cmd)
146+
147+
def send_get_stats_packets(serial_interface):
148+
"""Send command to get packet stats"""
149+
cmd = bytes([56, 2]) # CMD_GET_STATS (56) + STATS_TYPE_PACKETS (2)
150+
serial_interface.write(cmd)
151+
```
152+
153+
---
154+
155+
## Response Parsing Example (Python)
156+
157+
```python
158+
import struct
159+
160+
def parse_stats_core(frame):
161+
"""Parse RESP_CODE_STATS + STATS_TYPE_CORE frame (11 bytes)"""
162+
response_code, stats_type, battery_mv, uptime_secs, errors, queue_len = \
163+
struct.unpack('<B B H I H B', frame)
164+
assert response_code == 24 and stats_type == 0, "Invalid response type"
165+
return {
166+
'battery_mv': battery_mv,
167+
'uptime_secs': uptime_secs,
168+
'errors': errors,
169+
'queue_len': queue_len
170+
}
171+
172+
def parse_stats_radio(frame):
173+
"""Parse RESP_CODE_STATS + STATS_TYPE_RADIO frame (14 bytes)"""
174+
response_code, stats_type, noise_floor, last_rssi, last_snr, tx_air_secs, rx_air_secs = \
175+
struct.unpack('<B B h b b I I', frame)
176+
assert response_code == 24 and stats_type == 1, "Invalid response type"
177+
return {
178+
'noise_floor': noise_floor,
179+
'last_rssi': last_rssi,
180+
'last_snr': last_snr / 4.0, # Unscale SNR
181+
'tx_air_secs': tx_air_secs,
182+
'rx_air_secs': rx_air_secs
183+
}
184+
185+
def parse_stats_packets(frame):
186+
"""Parse RESP_CODE_STATS + STATS_TYPE_PACKETS frame (26 bytes)"""
187+
response_code, stats_type, recv, sent, flood_tx, direct_tx, flood_rx, direct_rx = \
188+
struct.unpack('<B B I I I I I I', frame)
189+
assert response_code == 24 and stats_type == 2, "Invalid response type"
190+
return {
191+
'recv': recv,
192+
'sent': sent,
193+
'flood_tx': flood_tx,
194+
'direct_tx': direct_tx,
195+
'flood_rx': flood_rx,
196+
'direct_rx': direct_rx
197+
}
198+
```
199+
200+
---
201+
202+
## Command Usage Example (JavaScript/TypeScript)
203+
204+
```typescript
205+
// Send CMD_GET_STATS command
206+
const CMD_GET_STATS = 56;
207+
const STATS_TYPE_CORE = 0;
208+
const STATS_TYPE_RADIO = 1;
209+
const STATS_TYPE_PACKETS = 2;
210+
211+
function sendGetStatsCore(serialInterface: SerialPort): void {
212+
const cmd = new Uint8Array([CMD_GET_STATS, STATS_TYPE_CORE]);
213+
serialInterface.write(cmd);
214+
}
215+
216+
function sendGetStatsRadio(serialInterface: SerialPort): void {
217+
const cmd = new Uint8Array([CMD_GET_STATS, STATS_TYPE_RADIO]);
218+
serialInterface.write(cmd);
219+
}
220+
221+
function sendGetStatsPackets(serialInterface: SerialPort): void {
222+
const cmd = new Uint8Array([CMD_GET_STATS, STATS_TYPE_PACKETS]);
223+
serialInterface.write(cmd);
224+
}
225+
```
226+
227+
---
228+
229+
## Response Parsing Example (JavaScript/TypeScript)
230+
231+
```typescript
232+
interface StatsCore {
233+
battery_mv: number;
234+
uptime_secs: number;
235+
errors: number;
236+
queue_len: number;
237+
}
238+
239+
interface StatsRadio {
240+
noise_floor: number;
241+
last_rssi: number;
242+
last_snr: number;
243+
tx_air_secs: number;
244+
rx_air_secs: number;
245+
}
246+
247+
interface StatsPackets {
248+
recv: number;
249+
sent: number;
250+
flood_tx: number;
251+
direct_tx: number;
252+
flood_rx: number;
253+
direct_rx: number;
254+
}
255+
256+
function parseStatsCore(buffer: ArrayBuffer): StatsCore {
257+
const view = new DataView(buffer);
258+
const response_code = view.getUint8(0);
259+
const stats_type = view.getUint8(1);
260+
if (response_code !== 24 || stats_type !== 0) {
261+
throw new Error('Invalid response type');
262+
}
263+
return {
264+
battery_mv: view.getUint16(2, true),
265+
uptime_secs: view.getUint32(4, true),
266+
errors: view.getUint16(8, true),
267+
queue_len: view.getUint8(10)
268+
};
269+
}
270+
271+
function parseStatsRadio(buffer: ArrayBuffer): StatsRadio {
272+
const view = new DataView(buffer);
273+
const response_code = view.getUint8(0);
274+
const stats_type = view.getUint8(1);
275+
if (response_code !== 24 || stats_type !== 1) {
276+
throw new Error('Invalid response type');
277+
}
278+
return {
279+
noise_floor: view.getInt16(2, true),
280+
last_rssi: view.getInt8(4),
281+
last_snr: view.getInt8(5) / 4.0, // Unscale SNR
282+
tx_air_secs: view.getUint32(6, true),
283+
rx_air_secs: view.getUint32(10, true)
284+
};
285+
}
286+
287+
function parseStatsPackets(buffer: ArrayBuffer): StatsPackets {
288+
const view = new DataView(buffer);
289+
const response_code = view.getUint8(0);
290+
const stats_type = view.getUint8(1);
291+
if (response_code !== 24 || stats_type !== 2) {
292+
throw new Error('Invalid response type');
293+
}
294+
return {
295+
recv: view.getUint32(2, true),
296+
sent: view.getUint32(6, true),
297+
flood_tx: view.getUint32(10, true),
298+
direct_tx: view.getUint32(14, true),
299+
flood_rx: view.getUint32(18, true),
300+
direct_rx: view.getUint32(22, true)
301+
};
302+
}
303+
```
304+
305+
---
306+
307+
## Field Size Considerations
308+
309+
- Packet counters (uint32_t): May wrap after extended high-traffic operation.
310+
- Time fields (uint32_t): Max ~136 years.
311+
- SNR (int8_t, scaled by 4): Range -32 to +31.75 dB, 0.25 dB precision.
312+

examples/companion_radio/MyMesh.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
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 56 // v8+, second byte is stats type
56+
57+
// Stats sub-types for CMD_GET_STATS
58+
#define STATS_TYPE_CORE 0
59+
#define STATS_TYPE_RADIO 1
60+
#define STATS_TYPE_PACKETS 2
5561

5662
#define RESP_CODE_OK 0
5763
#define RESP_CODE_ERR 1
@@ -77,6 +83,7 @@
7783
#define RESP_CODE_CUSTOM_VARS 21
7884
#define RESP_CODE_ADVERT_PATH 22
7985
#define RESP_CODE_TUNING_PARAMS 23
86+
#define RESP_CODE_STATS 24 // v8+, second byte is stats type
8087

8188
#define SEND_TIMEOUT_BASE_MILLIS 500
8289
#define FLOOD_SEND_TIMEOUT_FACTOR 16.0f
@@ -1541,6 +1548,55 @@ void MyMesh::handleCmdFrame(size_t len) {
15411548
} else {
15421549
writeErrFrame(ERR_CODE_NOT_FOUND);
15431550
}
1551+
} else if (cmd_frame[0] == CMD_GET_STATS && len >= 2) {
1552+
uint8_t stats_type = cmd_frame[1];
1553+
if (stats_type == STATS_TYPE_CORE) {
1554+
int i = 0;
1555+
out_frame[i++] = RESP_CODE_STATS;
1556+
out_frame[i++] = STATS_TYPE_CORE;
1557+
uint16_t battery_mv = board.getBattMilliVolts();
1558+
uint32_t uptime_secs = _ms->getMillis() / 1000;
1559+
uint8_t queue_len = (uint8_t)_mgr->getOutboundCount(0xFFFFFFFF);
1560+
memcpy(&out_frame[i], &battery_mv, 2); i += 2;
1561+
memcpy(&out_frame[i], &uptime_secs, 4); i += 4;
1562+
memcpy(&out_frame[i], &_err_flags, 2); i += 2;
1563+
out_frame[i++] = queue_len;
1564+
_serial->writeFrame(out_frame, i);
1565+
} else if (stats_type == STATS_TYPE_RADIO) {
1566+
int i = 0;
1567+
out_frame[i++] = RESP_CODE_STATS;
1568+
out_frame[i++] = STATS_TYPE_RADIO;
1569+
int16_t noise_floor = (int16_t)_radio->getNoiseFloor();
1570+
int8_t last_rssi = (int8_t)radio_driver.getLastRSSI();
1571+
int8_t last_snr = (int8_t)(radio_driver.getLastSNR() * 4); // scaled by 4 for 0.25 dB precision
1572+
uint32_t tx_air_secs = getTotalAirTime() / 1000;
1573+
uint32_t rx_air_secs = getReceiveAirTime() / 1000;
1574+
memcpy(&out_frame[i], &noise_floor, 2); i += 2;
1575+
out_frame[i++] = last_rssi;
1576+
out_frame[i++] = last_snr;
1577+
memcpy(&out_frame[i], &tx_air_secs, 4); i += 4;
1578+
memcpy(&out_frame[i], &rx_air_secs, 4); i += 4;
1579+
_serial->writeFrame(out_frame, i);
1580+
} else if (stats_type == STATS_TYPE_PACKETS) {
1581+
int i = 0;
1582+
out_frame[i++] = RESP_CODE_STATS;
1583+
out_frame[i++] = STATS_TYPE_PACKETS;
1584+
uint32_t recv = radio_driver.getPacketsRecv();
1585+
uint32_t sent = radio_driver.getPacketsSent();
1586+
uint32_t n_sent_flood = getNumSentFlood();
1587+
uint32_t n_sent_direct = getNumSentDirect();
1588+
uint32_t n_recv_flood = getNumRecvFlood();
1589+
uint32_t n_recv_direct = getNumRecvDirect();
1590+
memcpy(&out_frame[i], &recv, 4); i += 4;
1591+
memcpy(&out_frame[i], &sent, 4); i += 4;
1592+
memcpy(&out_frame[i], &n_sent_flood, 4); i += 4;
1593+
memcpy(&out_frame[i], &n_sent_direct, 4); i += 4;
1594+
memcpy(&out_frame[i], &n_recv_flood, 4); i += 4;
1595+
memcpy(&out_frame[i], &n_recv_direct, 4); i += 4;
1596+
_serial->writeFrame(out_frame, i);
1597+
} else {
1598+
writeErrFrame(ERR_CODE_ILLEGAL_ARG); // invalid stats sub-type
1599+
}
15441600
} else if (cmd_frame[0] == CMD_FACTORY_RESET && memcmp(&cmd_frame[1], "reset", 5) == 0) {
15451601
bool success = _store->formatFileSystem();
15461602
if (success) {

0 commit comments

Comments
 (0)