Print RX stats periodically instead of debug logging

This commit is contained in:
Thomas Kolb 2024-01-02 19:04:19 +01:00
parent 112a8a1215
commit 7995f5bef0

View file

@ -38,6 +38,12 @@ static bool m_running = true;
static double next_tx_switch_time = 0.0; static double next_tx_switch_time = 0.0;
static struct {
size_t preambles_found;
size_t successful_decodes;
size_t failed_decodes;
} m_stats;
static void block_tx_for(unsigned offset_ms) static void block_tx_for(unsigned offset_ms)
{ {
@ -102,18 +108,21 @@ void cb_rx(rx_evt_t evt, uint8_t *packet_data, size_t packet_len)
switch(evt) switch(evt)
{ {
case RX_EVT_CHECKSUM_ERROR: case RX_EVT_CHECKSUM_ERROR:
fprintf(stderr, "Received a message of %zu bytes, but decoding failed.\n", packet_len); //fprintf(stderr, "Received a message of %zu bytes, but decoding failed.\n", packet_len);
//fprintf(stderr, "=== FAILED PAYLOAD ===\n"); //fprintf(stderr, "=== FAILED PAYLOAD ===\n");
//hexdump(packet_data, packet_len); //hexdump(packet_data, packet_len);
//fprintf(stderr, "=======================\n"); //fprintf(stderr, "=======================\n");
m_stats.failed_decodes++;
break; break;
case RX_EVT_PACKET_RECEIVED: case RX_EVT_PACKET_RECEIVED:
fprintf(stderr, "A message of %zu bytes was decoded successfully.\n", packet_len); //fprintf(stderr, "A message of %zu bytes was decoded successfully.\n", packet_len);
//fprintf(stderr, "=== DECODED PAYLOAD (%4zu bytes) ===\n", packet_len); //fprintf(stderr, "=== DECODED PAYLOAD (%4zu bytes) ===\n", packet_len);
//hexdump(packet_data, packet_len < 64 ? packet_len : 64); //hexdump(packet_data, packet_len < 64 ? packet_len : 64);
//fprintf(stderr, "====================================\n"); //fprintf(stderr, "====================================\n");
m_stats.successful_decodes++;
block_tx_for(TX_SWITCH_BACKOFF_END_OF_PACKET_MS); block_tx_for(TX_SWITCH_BACKOFF_END_OF_PACKET_MS);
ret = write(m_tunfd, packet_data, packet_len); ret = write(m_tunfd, packet_data, packet_len);
@ -123,8 +132,9 @@ void cb_rx(rx_evt_t evt, uint8_t *packet_data, size_t packet_len)
break; break;
case RX_EVT_PREAMBLE_FOUND: case RX_EVT_PREAMBLE_FOUND:
fprintf(stderr, "Found preamble!\n"); //fprintf(stderr, "Found preamble!\n");
block_tx_for(TX_SWITCH_BACKOFF_PREAMBLE_MS); block_tx_for(TX_SWITCH_BACKOFF_PREAMBLE_MS);
m_stats.preambles_found++;
break; break;
} }
} }
@ -220,7 +230,7 @@ int main(void)
double old = get_hires_time(); double old = get_hires_time();
size_t total_samples = 0; size_t total_samples = 0;
double next_rate_print_time = old + 0.5; double next_stats_print_time = old + 0.5;
while(m_running) { while(m_running) {
double now = get_hires_time(); double now = get_hires_time();
@ -300,10 +310,16 @@ int main(void)
total_samples += n_rf_samples; total_samples += n_rf_samples;
double new = get_hires_time(); double new = get_hires_time();
if(new >= next_rate_print_time) { if(new >= next_stats_print_time) {
double rate = total_samples / (new - old); double rate = total_samples / (new - old);
fprintf(stderr, "\nEstimated rate: %.3f MS/s\n", rate / 1e6); fprintf(stderr, "\nEstimated rate: %.3f MS/s\n", rate / 1e6);
next_rate_print_time += 0.5; fprintf(stderr, "Receiver statistics:\n");
fprintf(stderr, " Preambles found: %8zd\n", m_stats.preambles_found);
fprintf(stderr, " Successful decodes: %8zd (%6.2f %%)\n",
m_stats.successful_decodes, m_stats.successful_decodes * 100.0f / m_stats.preambles_found);
fprintf(stderr, " Failed decodes: %8zd (%6.2f %%)\n",
m_stats.failed_decodes, m_stats.failed_decodes * 100.0f / m_stats.preambles_found);
next_stats_print_time += 0.5;
total_samples = 0; total_samples = 0;
old = new; old = new;