-
Notifications
You must be signed in to change notification settings - Fork 10
/
rtl-trx.c
220 lines (175 loc) · 4.98 KB
/
rtl-trx.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
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
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/timerfd.h>
#include <rtl-sdr.h>
#include <pthread.h>
#include <signal.h>
#include <liquid/liquid.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#define BAUDOT_PREAMBLE_LENGTH 8
#include "baudot.h"
int if_freq = 3.6e6;
unsigned int center_freq = 1250e6;
int rx_gain = 500, tx_gain = 200;
int tone1 = 200, shift = 440;
// decimation from 2.4 MHz to 8 kHz/2
#define DECIM1 600
#define RTL_CHECK(x, y) { \
ret = (x y); \
if (ret < 0) { \
fprintf(stderr, "Error: %s returned %d at %s:%d.\n", #x, ret, __FILE__, __LINE__); \
goto err; \
} \
}
pthread_t control_thrd=0;
rtlsdr_dev_t *dev = NULL;
int do_exit = 0;
static void sighandler(int signum) {
(void)signum;
do_exit = 1;
}
#define TXTEXT_LEN 500
#define TXBITS_LEN 512
static void *control(void *arg) {
unsigned int txfreq, txfreq_tune, txfreq_tune0, txfreq_tune1, txfreq_prev;
(void)arg;
int ret;
char txtext[TXTEXT_LEN];
uint8_t txbits[TXBITS_LEN];
int tfd=-1;
for(;;) {
int nbits, nbytes;
rtlsdr_set_center_freq(dev, center_freq);
rtlsdr_set_tuner_gain(dev, rx_gain);
printf("Ready\n");
char *r = fgets(txtext, TXTEXT_LEN, stdin);
if(r == NULL) break;
if(txtext[0] == 'F') {
center_freq = atof(txtext+1);
printf("Changing frequency to %d\n", center_freq);
continue;
}
nbytes = baudot_from_ascii(txbits, TXBITS_LEN, txtext);
nbits = nbytes*8;
tfd = timerfd_create(CLOCK_MONOTONIC, 0);
struct itimerspec tfd_ts;
tfd_ts.it_value.tv_sec = 0;
tfd_ts.it_value.tv_nsec = 1;
tfd_ts.it_interval.tv_sec = 0;
tfd_ts.it_interval.tv_nsec = 1e9 / 45.45 + 0.5;
ret = timerfd_settime(tfd, 0, &tfd_ts, NULL);
//printf("%d\n", ret);
printf("Transmitting\n");
RTL_CHECK(rtlsdr_set_tuner_gain,(dev, tx_gain));
int bit_i = 0;
uint64_t tfd_e = 0;
txfreq = center_freq + tone1;
txfreq_tune1 = txfreq / 4 - if_freq;
txfreq = center_freq + tone1 - shift;
txfreq_tune0 = txfreq / 4 - if_freq;
txfreq_prev = 0;
ret = read(tfd, &tfd_e, sizeof(uint64_t));
while(bit_i < nbits) {
if(txbits[bit_i / 8] & (1<<(7&bit_i)))
txfreq_tune = txfreq_tune1;
else
txfreq_tune = txfreq_tune0;
if(txfreq_tune != txfreq_prev)
RTL_CHECK(rtlsdr_set_center_freq,(dev, txfreq_tune));
txfreq_prev = txfreq_tune;
ret = read(tfd, &tfd_e, sizeof(uint64_t));
if(ret < 0) break;
if(tfd_e > 1)
printf("Warning: missed %ld bits\n", tfd_e-1);
/* read from timerfd returns the number of timer
expirations. If we have missed some number of these,
we'll skip the same number of bits to keep timing
correct. */
bit_i += tfd_e;
}
err:
if(tfd > 0)
close(tfd);
tfd = -1;
}
do_exit = 1;
return NULL;
}
float complex decim1[DECIM1];
msresamp_crcf decim1_q;
firhilbf hilbert_q;
int audiopipe = 3;
int outsamples = 0;
#define DECIM1_BUF 8192
#define DECIM1_OUTBUF (DECIM1_BUF/DECIM1 + 1)
static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *arg) {
(void)arg;
float complex dec_in[DECIM1_BUF], dec_out[DECIM1_OUTBUF];
float audio_out[2*DECIM1_OUTBUF];
unsigned int dec_in_n, i, num_out=0;
int ret, bytes_out;
float audiogain = 0.1;
if(do_exit) {
printf("Canceling\n");
rtlsdr_cancel_async(dev);
return;
}
dec_in_n = len/2;
assert(dec_in_n <= DECIM1_BUF);
for(i = 0; i < dec_in_n; i++) {
dec_in[i] = ((float)buf[2*i] - 127.4f) + I*((float)buf[2*i+1] - 127.4f);
}
msresamp_crcf_execute(decim1_q, dec_in, dec_in_n, dec_out, &num_out);
assert(num_out <= DECIM1_OUTBUF);
for(i = 0; i < num_out; i++) {
float complex in = dec_out[i];
// hilbert transform seems to swap halves of spectrum, fix it here:
if(outsamples & 1)
in = -audiogain*in;
else
in = audiogain*in;
firhilbf_interp_execute(hilbert_q, in, audio_out + 2*i);
outsamples++;
}
bytes_out = 2*num_out*sizeof(float);
ret = write(audiopipe, audio_out, bytes_out);
if(ret != bytes_out)
printf("No audio pipe?\n");
}
int main() {
int idx = 0;
int sample_rate = 2.4e6;
int blocksize = DECIM1_BUF*2;
int ret;
struct sigaction sigact;
// setup signals
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);
// initialize liquid-dsp
decim1_q = msresamp_crcf_create(1.0/DECIM1, 60.0);
hilbert_q = firhilbf_create(50, 60.0);
// initialize rtl-sdr
RTL_CHECK(rtlsdr_open,(&dev, idx));
RTL_CHECK(rtlsdr_set_sample_rate,(dev, sample_rate));
RTL_CHECK(rtlsdr_set_dithering,(dev, 0));
RTL_CHECK(rtlsdr_set_if_freq,(dev, if_freq));
RTL_CHECK(rtlsdr_set_center_freq,(dev, center_freq));
RTL_CHECK(rtlsdr_set_tuner_gain_mode,(dev, 1));
RTL_CHECK(rtlsdr_set_tuner_gain,(dev, rx_gain));
// start
pthread_create(&control_thrd, 0, control, NULL);
RTL_CHECK(rtlsdr_reset_buffer,(dev));
RTL_CHECK(rtlsdr_read_async,(dev, rtlsdr_callback, NULL, 0, blocksize));
err:
if(control_thrd)
pthread_kill(control_thrd, SIGTERM);
return 0;
}