-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdecoder.c
473 lines (401 loc) · 11.4 KB
/
decoder.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "Python.h"
#include <math.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "rtl-sdr.h"
// Device/detection parameters
#define FREQUENCY 433800000
#define SAMPLE_RATE 1000000
#define SMOOTH_WINDOW 488
#define RTL_BUFFER_SIZE 32768
#define THRESHOLD 6800.0
static int do_exit = 0;
static rtlsdr_dev_t *dev = NULL;
/*
sighandler - Signal handler for the readRTL function
*/
static void sighandler(int signum)
{
fprintf(stderr, "Signal caught, exiting!\n");
do_exit = 1;
rtlsdr_cancel_async(dev);
}
// Setup the variables - time control
static int tStart, tNow, diff;
// Setup the variables - power detection
static float runningSum = 0;
static float *powerBuffer;
// Setup the control loop
static int loopTimeOut = 0;
static int prevPower = 0;
static long dataCounter = 0;
static long prevEdge = -1;
static long edgeCountDiff = -1;
static long halfTime = 0;
/*
decorder_callback - Function that receives the RTL-SDR buffer and performs
the Manchester decoding. ctx is a pointer to a PyList object that is updaated
when a new bit is found.
*/
static void decoder_callback(unsigned char *buf, uint32_t len, void *ctx) {
int j, power, edge, addBit;
float real, imag, instPower;
PyObject *temp;
if( ctx ) {
// Exit if we are done
if( do_exit ) {
return;
}
// Get the current time to figure out how long we've been running
tNow = (int) time(NULL);
diff = tNow - tStart;
if( loopTimeOut && diff > loopTimeOut ) {
do_exit = 1;
rtlsdr_cancel_async(dev);
}
// Process the buffer
for(j=0; j<len/2; j++) {
//// Unpack
real = ((float) *(buf + 2*j+0)) - 127.0;
imag = ((float) *(buf + 2*j+1)) - 127.0;
instPower = real*real + imag*imag;
dataCounter += 1;
//// Moving average
runningSum += instPower - *(powerBuffer + (dataCounter-1) % SMOOTH_WINDOW);
*(powerBuffer + (dataCounter-1) % SMOOTH_WINDOW) = instPower;
//// Convert to an integer
if( runningSum >= THRESHOLD*SMOOTH_WINDOW ) {
power = 1;
} else {
power = 0;
}
//// Edge detection
edge = power - prevPower;
prevPower = power;
//// Timing
if( edge != 0 ) {
if( prevEdge < 0 ) {
prevEdge = dataCounter;
}
edgeCountDiff = dataCounter - prevEdge;
}
if( edge == 1 ) {
////// Rising edge
if( edgeCountDiff > 80000 ) {
prevEdge = dataCounter;
halfTime = 0;
addBit = 1;
} else if( edgeCountDiff < 200 || edgeCountDiff > 1100 ) {
addBit = 0;
} else if( edgeCountDiff < 615 ) {
prevEdge = dataCounter;
halfTime += 1;
addBit = 1;
} else {
prevEdge = dataCounter;
halfTime += 2;
addBit = 1;
}
if( addBit && halfTime % 2 == 0 ) {
temp = PyInt_FromLong(1);
PyList_Append(ctx, temp);
Py_DECREF(temp);
}
} else if( edge == -1 ) {
////// Falling edge
if( edgeCountDiff > 80000 ) {
prevEdge = dataCounter;
halfTime = 0;
addBit = 1;
} else if( edgeCountDiff < 400 || edgeCountDiff > 1400 ) {
addBit = 0;
} else if( edgeCountDiff < 850 ) {
prevEdge = dataCounter;
halfTime += 1;
addBit = 1;
} else {
prevEdge = dataCounter;
halfTime += 2;
addBit = 1;
}
if( addBit && halfTime % 2 == 0 ) {
temp = PyInt_FromLong(0);
PyList_Append(ctx, temp);
Py_DECREF(temp);
}
}
}
// end buffer processing loop
}
}
/*
verbose_device_search - Device search function from the librtlsdr library
*/
int verbose_device_search(char *s) {
int i, device_count, device, offset;
char *s2;
char vendor[256], product[256], serial[256];
device_count = rtlsdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported devices found.\n");
return -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");
/* does string look like raw id number */
device = (int)strtol(s, &s2, 0);
if (s2[0] == '\0' && device >= 0 && device < device_count) {
fprintf(stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name((uint32_t)device));
return device;
}
/* does string exact match a serial */
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
if (strcmp(s, serial) != 0) {
continue;}
device = i;
fprintf(stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name((uint32_t)device));
return device;
}
/* does string prefix match a serial */
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
if (strncmp(s, serial, strlen(s)) != 0) {
continue;}
device = i;
fprintf(stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name((uint32_t)device));
return device;
}
/* does string suffix match a serial */
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
offset = strlen(serial) - strlen(s);
if (offset < 0) {
continue;}
if (strncmp(s, serial+offset, strlen(s)) != 0) {
continue;}
device = i;
fprintf(stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name((uint32_t)device));
return device;
}
fprintf(stderr, "No matching devices found.\n");
return -1;
}
/*
readRTL - Function for reading directly from an RTL-SDR and returning a list of
Manchester decoded bits.
*/
static PyObject *readRTL(PyObject *self, PyObject *args) {
PyObject *output, *bits;
int r, i, dev_index;
long duration;
struct sigaction sigact;
if( !PyArg_ParseTuple(args, "i", &duration) ) {
PyErr_Format(PyExc_RuntimeError, "Invalid parameters");
return NULL;
}
// Validate the input
if( duration <= 0 ) {
PyErr_Format(PyExc_ValueError, "Duration value must be greater than zero");
return NULL;
}
// Setup the RTL SDR device
dev_index = verbose_device_search("0");
if( dev_index < 0 ) {
PyErr_Format(PyExc_RuntimeError, "RTL SDR device not found");
return NULL;
}
r = rtlsdr_open(&dev, (uint32_t)dev_index);
if( r < 0 ) {
PyErr_Format(PyExc_RuntimeError, "Cannot open RTL SDR device");
return NULL;
}
// Setup the signal handler so that we can exit the callback function
do_exit = 0;
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);
// Setup the radio
r = rtlsdr_set_sample_rate(dev, SAMPLE_RATE);
r = rtlsdr_set_center_freq(dev, FREQUENCY);
r = rtlsdr_set_tuner_gain_mode(dev, 0);
// Reset endpoint before we start reading from it (mandatory)
r = rtlsdr_reset_buffer(dev);
// Setup the output list
bits = PyList_New(0);
// Reset the loop control
runningSum = 0;
prevPower = 0;
dataCounter = 0;
prevEdge = -1;
edgeCountDiff = -1;
halfTime = 0;
// Setup the variables - Power Detection
powerBuffer = (float *) malloc(SMOOTH_WINDOW*sizeof(float));
for(i=0; i<SMOOTH_WINDOW; i++) {
*(powerBuffer + i) = 0.0;
}
// Setup the raw data buffer
unsigned char *raw;
raw = (unsigned char *) malloc(RTL_BUFFER_SIZE*sizeof(unsigned char));
// Read in data
tStart = (int) time(NULL);
loopTimeOut = (int) duration;
r = rtlsdr_read_async(dev, decoder_callback, (void *) bits, 0, RTL_BUFFER_SIZE);
// Done
if( do_exit ) {
fprintf(stderr, "\nUser cancel, exiting...\n");
} else {
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
}
/*
If the call to rtlsdr_close() here generates a segfault then try
updating to a newer libusb.
*/
rtlsdr_close(dev);
// Cleanup
free(raw);
free(powerBuffer);
// Return
output = Py_BuildValue("O", bits);
return output;
}
PyDoc_STRVAR(readRTL_doc, \
"Read in the data from a RTL SDR device and perform Manchester decoding, and\n\
return a list of bits (1 or 0) suitable for identifying Oregon Scientific v2.1\n\
and v3.0 sensor data.\n\
\n\
Inputs:\n\
* duration - integer number of seconds to capture data for\n\
\n\
Outputs:\n\
* bits - a list of ones and zeros for the data bits\n\
\n\
Based on:\n\
* http://www.osengr.org/WxShield/Downloads/OregonScientific-RF-Protocols-II.pdf\n\
* http://www.disk91.com/2013/technology/hardware/oregon-scientific-sensors-with-raspberry-pi/\n\
");
/*
readRTLFile - Function for reading from a file created by 'rtl_sdr' and
returning a list of Manchester decoded bits.
*/
static PyObject *readRTLFile(PyObject *self, PyObject *args) {
PyObject *output, *bits;
int i;
char *filename;
struct sigaction sigact;
if(!PyArg_ParseTuple(args, "s", &filename)) {
PyErr_Format(PyExc_RuntimeError, "Invalid parameters");
return NULL;
}
// Setup the signal handler so that we can exit the callback function
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);
// Ready the file
FILE *fh = fopen(filename, "r");
if(fh == NULL) {
PyErr_Format(PyExc_IOError, "Cannot open file for reading");
return NULL;
}
// Setup the output list
bits = PyList_New(0);
// Reset the loop control
runningSum = 0;
prevPower = 0;
dataCounter = 0;
prevEdge = -1;
edgeCountDiff = -1;
halfTime = 0;
// Setup the variables - Power Detection
powerBuffer = (float *) malloc(SMOOTH_WINDOW*sizeof(float));
for(i=0; i<SMOOTH_WINDOW; i++) {
*(powerBuffer + i) = 0.0;
}
// Setup the raw data buffer
unsigned char *raw;
raw = (unsigned char *) malloc(RTL_BUFFER_SIZE*sizeof(unsigned char));
// Reset the loop control
prevPower = 0;
dataCounter = 0;
prevEdge = -1;
edgeCountDiff = -1;
halfTime = 0;
// Read in data and decode it
loopTimeOut = 0;
while( (i = fread(raw, sizeof(unsigned char), RTL_BUFFER_SIZE, fh)) > 0 ) {
decoder_callback(raw, i, (void *) bits);
//// Check for a request to exit
if( do_exit ) {
break;
}
}
if( ferror(fh) ) {
PyErr_Format(PyExc_IOError, "Error while reading from file");
fclose(fh);
free(raw);
return NULL;
}
// Done
fclose(fh);
free(raw);
// Return
output = Py_BuildValue("O", bits);
return output;
}
PyDoc_STRVAR(readRTLFile_doc, \
"Given an open file handle pointing to a RTL SDR recording, read in the data,\n\
perform Manchester decoding, and return a list of bits (1 or 0) suitable for\n\
identifying Oregon Scientific v2.1 and v3.0 sensor data.\n\
\n\
Inputs:\n\
* filename - filename to open for reading\n\
\n\
Outputs:\n\
* bits - a list of ones and zeros for the data bits\n\
\n\
Based on:\n\
* http://www.osengr.org/WxShield/Downloads/OregonScientific-RF-Protocols-II.pdf\n\
* http://www.disk91.com/2013/technology/hardware/oregon-scientific-sensors-with-raspberry-pi/\n\
");
/*
Module Setup - Function Definitions and Documentation
*/
static PyMethodDef DecoderMethods[] = {
{"readRTL", (PyCFunction) readRTL, METH_VARARGS, readRTL_doc},
{"readRTLFile", (PyCFunction) readRTLFile, METH_VARARGS, readRTLFile_doc},
{NULL, NULL, 0, NULL}
};
PyDoc_STRVAR(Decoder_doc, \
"Module to read in and Manchester decode Oregon Scientific v2.1 and v3.0 weather\n\
station data.");
/*
Module Setup - Initialization
*/
PyMODINIT_FUNC initdecoder(void) {
PyObject *m;
// Module definitions and functions
m = Py_InitModule3("decoder", DecoderMethods, Decoder_doc);
// Version and revision information
PyModule_AddObject(m, "__version__", PyString_FromString("0.1"));
}