libpg2sdr
Host library for the ProStick Gen 2 SDR
Loading...
Searching...
No Matches
pg2sdr.h
Go to the documentation of this file.
1/*
2 * pg2sdr.h - PG2 host library, public API header
3 *
4 * Copyright (c) 2026 FlightAware All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef PG2SDR_H
31#define PG2SDR_H
32
72#include <libusb-1.0/libusb.h>
73#include <stdio.h>
74#include <fcntl.h>
75#include <unistd.h>
76#include <stdbool.h>
77#include <stdlib.h>
78#include <endian.h>
79#include <string.h>
80#include <errno.h>
81
82#if defined(__cplusplus)
83extern "C" {
84#endif
85
115typedef struct pg2sdr__context pg2sdr_context;
116
126
135typedef void (*pg2sdr_log_callback)(pg2sdr_context *context,
136 pg2sdr_log_level level,
137 const char *message);
138
139
140
150#define PG2SDR_API_VERSION 0x01000100U
151
168
169/* if documenting the API, gloss over the macro API stuff */
170#ifdef DOXYGEN
183#else /* !DOXYGEN */
184/* internal version of pg2sdr_init, checks that the runtime API version is at least min_api_version */
185int pg2sdr__init_version(pg2sdr_context **ctx, uint32_t min_api_version);
186#define pg2sdr_init(ctx) pg2sdr__init_version(ctx, PG2SDR_API_VERSION)
187#endif
188
207
230
325
337const char *pg2sdr_strerror(int error);
338
355const char *pg2sdr_strerror_r(int error, char *buf, size_t buflen);
356
392typedef struct pg2sdr__device pg2sdr_device;
393
405
417
429typedef struct {
430 const char *serial;
431 const char *ports;
432 libusb_device *lu_device;
434
449typedef struct {
462
468 int16_t *samples;
469
479 unsigned count;
480
491 uint64_t timestamp;
493
517typedef bool (*pg2sdr_stream_callback)(pg2sdr_device *dev, pg2sdr_sample_buffer *buffer, void *user_data);
518
519/* Device discovery and open/close (device.c) */
520
553 const char *match_serial_prefix,
554 const char *match_ports,
555 pg2sdr_usb_device ***usb_device_list);
556
567
584
602int pg2sdr_open_libusb_device(pg2sdr_context *ctx, libusb_device *lu_device, pg2sdr_device **device);
603
636 const char *serial_prefix,
637 const char *ports,
638 pg2sdr_device **device);
639
657
670
687
699
772
791
816int pg2sdr_set_buffer_size(pg2sdr_device *dev, size_t buffer_size);
817
830int pg2sdr_get_buffer_size(pg2sdr_device *dev, size_t *buffer_size);
831
861
891int pg2sdr_get_sample_rate(pg2sdr_device *dev, double *requested, double *actual);
892
906#define PG2SDR_DECIMATION_AUTO (-1)
907
947int pg2sdr_set_decimation_mode(pg2sdr_device *dev, int decimation_mode);
948
975int pg2sdr_get_decimation_mode(pg2sdr_device *dev, int *decimation_mode, unsigned *actual_decimation);
976
1015int pg2sdr_set_undersampling_mode(pg2sdr_device *dev, unsigned undersampling_mode);
1016
1029int pg2sdr_get_undersampling_mode(pg2sdr_device *dev, unsigned *undersampling_mode);
1030
1070int pg2sdr_set_adc_limit(pg2sdr_device *dev, double adc_limit);
1071
1084int pg2sdr_get_adc_limit(pg2sdr_device *dev, double *adc_limit);
1085
1102int pg2sdr_get_adc_rate(pg2sdr_device *dev, double *actual_adc_rate);
1103
1143
1157
1178int pg2sdr_set_frequency(pg2sdr_device *dev, double frequency);
1179
1209int pg2sdr_get_frequency(pg2sdr_device *dev, double *requested, double *actual);
1210
1253int pg2sdr_set_bandpass(pg2sdr_device *dev, double low, double high);
1254
1283int pg2sdr_get_bandpass(pg2sdr_device *dev, double *req_low, double *req_high, double *actual_low, double *actual_high);
1284
1335
1387int pg2sdr_set_lna_gain(pg2sdr_device *dev, unsigned gain);
1388
1401int pg2sdr_set_mix_gain(pg2sdr_device *dev, unsigned gain);
1402
1415int pg2sdr_set_vga_gain(pg2sdr_device *dev, unsigned gain);
1416
1436int pg2sdr_get_stage_gains(pg2sdr_device *dev, unsigned *lna, unsigned *mix, unsigned *vga);
1437
1454int pg2sdr_set_lna_gain_db(pg2sdr_device *dev, double gain_db);
1455
1472int pg2sdr_set_mix_gain_db(pg2sdr_device *dev, double gain_db);
1473
1490int pg2sdr_set_vga_gain_db(pg2sdr_device *dev, double gain_db);
1491
1513int pg2sdr_get_stage_gains_db(pg2sdr_device *dev, double *lna_db, double *mix_db, double *vga_db);
1514
1531int pg2sdr_set_total_gain_db(pg2sdr_device *dev, double gain_db);
1532
1558int pg2sdr_get_total_gain_db(pg2sdr_device *dev, double *gain_db);
1559
1594typedef struct {
1595 double gain_db;
1596 unsigned lna_gain : 4;
1597 unsigned mix_gain : 4;
1598 unsigned vga_gain : 4;
1600
1637 const pg2sdr_gain_table_t *gain_table, size_t gain_table_size,
1638 const double *lna_table,
1639 const double *mix_table,
1640 const double *vga_table);
1641
1672 pg2sdr_gain_table_t **gain_table,
1673 size_t *gain_table_size,
1674 double *lna_table,
1675 double *mix_table,
1676 double *vga_table);
1677
1706typedef struct {
1707 /* floats here to reduce the size of the (large) table */
1708
1711 float ripple;
1713 unsigned hpf_corner : 4;
1714 unsigned lpf_narrow : 1;
1715 unsigned lpf_coarse : 2;
1716 unsigned lpf_fine : 4;
1717 unsigned lpf_q : 1;
1719
1720
1749 const pg2sdr_bandpass_table_t *bandpass_table, size_t bandpass_table_size);
1750
1771 pg2sdr_bandpass_table_t **bandpass_table,
1772 size_t *bandpass_table_size);
1773
1825int pg2sdr_stream_data(pg2sdr_device *dev, pg2sdr_stream_callback callback, void *user_data);
1826
1847
1860
1861#if defined(__cplusplus)
1862}
1863#endif
1864
1865
1866#endif /* PG2SDR_H */
pg2sdr_conversion_mode_t
Enum controlling how ADC data is converted.
Definition pg2sdr.h:401
int pg2sdr_set_adc_limit(pg2sdr_device *dev, double adc_limit)
Set hardware ADC sampling rate limit.
int pg2sdr_get_adc_rate(pg2sdr_device *dev, double *actual_adc_rate)
Get current ADC sampling rate.
int pg2sdr_get_adc_limit(pg2sdr_device *dev, double *adc_limit)
Get current hardware ADC sampling rate limit.
int pg2sdr_get_decimation_mode(pg2sdr_device *dev, int *decimation_mode, unsigned *actual_decimation)
Get current decimation mode and/or actual number of decimation steps.
int pg2sdr_set_conversion_mode(pg2sdr_device *dev, pg2sdr_conversion_mode_t mode)
Set the conversion mode for sample data received by user callbacks.
int pg2sdr_set_sideband(pg2sdr_device *dev, pg2sdr_sideband_mode_t mode)
Set the current sideband tuning mode.
int pg2sdr_get_sideband(pg2sdr_device *dev, pg2sdr_sideband_mode_t *mode)
Get current sideband mode.
int pg2sdr_set_decimation_mode(pg2sdr_device *dev, int decimation_mode)
Set current decimation mode.
int pg2sdr_get_frequency(pg2sdr_device *dev, double *requested, double *actual)
Get current center frequency.
pg2sdr_sideband_mode_t
Enum controlling sideband tuning selection.
Definition pg2sdr.h:413
int pg2sdr_get_undersampling_mode(pg2sdr_device *dev, unsigned *undersampling_mode)
Get current undersampling mode.
int pg2sdr_set_undersampling_mode(pg2sdr_device *dev, unsigned undersampling_mode)
Set the current undersampling mode.
int pg2sdr_set_buffer_size(pg2sdr_device *dev, size_t buffer_size)
Set size of sample buffer passed to user callbacks.
int pg2sdr_set_bandpass(pg2sdr_device *dev, double low, double high)
Set bandpass filter limits.
int pg2sdr_get_sample_rate(pg2sdr_device *dev, double *requested, double *actual)
Get current user sampling rate.
int pg2sdr_set_sample_rate(pg2sdr_device *dev, double rate)
Set the current requested user sampling rate.
int pg2sdr_get_buffer_size(pg2sdr_device *dev, size_t *buffer_size)
Get size of sample buffer passed to user callbacks.
int pg2sdr_get_bandpass(pg2sdr_device *dev, double *req_low, double *req_high, double *actual_low, double *actual_high)
Get requested and actual bandpass filter settings.
int pg2sdr_apply_changes(pg2sdr_device *dev)
Apply pending configuration changes.
int pg2sdr_set_frequency(pg2sdr_device *dev, double frequency)
Set the center frequency of received data.
int pg2sdr_get_conversion_mode(pg2sdr_device *dev, pg2sdr_conversion_mode_t *mode)
Get the current sample conversion mode.
@ PG2SDR_MODE_LOWIF_REAL
Definition pg2sdr.h:402
@ PG2SDR_MODE_BASEBAND
Definition pg2sdr.h:403
@ PG2SDR_SIDEBAND_LOWER
Definition pg2sdr.h:414
@ PG2SDR_SIDEBAND_UPPER
Definition pg2sdr.h:415
struct pg2sdr__context pg2sdr_context
An opaque handle for library state.
Definition pg2sdr.h:115
pg2sdr_log_level
Severity of log messages passed to pg2sdr_log_callback()
Definition pg2sdr.h:121
void(* pg2sdr_log_callback)(pg2sdr_context *context, pg2sdr_log_level level, const char *message)
Callback function passed to pg2sdr_set_log_callback()
Definition pg2sdr.h:135
int pg2sdr_set_log_callback(pg2sdr_context *ctx, pg2sdr_log_callback callback)
Set a callback to receive library logging.
int pg2sdr_init(pg2sdr_context **ctx)
Allocate a new library context.
int pg2sdr_exit(pg2sdr_context *ctx)
Free a library context.
uint32_t pg2sdr_get_api_version(void)
Get runtime library API version.
@ PG2SDR_LOG_DEBUG
Definition pg2sdr.h:122
@ PG2SDR_LOG_ERROR
Definition pg2sdr.h:124
@ PG2SDR_LOG_INFO
Definition pg2sdr.h:123
int pg2sdr_close_device(pg2sdr_device *dev)
Close a pg2sdr device.
struct pg2sdr__device pg2sdr_device
Opaque type representing the state of an opened PG2SDR device.
Definition pg2sdr.h:392
const char * pg2sdr_get_serial(pg2sdr_device *dev)
Get the serial number of an opened device.
const char * pg2sdr_get_ports(pg2sdr_device *dev)
Get the USB port path of an opened device.
void pg2sdr_free_device_list(pg2sdr_usb_device **usb_device_list)
Free a device list previously allocated by pg2sdr_discover_devices()
int pg2sdr_open_single_device(pg2sdr_context *ctx, const char *serial_prefix, const char *ports, pg2sdr_device **device)
Open a single device by serial number or port path.
int pg2sdr_open_device(pg2sdr_context *ctx, pg2sdr_usb_device *usb_device, pg2sdr_device **device)
Open a device previously discovered by pg2sdr_discover_devices()
uint32_t pg2sdr_get_firmware_version(pg2sdr_device *dev)
Get the firmware version of an opened device.
int pg2sdr_open_libusb_device(pg2sdr_context *ctx, libusb_device *lu_device, pg2sdr_device **device)
Open a device directly from a libusb device.
ssize_t pg2sdr_discover_devices(pg2sdr_context *ctx, const char *match_serial_prefix, const char *match_ports, pg2sdr_usb_device ***usb_device_list)
Enumerate available pg2sdr devices.
const char * pg2sdr_strerror(int error)
Format a pg2sdr error code as a human-readable error message.
pg2sdr_error
Enumeration of possible negative error codes.
Definition pg2sdr.h:291
const char * pg2sdr_strerror_r(int error, char *buf, size_t buflen)
Format a pg2sdr error code as a human-readable error message.
@ PG2SDR_ERROR_TUNER_PLL_RANGE
Definition pg2sdr.h:315
@ PG2SDR_ERROR_TUNER_PLL_LOCK
Definition pg2sdr.h:314
@ PG2SDR_ERROR_SYSTEM_MAX
Definition pg2sdr.h:319
@ PG2SDR_ERROR_TRANSFER_OVERFLOW
Definition pg2sdr.h:310
@ PG2SDR_ERROR_DISCONNECTED
Definition pg2sdr.h:295
@ PG2SDR_ERROR_FIRMWARE_MISMATCH
Definition pg2sdr.h:299
@ PG2SDR_ERROR_TRANSFER_FORMAT
Definition pg2sdr.h:311
@ PG2SDR_ERROR_TIMEOUT
Definition pg2sdr.h:303
@ PG2SDR_ERROR_TRANSFER_OTHER
Definition pg2sdr.h:308
@ PG2SDR_SUCCESS
Definition pg2sdr.h:292
@ PG2SDR_ERROR_BAD_ARGUMENT
Definition pg2sdr.h:296
@ PG2SDR_ERROR_LIBRARY_VERSION
Definition pg2sdr.h:306
@ PG2SDR_ERROR_MULTIPLE_DEVICES
Definition pg2sdr.h:300
@ PG2SDR_ERROR_NOT_IMPLEMENTED
Definition pg2sdr.h:298
@ PG2SDR_ERROR_BUSY
Definition pg2sdr.h:301
@ PG2SDR_ERROR_TRANSFER_STALL
Definition pg2sdr.h:309
@ PG2SDR_ERROR_CORRUPTION
Definition pg2sdr.h:304
@ PG2SDR_ERROR_NO_MEMORY
Definition pg2sdr.h:297
@ PG2SDR_ERROR_ADC_RATE_RANGE
Definition pg2sdr.h:317
@ PG2SDR_ERROR_LIBUSB_MAX
Definition pg2sdr.h:322
@ PG2SDR_ERROR_LIBUSB_MIN
Definition pg2sdr.h:323
@ PG2SDR_ERROR_TUNER_I2C
Definition pg2sdr.h:316
@ PG2SDR_ERROR_NOT_FOUND
Definition pg2sdr.h:294
@ PG2SDR_ERROR_ACCESS
Definition pg2sdr.h:305
@ PG2SDR_ERROR_BAD_STATE
Definition pg2sdr.h:302
@ PG2SDR_ERROR_TUNER_DETECT
Definition pg2sdr.h:313
@ PG2SDR_ERROR_SYSTEM_MIN
Definition pg2sdr.h:320
int pg2sdr_get_bandpass_table(pg2sdr_device *dev, pg2sdr_bandpass_table_t **bandpass_table, size_t *bandpass_table_size)
Get the current filter calibration tables used for bandpass filter selection.
int pg2sdr_set_bandpass_table(pg2sdr_device *dev, const pg2sdr_bandpass_table_t *bandpass_table, size_t bandpass_table_size)
Set the filter calibration tables used for bandpass filter selection.
int pg2sdr_get_stage_gains_db(pg2sdr_device *dev, double *lna_db, double *mix_db, double *vga_db)
Get per-stage gain settings in dB.
int pg2sdr_get_stage_gains(pg2sdr_device *dev, unsigned *lna, unsigned *mix, unsigned *vga)
Get per-stage gain step settings.
int pg2sdr_set_mix_gain(pg2sdr_device *dev, unsigned gain)
Set MIX stage gain step.
int pg2sdr_set_total_gain_db(pg2sdr_device *dev, double gain_db)
Set total gain, in dB.
int pg2sdr_set_vga_gain_db(pg2sdr_device *dev, double gain_db)
Set VGA stage gain in dB.
int pg2sdr_set_mix_gain_db(pg2sdr_device *dev, double gain_db)
Set MIX stage gain in dB.
int pg2sdr_set_lna_gain(pg2sdr_device *dev, unsigned gain)
Set LNA stage gain step.
int pg2sdr_get_total_gain_db(pg2sdr_device *dev, double *gain_db)
Get total gain setting, in dB.
int pg2sdr_set_lna_gain_db(pg2sdr_device *dev, double gain_db)
Set LNA stage gain in dB.
int pg2sdr_set_vga_gain(pg2sdr_device *dev, unsigned gain)
Set VGA stage gain step.
int pg2sdr_get_gain_tables(pg2sdr_device *dev, pg2sdr_gain_table_t **gain_table, size_t *gain_table_size, double *lna_table, double *mix_table, double *vga_table)
Get the current gain tables used for the dB-based gain API.
int pg2sdr_set_gain_tables(pg2sdr_device *dev, const pg2sdr_gain_table_t *gain_table, size_t gain_table_size, const double *lna_table, const double *mix_table, const double *vga_table)
Set the gain tables used for the dB-based gain API.
void pg2sdr_release_buffer(pg2sdr_sample_buffer *buffer)
Free a sample buffer previously passed to a pg2sdr_stream_callback callback.
bool(* pg2sdr_stream_callback)(pg2sdr_device *dev, pg2sdr_sample_buffer *buffer, void *user_data)
Callback type that receives sample buffers.
Definition pg2sdr.h:517
int pg2sdr_stream_data(pg2sdr_device *dev, pg2sdr_stream_callback callback, void *user_data)
Stream ADC data and pass converted samples to user callback.
int pg2sdr_stop_streaming(pg2sdr_device *dev)
Halt data streaming.
Tuner bandpass filter table entry.
Definition pg2sdr.h:1706
float upper_corner
Definition pg2sdr.h:1710
unsigned lpf_fine
Definition pg2sdr.h:1716
unsigned lpf_coarse
Definition pg2sdr.h:1715
unsigned hpf_corner
Definition pg2sdr.h:1713
float ripple
Definition pg2sdr.h:1711
unsigned lpf_q
Definition pg2sdr.h:1717
unsigned lpf_narrow
Definition pg2sdr.h:1714
float lower_corner
Definition pg2sdr.h:1709
Gain table entry.
Definition pg2sdr.h:1594
unsigned vga_gain
Definition pg2sdr.h:1598
unsigned lna_gain
Definition pg2sdr.h:1596
double gain_db
Definition pg2sdr.h:1595
unsigned mix_gain
Definition pg2sdr.h:1597
Sample buffer passed to pg2sdr_stream_callback.
Definition pg2sdr.h:449
uint64_t timestamp
Sample timestamp at the start of this buffer.
Definition pg2sdr.h:491
int16_t * samples
Sample data.
Definition pg2sdr.h:468
pg2sdr_conversion_mode_t mode
Conversion mode applied to these samples.
Definition pg2sdr.h:461
unsigned count
Number of user samples available in samples.
Definition pg2sdr.h:479
Representation of an unopened PG2SDR device on the USB bus.
Definition pg2sdr.h:429
const char * serial
Definition pg2sdr.h:430
libusb_device * lu_device
Definition pg2sdr.h:432
const char * ports
Definition pg2sdr.h:431