forked from ninjablocks/433Utils
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRFSniffer.cpp
214 lines (180 loc) · 8.21 KB
/
RFSniffer.cpp
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
/*
RFSniffer
Usage: ./RFSniffer [<pulseLength>] [-v]
[] = optional
Requires modified rc-switch branch "protocollessreceiver"
with ReceivedInverted() function exposed.
Hacked from http://code.google.com/p/rc-switch/
by @justy to provide a handy RF code sniffer
by @Martin-Laclaustra to provide a protocol generator
*/
#include "../rc-switch/RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
// required to compute squareroot to get a standard deviation of timings
#include <math.h>
RCSwitch mySwitch;
int main(int argc, char *argv[]) {
// This pin is not the first pin on the RPi GPIO header!
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
// for more information.
int PIN = 2;
if(wiringPiSetup() == -1) {
printf("wiringPiSetup failed, exiting...");
return 0;
}
int pulseLength = 0;
if (argv[1] != NULL) pulseLength = atoi(argv[1]);
mySwitch = RCSwitch();
if (pulseLength != 0) mySwitch.setPulseLength(pulseLength);
mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
while(1) {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
printf("Unknown encoding\n");
} else {
printf("Received %i\n", mySwitch.getReceivedValue() );
if ((argc > 1 && strcmp(argv[1],"-v") == 0) || (argc > 2 && strcmp(argv[2],"-v") == 0)) {
int nbitlength = mySwitch.getReceivedBitlength();
int numberoftimings = 2 * nbitlength + 2;
int databuffer[64]; // get a copy of the received timings before they are overwritten
if(numberoftimings > 64) numberoftimings = 64;
for (int i = 0; i < numberoftimings; i++) {
databuffer[i] = mySwitch.getReceivedRawdata()[i];
}
int nreceived = mySwitch.getReceivedValue();
int nprotocol = mySwitch.getReceivedProtocol();
int npulselength = mySwitch.getReceivedDelay();
printf("\n=============================================\n");
printf("=============================================\n");
printf("\n");
printf("GENERAL\n");
printf("=======\n");
printf("Received %i\n", nreceived );
printf("Bitlength %i\n", nbitlength );
printf("Protocol %i\n", nprotocol );
printf("Delay(pulse length in us) %i\n", npulselength );
printf("(delay provided from rc-switch)\n");
printf("\n");
printf("STATISTICS\n");
printf("==========\n");
unsigned int databitsoffset = abs( (int)mySwitch.getReceivedLevelInFirstTiming() - (int)mySwitch.getReceivedInverted());
// compute duration of data bits
unsigned long dataduration = 0;
for (unsigned int i = 1 + databitsoffset; i < numberoftimings - 1 + databitsoffset; i++) {
dataduration += databuffer[i];
}
printf("Duration of data bits in pulse train : %i\n", dataduration );
// compute average databit duration
unsigned int averagebitduration = 0;
if(nbitlength != 0) {
averagebitduration = (int) (0.5 + (double)dataduration/(double)nbitlength); // warning, ensure avoiding 0 division (should not happen because rc-switch rejects < 4 bits)
}
// compute databit duration variability
int variancebitduration = 0;
for (unsigned int i = 1 + databitsoffset; i < numberoftimings - 1 + databitsoffset; i += 2) {
variancebitduration += (databuffer[i]+databuffer[i + 1]-averagebitduration)*(databuffer[i]+databuffer[i + 1]-averagebitduration);
}
if((nbitlength-1) != 0) {
variancebitduration /= (nbitlength-1); // warning, ensure avoiding 0 division (should not happen because rc-switch rejects < 4 bits)
} else {
variancebitduration = 0;
}
double sdbitduration = sqrt(variancebitduration);
printf("Data bit duration = %i and standard deviation = %.2f\n", averagebitduration, sdbitduration );
double coefficientofvariation = sdbitduration / averagebitduration;
printf("Coefficient of variation of data-bit duration = %.2f % (should be less than 10%)\n", 100*coefficientofvariation );
printf("Do not use the rest of the information if big coefficient of variation\n");
unsigned int protocolratio = (unsigned int)(0.5 + (double)(averagebitduration-npulselength)/(double)npulselength);
printf("Long-to-short duration ratio for data bits (rounded) = %i\n", protocolratio );
// sync bit
if(databitsoffset == 0) {
printf("Sync bit (in multiples of the pulseLength) = %i %i\n", (int) (0.5 + (double)databuffer[2*nbitlength+1]/(double)npulselength) ,(int) (0.5 + (double)databuffer[0]/(double)npulselength) );
} else {
printf("Sync bit (in multiples of the pulseLength) = %i %i\n", (int) (0.5 + (double)databuffer[0]/(double)npulselength) ,(int) (0.5 + (double)databuffer[1]/(double)npulselength) );
}
printf("\n");
printf("Proposed protocol for RCswitch\n");
printf("{ %i, { %i, %i }, { 1, %i }, { %i, 1 }, %s }\n",
npulselength,
(databitsoffset==0) ?
(int) (0.5 + (double)databuffer[2*nbitlength+1]/(double)npulselength)
:
(int) (0.5 + (double)databuffer[0]/(double)npulselength),
(databitsoffset==0) ?
(int) (0.5 + (double)databuffer[0]/(double)npulselength)
:
(int) (0.5 + (double)databuffer[1]/(double)npulselength),
protocolratio,
protocolratio,
(mySwitch.getReceivedInverted()) ? "true" : "false");
printf("\n");
printf("STATISTICS OF VARIATION BY LEVELS\n");
printf("=================================\n");
printf("(Differences here are probably artifacts from signal creation or detection)\n");
printf("(This might be completely ignored, but pay attention to them if big differences are present and emission does not work)\n");
int longup = 0;
int shortup = 0;
int longdown = 0;
int shortdown = 0;
int longupdownshortupdown[4] = {0,0,0,0}; // values
// high level long and short timings
for (unsigned int i = 1 + databitsoffset; i < 2*nbitlength + databitsoffset; i += 2) {
int firstisshortlevel = (int)(databuffer[i]<databuffer[i + 1]);
//int firstisdown = (int)(mySwitch.getReceivedInverted());
longupdownshortupdown[2*firstisshortlevel+(int)(mySwitch.getReceivedInverted())] += databuffer[i];
longupdownshortupdown[2*(1-firstisshortlevel)+(1-(int)(mySwitch.getReceivedInverted()))] += databuffer[i + 1];
}
longup = longupdownshortupdown[0];
shortup = longupdownshortupdown[2];
longdown = longupdownshortupdown[1];
shortdown = longupdownshortupdown[3];
// low level long and short timings
if(nreceived != 0) {
int numberofsetbits = __builtin_popcount(nreceived);
printf("number of bits set (in %i): %i\n",nreceived,numberofsetbits);
if(numberofsetbits == 0 || numberofsetbits == nbitlength) { // ensure avoiding 0 division
printf("all bits of the same value, skipping fractional analysis.");
} else {
if(mySwitch.getReceivedInverted()) {
longup /= nbitlength-numberofsetbits;
shortdown /= nbitlength-numberofsetbits;
shortup /= numberofsetbits;
longdown /= numberofsetbits;
} else {
longup /= numberofsetbits;
shortdown /= numberofsetbits;
shortup /= nbitlength-numberofsetbits;
longdown /= nbitlength-numberofsetbits;
}
printf("longup, longdown, shortup, shortdown\n");
printf("%i, %i, %i, %i\n",longup, longdown, shortup, shortdown);
printf("this might be useful for tweaking emmitting algorithms\n");
}
}
printf("\n=============================================\n");
// raw signal
if(2*nbitlength+2 < 64) { //prevent bufferoverrun
printf("\n");
printf("RAW SIGNAL (us)\n");
printf("===============\n");
printf("first level %s \n", (mySwitch.getReceivedLevelInFirstTiming() == 0) ? "down" : "up" );
for (int i = 0; i < 2*nbitlength + 2 - 1 + databitsoffset; i++) {
printf("%i ", databuffer[i] );
if((i - databitsoffset) % 16 == 0) printf("\n");
}
if ((2*nbitlength+2 - 1 + databitsoffset - 1) % 16 != 0) printf("\n");
if (databitsoffset != 1) printf("%i ", databuffer[2*nbitlength+1]);
printf("\n");
printf("=============================================\n");
printf("\n");
printf("\n");
printf("\n");
}
}
}
mySwitch.resetAvailable();
}
}
}