This repository was archived by the owner on Jan 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrfrecorder.c
406 lines (338 loc) · 13.2 KB
/
rfrecorder.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
#include "rfcommon.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <inttypes.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <wiringPi.h>
#define RECORD_PIN (2) /* pin number used by default */
#define RECORD_SIGNAL_BUFFER (250) /* default size of recording buffer */
#define RECORD_SIGNAL_ROUND (5) /* round duration (in usec) to multiples of this value */
#define RECORD_TIMEOUT_MIN_ENTRIES (10) /* minimum entries in buffer before recording timeout */
#define RECORD_TIMEOUT_TIME (1000000) /* recording timeout (in usec) 1 sec */
#define RECORD_SAMPLES_COUNT (5) /* number of samples collected before assuming it's correct */
#define RECORD_SAMPLES_FAILS (2)
#define PATTERN_MATCH_SETS_HIGH (5) /* number of sets to look ahead when finding gap between signal blobs */
#define PATTERN_MATCH_TOLERANCE (75) /* tolerance in usec when comparing signal durations */
#define PATTERN_MIN_APPEARANCES (2) /* minimum number of appearances of a pattern in the buffer for it to be accepted */
#define ABS(a) ((a) < 0 ? -(a) : (a)) /* get absolute value */
uint32_t
*buffer,
*buffer_end,
*buffer_top,
config_pin = RECORD_PIN,
config_buffer_size = RECORD_SIGNAL_BUFFER,
config_record_samples = RECORD_SAMPLES_COUNT,
config_record_failures = RECORD_SAMPLES_FAILS;
char
*config_filename = 0,
default_filename[128];
uint8_t pattern_near_match(uint32_t a, uint32_t b) {
return ABS((int32_t) a - (int32_t) b) < PATTERN_MATCH_TOLERANCE;
}
uint8_t pattern_near_match_pair(uint32_t *a, uint32_t *b) {
return pattern_near_match(a[0], b[0]) && pattern_near_match(a[1], b[1]);
}
uint8_t pattern_near_match_blob(uint32_t *a, uint32_t a_len, uint32_t *b, uint32_t b_len) {
uint32_t i;
if (a_len != b_len) {
return 0;
}
for (i = 0; i < a_len; i++) {
if (!pattern_near_match(a[i], b[i])) {
return 0;
}
}
return 1;
}
uint8_t pattern_match_buffer(uint32_t **pattern) {
uint32_t
i,
*current_search,
*candidate,
**starts_buffer,
**starts_top,
**starts_current,
**starts_check,
blob_len,
blob_best_len,
blob_appearances,
blob_best_appearances,
**blob_best_start;
uint8_t ok;
/* alloc starts buffer */
starts_buffer = (uint32_t **) malloc(sizeof(uint32_t * ) * 2 * config_buffer_size);
/*
* Start searching for start point. Assuming there is a significantly long low signal before same blob repeats
* again. Start halfway trough the buffer to avoid the signal being send late by the user. Stop looking before
* lookahead space is no longer available.
*/
for (current_search = buffer + (buffer_top - buffer) / 4 * 2;
current_search + 1 < buffer_top - PATTERN_MATCH_SETS_HIGH * 2;
current_search += 2) {
/*
* Look ahead in buffer. If there are any high signal with a longer or equally long off signal in front of it,
* this value is not the starting point.
*/
ok = 1;
for (i = 1; i < PATTERN_MATCH_SETS_HIGH; i++) {
uint32_t c = current_search[i * 2 + 1];
if (c > current_search[1] || pattern_near_match(current_search[1], c)) {
ok = 0;
break;
}
}
if (!ok) {
continue;
}
/* Find all the same values in the buffer. */
starts_top = starts_buffer;
for (candidate = buffer; candidate + 1 < buffer_top; candidate += 2) {
/* Check if candidate is similar to current search value. */
if (!pattern_near_match_pair(current_search, candidate)) {
continue;
}
*starts_top = candidate;
starts_top++;
}
/* Find most common blob length. */
blob_best_len = 0;
blob_best_appearances = 0;
for (starts_current = starts_buffer; starts_current < starts_top - 1; starts_current++) {
blob_len = starts_current[1] - starts_current[0];
if (blob_len == blob_best_len) {
continue;
}
blob_appearances = 1;
for (starts_check = starts_current + 1; starts_check < starts_top - 1; starts_check++) {
if (blob_len == starts_check[1] - starts_check[0]) {
blob_appearances++;
}
}
if (blob_appearances > blob_best_appearances) {
blob_best_appearances = blob_appearances;
blob_best_len = blob_len;
}
}
/* Blobs should appear at least a few times. */
if (blob_best_appearances < PATTERN_MIN_APPEARANCES) {
free(starts_buffer);
return 0;
}
/* Find the blob which matches most other blobs of the best length */
blob_best_start = 0;
blob_best_appearances = 0;
for (starts_current = starts_buffer; starts_current < starts_top - 1; starts_current++) {
if (starts_current[1] - starts_current[0] != blob_best_len) {
continue;
}
if (!blob_best_start) {
blob_best_start = starts_current;
continue;
}
/* Count appearances of blob in buffer*/
blob_appearances = 0;
for (starts_check = starts_buffer; starts_check < starts_top - 1; starts_check++) {
if (pattern_near_match_blob(*starts_current, blob_best_len,
*starts_check, starts_check[1] - starts_check[0])) {
blob_appearances++;
}
}
if (blob_appearances > blob_best_appearances) {
blob_best_appearances = blob_appearances;
blob_best_start = starts_current;
}
}
/* If no blob was found, clean up and return. */
if (!blob_best_start) {
free(starts_buffer);
return 0;
}
/* Copy the pattern */
if (pattern) {
*pattern = (uint32_t *) malloc(sizeof(uint32_t) * blob_best_len);
for (i = 0; i < blob_best_len; i++) {
/*
* The start we have been looking for is actually the last bit (because it was easy to find). Move the
* first pair to the end while copying.
*/
(*pattern)[i] = (*blob_best_start)[(i + 2) % blob_best_len];
}
}
free(starts_buffer);
return blob_best_len;
}
free(starts_buffer);
return 0;
}
void print_usage() {
printf("Usage: rfrecorder [options]\n");
printf("Options:\n");
printf(" --help Print this help message\n");
printf(" -o <path>, --output <path> Specifies the output file path\n");
printf(" -p <number>, --pin <number> The input (wiringPi) pin number (see http://pinout.xyz\n");
printf(" for details, default: %d)\n", RECORD_PIN);
printf(" --buffer-size <size> The size of the input buffer (default: %d)\n", RECORD_SIGNAL_BUFFER);
printf(" --record-samples <samples> The number of samples recorded (default: %d)\n", RECORD_SAMPLES_COUNT);
printf(" --record-failures <count> The number of samples not matching previous samples\n");
printf(" before discarding all previous samples (default: %d)\n", RECORD_SAMPLES_FAILS);
exit(1);
}
void parse_args(int argc, char **argv) {
uint32_t i;
for (i = 0; i < argc; i++) {
if(!strcmp(argv[i], "--help")) {
print_usage();
}
else if(!strcmp(argv[i], "--output") || !strcmp(argv[i], "-o")) {
config_filename = parse_args_string(i, argc, argv);
}
else if(!strcmp(argv[i], "--pin") || !strcmp(argv[i], "-p")) {
config_pin = parse_args_uint32(i, argc, argv);
if(config_pin >= 64){
print_usage();
}
}
else if(!strcmp(argv[i], "--buffer-size")) {
config_buffer_size = parse_args_uint32(i, argc, argv);
if(config_buffer_size < 2){
print_usage();
}
}
else if(!strcmp(argv[i], "--record-samples")) {
config_record_samples = parse_args_uint32(i, argc, argv);
if(config_record_samples < 1 || config_record_samples >= 256){
print_usage();
}
}
else if(!strcmp(argv[i], "--record-failures")) {
config_record_failures = parse_args_uint32(i, argc, argv);
if(config_record_failures < 1){
print_usage();
}
}
}
if(!config_filename) {
time_t nowt = time(NULL);
struct tm *t = localtime(&nowt);
strftime(default_filename, sizeof(default_filename), "%Y%m%d%H%M%S", t);
sprintf(default_filename, "%s.rfdat", default_filename);
config_filename = default_filename;
}
}
int main(int argc, char **argv) {
struct timespec last_change, now;
long elapsed;
int last = 0,
waiting = 0;
uint32_t
i, j,
match,
*samples[256],
sample_idx = 0,
sample_length = 0,
sample_fails = 0,
*sample,
sample_best_idx = 0,
sample_best_score = 0,
sample_score;
parse_args(argc, argv);
if (wiringPiSetup() < 0) {
printf("Unable to setup wiringPi: %s\n", strerror(errno));
return 1;
}
pinMode(config_pin, INPUT);
/* alloc pattern search buffer */
buffer = (uint32_t *) malloc(sizeof(uint32_t) * 2 * config_buffer_size);
buffer_end = buffer + 2 * config_buffer_size;
buffer_top = buffer;
printf("Recording...\n");
while (sample_idx < config_record_samples) {
/* read */
int on = digitalRead(config_pin);
/* time */
clock_gettime(CLOCK_REALTIME, &now);
elapsed = time_elapsed(last_change, now);
elapsed /= 1000;
/* round signal duration */
elapsed /= RECORD_SIGNAL_ROUND;
elapsed *= RECORD_SIGNAL_ROUND;
/* On change, add time to buffer */
if (on != last) {
last = on;
last_change = now;
/* Skip when waiting for start or when it's the first entry and the signal just turned on */
if ((waiting && !on) || (buffer_top == buffer && on)) {
waiting = 0;
continue;
}
/* store value to buffer */
*buffer_top = (uint32_t) elapsed;
buffer_top++;
if (buffer_top == buffer_end) {
match = pattern_match_buffer(&sample);
if (match) {
if (sample_idx == 0) {
samples[sample_idx++] = sample;
sample_length = match;
printf("Received sample (%d) %d/%d...\n", match, sample_idx, config_record_samples);
} else if (sample_length != match) {
sample_fails++;
printf("Received invalid sample (%d) %d/%d...\n", match, sample_idx, config_record_samples);
} else {
samples[sample_idx++] = sample;
printf("Received sample (%d) %d/%d...\n", match, sample_idx, config_record_samples);
}
if (sample_fails > config_record_failures) {
for (i = 0; i < sample_idx; i++) {
free(samples[i]);
}
sample_fails = 0;
sample_idx = 0;
printf("Failed! Restarting...\n");
}
}
buffer_top = buffer;
if (last) { waiting = 1; }
}
}
/* Reset on timeout */
if (!on && elapsed > RECORD_TIMEOUT_TIME && buffer_top - buffer >= RECORD_TIMEOUT_MIN_ENTRIES * 2) {
buffer_top = buffer;
if (last) { waiting = 1; }
}
}
for (i = 0; i < config_record_samples; i++) {
sample_score = 0;
for (j = 0; j < config_record_samples; j++) {
if(pattern_near_match_blob(samples[i], sample_length, samples[j], sample_length)) {
sample_score++;
}
}
if(sample_score > sample_best_score) {
sample_best_idx = i;
sample_best_score = sample_score;
}
}
printf("Done! Sample:\n");
for (i = 0; i < sample_length; i += 2) {
printf(">>> ON for %d usec, OFF for %d usec\n", samples[sample_best_idx][i], samples[sample_best_idx][i + 1]);
}
printf("\n");
printf("Sample has a score of %.2f%%.", (float)sample_best_score / config_record_samples * 100);
FILE *pFile = fopen(config_filename, "wb");
if (pFile) {
fwrite(samples[0], sizeof(uint32_t), sample_length, pFile);
}
fclose(pFile);
printf("Saved to %s!\n", config_filename);
for (i = 0; i < sample_idx; i++) {
free(samples[i]);
}
free(buffer);
return 0;
}