hamnet70/impl/test/layer1/test_rx_file.c

181 lines
4.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <liquid/liquid.h>
#include "jsonlogger.h"
#include "layer1/rx.h"
#include "config.h"
#define RESULT_CHECK(stmt) { \
result_t res = stmt; \
if(res != OK) { \
fprintf(stderr, "Error %d in %s:%d!\n", res, __FILE__, __LINE__); \
exit(1); \
} \
}
#define CHUNKSIZE_INPUT 8000
#define CHUNKSIZE_RF (CHUNKSIZE_INPUT/2)
#define CHUNKSIZE_BB (CHUNKSIZE_RF/SDR_OVERSAMPLING)
#define JSONLOGGER 0
static rx_stats_t m_rx_stats;
static result_t sdr_rf_to_baseband(nco_crcf nco, firdecim_crcf decim,
const float complex *rf_samples, size_t nrf,
float complex *bb_samples, size_t *nbb)
{
if((*nbb * SDR_OVERSAMPLING) < nrf) {
fprintf(stderr, "sdr_rf_to_baseband: result would not fit in output: %zd * %d < %zd\n", *nbb, SDR_OVERSAMPLING, nrf);
return ERR_SIZE;
}
*nbb = nrf / SDR_OVERSAMPLING;
for(size_t i = 0; i < *nbb; i++) {
float complex tmp[SDR_OVERSAMPLING];
assert(i*SDR_OVERSAMPLING < nrf);
nco_crcf_mix_block_down(nco,
(complex float*)(rf_samples + i * SDR_OVERSAMPLING),
tmp,
SDR_OVERSAMPLING);
firdecim_crcf_execute(decim, tmp, bb_samples + i);
}
return OK;
}
void cb_rx(rx_evt_t evt, const layer1_rx_t *rx, uint8_t *packet_data, size_t packet_len)
{
(void)packet_data;
(void)packet_len;
(void)rx;
switch(evt)
{
case RX_EVT_CHECKSUM_ERROR:
//fprintf(stderr, "Received a message of %zu bytes, but decoding failed.\n", packet_len);
//fprintf(stderr, "=== FAILED PAYLOAD ===\n");
//hexdump(packet_data, packet_len);
//fprintf(stderr, "=======================\n");
m_rx_stats.failed_decodes++;
break;
case RX_EVT_HEADER_ERROR:
m_rx_stats.header_errors++;
break;
case RX_EVT_PACKET_RECEIVED:
//fprintf(stderr, "A message of %zu bytes was decoded successfully.\n", packet_len);
//fprintf(stderr, "=== DECODED PAYLOAD (%4zu bytes) ===\n", packet_len);
//hexdump(packet_data, packet_len < 64 ? packet_len : 64);
//fprintf(stderr, "====================================\n");
m_rx_stats.successful_decodes++;
break;
case RX_EVT_PREAMBLE_FOUND:
//fprintf(stderr, "Found preamble!\n");
m_rx_stats.preambles_found++;
break;
case RX_EVT_PACKET_DEBUG_INFO_COMPLETE:
#if JSONLOGGER
jsonlogger_log_rx_packet_info(&rx->packet_debug_info);
#endif
break;
}
#if JSONLOGGER
jsonlogger_log_rx_stats(&m_rx_stats);
#endif
}
int main(int argc, char **argv)
{
if(argc < 2) {
fprintf(stderr, "Not enough arguments!\n");
fprintf(stderr, "usage: %s <dump-file>\n\n", argv[0]);
fprintf(stderr, "dump-file: HackRF dump in 8-bit signed interleaved I/Q format.\n");
return 1;
}
layer1_rx_t rx;
FILE *inputfile;
int8_t rbuf[CHUNKSIZE_INPUT];
// ** Initialize **
#if JSONLOGGER
if(!jsonlogger_init("jsonlog_test.fifo")) {
fprintf(stderr, "Could not initialize JSON logger.\n");
return EXIT_FAILURE;
}
#endif
firdecim_crcf decim = firdecim_crcf_create_kaiser(SDR_OVERSAMPLING, 9, 60.0f);
nco_crcf rx_nco = nco_crcf_create(LIQUID_NCO);
nco_crcf_set_frequency(rx_nco, 2 * 3.14159 * SDR_RX_IF_SHIFT / SDR_RX_SAMPLING_RATE);
inputfile = fopen(argv[1], "rb");
if(!inputfile) {
fprintf(stderr, "Could not open input file!\n");
return 1;
}
RESULT_CHECK(layer1_rx_init(&rx, cb_rx));
// ** Process packets **
size_t nread;
while((nread = fread(rbuf, 1, sizeof(rbuf), inputfile)) != 0) {
assert(nread % 2 == 0);
// ** Receive signal **
float complex rf_samples[CHUNKSIZE_RF];
float complex bb_samples[CHUNKSIZE_BB];
size_t n_rf_samples = CHUNKSIZE_RF;
size_t n_bb_samples = CHUNKSIZE_BB;
for(size_t iout = 0; iout < CHUNKSIZE_RF; iout++) {
rf_samples[iout] = 0.0078125f * (rbuf[2*iout + 0] + I * rbuf[2*iout + 1]);
}
RESULT_CHECK(sdr_rf_to_baseband(rx_nco, decim, rf_samples, n_rf_samples, bb_samples, &n_bb_samples));
RESULT_CHECK(layer1_rx_process(&rx, bb_samples, n_bb_samples));
fprintf(stderr, "Receiver statistics:\n");
fprintf(stderr, " Preambles found: %8zd\n", m_rx_stats.preambles_found);
fprintf(stderr, " Successful decodes: %8zd (%6.2f %%)\n",
m_rx_stats.successful_decodes, m_rx_stats.successful_decodes * 100.0f / m_rx_stats.preambles_found);
fprintf(stderr, " Header errors: %8zd (%6.2f %%)\n",
m_rx_stats.header_errors, m_rx_stats.header_errors * 100.0f / m_rx_stats.preambles_found);
fprintf(stderr, " Failed decodes: %8zd (%6.2f %%)\n",
m_rx_stats.failed_decodes, m_rx_stats.failed_decodes * 100.0f / m_rx_stats.preambles_found);
}
// ** Cleanup **
layer1_rx_shutdown(&rx);
#if JSONLOGGER
jsonlogger_shutdown();
#endif
fprintf(stderr, "Done.\n");
}