-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
243 lines (216 loc) · 5.95 KB
/
main.cc
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
//
// Copyright 2018, Jeremy Cooper
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include "RDATDecoder.h"
#include "NRZISyncDeframer.h"
#include "DATWordReceiver.h"
#include "DATTrackFramer.h"
#include "AudioFrameReceiver.h"
#include "DDSFrameReceiver.h"
#include "File.h"
enum { SAMPLES_PER_READ = 1000 };
static void usage(const char *prog);
static void sigint_handler(int);
static volatile bool running;
int
main(int argc, char *argv[])
{
float buf[SAMPLES_PER_READ];
size_t nread;
bool do_raw = false;
bool do_dat = false;
bool do_dds = false;
bool do_file = false;
bool do_output = false;
bool do_dds_session = false;
enum { DECODE_RAW, DECODE_DAT, DECODE_DDS } decode_mode = DECODE_DAT;
int c;
const char *filename, *outfile;
unsigned int dds_session;
while ((c = getopt(argc, argv, "hdraf:o:s:")) != -1) {
switch (c) {
default:
case 'h':
usage(argv[0]);
break;
case 'd':
do_dds = true;
break;
case 'r':
do_raw = true;
break;
case 'a':
do_dat = true;
break;
case 'f':
do_file = true;
filename = optarg;
break;
case 'o':
do_output = true;
outfile = optarg;
break;
case 's':
do_dds_session = true;
dds_session = strtoul(optarg, NULL, 0);
break;
}
}
//
// Only one decode mode allowed.
//
if ((do_raw && (do_dds || do_dat)) || (do_dat && do_dds)) {
usage(argv[0]);
}
//
// Can only do output if DAT or DDS is selected.
//
if (do_output && !do_dat && !do_dds) {
fprintf(stderr, "Can't dump result unless doing DAT audio or DDS.\n");
usage(argv[0]);
}
//
// Can only do DDS session if DDS is selected.
//
if (do_dds_session && !do_dds) {
fprintf(stderr, "DDS session number is only valid for DDS.\n");
usage(argv[0]);
}
//
// Default to DAT if no choice specified.
//
if (!do_raw && !do_dds && !do_dat)
do_raw = true;
if (do_raw)
decode_mode = DECODE_RAW;
else if (do_dat)
decode_mode = DECODE_DAT;
else if (do_dds)
decode_mode = DECODE_DDS;
File in;
if (do_file) {
if (!in.Open(filename, sizeof(float))) {
fprintf(stderr, "Can't open file '%s'.\n", filename);
exit(1);
}
} else {
in.Open(STDIN_FILENO, sizeof(float));
}
RDATDecoder *decoder;
NRZISyncDeframer *deframer;
DATWordReceiver *blocker;
DATTrackFramer *tracker = NULL;
DATFrameReceiver *streamer = NULL;
switch (decode_mode) {
case DECODE_DAT:
{
AudioFrameReceiver *audio = new AudioFrameReceiver();
if (do_output) {
if (!audio->SetDumpFile(outfile)) {
fprintf(stderr, "Can't dump to output file '%s'.\n", outfile);
exit(1);
}
}
// Cast receiver to generic base class for rest of code.
streamer = audio;
}
break;
case DECODE_DDS:
{
DDSFrameReceiver *dds = new DDSFrameReceiver();
if (do_output) {
dds->DumpToDirectory(outfile);
}
if (do_dds_session) {
dds->DumpSession(dds_session);
}
streamer = dds;
}
break;
default:
break;
}
if (decode_mode == DECODE_RAW) {
blocker = new DATWordReceiver(NULL, true);
} else {
tracker = new DATTrackFramer(*streamer);
blocker = new DATWordReceiver(tracker, false);
}
deframer = new NRZISyncDeframer(blocker);
decoder = new RDATDecoder(9408000.0 * 8);
decoder->SetSymbolDecoder(deframer);
running = true;
//
// Install the sigint handler so that the user can stop the
// processing safely.
//
struct sigaction int_handler = { .sa_handler = sigint_handler };
::sigaction(SIGINT, &int_handler, NULL);
while (running) {
nread = in.Read(buf, SAMPLES_PER_READ);
if (nread == 0)
break;
decoder->Process(buf, nread);
}
decoder->Stop();
in.Close();
delete decoder;
delete deframer;
delete blocker;
delete streamer;
return 0;
}
static void
usage(const char *prog)
{
fprintf(stderr,
"usage: %s [-r|-d|-a] [-s <number>] [-f <filename>] [-o <path>]\n"
"Decode DAT/DDS samples taken from an R-DAT RF head. Input must be in\n"
"IEEE-float format, in native-endian order, and sampled at 75.264MHz.\n"
" -a - Use DAT decode (Default)\n"
" -d - Use DDS decoder.\n"
" -r - Dump raw packets; don't interpret as DAT nor DDS.\n"
" -o - DAT mode: Write raw audio to file <path>.\n"
" DDS mode: Dump basic groups to directory <path>.\n"
" -f - Read data from filename. (Default is stdin).\n"
" -s - Dump DDS session <number> (DDS only)\n",
prog
);
exit(1);
}
static void
sigint_handler(int signal_received)
{
running = false;
}