-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtl_fm.c
More file actions
552 lines (488 loc) · 16.4 KB
/
rtl_fm.c
File metadata and controls
552 lines (488 loc) · 16.4 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
/*
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
* Copyright (C) 2012 by Hoernchen <la@tfc-server.de>
* Copyright (C) 2012 by Kyle Keen <keenerd@gmail.com>
* Copyright (C) 2013 by Elias Oenal <EliasOenal@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* written because people could not do real time
* FM demod on Atom hardware with GNU radio
* based on rtl_sdr.c and rtl_tcp.c
*
* lots of locks, but that is okay
* (no many-to-many locks)
*
* todo:
* sanity checks
* scale squelch to other input parameters
* test all the demodulations
* pad output on hop
* frequency ranges could be stored better
* scaled AM demod amplification
* auto-hop after time limit
* peak detector to tune onto stronger signals
* fifo for active hop frequency
* clips
* noise squelch
* merge stereo patch
* merge soft agc patch
* merge udp patch
* testmode to detect overruns
* watchdog to reset bad dongle
* fix oversampling
*/
#include "fm_httpd_common.h"
dongle_state*
dongle; //global because every dongle is independednt and only one in use
void usage( void )
{
fprintf( stderr,
"rtl_fm, a simple narrow band FM demodulator for RTL2832 based DVB-T receivers\n\n"
"Use:\trtl_fm -f freq [-options] [filename]\n"
"\t-f frequency_to_tune_to [Hz]\n"
"\t use multiple -f for scanning (requires squelch)\n"
"\t ranges supported, -f 118M:137M:25k\n"
"\t[-M modulation (default: fm)]\n"
"\t fm, wbfm, raw, am, usb, lsb\n"
"\t wbfm == -M fm -s 170k -o 4 -A fast -r 32k -l 0 -E deemp\n"
"\t raw mode outputs 2x16 bit IQ pairs\n"
"\t[-s sample_rate (default: 24k)]\n"
"\t[-d device_index (default: 0)]\n"
"\t[-g tuner_gain (default: automatic)]\n"
"\t[-l squelch_level (default: 0/off)]\n"
//"\t for fm squelch is inverted\n"
//"\t[-o oversampling (default: 1, 4 recommended)]\n"
"\t[-p ppm_error (default: 0)]\n"
"\t[-E enable_option (default: none)]\n"
"\t use multiple -E to enable multiple options\n"
"\t edge: enable lower edge tuning\n"
"\t dc: enable dc blocking filter\n"
"\t deemp: enable de-emphasis filter\n"
"\t direct: enable direct sampling\n"
"\t offset: enable offset tuning\n"
"\tfilename ('-' means stdout)\n"
"\t omitting the filename also uses stdout\n\n"
"Experimental options:\n"
"\t[-r resample_rate (default: none / same as -s)]\n"
"\t[-t squelch_delay (default: 10)]\n"
"\t +values will mute/scan, -values will exit\n"
"\t[-F fir_size (default: off)]\n"
"\t enables low-leakage downsample filter\n"
"\t size can be 0 or 9. 0 has bad roll off\n"
"\t[-A std/fast/lut choose atan math (default: std)]\n"
//"\t[-C clip_path (default: off)\n"
//"\t (create time stamped raw clips, requires squelch)\n"
//"\t (path must have '\%s' and will expand to date_time_freq)\n"
//"\t[-H hop_fifo (default: off)\n"
//"\t (fifo will contain the active frequency)\n"
"\n"
"Produces signed 16 bit ints, use Sox or aplay to hear them.\n"
"\trtl_fm ... | play -t raw -r 24k -es -b 16 -c 1 -V1 -\n"
"\t | aplay -r 24k -f S16_LE -t raw -c 1\n"
"\t -M wbfm | play -r 32k ... \n"
"\t -s 22050 | multimon -t raw /dev/stdin\n\n" );
exit( 1 );
}
#ifdef _WIN32
BOOL WINAPI
sighandler( int signum )
{
if ( CTRL_C_EVENT == signum ) {
fprintf( stderr, "Signal caught, exiting!\n" );
do_exit = 1;
rtlsdr_cancel_async( dongle->dev );
return TRUE;
}
return FALSE;
}
#else
static void sighandler( int signum )
{
if( do_exit != 1 ) {
fprintf( stderr, "Signal caught, exiting!\n" );
do_exit = 1;
rtlsdr_cancel_async( dongle->dev );
}
}
#endif
void rtlsdr_callback( unsigned char* buf, uint32_t len, void* ctx )
{
int i;
dongle_state* s = ctx;
demod_state* d = s->demod_target;
if ( do_exit ) {
return;
}
if ( !ctx ) {
return;
}
if ( s->mute ) {
for ( i = 0; i < s->mute; i++ ) {
buf[i] = 127;
}
s->mute = 0;
}
if ( !s->offset_tuning ) {
rotate_90( buf, len );
}
for ( i = 0; i < ( int )len; i++ ) {
s->buf16[i] = ( int16_t )buf[i] - 127;
}
pthread_rwlock_wrlock( &d->rw );
memcpy( d->lowpassed, s->buf16, 2 * len );
d->lp_len = len;
pthread_rwlock_unlock( &d->rw );
safe_cond_signal( &d->ready, &d->ready_m );
}
static void* dongle_thread_fn( void* arg )
{
dongle_state* s = arg;
rtlsdr_read_async( s->dev, rtlsdr_callback, s, 0, s->buf_len );
return 0;
}
static void* demod_thread_fn( void* arg )
{
dongle_state* dongle = arg;
demod_state* d = dongle->demod_target;
output_state* o = dongle->output_target;
controller_state* controller = dongle->controller;
while ( !do_exit ) {
safe_cond_wait( &d->ready, &d->ready_m );
pthread_rwlock_wrlock( &d->rw );
full_demod( d );
pthread_rwlock_unlock( &d->rw );
if ( d->exit_flag ) {
do_exit = 1;
}
if ( d->squelch_level && d->squelch_hits > d->conseq_squelch ) {
d->squelch_hits = d->conseq_squelch + 1; /* hair trigger */
safe_cond_signal( &controller->hop, &controller->hop_m );
//continue; instead of skiping write, we overwrite zeros
memset ( d->result, 0, 2 * d->result_len );
}
pthread_rwlock_wrlock( &o->rw );
memcpy( o->result, d->result, 2 * d->result_len );
o->result_len = d->result_len;
pthread_rwlock_unlock( &o->rw );
safe_cond_signal( &o->ready, &o->ready_m );
}
return 0;
}
static void* output_thread_fn( void* arg )
{
dongle_state* dongle = arg;
output_state* s = dongle->output_target;
while ( !do_exit ) {
// use timedwait and pad out under runs
safe_cond_wait( &s->ready, &s->ready_m );
pthread_rwlock_rdlock( &s->rw );
fwrite( s->result, 2, s->result_len, s->file );
pthread_rwlock_unlock( &s->rw );
}
return 0;
}
static void* controller_thread_fn( void* arg )
{
//This is where the channel hopping and dongle selection should happen.
uint64_t i;
dongle_state* dongle = arg;
controller_state* s = dongle->controller;
demod_state* demod = dongle->demod_target;
for( i = 0; i < s->freqs->size; i++ ) {
scan_node* n = ( scan_node* ) CirLinkList_get( s->freqs, i );
if( strcasecmp( "WBFM", n->mod ) == 0 ) {
n->freq += 16000;
}
}
scan_node* n = ( scan_node* ) CirLinkList_peek( s->freqs ); //first frequancy
while ( !do_exit ) {
setdemod( demod, n->mod );
//Set Squlch
demod->squelch_level = n->squelch_level;
/* Set the tuner gain */
/* Set Gain */
if ( n->gain == AUTO_GAIN ) {
verbose_auto_gain( dongle->dev );
} else {
n->gain = nearest_gain( dongle->dev, n->gain );
verbose_gain_set( dongle->dev, n->gain );
}
/* set up primary channel */
optimal_settings( n->freq, demod->rate_in, dongle );
if ( dongle->direct_sampling ) {
verbose_direct_sampling( dongle->dev, 1 );
}
if ( dongle->offset_tuning ) {
verbose_offset_tuning( dongle->dev );
}
/* Set the frequency */
verbose_set_frequency( dongle->dev, dongle->freq );
fprintf( stderr, "Oversampling input by: %ix.\n", demod->downsample );
fprintf( stderr, "Oversampling output by: %ix.\n", demod->post_downsample );
fprintf( stderr, "Buffer size: %0.2fms\n",
1000 * 0.5 * ( float )ACTUAL_BUF_LENGTH / ( float )dongle->rate );
/* Set the sample rate */
verbose_set_sample_rate( dongle->dev, dongle->rate );
fprintf( stderr, "Output at %u Hz.\n",
demod->rate_in / demod->post_downsample );
//Wait for Channel hop
if( do_exit ) {
return 0; //just check before wait
}
safe_cond_wait( &s->hop, &s->hop_m );
if ( s->freqs->size <= 1 ) {
continue;
}
/* Next Frequancy */
n = ( scan_node* ) CirLinkList_inc( s->freqs );
dongle->mute = BUFFER_DUMP;
}
return 0;
}
void frequency_range( controller_state* s, char* arg )
{
char* start, *stop, *step;
int i;
start = arg;
stop = strchr( start, ':' ) + 1;
stop[-1] = '\0';
step = strchr( stop, ':' ) + 1;
step[-1] = '\0';
for( i = ( int )atofs( start ); i <= ( int )atofs( stop );
i += ( int )atofs( step ) ) {
scan_node* node = malloc( sizeof( scan_node ) );
node->freq = ( uint32_t )i;
CirLinkList_push( s->freqs, node ); //add to scanner list
}
stop[-1] = ':';
step[-1] = ':';
}
int main( int argc, char** argv )
{
#ifndef _WIN32
struct sigaction sigact;
#endif
int r, opt;
int dev_given = 0;
int custom_ppm = 0;
controller_state* controller = controller_init();
demod_state* demod = demod_init();
output_state* output = output_init();
dongle = dongle_init( controller, demod, output ); //global var
scan_node* s_node = NULL;
while ( ( opt = getopt( argc, argv,
"d:f:g:s:b:l:o:t:r:p:E:F:A:M:h" ) ) != -1 ) {
switch ( opt ) {
case 'd':
dongle->dev_index = verbose_device_search( optarg );
dev_given = 1;
break;
case 'f':
if ( strchr( optarg, ':' ) ) {
frequency_range( controller, optarg );
} else {
//no current scan nodes
if(!s_node){
s_node=malloc( sizeof( scan_node ) );
CirLinkList_push( controller->freqs, s_node ); //add to scan list
}else{//submit this scan node an create new one with the defaults of the old scan node
CirLinkList_push( controller->freqs, s_node ); //add to scan list
s_node=malloc( sizeof( scan_node ) );
//copy accross old settings
memcpy( s_node, CirLinkList_peek( controller->freqs ), sizeof( scan_node ) ) ;
}
s_node->freq = ( uint32_t )atofs( optarg );
}
break;
case 'g':
if(!s_node) return 5;
s_node->gain = ( int )( atof( optarg ) * 10 );
break;
case 'l':
if(!s_node) return 5;
s_node->squelch_level =( int )atof( optarg );
break;
case 's':
demod->rate_in = ( uint32_t )atofs( optarg );
demod->rate_out = ( uint32_t )atofs( optarg );
break;
case 'r':
output->rate = ( int )atofs( optarg );
demod->rate_out2 = ( int )atofs( optarg );
break;
case 'o':
fprintf( stderr, "Warning: -o is very buggy\n" );
demod->post_downsample = ( int )atof( optarg );
if ( demod->post_downsample < 1 ||
demod->post_downsample > MAXIMUM_OVERSAMPLE ) {
fprintf( stderr, "Oversample must be between 1 and %i\n", MAXIMUM_OVERSAMPLE );
}
break;
case 't':
//TODO: set per freq
demod->conseq_squelch = ( int )atof( optarg );
if ( demod->conseq_squelch < 0 ) {
demod->conseq_squelch = -demod->conseq_squelch;
demod->terminate_on_squelch = 1;
}
break;
case 'p':
dongle->ppm_error = atoi( optarg );
custom_ppm = 1;
break;
case 'E':
//TODO: add some to perfreq
if ( strcmp( "edge", optarg ) == 0 ) {
controller->edge = 1;
}
if ( strcmp( "dc", optarg ) == 0 ) {
demod->dc_block = 1;
}
if ( strcmp( "deemp", optarg ) == 0 ) {
demod->deemph = 1;
}
if ( strcmp( "direct", optarg ) == 0 ) {
dongle->direct_sampling = 1;
}
if ( strcmp( "offset", optarg ) == 0 ) {
dongle->offset_tuning = 1;
}
break;
case 'F':
demod->downsample_passes = 1; /* truthy placeholder */
demod->comp_fir_size = atoi( optarg );
break;
case 'A':
if ( strcmp( "std", optarg ) == 0 ) {
demod->custom_atan = 0;
}
if ( strcmp( "fast", optarg ) == 0 ) {
demod->custom_atan = 1;
}
if ( strcmp( "lut", optarg ) == 0 ) {
atan_lut_init();
demod->custom_atan = 2;
}
break;
case 'M':
if(!s_node) return 5;
s_node->mod = optarg;
setdemod( demod, optarg );
break;
case 'h':
default:
usage();
break;
}
}
/* quadruple sample_rate to limit to Δθ to ±π/2 */
demod->rate_in *= demod->post_downsample;
if ( !output->rate ) {
output->rate = demod->rate_out;
}
sanity_checks( controller );
if ( controller->freqs->size > 1 ) {
demod->terminate_on_squelch = 0;
}
if ( argc <= optind ) {
output->filename = "-";
} else {
output->filename = argv[optind];
}
ACTUAL_BUF_LENGTH = lcm_post[demod->post_downsample] * DEFAULT_BUF_LENGTH;
if ( !dev_given ) {
dongle->dev_index = verbose_device_search( "0" );
}
if ( dongle->dev_index < 0 ) {
exit( 1 );
}
r = rtlsdr_open( &dongle->dev, ( uint32_t )dongle->dev_index );
if ( r < 0 ) {
fprintf( stderr, "Failed to open rtlsdr device #%d.\n", dongle->dev_index );
exit( 1 );
}
#ifndef _WIN32
sigact.sa_handler = sighandler;
sigemptyset( &sigact.sa_mask );
sigact.sa_flags = 0;
sigaction( SIGINT, &sigact, NULL );
sigaction( SIGTERM, &sigact, NULL );
sigaction( SIGQUIT, &sigact, NULL );
sigaction( SIGPIPE, &sigact, NULL );
#else
SetConsoleCtrlHandler( ( PHANDLER_ROUTINE ) sighandler, TRUE );
#endif
if ( demod->deemph ) {
demod->deemph_a = ( int )round( 1.0 / ( ( 1.0 - exp( -1.0 /
( demod->rate_out * 75e-6 ) ) ) ) );
}
/* Set the tuner gain */
if ( dongle->gain == AUTO_GAIN ) {
verbose_auto_gain( dongle->dev );
} else {
dongle->gain = nearest_gain( dongle->dev, dongle->gain );
verbose_gain_set( dongle->dev, dongle->gain );
}
verbose_ppm_set( dongle->dev, dongle->ppm_error );
if ( strcmp( output->filename, "-" ) == 0 ) { /* Write samples to stdout */
output->file = stdout;
#ifdef _WIN32
_setmode( _fileno( output->file ), _O_BINARY );
#endif
} else {
output->file = fopen( output->filename, "wb" );
if ( !output->file ) {
fprintf( stderr, "Failed to open %s\n", output->filename );
exit( 1 );
}
}
//r = rtlsdr_set_testmode(dongle->dev, 1);
/* Reset endpoint before we start reading from it (mandatory) */
verbose_reset_buffer( dongle->dev );
pthread_create( &controller->thread, NULL, controller_thread_fn,
( void* )( dongle ) );
usleep( 100000 );
pthread_create( &output->thread, NULL, output_thread_fn, ( void* )( dongle ) );
pthread_create( &demod->thread, NULL, demod_thread_fn, ( void* )( dongle ) );
pthread_create( &dongle->thread, NULL, dongle_thread_fn, ( void* )( dongle ) );
while ( !do_exit ) {
usleep( 100000 );
}
if ( do_exit ) {
fprintf( stderr, "\nUser cancel, exiting...\n" );
} else {
fprintf( stderr, "\nLibrary error %d, exiting...\n", r );
}
rtlsdr_cancel_async( dongle->dev );
pthread_join( dongle->thread, NULL );
fprintf( stderr, "Dongle Ended\n" );
safe_cond_signal( &demod->ready, &demod->ready_m );
pthread_join( demod->thread, NULL );
fprintf( stderr, "Demod Ended\n" );
safe_cond_signal( &output->ready, &output->ready_m );
pthread_join( output->thread, NULL );
fprintf( stderr, "Output Ended\n" );
safe_cond_signal( &controller->hop, &controller->hop_m );
pthread_join( controller->thread, NULL );
fprintf( stderr, "Controller Ended\n" );
demod_cleanup( demod );
output_cleanup( output );
controller_cleanup( controller );
dongle_cleanup( dongle ); //dongle stopped in sighandle
return r >= 0 ? r : -r;
}
// vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab