1from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
2from libcpp cimport bool
3
4cdef extern from "libbladeRF.h":
5    struct bladerf
6
7    enum bladerf_backend:
8        BLADERF_BACKEND_ANY         # "Don't Care" -- use any available backend
9        BLADERF_BACKEND_LINUX       # Linux kernel driver
10        BLADERF_BACKEND_LIBUSB      # libusb
11        BLADERF_BACKEND_CYPRESS     # CyAPI
12        BLADERF_BACKEND_DUMMY = 100 # Dummy used for development purposes
13
14    struct bladerf_devinfo:
15        bladerf_backend backend # Backend to use when connecting to device
16        char serial[33]         # Device serial number string
17        uint8_t usb_bus         # Bus # device is attached to
18        uint8_t usb_addr        # Device address on bus
19        unsigned int instance   # Device instance or ID
20
21    int bladerf_get_device_list(bladerf_devinfo **devices)
22    int bladerf_free_device_list(bladerf_devinfo *devices)
23
24    int bladerf_open(bladerf **device, const char *device_identifier)
25    void bladerf_close(bladerf *device)
26
27    struct s_bladerf_version "bladerf_version":
28        uint16_t major
29        uint16_t minor
30        uint16_t patch
31        const char* describe
32
33    void bladerf_version(s_bladerf_version *version)
34    int bladerf_get_serial(bladerf *dev, char *serial)
35
36    ctypedef enum bladerf_module:
37        BLADERF_MODULE_INVALID = -1    # Invalid module entry
38        BLADERF_MODULE_RX              # Receive Module
39        BLADERF_MODULE_TX              # Transmit Module
40
41
42    IF BLADERF_API_VERSION >= 1.91:
43        ctypedef int bladerf_channel
44        bladerf_channel BLADERF_CHANNEL_RX(bladerf_channel ch)
45        bladerf_channel BLADERF_CHANNEL_TX(bladerf_channel ch)
46        bladerf_channel BLADERF_CHANNEL_INVALID()
47        bool BLADERF_CHANNEL_IS_TX(bladerf_channel ch)
48        int bladerf_enable_module(bladerf *dev, bladerf_channel ch, bool enable)
49    ELSE:
50        int bladerf_enable_module(bladerf *dev, bladerf_module m, bool enable)
51
52
53
54    ctypedef enum bladerf_direction:
55        BLADERF_RX = 0
56        BLADERF_TX = 1
57
58    ctypedef enum bladerf_channel_layout:
59        BLADERF_RX_X1 = 0  # x1 RX (SISO)
60        BLADERF_TX_X1 = 1  # x1 TX (SISO
61        BLADERF_RX_X2 = 2  # x2 RX (MIMO)
62        BLADERF_TX_X2 = 3  # x2 TX (MIMO)
63
64    IF BLADERF_API_VERSION >= 1.91:
65        ctypedef enum bladerf_gain_mode:
66            BLADERF_GAIN_DEFAULT
67            BLADERF_GAIN_MGC
68            BLADERF_GAIN_FASTATTACK_AGC
69            BLADERF_GAIN_SLOWATTACK_AGC
70            BLADERF_GAIN_HYBRID_AGC
71    ELSE:
72        ctypedef enum bladerf_gain_mode:
73            BLADERF_GAIN_AUTOMATIC
74            BLADERF_GAIN_MANUAL
75
76    IF BLADERF_API_VERSION >= 1.91:
77        int bladerf_set_gain(bladerf *dev, bladerf_channel ch, int gain)
78        int bladerf_set_gain_mode(bladerf *dev, bladerf_channel ch, bladerf_gain_mode mode)
79    ELSE:
80        int bladerf_set_gain(bladerf *dev, bladerf_module mod, int gain)
81        int bladerf_set_gain_mode(bladerf *dev, bladerf_module mod, bladerf_gain_mode mode)
82
83    IF BLADERF_API_VERSION >= 1.91:
84        int bladerf_set_sample_rate(bladerf *dev, bladerf_channel ch, unsigned int rate, unsigned int *actual)
85        int bladerf_get_sample_rate(bladerf *dev, bladerf_channel ch, unsigned int  *rate)
86    ELSE:
87        int bladerf_set_sample_rate(bladerf *dev, bladerf_module module, unsigned int rate, unsigned int  *actual)
88        int bladerf_get_sample_rate(bladerf *dev, bladerf_module module, unsigned int *rate)
89
90    IF BLADERF_API_VERSION >= 1.91:
91        int bladerf_set_bandwidth(bladerf *dev, bladerf_channel ch, unsigned int bandwidth, unsigned int *actual)
92        int bladerf_get_bandwidth(bladerf *dev, bladerf_channel ch, unsigned int *bandwidth)
93    ELSE:
94        int bladerf_set_bandwidth(bladerf *dev, bladerf_module module, unsigned int bandwidth, unsigned int *actual)
95        int bladerf_get_bandwidth(bladerf *dev, bladerf_module module, unsigned int *bandwidth)
96
97    IF BLADERF_API_VERSION >= 2:
98        int bladerf_set_frequency(bladerf *dev, bladerf_channel ch, uint64_t frequency)
99        int bladerf_get_frequency(bladerf *dev, bladerf_channel ch, uint64_t *frequency)
100    ELIF BLADERF_API_VERSION >= 1.91:
101        int bladerf_set_frequency(bladerf *dev, bladerf_channel ch, unsigned int frequency)
102        int bladerf_get_frequency(bladerf *dev, bladerf_channel ch, unsigned int *frequency)
103    ELSE:
104        int bladerf_set_frequency(bladerf *dev, bladerf_module module, unsigned int frequency)
105        int bladerf_get_frequency(bladerf *dev, bladerf_module module, unsigned int *frequency)
106
107    ctypedef enum bladerf_format:
108        BLADERF_FORMAT_SC16_Q11
109        BLADERF_FORMAT_SC16_Q11_META
110
111    ctypedef uint64_t bladerf_timestamp
112
113    struct bladerf_metadata:
114        bladerf_timestamp timestamp
115        uint32_t flags
116        uint32_t status
117        unsigned int actual_count
118        uint8_t reserved[32]
119
120    IF BLADERF_API_VERSION >= 1.91:
121        int bladerf_sync_config(bladerf *dev, bladerf_channel_layout layout, bladerf_format format, unsigned int num_buffers, unsigned int buffer_size, unsigned int num_transfers, unsigned int stream_timeout)
122    ELSE:
123        int bladerf_sync_config(bladerf *dev, bladerf_module module, bladerf_format format, unsigned int num_buffers, unsigned int buffer_size, unsigned int num_transfers, unsigned int stream_timeout)
124
125    int bladerf_sync_rx(bladerf *dev, void *samples, unsigned int num_samples, bladerf_metadata *metadata, unsigned int timeout_ms)
126    int bladerf_sync_tx(bladerf *dev, const void *samples, unsigned int num_samples, bladerf_metadata *metadata, unsigned int timeout_ms)
127
128IF BLADERF_API_VERSION >= 2:
129    ctypedef uint64_t bladerf_frequency
130ELSE:
131    ctypedef unsigned int bladerf_frequency
132
133ctypedef unsigned int bladerf_sample_rate
134ctypedef unsigned int bladerf_bandwidth
135ctypedef int bladerf_gain