Skip to content

Commit

Permalink
open/close airspy every time it needs to start/stop
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Jan 2, 2025
1 parent cd3800a commit f2cb72f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
82 changes: 44 additions & 38 deletions src/sdr/airspy_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
int __err_rc = (x); \
if (__err_rc != 0) { \
fprintf(stderr, "%s: %d\n", y, __err_rc); \
airspy_device_destroy(device); \
if (device->dev != NULL) { \
device->lib->airspy_close(device->dev); \
device->dev = NULL; \
} \
return __err_rc; \
} \
} while (0)
Expand All @@ -20,6 +23,7 @@ struct airspy_device_t {
void (*sdr_callback)(uint8_t *buf, uint32_t len, void *ctx);
void *ctx;

struct server_config *server_config;
airspy_lib *lib;
};

Expand All @@ -32,32 +36,56 @@ int airspy_device_create(uint32_t id, struct server_config *server_config, airsp
device->lib = lib;
device->sdr_callback = sdr_callback;
device->ctx = ctx;
ERROR_CHECK(lib->airspy_open(&device->dev), "<3>unable to init airspy device");
ERROR_CHECK(lib->airspy_set_sample_type(device->dev, AIRSPY_SAMPLE_INT16_IQ), "<3>unable to set sample type int16 iq");
ERROR_CHECK(lib->airspy_set_samplerate(device->dev, server_config->band_sampling_rate), "<3>unable to set sample rate");
ERROR_CHECK(lib->airspy_set_packing(device->dev, 1), "<3>unable to set packing");
ERROR_CHECK(lib->airspy_set_rf_bias(device->dev, server_config->bias_t), "<3>unable to set bias_t");
device->server_config = server_config;
fprintf(stdout, "airspy device created\n");
*plugin = device;
return 0;
}

void airspy_device_destroy(void *plugin) {
if (plugin == NULL) {
return;
}
struct airspy_device_t *device = (struct airspy_device_t *)plugin;
free(device);
fprintf(stdout, "airspy device destroyed\n");
}

int airspy_device_callback(airspy_transfer *transfer) {
struct airspy_device_t *device = (struct airspy_device_t *)transfer->ctx;
device->sdr_callback(transfer->samples, transfer->sample_count * 2 * sizeof(int16_t), device->ctx);
return 0;
}

int airspy_device_start_rx(uint32_t band_freq, void *plugin) {
struct airspy_device_t *device = (struct airspy_device_t *)plugin;
struct server_config *server_config = device->server_config;
ERROR_CHECK(device->lib->airspy_open(&device->dev), "<3>unable to init airspy device");
ERROR_CHECK(device->lib->airspy_set_sample_type(device->dev, AIRSPY_SAMPLE_INT16_IQ), "<3>unable to set sample type int16 iq");
ERROR_CHECK(device->lib->airspy_set_samplerate(device->dev, server_config->band_sampling_rate), "<3>unable to set sample rate");
ERROR_CHECK(device->lib->airspy_set_packing(device->dev, 1), "<3>unable to set packing");
ERROR_CHECK(device->lib->airspy_set_rf_bias(device->dev, server_config->bias_t), "<3>unable to set bias_t");
switch (server_config->airspy_gain_mode) {
case AIRSPY_GAIN_SENSITIVITY: {
ERROR_CHECK(lib->airspy_set_sensitivity_gain(device->dev, server_config->airspy_sensitivity_gain), "<3>unable to set sensitivity gain");
ERROR_CHECK(device->lib->airspy_set_sensitivity_gain(device->dev, server_config->airspy_sensitivity_gain), "<3>unable to set sensitivity gain");
fprintf(stdout, "sensitivity gain is configured: %d\n", server_config->airspy_sensitivity_gain);
break;
}
case AIRSPY_GAIN_LINEARITY: {
ERROR_CHECK(lib->airspy_set_linearity_gain(device->dev, server_config->airspy_linearity_gain), "<3>unable to set linearity gain");
ERROR_CHECK(device->lib->airspy_set_linearity_gain(device->dev, server_config->airspy_linearity_gain), "<3>unable to set linearity gain");
fprintf(stdout, "linearity gain is configured: %d\n", server_config->airspy_linearity_gain);
break;
}
case AIRSPY_GAIN_AUTO: {
ERROR_CHECK(lib->airspy_set_lna_agc(device->dev, 1), "<3>unable to set lna agc");
ERROR_CHECK(lib->airspy_set_mixer_agc(device->dev, 1), "<3>unable to set mixer agc");
ERROR_CHECK(device->lib->airspy_set_lna_agc(device->dev, 1), "<3>unable to set lna agc");
ERROR_CHECK(device->lib->airspy_set_mixer_agc(device->dev, 1), "<3>unable to set mixer agc");
fprintf(stdout, "auto gain is configured\n");
break;
}
case AIRSPY_GAIN_MANUAL: {
ERROR_CHECK(lib->airspy_set_vga_gain(device->dev, server_config->airspy_vga_gain), "<3>unable to set vga gain");
ERROR_CHECK(lib->airspy_set_mixer_gain(device->dev, server_config->airspy_mixer_gain), "<3>unable to set mixer gain");
ERROR_CHECK(lib->airspy_set_lna_gain(device->dev, server_config->airspy_lna_gain), "<3>unable to set lna gain");
ERROR_CHECK(device->lib->airspy_set_vga_gain(device->dev, server_config->airspy_vga_gain), "<3>unable to set vga gain");
ERROR_CHECK(device->lib->airspy_set_mixer_gain(device->dev, server_config->airspy_mixer_gain), "<3>unable to set mixer gain");
ERROR_CHECK(device->lib->airspy_set_lna_gain(device->dev, server_config->airspy_lna_gain), "<3>unable to set lna gain");
fprintf(stdout, "manual gain is configured: %d %d %d\n", server_config->airspy_vga_gain, server_config->airspy_mixer_gain, server_config->airspy_lna_gain);
break;
}
Expand All @@ -67,36 +95,14 @@ int airspy_device_create(uint32_t id, struct server_config *server_config, airsp
return 1;
}
}
fprintf(stdout, "airspy device enabled\n");
*plugin = device;
return 0;
}

void airspy_device_destroy(void *plugin) {
if (plugin == NULL) {
return;
}
struct airspy_device_t *device = (struct airspy_device_t *)plugin;
if (device->dev != NULL) {
device->lib->airspy_close(device->dev);
}
free(device);
fprintf(stdout, "airspy device disabled\n");
}

int airspy_device_callback(airspy_transfer *transfer) {
struct airspy_device_t *device = (struct airspy_device_t *)transfer->ctx;
device->sdr_callback(transfer->samples, transfer->sample_count * 2 * sizeof(int16_t), device->ctx);
return 0;
}

int airspy_device_start_rx(uint32_t band_freq, void *plugin) {
struct airspy_device_t *device = (struct airspy_device_t *)plugin;
ERROR_CHECK(device->lib->airspy_set_freq(device->dev, band_freq), "<3>unable to set freq");
return device->lib->airspy_start_rx(device->dev, airspy_device_callback, device);
}

void airspy_device_stop_rx(void *plugin) {
struct airspy_device_t *device = (struct airspy_device_t *)plugin;
device->lib->airspy_stop_rx(device->dev);
if (device->dev != NULL) {
device->lib->airspy_close(device->dev);
}
}
2 changes: 1 addition & 1 deletion src/sdr/rtlsdr_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,5 @@ void rtlsdr_device_destroy(void *plugin) {
free(device->output);
}
free(device);
fprintf(stdout, "rtl-sdr device disabled\n");
fprintf(stdout, "rtl-sdr device destroyed\n");
}

0 comments on commit f2cb72f

Please sign in to comment.