-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg2sdr.h
More file actions
1866 lines (1770 loc) · 72.5 KB
/
Copy pathpg2sdr.h
File metadata and controls
1866 lines (1770 loc) · 72.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* pg2sdr.h - PG2 host library, public API header
*
* Copyright (c) 2026 FlightAware All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PG2SDR_H
#define PG2SDR_H
/**
* \mainpage libpg2sdr API
*
* These pages document the libpg2sdr API.
*
* \section main_intro Introduction
*
* libpg2sdr provides an API for configuring and reading sample
* data from a FlightAware Pro Stick Gen 2 software-defined radio.
* For more details, see the main page at [somewhere]
*
* \section main_notes General notes
*
* libpg2sdr provides a C11-compatible API. Users of the library
* should `#include <pg2sdr.h>` and link against `-lpg2sdr`.
*
* Except as otherwise mentioned, all parts of the API are
* thread-safe, but are _not_ async-signal-safe.
*
* For API details, see the following topics:
*
* * \ref context
* * \ref errors
* * \ref device
* * \ref config
* * \ref gain
* * \ref streaming
* * \ref gaintable
* * \ref filtertable
*/
/**
* \file pg2sdr.h
*
* \brief The main pg2sdr header.
*
* The public pg2sdr API is entirely contained in a single header file.
*/
#include <libusb-1.0/libusb.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <endian.h>
#include <string.h>
#include <errno.h>
#if defined(__cplusplus)
extern "C" {
#endif
/**
* \defgroup context Library context and init/deinit
*
* Library contexts allow for multiple users of the pg2sdr library in
* the same process, without requiring coordination between the
* different users. Each library user should allocate a separate
* context, and pass it to the various API functions that expect a
* context.
*
* There is a context associated with each ::pg2sdr_device instance.
* APIs that accept ::pg2sdr_device arguments implicitly use the
* associated context.
*
* The lifetime of contexts is managed by pg2sdr_init() and
* pg2sdr_exit().
*
*/
/**
* \brief An opaque handle for library state.
* \ingroup context
*
* Library context is represented as a pointer to an opaque
* pg2sdr_context struct, where the details of the struct are
* internal to the library.
*
* \sa pg2sdr_init()
* \sa pg2sdr_exit()
*/
typedef struct pg2sdr__context pg2sdr_context;
/**
* \brief Severity of log messages passed to pg2sdr_log_callback()
* \ingroup context
*/
typedef enum {
PG2SDR_LOG_DEBUG, /**< debug messages */
PG2SDR_LOG_INFO, /**< informational messages */
PG2SDR_LOG_ERROR /**< errors */
} pg2sdr_log_level;
/**
* \brief Callback function passed to pg2sdr_set_log_callback()
* \ingroup context
*
* \param context the library context associated with this log message
* \param level the log level of this message
* \param message an ASCII message to log
*/
typedef void (*pg2sdr_log_callback)(pg2sdr_context *context,
pg2sdr_log_level level,
const char *message);
/**
* \brief Compile-time library API version
* \ingroup context
*
* A monotonically-increasing unsigned integer that will be increased
* on API change. This can be used to detect the presence of functions
* added in later library versions. (Currently, nothing makes use of
* this as there's only one library API released so far).
*/
#define PG2SDR_API_VERSION 0x01000100U
/**
* \brief Get runtime library API version
* \ingroup context
*
* This gets the _runtime_ API version of the library that was actually
* linked to. It may be higher than the version originally compiled
* against, if there were ABI-compatible upgrades to the installed
* library in the meantime.
*
* If the returned value is less than PG2SDR_API_VERSION, then you have
* a version skew problem -- your executable is linked to an older
* library version than the version it was compiled for.
*
* \return the PG2SDR_API_VERSION value of the currently linked library
*/
uint32_t pg2sdr_get_api_version(void);
/* if documenting the API, gloss over the macro API stuff */
#ifdef DOXYGEN
/**
* \brief Allocate a new library context.
* \ingroup context
*
* Context should be eventually freed by calling pg2sdr_exit()
*
* \param[out] ctx Storage for a pointer to the newly allocated context
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_LIBRARY_VERSION runtime library version is older than version compiled against
* \retval <0 negative error code on failure
*/
int pg2sdr_init(pg2sdr_context **ctx);
#else /* !DOXYGEN */
/* internal version of pg2sdr_init, checks that the runtime API version is at least min_api_version */
int pg2sdr__init_version(pg2sdr_context **ctx, uint32_t min_api_version);
#define pg2sdr_init(ctx) pg2sdr__init_version(ctx, PG2SDR_API_VERSION)
#endif
/**
* \brief Free a library context.
* \ingroup context
*
* Releases resources associated with a context previously allocated by
* pg2sdr_init().
*
* Must not be called while there are in-progress API calls using the
* context.
*
* After a call to pg2sdr_exit(), the context is no longer valid and
* should not be used.
*
* \param[in] ctx The context to free
* \retval ::PG2SDR_SUCCESS success
* \retval <0 negative error code on failure
*/
int pg2sdr_exit(pg2sdr_context *ctx);
/**
* \brief Set a callback to receive library logging.
* \ingroup context
*
* By default, libpg2sdr will log INFO and ERROR messages to stderr.
* If the PG2SDR_DEBUG environment variable is set, it will also log
* DEBUG messages to stderr.
*
* If log messages should be handled differently, provide a logging
* callback by calling this function. libpg2sdr will call the logging
* callback to perform logging, replacing the default behaviour.
*
* Each library context has a separate log callback setting.
*
* Logging callbacks must be threadsafe.
*
* \param[in] ctx The library context to change
* \param[in] callback The callback to call for each log message
* \retval ::PG2SDR_SUCCESS success
* \retval <0 negative error code on failure
*/
int pg2sdr_set_log_callback(pg2sdr_context *ctx, pg2sdr_log_callback callback);
/**
* \defgroup errors Error handling
*
* Most libpg2sdr API functions return an integer, with 0 indicating
* success and negative values representing errors. A few APIs can
* also return positive values on success.
*
* Negative error codes are chosen from the ::pg2sdr_error
* enumeration. Users of the library API can directly match on these
* values to handle specific errors differently, or use
* pg2sdr_strerror() and pg2sdr_strerror_r() to format the errors for
* human consumption.
*
* When libpg2sdr encounters an error from a system call or libusb,
* that error is remapped into a value within the
* ::PG2SDR_ERROR_SYSTEM_MIN .. ::PG2SDR_ERROR_SYSTEM_MAX or
* ::PG2SDR_ERROR_LIBUSB_MIN .. ::PG2SDR_ERROR_LIBUSB_MAX ranges,
* preserving the underlying error. pg2sdr_strerror() knows how to
* interpret values in this range by calling strerror() or
* libusb_strerror() as needed.
*
* Some error codes can be returned from any function, and are
* not individually documented in each function:
*
* * ::PG2SDR_ERROR_SYSTEM_MIN .. ::PG2SDR_ERROR_SYSTEM_MAX are returned
* if there is an error in a system/library call that sets errno which
* does not have a more specific PG2SDR_ERROR_* error code.
*
* * ::PG2SDR_ERROR_LIBUSB_MIN .. ::PG2SDR_ERROR_LIBUSB_MAX are returned
* if there is an error in a libusb call that does not have a more
* specific PG2SDR_ERROR_* error code.
*
* * ::PG2SDR_ERROR_NO_MEMORY is returned if memory allocation is needed,
* but allocation failed.
*
* * ::PG2SDR_ERROR_BAD_ARGUMENT is returned if a NULL device or context
* pointer is provided
*
* * ::PG2SDR_ERROR_CORRUPTION _might_ be returned if an invalid or
* freed device or context pointer is provided, but this is just a
* courtesy to try to detect bugs before heap corruption occurs -
* callers should not rely on this.
*
* * ::PG2SDR_ERROR_FIRMWARE_MISMATCH is returned if there is an
* unexpected protocol error in messages exchanged with the device;
* this generally indicates a bug or a mismatch in firmware versus
* library versions.
*
* * ::PG2SDR_ERROR_DISCONNECTED is returned if the device has disconnected
* from the USB bus (might be a power issue!)
*/
/**
* \brief Enumeration of possible negative error codes.
* \ingroup errors
*
* libpg2sdr API functions return an integer error code, where values
* >= 0 indicate success and values <0 indicate errors. The
* ::pg2sdr_error enumeration describes those error codes.
*/
enum pg2sdr_error {
PG2SDR_SUCCESS = 0, /**< no error */
PG2SDR_ERROR_NOT_FOUND = -1, /**< pg2sdr_open_single_device() found no matching devices */
PG2SDR_ERROR_DISCONNECTED = -2, /**< Device unexpectedly disconnected */
PG2SDR_ERROR_BAD_ARGUMENT = -3, /**< Bad argument to API call */
PG2SDR_ERROR_NO_MEMORY = -4, /**< Memory allocation failed */
PG2SDR_ERROR_NOT_IMPLEMENTED = -5, /**< Operation not implemented */
PG2SDR_ERROR_FIRMWARE_MISMATCH = -6, /**< Host/firmware version mismatch */
PG2SDR_ERROR_MULTIPLE_DEVICES = -7, /**< pg2sdr_open_single_device() found more than one matching device */
PG2SDR_ERROR_BUSY = -8, /**< Device already in use */
PG2SDR_ERROR_BAD_STATE = -9, /**< Operation not possible in this state */
PG2SDR_ERROR_TIMEOUT = -10, /**< Operation timed out */
PG2SDR_ERROR_CORRUPTION = -11, /**< Heap corruption, double-free, or use-after-free detected */
PG2SDR_ERROR_ACCESS = -12, /**< Insufficient permissions to access device */
PG2SDR_ERROR_LIBRARY_VERSION = -13, /**< Library version is older than expected version */
PG2SDR_ERROR_TRANSFER_OTHER = -200, /**< Unexpected libusb transfer status */
PG2SDR_ERROR_TRANSFER_STALL = -201, /**< Bulk endpoint stalled (libusb transfer status LIBUSB_TRANSFER_STALL) */
PG2SDR_ERROR_TRANSFER_OVERFLOW = -202, /**< Received unexpected data on bulk endpoint (libusb transfer status LIBUSB_TRANSFER_OVERFLOW) */
PG2SDR_ERROR_TRANSFER_FORMAT = -203, /**< Received malformed data on bulk endpoint */
PG2SDR_ERROR_TUNER_DETECT = -300, /**< Tuner not present on I2C bus */
PG2SDR_ERROR_TUNER_PLL_LOCK = -301, /**< Tuner LO PLL did not lock */
PG2SDR_ERROR_TUNER_PLL_RANGE = -302, /**< Required tuner LO PLL frequency out of range */
PG2SDR_ERROR_TUNER_I2C = -303, /**< Tuner I2C bus communication error */
PG2SDR_ERROR_ADC_RATE_RANGE = -304, /**< Required ADC sample rate out of range for ADC hardware */
PG2SDR_ERROR_SYSTEM_MAX = -1000, /**< Upper end of remapped errno range */
PG2SDR_ERROR_SYSTEM_MIN = -1999, /**< Lower end of remapped errno range */
PG2SDR_ERROR_LIBUSB_MAX = -2000, /**< Upper end of remapped libusb error range */
PG2SDR_ERROR_LIBUSB_MIN = -2999, /**< Lower end of remapped libusb error range */
};
/**
* \brief Format a pg2sdr error code as a human-readable error message.
* \ingroup errors
*
* This function is not threadsafe; the returned message may be a pointer
* to a static buffer area that is reused on each call. For a threadsafe
* version, use pg2sdr_strerror_r().
*
* \param[in] error an error code returned by the PG2SDR API
* \return an ASCIIZ error message, that may point to shared buffer space
*/
const char *pg2sdr_strerror(int error);
/**
* \brief Format a pg2sdr error code as a human-readable error message.
* \ingroup errors
*
* This is the threadsafe variant of pg2sdr_strerror(), using a user-
* provided buffer if needed.
*
* Note that in cases where the error message is fixed, \p buf is not
* used and a pointer to the fixed error message is returned directly.
*
* \param[in] error an error code returned by the PG2SDR API
* \param[in] buf a buffer to use if a non-static error message needs
* to be generated
* \param[in] buflen the size of \p buf
* \return an ASCIIZ error message, that may point within \p buf
*/
const char *pg2sdr_strerror_r(int error, char *buf, size_t buflen);
/**
* \defgroup device Device discovery and management
*
* This group of functions allow a libpg2sdr user to enumerate
* available PG2SDR devices, and open them for streaming.
*
* Two main datatypes are used by the library:
*
* pg2sdr_usb_device describes a single device available on the USB
* bus, without actively opening the device for streaming data. Arrays
* of pg2sdr_usb_device are created by pg2sdr_discover_devices()
* during device discovery, and should be freed by calling
* pg2sdr_free_device_list() when no longer required.
*
* pg2sdr_device is an opaque type used after device discovery has
* completed, to represent a device that has been actively opened for
* use, together with the associated library state. Instances of
* pg2sdr_device are allocated by pg2sdr_open_device(),
* pg2sdr_open_libusb_device(), or pg2sdr_open_single_device(), and
* should be closed/freed by calling pg2sdr_close_device() when no
* longer required.
*/
/**
* \brief Opaque type representing the state of an opened PG2SDR device.
* \ingroup device
*
* Allocation of these instances is managed by the library, users
* of the library deal only in terms of pointers to this type.
*
* \sa pg2sdr_open_device()
* \sa pg2sdr_open_libusb_device()
* \sa pg2sdr_open_single_device()
* \sa pg2sdr_close_device()
*/
typedef struct pg2sdr__device pg2sdr_device;
/**
* \brief Enum controlling how ADC data is converted.
* \ingroup config
*
* \sa pg2sdr_get_conversion_mode()
* \sa pg2sdr_set_conversion_mode()
*/
typedef enum {
PG2SDR_MODE_LOWIF_REAL, /**< Provide real-valued samples at low intermediate frequency (unconverted ADC output) */
PG2SDR_MODE_BASEBAND /**< Provide complex-valued samples at baseband */
} pg2sdr_conversion_mode_t;
/**
* \brief Enum controlling sideband tuning selection
* \ingroup config
*
* \sa pg2sdr_get_sideband()
* \sa pg2sdr_set_sideband()
*/
typedef enum {
PG2SDR_SIDEBAND_LOWER, /**< Place tuner LO above target frequency, and capture lower sideband */
PG2SDR_SIDEBAND_UPPER /**< Place tuner LO below target frequency, and capture upper sideband */
} pg2sdr_sideband_mode_t;
/**
* \brief Representation of an unopened PG2SDR device on the USB bus.
* \ingroup device
*
* This is allocated during device discovery by
* pg2sdr_discover_devices(), and freed by pg2sdr_free_device_list().
*
* The contents of pg2sdr_usb_device should not be directly modified.
* pg2sdr_usb_device::lu_device may be used as needed to discover additional information
* about the device if needed.
*/
typedef struct {
const char *serial; /**< Serial number of this device, ASCIIZ */
const char *ports; /**< Physical USB bus/port path for this device, ASCIIZ */
libusb_device *lu_device; /**< Underlying libusb_device for this device. */
} pg2sdr_usb_device;
/**
* \brief Sample buffer passed to ::pg2sdr_stream_callback
* \ingroup streaming
*
* Contains metadata and actual received samples to be processed by
* the library user.
*
* libpg2sdr guarantees that (to the best of the ability of the
* hardware to detect it), within a single ::pg2sdr_sample_buffer,
* there are no gaps in the received data. That is, if sample data is
* dropped and there is a detectable gap in the sample stream, then
* the library will ensure that the gap appears at a boundary between
* buffers, not within a buffer.
*/
typedef struct {
/** \brief Conversion mode applied to these samples.
*
* This determines the number of int16_t values per user sample.
*
* In ::PG2SDR_MODE_LOWIF_REAL mode,
* each sample is a single 16-bit value.
*
* In ::PG2SDR_MODE_BASEBAND mode, each
* sample is two 16-bit values representing the I and Q channels
* respectively.
*/
pg2sdr_conversion_mode_t mode;
/** \brief Sample data.
*
* Each user sample is one or two int16_t values, depending on the
* conversion mode (see #mode)
*/
int16_t *samples;
/** \brief Number of user samples available in #samples.
*
* This counts *user* samples, not int16_t values. The number of
* int16_t values per sample depends on the value of #mode.
*
* pg2sdr_set_buffer_size() controls the maximum number of user
* samples provided per sample buffer, i.e. the maximum potential
* value of "count".
*/
unsigned count;
/** \brief Sample timestamp at the start of this buffer.
*
* The sample timestamp is the cumulative number of received
* user samples, incrementing at the configured sampling rate.
*
* This counter may not initially start at zero, and may be
* discontinuous between buffers if an overrun causes data to be
* dropped. To detect discontinuities, look for callbacks where
* `current_buffer->timestamp > last_buffer->timestamp + last_buffer->count`.
*/
uint64_t timestamp;
} pg2sdr_sample_buffer;
/**
* \brief Callback type that receives sample buffers
* \ingroup streaming
*
* While streaming data with pg2sdr_stream_data(), this callback is
* repeatedly called as samples are received.
*
* If the callback returns true, the provided buffer is
* immediately freed. If the callback returns false, then it is
* the responsibility of the user code to eventually call
* pg2sdr_release_buffer() when the buffer is no longer needed. Sample
* buffers are heap-allocated as needed, so failing to release buffers
* will eventually exhaust available memory.
*
* \param[in] dev the device generating this callback
* \param[in] buffer Newly received samples to be processed. This
* buffer remains valid until either the callback function returns
* non-zero, or pg2sdr_release_buffer() is called.
* \param[in] user_data The opaque user_data value passed to
* pg2sdr_stream_data()
* \retval false caller will not free \p buffer
* \retval true caller will automatically free \p buffer
*/
typedef bool (*pg2sdr_stream_callback)(pg2sdr_device *dev, pg2sdr_sample_buffer *buffer, void *user_data);
/* Device discovery and open/close (device.c) */
/**
* \brief Enumerate available pg2sdr devices.
* \ingroup device
*
* Enumerates available devices and creates an array of pg2sdr_usb_device *,
* one per discovered device.
*
* Optionally, a serial prefix and/or port path can be provided to
* limit the search to only devices matching that prefix or path.
*
* The caller should not modify the returned device list. Once the
* device list is no longer required, pg2sdr_free_device_list() should
* be called to free storage associated with the list.
*
* The final value in the array is a sentinel NULL pointer. The return
* value indicates the number of devices in the array (possibly zero),
* not including the sentinel.
*
* \param[in] ctx A library context.
* \param[in] match_serial_prefix if not NULL, an ASCIIZ serial number
* prefix to match devices against
* \param[in] match_ports if not NULL, an ASCIIZ port path to match
* devices against
* \param[out] usb_device_list Storage for a pointer to an array of
* pg2sdr_usb_device
* \retval >=0 success, return value indicates size of returned array
* \retval <0 failure, negative error code
*
* \sa pg2sdr_free_device_list
* \sa pg2sdr_open_device
*/
ssize_t pg2sdr_discover_devices(pg2sdr_context *ctx,
const char *match_serial_prefix,
const char *match_ports,
pg2sdr_usb_device ***usb_device_list);
/**
* \brief Free a device list previously allocated by pg2sdr_discover_devices()
* \ingroup device
*
* After a device list is freed, the individual ::pg2sdr_usb_device
* instances in the list should not be used.
*
* \param[in] usb_device_list The device list to free
*/
void pg2sdr_free_device_list(pg2sdr_usb_device **usb_device_list);
/**
* \brief Open a device previously discovered by pg2sdr_discover_devices()
* \ingroup device
*
* \param[in] ctx The library context that allocated the device
* \param[in] usb_device The USB device to open
* \param[out] device Storage for the newly opened device instance
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_BUSY device is already in use by another process
* \retval ::PG2SDR_ERROR_ACCESS insufficient permissions to open device
* \retval <0 negative error code on failure
*
* \sa pg2sdr_discover_devices()
* \sa pg2sdr_close_device()
*/
int pg2sdr_open_device(pg2sdr_context *ctx, pg2sdr_usb_device *usb_device, pg2sdr_device **device);
/**
* \brief Open a device directly from a libusb device.
* \ingroup device
*
* No special checks are done to see if the device really is a PG2SDR,
* it's assumed to be a PG2SDR and used directly.
*
* \param[in] ctx The library context that allocated the device
* \param[in] lu_device The libusb device to open
* \param[out] device Storage for the newly opened device instance
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_BUSY device is already in use by another process
* \retval ::PG2SDR_ERROR_ACCESS insufficient permissions to open device
* \retval <0 negative error code on failure
*
* \sa pg2sdr_close_device()
*/
int pg2sdr_open_libusb_device(pg2sdr_context *ctx, libusb_device *lu_device, pg2sdr_device **device);
/**
* \brief Open a single device by serial number or port path.
* \ingroup device
*
* Search for a single connected pg2sdr device and open it. This is
* the preferred way for simple single-device library users to open a
* device.
*
* Optionally, a serial prefix and/or port path can be provided to
* limit the search to only devices matching that prefix or path.
*
* The search must be unambiguous -- exactly one device should match
* the given criteria. In most cases, there will only be a single
* pg2sdr device connected, so no special criteria are needed. If more
* than one device is connected, then match_serial_prefix or match_ports
* must be provided to select a single device.
*
* \param[in] ctx The library context to use to open the device
* \param[in] serial_prefix if not NULL, an ASCIIZ serial number
* prefix to match devices against
* \param[in] ports if not NULL, an ASCIIZ port path to match devices against
* \param[out] device Storage for the newly opened device instance
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_NOT_FOUND no matching device found
* \retval ::PG2SDR_ERROR_MULTIPLE_DEVICES more than one matching device found
* \retval ::PG2SDR_ERROR_BUSY device is already in use by another process
* \retval ::PG2SDR_ERROR_ACCESS insufficient permissions to open device
* \retval <0 negative error code on failure
*
* \sa pg2sdr_close_device()
*/
int pg2sdr_open_single_device(pg2sdr_context *ctx,
const char *serial_prefix,
const char *ports,
pg2sdr_device **device);
/**
* \brief Close a pg2sdr device.
* \ingroup device
*
* If the device is currently streaming data,
* ::PG2SDR_ERROR_BAD_STATE will be returned. To avoid this,
* call pg2sdr_stop_streaming() and wait for pg2sdr_stream_data() to
* return before calling pg2sdr_close_device().
*
* The device handle should not be used after being closed.
*
* \param[in] dev the device to close
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_BAD_STATE device is currently streaming data, cannot close
* \retval <0 negative error code on failure
*/
int pg2sdr_close_device(pg2sdr_device *dev);
/**
* \brief Get the serial number of an opened device.
* \ingroup device
*
* The returned string is a shared value specific to the device instance and
* should not be modified or freed, or used after the device is closed.
*
* \param[in] dev the device to query
* \return pointer to an ASCIIZ string, or NULL if the given device is
* not valid.
*/
const char *pg2sdr_get_serial(pg2sdr_device *dev);
/**
* \brief Get the USB port path of an opened device.
* \ingroup device
*
* The USB port path represents the physical port a device is
* connected to, in the same format used by ::pg2sdr_usb_device::ports
* and the "port" parameter of pg2sdr_open_single_device().
*
* The returned string is a shared value specific to the device
* instance and should not be modified or freed, or used after the
* device is closed.
*
* \param[in] dev the device to query
* \return pointer to an ASCIIZ string, or NULL if the given device is not valid.
*/
const char *pg2sdr_get_ports(pg2sdr_device *dev);
/**
* \brief Get the firmware version of an opened device.
* \ingroup device
*
* Firmware versions are 32-bit unsigned integers that can be directly
* compared.
*
* \param[in] dev the device to query
* \return the firmware version of the connected device, or 0 if the given device is not valid.
*/
uint32_t pg2sdr_get_firmware_version(pg2sdr_device *dev);
/**
* \defgroup config Device configuration
*
* This group of functions control the capture configuration of an
* opened device: tuned frequency and bandwidth, sample rate and
* format, gain settings, etc.
*
* Most changes to configuration settings require a subsequent call to
* pg2sdr_apply_changes() before taking effect. pg2sdr_apply_changes()
* is implicitly called by pg2sdr_stream_data(), so for simple cases
* where the configuration does not change at runtime, it's sufficient
* to just configure the device in advance of calling
* pg2sdr_stream_data().
*
* In cases where configuration settings interact, it is possible that
* out-of-range combinations of configuration settings are discovered
* only when pg2sdr_apply_changes() is called. It is also possible
* that, when several settings need to be changed, the intermediate
* configurations after each individual setting change are
* out-of-range, even if the final combination of all changes is
* valid. If you have several settings to change, call
* pg2sdr_apply_changes() once after all changes have been made, to
* avoid trying to apply an intermediate configuration that may
* be out-of-range.
*
* Some configuration settings cannot be changed while streaming is
* active. In these cases, ::PG2SDR_ERROR_BAD_STATE is returned, and
* changes are not applied.
*/
/**
* \brief Set the conversion mode for sample data received by user callbacks
* \ingroup config
*
* Sets the current conversion mode, controlling how raw ADC data is
* converted into samples passed to the user callback (user samples).
*
* If \p mode is ::PG2SDR_MODE_BASEBAND, then ADC data is converted to
* a complex baseband representation. A user sample is two int16_t
* values (I/Q, or real/imaginary). The complex baseband signal,
* centered around 0Hz, corresponds to the RF signal centered around
* the configured center frequency. This is the default mode, and the
* mode that most SDR clients will want to use.
*
* If \p mode is ::PG2SDR_MODE_LOWIF_REAL, then ADC data from the
* hardware is scaled to 16 bits, but is otherwise unmodified. Each
* user sample is a single int16_t value, with the full signed 16-bit
* range used. These samples are a digitization of the low-IF signal
* produced by the tuner, corresponding to one sideband of the RF
* input, either above or below the configured frequency depending on
* the configured sideband mode. The configured RF frequency maps to
* an IF frequency of 0Hz (though there will not be a useful signal at
* 0Hz, due to both the limits of the tuner bandpass filter, and LO
* leakage). This mode is mostly for lower-level debugging of the
* PG2SDR hardware or software itself, where direct inspection of the
* ADC data is useful.
*
* The conversion mode may not be changed while streaming is active.
*
* Call pg2sdr_apply_changes() to complete a change in conversion mode.
*
* \param[in] dev the device to configure
* \param[in] mode the new conversion mode to set
* \retval ::PG2SDR_SUCCESS Success
* \retval ::PG2SDR_ERROR_BAD_ARGUMENT \p mode is not a recognized conversion mode
* \retval ::PG2SDR_ERROR_BAD_STATE cannot change conversion mode while streaming is active
* \retval <0 negative error code on failure
*
* \sa pg2sdr_get_conversion_mode()
* \sa pg2sdr_apply_changes()
*/
int pg2sdr_set_conversion_mode(pg2sdr_device *dev, pg2sdr_conversion_mode_t mode);
/**
* \brief Get the current sample conversion mode
* \ingroup config
*
* This returns the currently requested mode, i.e. the mode
* passed to the last successful call to
* pg2sdr_set_conversion_mode(), even if pg2sdr_apply_changes() has
* not yet been called.
*
* \param[in] dev the device to query
* \param[out] mode a non-NULL pointer where the current format will be stored on success
* \retval ::PG2SDR_SUCCESS success
* \retval <0 negative error code on failure
*
* \sa pg2sdr_set_conversion_mode()
* \sa pg2sdr_apply_changes()
*/
int pg2sdr_get_conversion_mode(pg2sdr_device *dev, pg2sdr_conversion_mode_t *mode);
/**
* \brief Set size of sample buffer passed to user callbacks.
* \ingroup config
*
* Set the user buffer size to \p buffer_size user samples. The user
* buffer size controls the maximum number of samples contained in
* each pg2sdr_sample_buffer passed to the user callback while
* pg2sdr_stream_data is running.
*
* This setting also influences the size of internally allocated USB
* buffers. Setting large values may exceed system limits on USB
* buffer size and return an error.
*
* The buffer size may not be changed while streaming is active.
*
* \param[in] dev the device to configure
* \param[in] buffer_size the user buffer size, in samples
* \retval ::PG2SDR_SUCCESS Success
* \retval ::PG2SDR_ERROR_BAD_ARGUMENT \p buffer_size is out of range
* \retval ::PG2SDR_ERROR_BAD_STATE cannot change buffer size while streaming is active
* \retval <0 negative error code on failure
*
* \sa pg2sdr_get_buffer_size()
*/
int pg2sdr_set_buffer_size(pg2sdr_device *dev, size_t buffer_size);
/**
* \brief Get size of sample buffer passed to user callbacks.
* \ingroup config
*
* \param[in] dev the device to query
* \param[out] buffer_size a non-NULL pointer where the current buffer size will be stored on success
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_BAD_ARGUMENT if \p buffer_size is NULL
* \retval <0 negative error code on failure
*
* \sa pg2sdr_set_buffer_size()
*/
int pg2sdr_get_buffer_size(pg2sdr_device *dev, size_t *buffer_size);
/**
* \brief Set the current requested user sampling rate
* \ingroup config
*
* Change the requested user sampling rate. The user sampling rate is
* the effective sampling rate seen by the user-provided callback: it
* is the rate at which user samples are provided, \p rate user
* samples per second. The actual ADC sampling rate may be higher than
* the user sampling rate, with libpg2sdr providing decimation down to
* the user sampling rate.
*
* As a broad sanity check, libpg2sdr will immediately reject sample rates
* that are <1kHz or >100MHz. Sample rate support is still limited by
* hardware support, and rates that are accepted by this function may
* later be rejected by pg2sdr_apply_changes().
*
* May be called at any time; does not affect the sample rate of any
* currently active stream. Call pg2sdr_apply_changes() while not
* streaming data to complete the configuration change.
*
* \param[in] dev the device to configure
* \param[in] rate the new sampling rate, in Hz
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_BAD_ARGUMENT \p rate is out of range
* \retval <0 negative error code on failure
*
* \sa pg2sdr_get_sample_rate()
*/
int pg2sdr_set_sample_rate(pg2sdr_device *dev, double rate);
/**
* \brief Get current user sampling rate
* \ingroup config
*
* Gets the currently requested user sampling rate, and/or the actual
* effective sampling rate in use.
*
* The requested user sampling rate is the rate set by the last
* call to pg2sdr_set_sample_rate().
*
* The actual effective sampling rate is the effective sampling rate
* in use by the hardware, and may vary slightly from the requested
* rate due to hardware limitations (not all sampling rates can be
* exactly supported).
*
* If the requested sampling rate has been changed by calling
* pg2sdr_set_sample_rate(), but pg2sdr_apply_changes() has not yet
* been called, then the actual sampling rate has not yet been
* determined, and will return 0.
*
* \param[in] dev the device to query
* \param[out] requested if not NULL, location to store the requested user sampling rate
* \param[out] actual if not NULL, location to store the actual user sampling rate
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_BAD_ARGUMENT if both \p requested and \p actual are NULL
* \retval <0 negative error code on failure
*
* \sa pg2sdr_set_sample_rate()
*/
int pg2sdr_get_sample_rate(pg2sdr_device *dev, double *requested, double *actual);
/**
* \brief Decimation mode that auto-selects decimation to avoid bandpass filter issues
* \ingroup config
*
* This decimation mode adds decimation steps, if needed, to avoid the
* received signal falling into the high-pass filter region of the
* tuner bandpass filter. This avoids problems with the effective
* bandwidth being limited by the available tuner bandpass settings
* near 0Hz, an issue that mostly affects lower sample rates. This is
* the default decimation mode.
*
* \sa pg2sdr_set_decimation_mode()
*/
#define PG2SDR_DECIMATION_AUTO (-1)
/**
* \brief Set current decimation mode
* \ingroup config
*
* Configures the decimation mode, which controls additional,
* transparent, ADC sampling rate scaling and decimation performed in
* the receive path. In all cases, the user callback still receives
* data at the requested user sampling rate.
*
* Extra decimation can provide lower noise or better bandpass
* filtering characteristics, at the cost of needing to run the ADC at
* a higher sample rate and transfer more data over the USB bus.
*
* If \p decimation_mode is zero or positive, it is interpreted as the
* maximum number of decimate-by-2 stages to use, i.e. the total
* decimation factor is 2^decimation_node. The ADC sampling rate is
* scaled correspondingly. The actual number of stages used may be
* limited by the maximum available ADC sampling rate.
*
* Otherwise, \p decimation_mode may be ::PG2SDR_DECIMATION_AUTO,
* which adds decimation stages as needed to avoid losing signal to
* the low end of the tuner bandpass filter. This is the default
* setting.
*
* Decimation settings are ignored when the conversion mode is
* ::PG2SDR_MODE_LOWIF_REAL.
*
* May be called at any time; does not affect the decimation of any
* currently active stream. Call pg2sdr_apply_changes() while not
* streaming data to complete the configuration change.
*
* \param[in] dev the device to configure
* \param[in] decimation_mode the new decimation mode to set
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_BAD_ARGUMENT \p decimation_mode is not a valid decimation mode
* \retval <0 negative error code on failure
*
* \sa pg2sdr_get_decimation_mode()
*/
int pg2sdr_set_decimation_mode(pg2sdr_device *dev, int decimation_mode);
/**
* \brief Get current decimation mode and/or actual number of decimation steps
* \ingroup config
*
* The currently requested decimation mode is the mode given to the last call
* to pg2sdr_set_decimation_mode().
*
* The actual decimation is the number of decimation stages configured
* after a call to pg2sdr_apply_changes(). That is, for a returned
* value of N, there are N decimate-by-2 stages configured, with a
* total decimation factor of 2^N.
*
* If configuration settings have been changed but
* pg2sdr_apply_changes() has not yet been called, then the actual
* number of decimation steps has not yet been determined and will
* return 0.
*
* \param[in] dev the device to query
* \param[out] decimation_mode if non-NULL, pointer where the currently requested decimation mode will be stored on success
* \param[out] actual_decimation if non-NULL, pointer where the current number of decimation steps will be stored on success
* \retval ::PG2SDR_SUCCESS success
* \retval ::PG2SDR_ERROR_BAD_ARGUMENT if both \p decimation_mode and \p actual_decimation are NULL
* \retval <0 negative error code on failure
*
* \sa pg2sdr_set_decimation_mode()
*/
int pg2sdr_get_decimation_mode(pg2sdr_device *dev, int *decimation_mode, unsigned *actual_decimation);
/**
* \brief Set the current undersampling mode.
* \ingroup config
*
* Experimental, use with caution.
*
* libpg2sdr can be configured to run the ADC at a sampling rate that
* is less than the Nyquist rate of the IF signal, and deliberately
* capture parts of the signal above the Nyquist rate as aliases.
*
* The default mode (N=1) corresponds to the normal case where the ADC
* sampling rate is Fs, and we arrange for the IF signal to be
* contained between 0 .. Fs/2
*
* For values N>1, we arrange for the IF signal to be placed between
* (N-1)*Fs/2 and N*Fs/2. For example, N=2 places the IF signal
* between Fs/2 and Fs. This causes undersampling (a.k.a. bandpass
* sampling) to happen. The IF signal is above the Nyquist frequency
* for the ADC's sampling rate. One of the spectral replicas of the IF
* signal will lie between 0..Fs/2, and it is this replica/alias that
* is captured by the ADC.
*
* Undersampling with N>1 can help in cases where there's no suitable
* tuner bandpass filter available for f < Fs/2. There are tradeoffs,