-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdr.c
69 lines (60 loc) · 2.27 KB
/
sdr.c
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
#include "sdr.h"
#include "data.h"
extern struct Modes Modes;
/* =============================== RTLSDR handling ========================== */
void modesInitRTLSDR(void)
{
if (!rtlsdr_get_device_count()) {
fprintf(stderr, "No supported RTLSDR devices found.\n");
exit(1);
}
if (rtlsdr_open(&Modes.dev, 0) < 0) {
fprintf(stderr, "Error opening the RTLSDR device: %s\n",
strerror(errno));
exit(1);
}
/* Set gain, frequency, sample rate, and reset the device. */
rtlsdr_set_tuner_gain_mode(Modes.dev, 1);
int numgains;
int gains[100];
numgains = rtlsdr_get_tuner_gains(Modes.dev, gains);
rtlsdr_set_tuner_gain(Modes.dev, gains[numgains - 1]);
rtlsdr_set_freq_correction(Modes.dev, 0);
rtlsdr_set_center_freq(Modes.dev, MODES_DEFAULT_FREQ);
rtlsdr_set_sample_rate(Modes.dev, MODES_DEFAULT_RATE);
rtlsdr_reset_buffer(Modes.dev);
fprintf(stderr, "Gain reported by device: %.2f\n",
rtlsdr_get_tuner_gain(Modes.dev) / 10.0);
}
/* We use a thread reading data in background, while the main thread
* handles decoding and visualization of data to the user.
*
* The reading thread calls the RTLSDR API to read data asynchronously, and
* uses a callback to populate the data buffer.
* A Mutex is used to avoid races with the decoding thread. */
void rtlsdrCallback(unsigned char* buf, uint32_t len, void* ctx)
{
MODES_NOTUSED(ctx);
pthread_mutex_lock(&Modes.data_mutex);
if (len > MODES_DATA_LEN)
len = MODES_DATA_LEN;
/* Move the last part of the previous buffer, that was not processed,
* on the start of the new buffer. */
memcpy(Modes.data, Modes.data + MODES_DATA_LEN, (MODES_FULL_LEN - 1) * 4);
/* Read the new data. */
memcpy(Modes.data + (MODES_FULL_LEN - 1) * 4, buf, len);
Modes.data_ready = 1;
/* Signal to the other thread that new data is ready */
pthread_cond_signal(&Modes.data_cond);
pthread_mutex_unlock(&Modes.data_mutex);
}
/* We read data using a thread, so the main thread only handles decoding
* without caring about data acquisition. */
void* readerThreadEntryPoint(void* arg)
{
MODES_NOTUSED(arg);
rtlsdr_read_async(Modes.dev, rtlsdrCallback, NULL,
MODES_ASYNC_BUF_NUMBER,
MODES_DATA_LEN);
return NULL;
}