-
Notifications
You must be signed in to change notification settings - Fork 0
/
dab2eti.cpp
308 lines (255 loc) · 8.37 KB
/
dab2eti.cpp
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
/*
This file is part of rtl-dab
trl-dab 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 3 of the License, or
(at your option) any later version.
Foobar 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 rtl-dab. If not, see <http://www.gnu.org/licenses/>.
Original by:
david may 2012
Modifications for the library wrapper by Stefan Heimers:
- remove main(), functionality goes into dab2eti::receiver() in libdabplus.cpp
- convert parts to c++
- make eti_callback() fill the internal fifo instead
to write to stdout.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <rtl-sdr.h>
#include <unistd.h>
#include <string>
#include <queue>
extern "C" {
#include "../dabtools/src/dab.h"
#include "../dabtools/src/input_sdr.h"
#include "../dabtools/src/input_wf.h"
}
#include "libdabplus.h"
int do_exit = 0;
static pthread_t demod_thread;
static sem_t data_ready;
#define DEFAULT_ASYNC_BUF_NUMBER 32
uint32_t corr_counter;
uint32_t ccount=0;
// void dab2eti::sighandler(int signum)
// {
// fprintf(stderr, "Signal caught, exiting!\n");
// do_exit = 1;
// rtlsdr_cancel_async(dev);
// }
void *dab2eti::demod_thread_fn(void *arg)
{
struct dab_state_t *dab = (dab_state_t *)arg;
struct sdr_state_t *sdr = (sdr_state_t *)dab->device_state;
while (!do_exit) {
sem_wait(&data_ready);
int ok = sdr_demod(&dab->tfs[dab->tfidx], sdr);
if (ok) {
dab_process_frame(dab);
}
//dab_fic_parser(dab->fib,&sinfo,&ana);
// calculate error rates
//dab_analyzer_calculate_error_rates(&ana,dab);
// if the user changed the frequency
if(new_frequency){
sdr->frequency = frequency;
new_frequency = false;
}
// if the user changed the gain
if(new_gain){
int r;
if (gain == AUTO_GAIN) {
r = rtlsdr_set_tuner_gain_mode(dev, 0);
} else {
r = rtlsdr_set_tuner_gain_mode(dev, 1);
r = rtlsdr_set_tuner_gain(dev, gain);
}
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
} else if (gain == AUTO_GAIN) {
fprintf(stderr, "Tuner gain set to automatic.\n");
} else {
fprintf(stderr, "Tuner gain set to %0.2f dB.\n", gain/10.0);
}
new_gain = false;
}
// automatic fine tuning
if (abs(sdr->coarse_freq_shift)>1) {
if (sdr->coarse_freq_shift<0)
sdr->frequency = sdr->frequency -1000;
else
sdr->frequency = sdr->frequency +1000;
rtlsdr_set_center_freq(dev,sdr->frequency);
}
if (abs(sdr->coarse_freq_shift) ==1) {
if (sdr->coarse_freq_shift<0)
sdr->frequency = sdr->frequency -rand() % 1000;
else
sdr->frequency = sdr->frequency +rand() % 1000;
rtlsdr_set_center_freq(dev,sdr->frequency);
//fprintf(stderr,"new center freq : %i\n",rtlsdr_get_center_freq(dev));
}
if (abs(sdr->coarse_freq_shift)<1 && (abs(sdr->fine_freq_shift) > 50)) {
sdr->frequency = sdr->frequency + (sdr->fine_freq_shift/3);
rtlsdr_set_center_freq(dev,sdr->frequency);
//fprintf(stderr,"ffs : %f\n",sdr->fine_freq_shift);
}
//if (sdr->frequency != prev_freq) {
// fprintf(stderr,"Adjusting centre-frequency to %dHz\n",sdr->frequency);
//}
ccount += 1;
if (ccount == 10) {
ccount = 0;
//print_status(dab);
}
}
return 0;
}
void rtlsdr_callback(uint8_t *buf, uint32_t len, void *ctx)
{
struct sdr_state_t *sdr = (sdr_state_t *)ctx;
int dr_val;
if (do_exit) {
return;}
if (!ctx) {
return;}
memcpy(sdr->input_buffer,buf,len);
sdr->input_buffer_len = len;
sem_getvalue(&data_ready, &dr_val);
if (!dr_val) {
sem_post(&data_ready);}
}
void dab2eti::eti_callback(uint8_t* eti)
{
etiFrame frame;
frame.frameSize = 6144;
frame.type = ETI_NI_G703;
memcpy(frame.data, eti,6144);
etififo.push(frame);
}
int dab2eti::do_sdr_decode(struct dab_state_t* dab, int frequency)
{
uint32_t dev_index = 0;
int32_t device_count;
int i,r;
char vendor[256], product[256], serial[256];
uint32_t samp_rate = 2048000;
memset(&sdr,0,sizeof(struct sdr_state_t));
sdr.frequency = frequency;
//fprintf(stderr,"%i\n",sdr.frequency);
/*---------------------------------------------------
Looking for device and open connection
----------------------------------------------------*/
device_count = rtlsdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported devices found.\n");
exit(1);
}
fprintf(stderr, "Found %d device(s):\n", device_count);
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
}
fprintf(stderr, "\n");
fprintf(stderr, "Using device %d: %s\n",dev_index, rtlsdr_get_device_name(dev_index));
r = rtlsdr_open(&dev, dev_index);
if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
exit(1);
}
int gains[100];
int count = rtlsdr_get_tuner_gains(dev, gains);
fprintf(stderr, "Supported gain values (%d): ", count);
for (i = 0; i < count; i++)
fprintf(stderr, "%.1f ", gains[i] / 10.0);
fprintf(stderr, "\n");
/*-------------------------------------------------
Set Frequency & Sample Rate
--------------------------------------------------*/
/* Set the sample rate */
r = rtlsdr_set_sample_rate(dev, samp_rate);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set sample rate.\n");
/* Set the frequency */
r = rtlsdr_set_center_freq(dev, sdr.frequency);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set center freq.\n");
else
fprintf(stderr, "Tuned to %u Hz.\n", sdr.frequency);
/*------------------------------------------------
Setting gain
-------------------------------------------------*/
if (gain == AUTO_GAIN) {
r = rtlsdr_set_tuner_gain_mode(dev, 0);
} else {
r = rtlsdr_set_tuner_gain_mode(dev, 1);
r = rtlsdr_set_tuner_gain(dev, gain);
}
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
} else if (gain == AUTO_GAIN) {
fprintf(stderr, "Tuner gain set to automatic.\n");
} else {
fprintf(stderr, "Tuner gain set to %0.2f dB.\n", gain/10.0);
}
/*-----------------------------------------------
/ Reset endpoint (mandatory)
------------------------------------------------*/
r = rtlsdr_reset_buffer(dev);
/*-----------------------------------------------
/ Signal handler
------------------------------------------------*/
// 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);
/*-----------------------------------------------
/ start demod thread & rtl read
-----------------------------------------------*/
fprintf(stderr,"Waiting for sync...\n");
sdr_init(&sdr);
//dab_fic_parser_init(&sinfo);
//dab_analyzer_init(&ana);
pthread_create(&demod_thread, NULL, demod_thread_fn, (void *)(dab));
rtlsdr_read_async(dev, rtlsdr_callback, (void *)(&sdr),
DEFAULT_ASYNC_BUF_NUMBER, DEFAULT_BUF_LENGTH);
if (do_exit) {
fprintf(stderr, "\nUser cancel, exiting...\n");}
else {
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);}
rtlsdr_cancel_async(dev);
//dab_demod_close(&dab);
rtlsdr_close(dev);
return 1;
}
int dab2eti::do_wf_decode(struct dab_state_t* dab, int frequency)
{
struct wavefinder_t *wf = (wavefinder_t *)dab->device_state;
int displayed_lock = 0;
wf_init(wf);
wf_tune(wf, (frequency+500)/1000); /* Round frequency to the nearest KHz */
fprintf(stderr,"Waiting for sync...");
/* Read (and discard) the first frame - we know it is missing the FIC symbols */
wf_read_frame(wf,&dab->tfs[0]);
if ((wf->sync_locked) && (!displayed_lock)) {
fprintf(stderr,"LOCKED\n");
displayed_lock = 1;
}
while (1) {
wf_read_frame(wf,&dab->tfs[dab->tfidx]);
dab_process_frame(dab);
}
}