26
26
#include <math.h>
27
27
#include <rtl-sdr.h>
28
28
#include "acarsdec.h"
29
+ #include <signal.h>
30
+ #include <unistd.h>
29
31
30
32
// set the sameple rate by changing RTMULT
31
33
// 2.5Ms/s is the best but could be over limit for some hardware
@@ -39,6 +41,9 @@ static int status = 0;
39
41
static int rtlInBufSize = 0 ;
40
42
static int rtlInRate = 0 ;
41
43
44
+ static int watchdogCounter = 50 ;
45
+ static pthread_mutex_t cbMutex = PTHREAD_MUTEX_INITIALIZER ;
46
+
42
47
#define RTLOUTBUFSZ 1024
43
48
44
49
@@ -300,6 +305,10 @@ static void in_callback(unsigned char *rtlinbuff, uint32_t nread, void *ctx)
300
305
{
301
306
int n ;
302
307
308
+ pthread_mutex_lock (& cbMutex );
309
+ watchdogCounter = 50 ;
310
+ pthread_mutex_unlock (& cbMutex );
311
+
303
312
if (nread != rtlInBufSize ) {
304
313
fprintf (stderr , "warning: partial read\n" );
305
314
return ;
@@ -334,9 +343,46 @@ static void in_callback(unsigned char *rtlinbuff, uint32_t nread, void *ctx)
334
343
}
335
344
}
336
345
346
+ static void * readThreadEntryPoint (void * arg ) {
347
+ rtlsdr_read_async (dev , in_callback , NULL , 4 , rtlInBufSize );
348
+ pthread_mutex_lock (& cbMutex );
349
+ signalExit = 1 ;
350
+ pthread_mutex_unlock (& cbMutex );
351
+ return NULL ;
352
+ }
353
+
337
354
int runRtlSample (void )
338
355
{
339
- rtlsdr_read_async (dev , in_callback , NULL , 4 , rtlInBufSize );
356
+ pthread_t readThread ;
357
+ pthread_create (& readThread , NULL , readThreadEntryPoint , NULL );
358
+
359
+ pthread_mutex_lock (& cbMutex );
360
+
361
+ while (!signalExit ) {
362
+ if (-- watchdogCounter <= 0 ) {
363
+ fprintf (stderr , "No data from the SDR for 5 seconds, exiting ...\n" );
364
+ runRtlCancel (); // watchdog triggered after 5 seconds of no data from SDR
365
+ break ;
366
+ }
367
+ pthread_mutex_unlock (& cbMutex );
368
+ usleep (100 * 1000 ); // 0.1 seconds
369
+ pthread_mutex_lock (& cbMutex );
370
+ }
371
+
372
+ pthread_mutex_unlock (& cbMutex );
373
+
374
+ int count = 100 ; // 10 seconds
375
+ int err = 0 ;
376
+ // Wait on reader thread exit
377
+ while (count -- > 0 && (err = pthread_tryjoin_np (readThread , NULL ))) {
378
+ usleep (100 * 1000 ); // 0.1 seconds
379
+ }
380
+ if (err ) {
381
+ fprintf (stderr , "Receive thread termination failed, will raise SIGKILL to ensure we die!\n" );
382
+ raise (SIGKILL );
383
+ return 1 ;
384
+ }
385
+
340
386
return 0 ;
341
387
}
342
388
0 commit comments