1- // Part of dump1090, a Mode S message decoder for RTLSDR devices.
1+ h // Part of dump1090, a Mode S message decoder for RTLSDR devices.
22//
33// sdr_bladerf.h: UHD support (header)
44//
2525// complex int16
2626#define SAMPLE_SIZE 4
2727
28- void free_usrp ();
29- void free_device_args ();
30- void free_rx_metadata ();
31- void free_rx_streamer ();
32-
33-
3428static struct {
35- uhd_usrp_handle usrp ;
36- uhd_rx_streamer_handle rx_streamer ;
37- uhd_rx_metadata_handle md ;
29+ uhd_usrp_handle * usrp ;
30+ uhd_rx_metadata_handle * md ;
31+ uhd_rx_streamer_handle * rx_streamer ;
3832 char * device_args ;
39- size_t free_func_ptr ;
4033 size_t channel ;
4134 char * sample_data ;
4235
4336 iq_convert_fn converter ;
4437 struct converter_state * converter_state ;
4538} uhd ;
4639
47-
48- void (* free_func []) (void ) = {
49- NULL ,
50- free_device_args ,
51- free_usrp ,
52- free_rx_streamer ,
53- free_rx_metadata ,
54- };
55-
5640void uhdClose () {
57- while (uhd .free_func_ptr ) {
58- (* free_func [uhd .free_func_ptr -- ])();
41+ if (uhd .rx_streamer ) {
42+ uhd_rx_streamer_free (uhd .rx_streamer );
43+ }
44+ if (uhd .md ) {
45+ uhd_rx_metadata_free (uhd .md );
46+ }
47+ if (uhd .usrp ) {
48+ uhd_usrp_free (uhd .usrp );
49+ }
50+ if (uhd .sample_data ) {
51+ free (uhd .sample_data );
52+ }
53+ if (uhd .device_args ) {
54+ free (uhd .device_args );
5955 }
6056}
6157
6258void uhdInitConfig () {
59+ uhd .usrp = NULL ;
60+ uhd .md = NULL ;
61+ uhd .rx_streamer = NULL ;
6362 uhd .device_args = NULL ;
64- uhd .free_func_ptr = 0 ;
63+ uhd .sample_data = NULL ;
6564 uhd .channel = 0 ;
6665}
6766
@@ -76,133 +75,158 @@ bool uhdHandleOption(int argc, char **argv, int *jptr) {
7675 int j = * jptr ;
7776 bool more = (j + 1 < argc );
7877 if (!strcmp (argv [j ], "--uhd-device-args" ) && more ) {
79- uhd .device_args = strdup (argv [++ j ]);
78+ uhd .device_args = strdup (argv [++ j ]);
8079 } else {
81- return false;
80+ return false;
8281 }
8382
8483 * jptr = j ;
8584 return true;
8685}
8786
88- #define UHD_RUN (x ) {if (x) { uhdClose(); return false; }}
89- #define UHD_INIT (x ) {++uhd.free_func_ptr; UHD_RUN(x)}
90-
9187bool uhdOpen () {
9288 uhd_tune_request_t tune_request = {
93- .target_freq = Modes .freq ,
94- .rf_freq_policy = UHD_TUNE_REQUEST_POLICY_AUTO ,
95- .dsp_freq_policy = UHD_TUNE_REQUEST_POLICY_AUTO ,
89+ .target_freq = Modes .freq ,
90+ .rf_freq_policy = UHD_TUNE_REQUEST_POLICY_AUTO ,
91+ .dsp_freq_policy = UHD_TUNE_REQUEST_POLICY_AUTO ,
9692 };
9793 uhd_tune_result_t tune_result ;
9894
99- ++ uhd .free_func_ptr ;
10095 if (!uhd .device_args ) {
101- uhd .device_args = strdup ("" );
96+ uhd .device_args = strdup ("" );
97+ }
98+
99+ int uhd_error ;
100+
101+ uhd .usrp = malloc (sizeof (uhd_usrp_handle ));
102+ uhd_error = uhd_usrp_make (uhd .usrp , uhd .device_args );
103+ if (uhd_error ) {
104+ fprintf (stderr , "uhd_usrp_make() failed: %x" , uhd_error );
105+ goto failed ;
106+ }
107+
108+ uhd .md = malloc (sizeof (uhd_rx_metadata_handle ));
109+ uhd_error = uhd_rx_metadata_make (uhd .md );
110+ if (uhd_error ) {
111+ fprintf (stderr , "uhd_rx_metadata_make() failed: %x" , uhd_error );
112+ goto failed ;
102113 }
103114
104- UHD_INIT (uhd_usrp_make (& uhd .usrp , uhd .device_args ))
105- UHD_INIT (uhd_rx_streamer_make (& uhd .rx_streamer ))
106- UHD_INIT (uhd_rx_metadata_make (& uhd .md ))
115+ uhd .rx_streamer = malloc (sizeof (uhd_rx_streamer_handle ));
116+ uhd_error = uhd_rx_streamer_make (uhd .rx_streamer );
117+ if (uhd_error ) {
118+ fprintf (stderr , "uhd_rx_streamer_make() failed: %x" , uhd_error );
119+ goto failed ;
120+ }
107121
108122 fprintf (stderr , "setting bw, rx rate to %fMsps\n" , Modes .sample_rate / 1e6 );
109- UHD_RUN (uhd_usrp_set_rx_rate (uhd .usrp , Modes .sample_rate , uhd .channel ))
110- UHD_RUN (uhd_usrp_set_rx_bandwidth (uhd .usrp , Modes .sample_rate , uhd .channel ))
123+ uhd_error = uhd_usrp_set_rx_rate (* uhd .usrp , Modes .sample_rate , uhd .channel );
124+ if (uhd_error ) {
125+ fprintf (stderr , "uhd_usrp_set_rx_rate() failed: %x" , uhd_error );
126+ goto failed ;
127+ }
128+
129+ uhd_error = uhd_usrp_set_rx_bandwidth (* uhd .usrp , Modes .sample_rate , uhd .channel );
130+ if (uhd_error ) {
131+ fprintf (stderr , "uhd_usrp_set_rx_bandwidth() failed: %x" , uhd_error );
132+ goto failed ;
133+ }
134+
111135 if (Modes .gain != MODES_DEFAULT_GAIN ) {
112- fprintf (stderr , "setting rx gain to %f\n" , Modes .gain );
113- UHD_RUN (uhd_usrp_set_rx_gain (uhd .usrp , Modes .gain , uhd .channel , "" ))
136+ fprintf (stderr , "setting rx gain to %f\n" , Modes .gain );
137+ uhd_error = uhd_usrp_set_rx_gain (* uhd .usrp , Modes .gain , uhd .channel , "" );
138+ if (!uhd_error ) {
139+ fprintf (stderr , "uhd_usrp_set_rx_gain() failed: %x" , uhd_error );
140+ goto failed ;
141+ }
114142 }
143+
115144 fprintf (stderr , "setting rx freq to %fMHz\n" , Modes .freq / 1e6 );
116- UHD_RUN (uhd_usrp_set_rx_freq (uhd .usrp , & tune_request , uhd .channel , & tune_result ))
145+ uhd_error = uhd_usrp_set_rx_freq (* uhd .usrp , & tune_request , uhd .channel , & tune_result );
146+ if (uhd_error ) {
147+ fprintf (stderr , "uhd_usrp_set_rx_freq() failed: %x" , uhd_error );
148+ goto failed ;
149+ }
117150
118151 uhd .converter = init_converter (
119- INPUT_SC16Q11 , Modes .sample_rate , Modes .dc_filter , & uhd .converter_state );
152+ INPUT_SC16Q11 , Modes .sample_rate , Modes .dc_filter , & uhd .converter_state );
120153
121154 return true;
155+
156+ failed :
157+ uhdClose ();
158+ return false;
122159}
123160
124161void uhdRun () {
125162 uhd_stream_args_t stream_args = {
126- .cpu_format = "sc16" ,
127- .otw_format = "sc16" ,
128- .args = "" ,
129- .channel_list = & uhd .channel ,
130- .n_channels = 1
163+ .cpu_format = "sc16" ,
164+ .otw_format = "sc16" ,
165+ .args = "" ,
166+ .channel_list = & uhd .channel ,
167+ .n_channels = 1
131168 };
132169
133170 uhd_stream_cmd_t stream_cmd = {
134- .stream_mode = UHD_STREAM_MODE_START_CONTINUOUS ,
135- .stream_now = true
171+ .stream_mode = UHD_STREAM_MODE_START_CONTINUOUS ,
172+ .stream_now = true
136173 };
137174
138175 uhd_stream_cmd_t stop_stream_cmd = {
139- .stream_mode = UHD_STREAM_MODE_STOP_CONTINUOUS ,
176+ .stream_mode = UHD_STREAM_MODE_STOP_CONTINUOUS ,
140177 };
141178
142179 size_t num_rx_samps = 0 ;
143180 struct mag_buf * outbuf = NULL ;
181+ int uhd_error ;
144182
145- if (uhd_usrp_get_rx_stream (uhd .usrp , & stream_args , uhd .rx_streamer )) {
146- fprintf (stderr , "uhd_usrp_get_rx_stream() failed\n" );
147- return ;
183+ uhd_error = uhd_usrp_get_rx_stream (* uhd .usrp , & stream_args , * uhd .rx_streamer );
184+ if (uhd_error ) {
185+ fprintf (stderr , "uhd_usrp_get_rx_stream() failed: %x\n" , uhd_error );
186+ return ;
148187 }
149188
150189 fprintf (stderr , "streaming...\n" );
151190 uhd .sample_data = malloc (MODES_MAG_BUF_SAMPLES * SAMPLE_SIZE );
152191 int64_t full_secs ;
153192 double frac_secs ;
154- uhd_rx_metadata_time_spec (uhd .md , & full_secs , & frac_secs );
193+ uhd_rx_metadata_time_spec (* uhd .md , & full_secs , & frac_secs );
155194 double first_recv_time = full_secs + frac_secs ;
156195
157- if (uhd_rx_streamer_issue_stream_cmd (uhd .rx_streamer , & stream_cmd )) {
158- fprintf (stderr , "could not start stream\n" );
159- return ;
196+ uhd_error = uhd_rx_streamer_issue_stream_cmd (* uhd .rx_streamer , & stream_cmd );
197+ if (uhd_error ) {
198+ fprintf (stderr , "uhd_rx_streamer_issue_stream_cmd() failed: %x\n" , uhd_error );
199+ return ;
160200 }
161201
162202 for (;;) {
163- outbuf = fifo_acquire (0 );
164- if (!outbuf ) {
165- fprintf (stderr , "could not get outbuf\n" );
166- break ;
167- }
168-
169- outbuf -> sysTimestamp = mstime ();
170- uhd_rx_streamer_recv (uhd .rx_streamer , (void * * ) & uhd .sample_data , MODES_MAG_BUF_SAMPLES , & uhd .md , 3.0 , false, & num_rx_samps );
171- uhd_rx_metadata_error_code_t error_code ;
172- uhd_rx_metadata_error_code (uhd .md , & error_code );
173- if (error_code != UHD_RX_METADATA_ERROR_CODE_NONE ) {
174- fprintf (stderr , "error code while streaming: %x\n" , error_code );
175- break ;
176- }
177- uhd_rx_metadata_time_spec (uhd .md , & full_secs , & frac_secs );
178- double recv_time = full_secs + frac_secs ;
179- outbuf -> sampleTimestamp = (recv_time - first_recv_time ) * 12e6 / Modes .sample_rate ;
180-
181- uhd .converter (uhd .sample_data , & outbuf -> data [outbuf -> overlap ], num_rx_samps , uhd .converter_state , & outbuf -> mean_level , & outbuf -> mean_power );
182- outbuf -> validLength = outbuf -> overlap + num_rx_samps ;
183- outbuf -> flags = 0 ;
184-
185- fifo_enqueue (outbuf );
186- }
187-
188- uhd_rx_streamer_issue_stream_cmd (uhd .rx_streamer , & stop_stream_cmd );
189- }
190-
191- void free_device_args (void ) {
192- if (uhd .sample_data ) {
193- free (uhd .sample_data );
203+ outbuf = fifo_acquire (0 );
204+ if (!outbuf ) {
205+ if (Modes .exit ) {
206+ break ;
207+ }
208+ fprintf (stderr , "could not get outbuf\n" );
209+ continue ;
210+ }
211+
212+ outbuf -> sysTimestamp = mstime ();
213+ uhd_rx_streamer_recv (* uhd .rx_streamer , (void * * ) & uhd .sample_data , MODES_MAG_BUF_SAMPLES , uhd .md , 3.0 , false, & num_rx_samps );
214+ uhd_rx_metadata_error_code_t error_code ;
215+ uhd_rx_metadata_error_code (* uhd .md , & error_code );
216+ if (error_code != UHD_RX_METADATA_ERROR_CODE_NONE ) {
217+ fprintf (stderr , "error code while streaming: %x\n" , error_code );
218+ break ;
219+ }
220+ uhd_rx_metadata_time_spec (* uhd .md , & full_secs , & frac_secs );
221+ double recv_time = full_secs + frac_secs ;
222+ outbuf -> sampleTimestamp = (recv_time - first_recv_time ) * 12e6 / Modes .sample_rate ;
223+
224+ uhd .converter (uhd .sample_data , & outbuf -> data [outbuf -> overlap ], num_rx_samps , uhd .converter_state , & outbuf -> mean_level , & outbuf -> mean_power );
225+ outbuf -> validLength = outbuf -> overlap + num_rx_samps ;
226+ outbuf -> flags = 0 ;
227+
228+ fifo_enqueue (outbuf );
194229 }
195- free (uhd .device_args );
196- }
197-
198- void free_usrp (void ) {
199- uhd_usrp_free (& uhd .usrp );
200- }
201-
202- void free_rx_metadata (void ) {
203- uhd_rx_metadata_free (& uhd .md );
204- }
205230
206- void free_rx_streamer (void ) {
207- uhd_rx_streamer_free (& uhd .rx_streamer );
231+ uhd_rx_streamer_issue_stream_cmd (* uhd .rx_streamer , & stop_stream_cmd );
208232}
0 commit comments