Skip to content

Commit 22f0c81

Browse files
committed
add watchDog to detect hanging rtl-sdr
1 parent ad8fd11 commit 22f0c81

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

acarsdec.c

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ int mdly=600;
4444
int hourly = 0;
4545
int daily = 0;
4646

47+
int signalExit = 0;
48+
4749
#ifdef WITH_RTL
4850
int gain = -100;
4951
int ppm = 0;
@@ -159,6 +161,7 @@ static void sigintHandler(int signum)
159161
else
160162
fprintf(stderr, "Received signal %d, exiting.\n", strsignal(signum));
161163
#ifdef WITH_RTL
164+
signalExit = 1;
162165
runRtlCancel();
163166
#else
164167
exit(0);

acarsdec.h

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ extern unsigned long wrkmask;
130130
extern pthread_mutex_t datamtx;
131131
extern pthread_cond_t datawcd;
132132

133+
extern int signalExit;
133134

134135
extern int inpmode;
135136
extern int verbose;

rtl.c

+47-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <math.h>
2727
#include <rtl-sdr.h>
2828
#include "acarsdec.h"
29+
#include <signal.h>
30+
#include <unistd.h>
2931

3032
// set the sameple rate by changing RTMULT
3133
// 2.5Ms/s is the best but could be over limit for some hardware
@@ -39,6 +41,9 @@ static int status = 0;
3941
static int rtlInBufSize = 0;
4042
static int rtlInRate = 0;
4143

44+
static int watchdogCounter = 50;
45+
static pthread_mutex_t cbMutex = PTHREAD_MUTEX_INITIALIZER;
46+
4247
#define RTLOUTBUFSZ 1024
4348

4449

@@ -300,6 +305,10 @@ static void in_callback(unsigned char *rtlinbuff, uint32_t nread, void *ctx)
300305
{
301306
int n;
302307

308+
pthread_mutex_lock(&cbMutex);
309+
watchdogCounter = 50;
310+
pthread_mutex_unlock(&cbMutex);
311+
303312
if (nread != rtlInBufSize) {
304313
fprintf(stderr, "warning: partial read\n");
305314
return;
@@ -334,9 +343,46 @@ static void in_callback(unsigned char *rtlinbuff, uint32_t nread, void *ctx)
334343
}
335344
}
336345

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+
337354
int runRtlSample(void)
338355
{
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+
340386
return 0;
341387
}
342388

0 commit comments

Comments
 (0)