sdr: use the logger module

This commit is contained in:
Thomas Kolb 2024-10-04 20:49:11 +01:00
parent ec8770f399
commit 79d1178150

View file

@ -8,13 +8,15 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include "logger.h"
#include "config.h" #include "config.h"
#include "sdr_sxceiver.h" #include "sdr_sxceiver.h"
void soapy_log_handler(const SoapySDRLogLevel logLevel, const char *message) void soapy_log_handler(const SoapySDRLogLevel logLevel, const char *message)
{ {
fprintf(stderr, "soapy [%d]: %s\n", logLevel, message); LOG(LVL_ERR, "soapy [%d]: %s", logLevel, message);
} }
static void close_streams(sdr_ctx_t *ctx) static void close_streams(sdr_ctx_t *ctx)
@ -52,12 +54,12 @@ result_t sdr_init(sdr_ctx_t *ctx)
SoapySDRKwargs *results = SoapySDRDevice_enumerate(NULL, &length); SoapySDRKwargs *results = SoapySDRDevice_enumerate(NULL, &length);
for (size_t i = 0; i < length; i++) for (size_t i = 0; i < length; i++)
{ {
fprintf(stderr, "sdr: Found device #%d:\n", (int)i); LOG(LVL_INFO, "Found device #%d:", (int)i);
for (size_t j = 0; j < results[i].size; j++) for (size_t j = 0; j < results[i].size; j++)
{ {
fprintf(stderr, "sdr: - %s=%s\n", results[i].keys[j], results[i].vals[j]); LOG(LVL_INFO, "- %s=%s", results[i].keys[j], results[i].vals[j]);
} }
fprintf(stderr, "sdr: \n"); LOG(LVL_ERR, "");
} }
SoapySDRKwargsList_clear(results, length); SoapySDRKwargsList_clear(results, length);
@ -71,85 +73,85 @@ result_t sdr_init(sdr_ctx_t *ctx)
if (ctx->sdr == NULL) if (ctx->sdr == NULL)
{ {
fprintf(stderr, "sdr: SoapySDRDevice_make fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "SoapySDRDevice_make fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
//query device info //query device info
SoapySDRRange *ranges; SoapySDRRange *ranges;
char** names = SoapySDRDevice_listAntennas(ctx->sdr, SOAPY_SDR_RX, 0, &length); char** names = SoapySDRDevice_listAntennas(ctx->sdr, SOAPY_SDR_RX, 0, &length);
fprintf(stderr, "sdr: Rx antennas: "); LOG(LVL_INFO, "Rx antennas: ");
for (size_t i = 0; i < length; i++) fprintf(stderr, "%s, ", names[i]); for (size_t i = 0; i < length; i++) LOG(LVL_INFO, "%s, ", names[i]);
fprintf(stderr, "\n"); LOG(LVL_INFO, "");
SoapySDRStrings_clear(&names, length); SoapySDRStrings_clear(&names, length);
names = SoapySDRDevice_listGains(ctx->sdr, SOAPY_SDR_RX, 0, &length); names = SoapySDRDevice_listGains(ctx->sdr, SOAPY_SDR_RX, 0, &length);
fprintf(stderr, "sdr: Rx gains: "); LOG(LVL_INFO, "Rx gains: ");
for (size_t i = 0; i < length; i++) { for (size_t i = 0; i < length; i++) {
SoapySDRRange range = SoapySDRDevice_getGainRange(ctx->sdr, SOAPY_SDR_RX, i); SoapySDRRange range = SoapySDRDevice_getGainRange(ctx->sdr, SOAPY_SDR_RX, i);
fprintf(stderr, "%s [%.3lf .. %.3lf / %.3lf], ", names[i], range.minimum, range.maximum, range.step); LOG(LVL_INFO, "%s [%.3lf .. %.3lf / %.3lf], ", names[i], range.minimum, range.maximum, range.step);
} }
fprintf(stderr, "\n"); LOG(LVL_INFO, "");
SoapySDRStrings_clear(&names, length); SoapySDRStrings_clear(&names, length);
ranges = SoapySDRDevice_getFrequencyRange(ctx->sdr, SOAPY_SDR_RX, 0, &length); ranges = SoapySDRDevice_getFrequencyRange(ctx->sdr, SOAPY_SDR_RX, 0, &length);
fprintf(stderr, "sdr: Rx freq ranges: "); LOG(LVL_INFO, "Rx freq ranges: ");
for (size_t i = 0; i < length; i++) fprintf(stderr, "[%g Hz -> %g Hz], ", ranges[i].minimum, ranges[i].maximum); for (size_t i = 0; i < length; i++) LOG(LVL_INFO, "[%g Hz -> %g Hz], ", ranges[i].minimum, ranges[i].maximum);
fprintf(stderr, "\n"); LOG(LVL_INFO, "");
free(ranges); free(ranges);
names = SoapySDRDevice_listAntennas(ctx->sdr, SOAPY_SDR_TX, 0, &length); names = SoapySDRDevice_listAntennas(ctx->sdr, SOAPY_SDR_TX, 0, &length);
fprintf(stderr, "sdr: Tx antennas: "); LOG(LVL_INFO, "Tx antennas: ");
for (size_t i = 0; i < length; i++) fprintf(stderr, "%s, ", names[i]); for (size_t i = 0; i < length; i++) LOG(LVL_INFO, "%s, ", names[i]);
fprintf(stderr, "\n"); LOG(LVL_INFO, "");
SoapySDRStrings_clear(&names, length); SoapySDRStrings_clear(&names, length);
names = SoapySDRDevice_listGains(ctx->sdr, SOAPY_SDR_TX, 0, &length); names = SoapySDRDevice_listGains(ctx->sdr, SOAPY_SDR_TX, 0, &length);
fprintf(stderr, "sdr: Tx gains: "); LOG(LVL_INFO, "Tx gains: ");
for (size_t i = 0; i < length; i++) { for (size_t i = 0; i < length; i++) {
SoapySDRRange range = SoapySDRDevice_getGainRange(ctx->sdr, SOAPY_SDR_TX, i); SoapySDRRange range = SoapySDRDevice_getGainRange(ctx->sdr, SOAPY_SDR_TX, i);
fprintf(stderr, "%s [%.3lf .. %.3lf / %.3lf], ", names[i], range.minimum, range.maximum, range.step); LOG(LVL_INFO, "%s [%.3lf .. %.3lf / %.3lf], ", names[i], range.minimum, range.maximum, range.step);
} }
fprintf(stderr, "\n"); LOG(LVL_INFO, "");
SoapySDRStrings_clear(&names, length); SoapySDRStrings_clear(&names, length);
ranges = SoapySDRDevice_getFrequencyRange(ctx->sdr, SOAPY_SDR_TX, 0, &length); ranges = SoapySDRDevice_getFrequencyRange(ctx->sdr, SOAPY_SDR_TX, 0, &length);
fprintf(stderr, "sdr: Tx freq ranges: "); LOG(LVL_INFO, "Tx freq ranges: ");
for (size_t i = 0; i < length; i++) fprintf(stderr, "[%g Hz -> %g Hz], ", ranges[i].minimum, ranges[i].maximum); for (size_t i = 0; i < length; i++) LOG(LVL_INFO, "[%g Hz -> %g Hz], ", ranges[i].minimum, ranges[i].maximum);
fprintf(stderr, "\n"); LOG(LVL_INFO, "");
free(ranges); free(ranges);
//setup streams //setup streams
ctx->rx_stream = SoapySDRDevice_setupStream(ctx->sdr, SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL); ctx->rx_stream = SoapySDRDevice_setupStream(ctx->sdr, SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL);
if(ctx->rx_stream == NULL) { if(ctx->rx_stream == NULL) {
fprintf(stderr, "sdr: setupStream fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setupStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
ctx->tx_stream = SoapySDRDevice_setupStream(ctx->sdr, SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL); ctx->tx_stream = SoapySDRDevice_setupStream(ctx->sdr, SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL);
if(ctx->tx_stream == NULL) { if(ctx->tx_stream == NULL) {
fprintf(stderr, "sdr: setupStream fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setupStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
//apply settings //apply settings
if (SoapySDRDevice_setSampleRate(ctx->sdr, SOAPY_SDR_RX, 0, SDR_RX_SAMPLING_RATE) != 0) { if (SoapySDRDevice_setSampleRate(ctx->sdr, SOAPY_SDR_RX, 0, SDR_RX_SAMPLING_RATE) != 0) {
fprintf(stderr, "sdr: setSampleRate fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setSampleRate fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
if (SoapySDRDevice_setFrequency(ctx->sdr, SOAPY_SDR_RX, 0, SDR_RX_FREQ - SDR_RX_IF_SHIFT, NULL) != 0) { if (SoapySDRDevice_setFrequency(ctx->sdr, SOAPY_SDR_RX, 0, SDR_RX_FREQ - SDR_RX_IF_SHIFT, NULL) != 0) {
fprintf(stderr, "sdr: setFrequency fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setFrequency fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
if (SoapySDRDevice_setSampleRate(ctx->sdr, SOAPY_SDR_TX, 0, SDR_TX_SAMPLING_RATE) != 0) { if (SoapySDRDevice_setSampleRate(ctx->sdr, SOAPY_SDR_TX, 0, SDR_TX_SAMPLING_RATE) != 0) {
fprintf(stderr, "sdr: setSampleRate fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setSampleRate fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
if (SoapySDRDevice_setFrequency(ctx->sdr, SOAPY_SDR_TX, 0, SDR_TX_FREQ, NULL) != 0) { if (SoapySDRDevice_setFrequency(ctx->sdr, SOAPY_SDR_TX, 0, SDR_TX_FREQ, NULL) != 0) {
fprintf(stderr, "sdr: setFrequency fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setFrequency fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
@ -176,17 +178,17 @@ result_t sdr_start_rx(sdr_ctx_t *ctx)
{ {
// set gains // set gains
if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_RX, 0, "PGA", SDR_GAIN_RX_PGA) != 0) { if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_RX, 0, "PGA", SDR_GAIN_RX_PGA) != 0) {
fprintf(stderr, "sdr: setGainElement fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setGainElement fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_RX, 0, "LNA", SDR_GAIN_RX_LNA) != 0) { if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_RX, 0, "LNA", SDR_GAIN_RX_LNA) != 0) {
fprintf(stderr, "sdr: setGainElement fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setGainElement fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
if(SoapySDRDevice_activateStream(ctx->sdr, ctx->rx_stream, 0, 0, 0) != 0) { if(SoapySDRDevice_activateStream(ctx->sdr, ctx->rx_stream, 0, 0, 0) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "activateStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
@ -198,17 +200,17 @@ result_t sdr_start_tx(sdr_ctx_t *ctx, size_t burst_size)
{ {
// set gain // set gain
if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_TX, 0, "DAC", SDR_GAIN_TX_DAC) != 0) { if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_TX, 0, "DAC", SDR_GAIN_TX_DAC) != 0) {
fprintf(stderr, "sdr: setGainElement fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setGainElement fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_TX, 0, "MIXER", SDR_GAIN_TX_MIXER) != 0) { if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_TX, 0, "MIXER", SDR_GAIN_TX_MIXER) != 0) {
fprintf(stderr, "sdr: setGainElement fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "setGainElement fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
if(SoapySDRDevice_activateStream(ctx->sdr, ctx->tx_stream, 0, 0, burst_size) != 0) { if(SoapySDRDevice_activateStream(ctx->sdr, ctx->tx_stream, 0, 0, burst_size) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "activateStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
@ -219,7 +221,7 @@ result_t sdr_start_tx(sdr_ctx_t *ctx, size_t burst_size)
result_t sdr_stop_rx(sdr_ctx_t *ctx) result_t sdr_stop_rx(sdr_ctx_t *ctx)
{ {
if(SoapySDRDevice_deactivateStream(ctx->sdr, ctx->rx_stream, 0, 0) != 0) { if(SoapySDRDevice_deactivateStream(ctx->sdr, ctx->rx_stream, 0, 0) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "activateStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
@ -230,7 +232,7 @@ result_t sdr_stop_rx(sdr_ctx_t *ctx)
result_t sdr_stop_tx(sdr_ctx_t *ctx) result_t sdr_stop_tx(sdr_ctx_t *ctx)
{ {
if(SoapySDRDevice_deactivateStream(ctx->sdr, ctx->tx_stream, 0, 0) != 0) { if(SoapySDRDevice_deactivateStream(ctx->sdr, ctx->tx_stream, 0, 0) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "activateStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
@ -250,7 +252,7 @@ result_t sdr_transmit(sdr_ctx_t *ctx, const float complex *samples, size_t nsamp
int ret = SoapySDRDevice_writeStream(ctx->sdr, ctx->tx_stream, (const void* const*)buffs, nsamples, &flags, 0, timeout_us); int ret = SoapySDRDevice_writeStream(ctx->sdr, ctx->tx_stream, (const void* const*)buffs, nsamples, &flags, 0, timeout_us);
if(ret <= 0) { if(ret <= 0) {
fprintf(stderr, "sdr: writeStream fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "writeStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }
@ -271,7 +273,7 @@ result_t sdr_receive(sdr_ctx_t *ctx, float complex *samples, size_t *nsamples, l
int ret = SoapySDRDevice_readStream(ctx->sdr, ctx->rx_stream, (void* const*)buffs, *nsamples, &flags, &timeNs, timeout_us); int ret = SoapySDRDevice_readStream(ctx->sdr, ctx->rx_stream, (void* const*)buffs, *nsamples, &flags, &timeNs, timeout_us);
if(ret <= 0) { if(ret <= 0) {
fprintf(stderr, "sdr: readStream fail: %s\n", SoapySDRDevice_lastError()); LOG(LVL_ERR, "readStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY; return ERR_SOAPY;
} }