forked from tomwimmenhove/subarufobrob
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fobrob.c
324 lines (277 loc) · 7.29 KB
/
fobrob.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright 2017 Tom Wimmenhove<[email protected]>
*
* This file is part of Subarufobrob
*
* Subarufobrob 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.
*
* Subarufobrob 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 Subarufobrob. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <pthread.h>
#include <rtl-sdr.h>
#include "hex.h"
#include "protocol.h"
#include "runningavg.h"
#include "filter.h"
#include "demodulator.h"
void handlePacket(unsigned char* packet)
{
unsigned char hexString[21];
hexify(hexString, packet, 10);
fprintf(stderr, "Valid packet received\n");
fprintf(stderr, " * Code: %s\n", hexString);
fprintf(stderr, " * Command: %s\n", commandName(getCommand(packet)));
fprintf(stderr, " * Rolling code: %d\n", getCode(packet));
FILE* f = fopen("latestcode.txt", "w");
fprintf(f, "%s\n", hexString);
fclose(f);
f = fopen("receivedcodes.txt", "a");
fprintf(f, "%s\n", hexString);
fclose(f);
}
#define DEFAULT_BUF_LENGTH (16 * 16384)
typedef struct
{
float i;
float q;
} __attribute__((packed)) ComplexSample;
typedef struct
{
int fd;
rtlsdr_dev_t *dev;
} Context;
void rtlsdr_callback(unsigned char *buf, uint32_t len, void *c)
{
Context* ctx = (Context*) c;
if (write(ctx->fd, buf, len) == -1)
{
perror("rtlsdr_callback write():");
exit(1);
}
}
void *startSampler(void *c)
{
Context* ctx = (Context*) c;
int ret = rtlsdr_read_async(ctx->dev, rtlsdr_callback, c, 0, DEFAULT_BUF_LENGTH);
if (ret == -1)
{
fprintf(stderr, "rtlsdr_read_async returned with %d\n", ret);
exit(1);
}
return NULL;
}
void printHelp(char *s)
{
fprintf(stderr, "usage %s [options]\n", s);
fprintf(stderr, "\t-h, --help : this\n");
fprintf(stderr, "\t-p, --ppm <ppm> : set the frequency correction\n");
fprintf(stderr, "\t-a, --agc : enable autogain\n");
fprintf(stderr, "\t-t, --tunergain <gain value> : set rtlsdr_set_tuner_gain() (defaults to 8.7)\n");
fprintf(stderr, "\t-d, --debug <int> : debug stuff. Look in the code if you're interested\n");
}
int main(int argc, char **argv)
{
double freq = 433.92e+6;
// uint32_t sampleRate = 995840;// 1024 raw samples per bit at 995840Hz samplerate
uint32_t sampleRate = 972500; // 1000 raw samples per bit at 972500Hz samplerate
int decimation1 = 5; // We're going from ~1Mhz to ~200Khz
int samplesPerSymbol = 200; // at ~200Khz we're getting exactly 200 samples per bit
uint16_t buf[DEFAULT_BUF_LENGTH / sizeof(uint16_t)];
int minPreambleBits = 42;
int maxPreambleTimingError = 40; // Might be a little high but reduces false positives by a LOT
SampleFilter lpfi1;
SampleFilter lpfq1;
SampleFilter_init(&lpfi1);
SampleFilter_init(&lpfq1);
DemodContext demodCtx;
demodInit(&demodCtx, samplesPerSymbol, minPreambleBits, maxPreambleTimingError, &handlePacket);
int decimator = 0;
int ppm = 0;
int agc = 0;
int tunergain = 87; // XXX: Pulled from GNURadio source. Dunno what it means
rtlsdr_dev_t *dev = NULL;
unsigned int dev_index = 0;
int n;
int debug = 0;
int c;
/* Parse options */
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"ppm", required_argument, 0, 'p' },
{"tunergain", required_argument, 0, 't' },
{"agc", no_argument, 0, 'a' },
{"help", no_argument, 0, 'h' },
{"debug", required_argument, 0, 'd' },
{0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "p:t:ahd:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
default:
case 'h':
case 0:
printHelp(argv[0]);
return c == 'h' ? 0 : -1;
case 'p':
ppm = atoi(optarg);
break;
case 't':
tunergain = (int)(10.0 * atof(optarg));
break;
case 'a':
agc = 1;
break;
case 'd':
debug = atoi(optarg);
break;
}
}
/* Build look-up table for float conversion */
ComplexSample lut[0x10000];;
for (unsigned int i = 0; i < 0x10000; i++)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
lut[i].i = (((float)(i & 0xff)) - 127.4f) * (1.0f/128.0f);
lut[i].q = (((float)(i >> 8)) - 127.4f) * (1.0f/128.0f);
#else // BIG_ENDIAN
lut[i].i = (((float)(i >> 8)) - 127.4f) * (1.0f/128.0f);
lut[i].q = (((float)(i & 0xff) - 127.4f) * (1.0f/128.0f);
#endif
}
Context ctx;
int pipefd[2];
if (debug != 2)
{
/* Open the rtlsdr */
if (rtlsdr_open(&dev, dev_index) == -1)
{
fprintf(stderr, "Can't open device");
return -1;
}
/* Set rtl-sdr parameters */
if (agc)
{
rtlsdr_set_tuner_gain_mode(dev, 0);
rtlsdr_set_agc_mode(dev, 1);
}
else
{
rtlsdr_set_tuner_gain_mode(dev, 1);
rtlsdr_set_agc_mode(dev, 0);
if (rtlsdr_set_tuner_gain(dev, tunergain) == -1)
{
fprintf(stderr, "Can not set gain\n");
}
}
rtlsdr_set_sample_rate(dev, sampleRate);
rtlsdr_set_center_freq(dev, (uint32_t) freq);
rtlsdr_reset_buffer(dev);
rtlsdr_set_freq_correction(dev, ppm);
/* create our data pipe */
if (pipe(pipefd) == -1)
{
perror("pipe()");
return -1;
}
ctx.fd = pipefd[1];
ctx.dev = dev;
pthread_t sampleThread;
if (pthread_create(&sampleThread, NULL, startSampler, &ctx) != 0)
{
perror("pthread_create()");
return -1;
}
}
if (debug == 2) /* Don't capture, just read from stdin */
{
pipefd[0] = 0;
}
ComplexSample* debugout = NULL;
if (debug == 3 || debug == 1)
{
debugout = malloc(DEFAULT_BUF_LENGTH / sizeof(uint16_t) * sizeof(ComplexSample));
}
int nDebugout = -1;
while ((n = read(pipefd[0], buf, sizeof(buf))) > 0)
{
int nout;
if (debug == 2)
{
nout = n / sizeof(ComplexSample);
}
else
{
nout = n / sizeof(uint16_t);
}
for (int i = 0 ; i < nout; i++)
{
ComplexSample* cSample;
if (debug == 2)
{
cSample = (ComplexSample*) &buf[i / sizeof(uint16_t) * sizeof(ComplexSample)];
}
else
{
cSample = &lut[buf[i]];
}
if (debug == 1)
{
debugout[nDebugout].i = cSample->i;
debugout[nDebugout].q = cSample->q;
nDebugout++;
}
// Decimation: 1M -> 200Khz
SampleFilter_put(&lpfi1, cSample->i);
SampleFilter_put(&lpfq1, cSample->q);
if (decimation1 > ++decimator)
{
continue;
}
decimator = 0;
double si = SampleFilter_get(&lpfi1);
double sq = SampleFilter_get(&lpfq1);
if (debug == 3)
{
debugout[nDebugout].i = si;
debugout[nDebugout].q = sq;
nDebugout++;
}
/* Convert complex sample to magnitude squared */
double sample = si*si + sq*sq;
demodSample(&demodCtx, sample);
}
if (debug == 3 || debug == 1)
{
if (write(1, debugout, nDebugout * sizeof(ComplexSample)) == -1)
{
perror("write()");
return -1;
}
nDebugout = 0;
}
}
return 0;
}