forked from vgire/UKHASnet-decoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUKHASnet-decoder.c
executable file
·321 lines (288 loc) · 9.77 KB
/
UKHASnet-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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <inttypes.h>
#include <unistd.h> // getopt
#include <curl/curl.h>
#ifdef _WIN32 // We could also use __MINGW32__
#include <fcntl.h>
#endif
// The identifier of this gateway
#define GATEWAY_ID "J0"
// Size of the character buffer to use for curl requests
#define CURLBUF_SIZE 250
// Ukhas protocol layer 1: https://www.ukhas.net/wiki/protocol_details
#define BIT_RATE 2000
// CRC computation start value
// Warning: there are many falvours of CRC-CCITT: XModem, 0xFFFF, 0x1D0F, Kermit
// 0x1D0F seems to be the one uses by the RFM69 radios
#define CRC_START 0x1D0F
// maximum packet size, not including length byte nor CRC checksum
#define MAX_PACKET_SIZE 255
// preamble is not used now (only for debugging)
#define PREAMBLE 0xAAAA
// 2 bytes sync words: https://www.ukhas.net/wiki/protocol_details
// TODO: add sync tolerance (maybe with preamble to be more robust)
#define SYNC_WORD 0x2DAA
uint16_t syncBuffer;
// Output more details (for debug)
bool verbose = false;
// Output only decoded packets?
bool quiet = false;
// Sink data to the net?
bool api = false;
// Default value assumes a 64kHz sampling rate
uint16_t sampleRate = 64000;
// byte buffers used when sync word has been recognized
uint8_t byte;
uint8_t buffer[MAX_PACKET_SIZE];
// frequency shift threshold automatically computed with a sample moving averge over a 8 bit window
int16_t threshold = 0;
// indicates that we are currently synchronized with bit stream for packet decoding
bool packetSync = false;
/////////////// PACKET parsing //////////////
// TODO refactor in another class
// packet length (without length byte)
int len = -1;
// packet offset
int offset = 0;
// packet crc16 computed on the fly
uint16_t computedCrc = 0x1D0F;
// packet crc read
uint16_t readCrc = 0;
// Use libcurl to submit data to server
CURL *curl;
CURLcode res;
// write http responses to /dev/null for now
FILE *devnull;
// see http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__util__crc_1gaca726c22a1900f9bad52594c8846115f.html
uint16_t crc_xmodem_update (uint16_t crc, uint8_t data) {
crc = crc ^ ((uint16_t) data << 8);
for (int i=0; i<8; i++) {
if (crc & 0x8000) {
crc = (crc << 1) ^ 0x1021;
} else {
crc <<= 1;
}
}
return crc;
}
// print time
void printTime() {
char buff[100];
time_t now = time(0);
strftime(buff, 100, "%Y-%m-%d %H:%M:%S", localtime(&now));
printf("%s ", buff);
}
// process one byte
bool processByte(uint8_t byte) {
// Buffer for the post request
char curlbuf[CURLBUF_SIZE];
if (len == -1) {
// read length (does not account for length byte)
len = (int) byte;
computedCrc = crc_xmodem_update(computedCrc, byte);
if (len <= MAX_PACKET_SIZE - 1) {
if (verbose) {
printTime();
printf("Parsing %d bytes\n", len);
}
// continue reading packet
return true;
} else {
if (verbose) {
printTime();
printf("Length: %d > %d, skip\n", len, MAX_PACKET_SIZE - 1);
}
// stop reading packet
return false;
}
} else if (offset < len) {
// read data
buffer[offset++] = byte;
computedCrc = crc_xmodem_update(computedCrc, byte);
// continue reading packet
return true;
} else if (offset == len) {
// null terminate the packet buffer
buffer[len] = '\0';
// read crc first byte
readCrc |= ((uint16_t) byte << 8);
offset++;
return true;
} else if (offset == len + 1) {
// read crc 2nd byte
readCrc |= byte;
// TODO
// I don't know why I need to invert this CRC to work
// bits could be all inverted but not just CRC...
// This seems to be the RFM which has a strange CRC implementation
readCrc = 0xffff - readCrc;
if (computedCrc == readCrc) {
if (!quiet) {
printTime();
printf("PACKET ");
}
for (int i=0; i<len; ++i) {
putchar(buffer[i]);
}
printf("\n");
fflush(stdout);
// Curl
if(curl && api)
{
snprintf(curlbuf, CURLBUF_SIZE, "origin=%s&data=%s",
GATEWAY_ID, buffer);
curl_easy_setopt(curl, CURLOPT_URL,
"http://www.ukhas.net/api/upload");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
curlbuf);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "CURL request failed (%s)\n", curl_easy_strerror(res));
//printf("CURL request failed, aborting...\n");
}
} else if (verbose) {
printTime();
printf("CRC mismatch: read(%04X), computed(%04X)\n", readCrc, computedCrc);
}
}
return false;
}
/////////////// END of PACKET parsing //////////////
int skipBit = 8;
// process one bit
void processBit(bool bit) {
if (packetSync) {
// process bits by 8 (byte)
byte = (byte << 1) | bit;
if (--skipBit < 1) {
// reset
skipBit = 8;
// process new byte
packetSync = processByte(byte);
}
} else {
syncBuffer = (syncBuffer << 1) | bit;
packetSync = (syncBuffer == SYNC_WORD || syncBuffer == 0xffff - SYNC_WORD);
if (packetSync) {
// TODO
// if sync with inverted SYNC_WORD, should invert all bits
if (verbose) {
printTime();
printf("Sync: %04X\n", syncBuffer);
}
// start reading bits 8 by 8 (as bytes)
skipBit = 8;
// initialize packet
len = -1;
offset = 0;
computedCrc = 0x1D0F;
readCrc = 0;
}
}
}
int main (int argc, char**argv){
// parse options
int opt;
while ((opt = getopt(argc, argv, "hvqws:")) != -1) {
switch (opt) {
case 's':
sampleRate = atoi(optarg);
if (sampleRate < 2 * BIT_RATE || sampleRate % BIT_RATE != 0) {
fprintf(stderr, "Illegal sampling rate - %d.\n", sampleRate);
fprintf(stderr, "Must be over %d Hz and a multiple of %d Hz.\n", 2*BIT_RATE, BIT_RATE);
return 1;
}
break;
case 'v':
quiet = false;
verbose = true;
break;
case 'q':
verbose = false;
quiet = true;
break;
case 'w':
api = true;
break;
case 'h':
default:
printf("Usage: UKHASnet-decoder [-s sample_rate][-v][-h] \n"
"Expects rtl_fm output:\n"
"\trtl_fm -f 433961890 -s 64k -g 0 -p 162 | ./UKHASnet-decoder -v -s 64000\n"
"\trtl_fm -f 433961890 -s 64k -g 0 -p 162 -r 8000 | ./UKHASnet-decoder -v -s 8000\n"
"\t[-s sample_rate in Hz. Above 4kHz and a multiple of 2kHz.]\n"
"\t[-w submit data to ukhas.net api]\n"
"\t[-v verbose mode] (negates prev. quiet)\n"
"\t[-q quiet mode] (negates prev. verbose)\n"
"\t[-h this help. Check the code if more needed !]\n\n");
return 0;
break;
}
}
if (!quiet) {
printf("UKHAS decoder using rtl_fm\n");
printf("Sample rate: %d Hz\n", sampleRate);
if (verbose) {
printf("Verbose mode\n");
}
printf("\n");
}
// process samples
int32_t samples = 0;
time_t start_time = time(NULL);
// sample rate / BIT_RATE
int downSamples = sampleRate / BIT_RATE;
int skipSamples = downSamples;
// Configure curl if required
if(api)
{
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
// send all output to /dev/null
# ifdef __linux__
devnull = fopen("/dev/null", "w+");
# elif _WIN32
devnull = fopen("nul", "w+");
# endif
curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull);
}
#ifdef _WIN32 // We could also use __MINGW32__
// Set stdin to Binary Mode
setmode(fileno(stdin), O_BINARY);
#endif
// Read incoming data from dongle
while(!feof(stdin) ) {
// process 1 sample
int16_t sample = (int16_t) (fgetc(stdin) | fgetc(stdin) << 8);
// threshold is a moving average over 8 bits
// this suppose that packet has sufficient bit transitions
// TODO find better formula to account for not well balanced packets
// use all samples and not only subsampled bits
threshold = (sample + (8 * downSamples - 1) * threshold) / (8 * downSamples);
// only process 1 bit over g_srate
if (--skipSamples < 1) {
// reset
skipSamples = downSamples;
// process bit
bool bit = sample > threshold;
processBit(bit);
// to debug more deeply uncomment bellow and comment all other outputs,
// pipe to a file and plot !
//printf("%d,%d,%d,%d,%d\n", samples++, sample, threshold, bit ? 6000 : -6000, packetSync ? 6000 : 0);
//fflush(stdout);
}
}
if (!quiet) {
printf("%d samples in %d sec\n", samples, (int) (time(NULL)-start_time));
}
// Clean up curl
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}